Raspberry Pi - Arduino Serial Communication

Introduction: Raspberry Pi - Arduino Serial Communication

About: A candle loses nothing of its light when lighting another

In this project, we will be making a communication between Arduino and Raspberry Pi which transfer vital information by sending data one bit at a time.

Serial communications are essential for every Micro-controllers to communicate between Micro-controllers and another device. The Micro-controller sends these 1 and 0 (bits) that contain necessary information one by one, or Serially. These bits form together and turn into bytes (composed of 8 bits). For further information, check it out sparkfun binary tutorials.

It’s a fairly easy project and can be used either on its own or part of something bigger (Check it Out >> Integrated Weather Station).

Step 1: List of Material

For this project, we will be using:

- Arduino boards (mine Arduino Uno Rev3)

- Raspberry Pi 2

- USB cable cost :~1.00 USD

Step 2: Hardware Connection

In common, the connections are fairly easy. Just connect Arduino USB Plug to Raspberry PI with USB cable and check the connection between Arduino and Raspberry pi by type "ls /dev/tty*" in Raspberry Pi terminal, the result should be content "/dev/ttyACM0" and you are good to go.

Step 3: Raspberry Pi Programming

Below is the Raspberry Pi Serial Communication code. Upload serial_test.ino code to your Arduino (Mine Arduino Uno Rev3) , Run serial_test.py Python code in Raspberry PI, Connect Arduino to Raspberry Pi through USB cable, and you should have no problems.

Step 4: Enjoy!

After making sure that everything works smoothly, you can take this project into bigger project (Check it Out >> Integrated Weather Station)!

Power through batteries or a plug and you’re good to go!

8 People Made This Project!

Recommendations

  • Puzzles Speed Challenge

    Puzzles Speed Challenge
  • "Can't Touch This" Family Contest

    "Can't Touch This" Family Contest
  • CNC Contest 2020

    CNC Contest 2020

33 Discussions

0
AdrieSentosa
AdrieSentosa

4 years ago

picocom /dev/ttyUSB0 -b 9600 -r -l

0
eric.jose
eric.jose

Question 2 months ago

I was trying to send a value from arduino to raspbberry pi.... but I'm getting error characters also with the value when printed... How can I remove it?

0
infantes2
infantes2

Tip 6 months ago

The output is 2 by 2.
That is because the python program is calling twice the readline() function.
Use this code instead, and notice the diference...

import serial

ser = serial.Serial('/dev/ttyACM0',9600)
s = [0,1]
while True:
read_serial=ser.readline()
s[0] = int (read_serial,16)
print s[0]
print read_serial
0
rensimathew
rensimathew

3 years ago

I am getting

2

01

4

03

6

05

8

07

...

and so on. Why? Pl help

0
infantes2
infantes2

Reply 6 months ago

that is because the python program is calling twice the readline() function.
use this code instead, and notice the diference...

import serial

ser = serial.Serial('/dev/ttyACM0',9600)
s = [0,1]
while True:
read_serial=ser.readline()
s[0] = int (read_serial,16)
print s[0]
print read_serial
0
MichelleD210
MichelleD210

Reply 2 years ago

i get it why you are having such result. the code goes like this. meaning there will be 2 outputs to be printed.

"print s[0]

print read_serial"

0
PranjalT3
PranjalT3

Reply 2 years ago

but how can we get continous values like:

1

2

3

....

0
RichardW111
RichardW111

9 months ago on Step 4

I got an error running the python program:

[Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: '/dev/ttyACM0'

Any ideas? Not real familar with RasPi serial

0
infantes2
infantes2

Reply 6 months ago

# before connection
ls /dev/tty* > before.txt

# after connection
ls /dev/tty* > after.txt

# now you can see what is the correct serial port
diff before.txt after.txt

# also is posible insuficient privileges (only works until you diconnect arduino)
sudo chmod 666 /dev/ttyACM0
# for persistent privileges, do:
usermod -a -G dialout yourUser
# logout or reboot

# also make sure you have pip and pyserial
sudo apt install python-pip
pip install pyserial

0
infantes2
infantes2

Tip 6 months ago

# before connection
ls /dev/tty* > before.txt

# after connection
ls /dev/tty* > after.txt

# now you can see what is the correct serial port
diff before.txt after.txt

# also is posible insuficient privileges (only works until you diconnect arduino)
sudo chmod 666 /dev/ttyACM0
# for persistent privileges, do:
usermod -a -G dialout yourUser
# logout or reboot

# also make sure you have pip and pyserial
sudo apt install python-pip
pip install pyserial

0
nnnayeem
nnnayeem

Question 9 months ago

Hello, I have followed your way. It worked correctly. But I am having problem.
I am sending json data from arduino to raspberry pi. But portion of data comes and rest of them is getting missed. Sometime data transfers properly without any data missing.
I am using usb type a/b as the instruction.
The code for reading data is included below:

bytesToRead = ser.inWaiting()
if bytesToRead > 0:
x = ser.read(bytesToRead)
self.dataProcessorWork.q.put((self.receve, x))

I am stuck here. I appreciate your help.
Thank you
0
nununo
nununo

3 years ago

What's the advantage of connecting an Arduino to a RPi if the RPi can read GPIO by itself? Can you please give me an example in which RPi alone won't work without an Arduino?

Thanks in advance,

0
rasterman101
rasterman101

Reply 1 year ago

There are no analog inputs on the Raspberry PI GPIO.

0
Jigneshk5
Jigneshk5

Reply 1 year ago

Rpi is not meant to handle the large amount of sensor data. On the other hand arduino can easily interfaced with more number of sensors and can also process them more quickly. We use Rpi for its camera interface used for image processing and similar stuffs.

0
MatheusD31
MatheusD31

Reply 3 years ago

RPi has only one PWM output and GPIO pins are very sensitive (as far as
I know). If you need to control many servos and already own an Arduino,
it's better and cheaper to use it than buy some PWM module. Hope it
helps

0
nununo
nununo

Reply 3 years ago

Understood. Thanks!

0
MojtabaK8
MojtabaK8

2 years ago

I made it. thanks!

0
kavyar6
kavyar6

2 years ago

Sir I could send a digital value from the arduino to raspberry pi3.I have to send two sensor values from arduino to the raspberry pi3 but i'm not aware of the process so can you please help me.

0
SeifM
SeifM

3 years ago

what if i wanna send data from pi to Arduino ; am trying control Stepper motor using arduino and in the other side there is pi with some image processing works , need to send angle / step to stepper to move on from pi to arduino

0
AyoA9
AyoA9

Reply 3 years ago

Are you struggling with the sending of data from the Raspberry Pi or the receiving data on the Arduino?