3

I want to send data from RaspberryPi to Arduino UNO using UART. I tried sending my data 10 times to Arduino but it received it once. It reads as follows.

Connected to PC
pi: ÿpi: ÿpi: ÿ

1) I modified two files:

boot/cmdline.txt: to

dwc_otg.lpm_enable=0 console=ttyAMA0,9600 kgdboc=ttyAMA0,9600 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait

etc/inittab: to

T0:23:respawn:/sbin/getty -L ttyAMA0 9600 vt10

2) I connected Arduino to my PC using USB UART and to Rpi

pin 10(Arduino) to pin 8(Raspberry)

pin 11(Arduino) to pin 10(Raspberry)

This is my Arduino code to receive data over UART:

/*
 Connects Arduino to Raspberry Pi
 Arduino: SoftSerial
 Raspberry Pi: GPIO UART 
 Just connect pin 10(Arduino) to pin 8(Raspberry)  
              pin 11(Arduino) to pin 10(Raspberry)  
 */
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

void setup()  
{
  Serial.begin(9600);
   while (!Serial) {
    ;
  }

  Serial.println("Connected to PC");
  mySerial.begin(9600);
}

void loop() 
{
  // If data is available on Raspberry Pi, print it to PC
  if (mySerial.available()){
      Serial.print("pi: ");
      Serial.write(mySerial.read());
  }
}

3) On Raspberry i use python:

import RPi.GPIO as GPIO ## Import GPIO Library 
import time ## Import 'time' library.  Allows us to use 'sleep' 
import sys, getopt
import serial

print('Dang thuc thi')

port = serial.Serial("/dev/ttyAMA0", baudrate=9600,timeout=1)
#port.write("\r\n" + args[0])
port.open();
a = 0;
while a<10:
    port.write('data ' + str(a))
    a=a+1
    print('times: '+ str(a))
    time.sleep(1.2)
port.close()

Where am I going wrong?

0

First of all, you need to disable serial console on Rpi which is on by default. If you're running a new version of raspbian. Run sudo raspi-config, go to 8. Advanced options, Go to Serial and select disable. It will disable your serial console. You will have to reboot to make it effective.

If you don't want to make changes using raspi-config way.

  1. Like you said, change your /boot/cmdline.txt to look like follow. Ie, remove any reference to /dev/ttyAMA0.

    dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait
    
  2. Open /etc/inittab and comment out (insert # at the very beginning) following line to make it look like as below.

    #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Second point, Rpi is a 3v3 device while Arduino is 5v device. So while connecting both of them, you must use a level shifter to convert 5v logic to 3v3 logic else it can damage your RPi.

Here is a connection circuit for your reference.

enter image description here

Hope it helps.

  • I also send data from Raspberry PI to Arduion. (Is it possiple when only connect RX (Arduino) with TX (Raspberry PI) ?) – user3326893 Jul 2 '15 at 10:52
  • @user3326893 Yes, ideally it is. In that case, it would be simplex communication ie, one way. – dhruvvyas90 Jul 2 '15 at 10:55
  • @user3326893 Make sure that you have disabled serial console as mentioned in my answer. – dhruvvyas90 Jul 2 '15 at 11:06
  • i remember I tried your way. but it doesn't work. I will try again! – user3326893 Jul 2 '15 at 11:07
  • I just try! when i connect RX (Arduino) with TX (Raspberry PI) . On arduino display ÿpi: ÿpi: ÿpi: ÿpi: ÿpi: ÿpi: ÿpi:pi: ÿpi: ÿpi: ÿpi: ÿpi: Çpi: ÿpi: ópi: ÿpi: ƒpi: pi: ÿpi: Ã very long! always receive data! – user3326893 Jul 2 '15 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.