Introduction: Raspberry Pi - Arduino Serial Communication

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!

Comments

author
AdrieSentosa (author)2016-05-31

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

author
SeifM (author)2017-06-01

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

author
AyoA9 (author)SeifM2017-06-29

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

author
rensimathew (author)2017-06-22

I am getting

2

01

4

03

6

05

8

07

...

and so on. Why? Pl help

author
SeifM (author)2017-06-01

made it but send data from pi3 to arduino to move motor , facing pb of keep serial 9600 connection open to be able to send data from pi3 .

IMG_20170602_020525.jpg
author
rthunder (author)2017-01-06

I appreciate that you put this Instructable together, but I could suggest a few improvements:

1. A link to an example USB cable product would be helpful. Is it some kind of special cross-over cable?

2. I'm not sure what this means: 'the result should be content "/dev/ttyACM0" and you are good to go.'

3. Your explanation of serial communication is perhaps a bit over simplified. Do I need to send any initializing sequence or anything?

4. Some Arduino clones have a micro USB cable connector that they also use for power. Can you still use a USB-to-USB connection? How is the Arduino powered then?

Thanks.

author
AustinC83 (author)rthunder2017-03-30

If you haven't found the answers or if someone else stumbles upon this:

1) No, any ol' USB cable will work

2) That means that if the Arduino is the first Serial connection the Pi OS "sees" that should be the name...if not you'll have to find what the filename of the connection is (everything in Linux is a file, even the keyboard and wifi card! If you know what to look for you can have some fun with this)

3) Serial is an extremely simple protocol, there is some magic that happens when doing Serial over USB but the FDI drivers will handle that. Once you get a hold of it in this method you can act just like you are reading and writing with user input. Reading up on Serial isn't a terrible idea for general knowledge, one hint that might save you some heartache: Serial uses 8 bits to represent characters, so it is NOT exactly ASCII or UTF-8. This can cause some annoying issues, it was my biggest pain point in my first Serial project.

4) The Vin pin and Gnd pin (I use the nearest to Vin, not sure if it matters) can be used to power any Arduino. Some also have other power inputs, but just know by connecting it directly you are taking responsibility for the quality of the power, it won't be converted or protected by the board. Make sure you use the right voltage (5V or 3.3V)!

Happy hacking!

author
nununo (author)2017-03-13

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,

author
MatheusD31 (author)nununo2017-03-18

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

author
nununo (author)MatheusD312017-03-18

Understood. Thanks!

author
JohnH803 (author)2017-01-16

If you'd like to just see if it is working before messing with python (and the missing serial module) then you can just run this from to the linux (assuming Rasbian here but should work for others) command line to verify connectivity:

cat /dev/ttyACM0

This will print out what the Arduino device is sending to the console. Then you can go fight with Python.

author
rthunder (author)2017-01-06

6. When you connect the USB cable, the Arduino may not come up as device /dev/ttyACM0. Please provide a way to find which device has been assigned to your USB serial port.

author
rthunder (author)2017-01-06

5. It may also help to be clear that in this example you are sending fromt he Arduino and receiving with the RasPi. You may want to show how to go the other way, or even how to have two way communication.

author
RumelD (author)2016-12-05

Hi! very good tutorial .. but can i only use Data+ and Data- in the usb cable because im powering up my arduino through pololu shield ??

author
kjkjindal (author)2016-09-16

Hi, just one question, how do we make the Pi continuously check for whether an arduino has been connected or not and transfer data when it detects it?

author
NiklasH5 (author)2016-06-17

Is it possible to power the Arduino with the USB and do serial communitation at the same time?

author
AdrieSentosa made it! (author)NiklasH52016-06-17

Yup, it is possible. because serial cable consist of Vcc, GND, Data +, and Data - . Below is the pinout diagram of cable USB A and USB B.

1.jpg
author

i usually make my USB cable myself w/ coax cable for high quality serial communication

author
DhityoY (author)2016-05-31

I'm adding this solution for people who make the same mistake as I did.

In most cases: rename your project file 'serial.py' and delete serial.pyc if exists, then you can do simple 'import serial' without attribute error.

Problem occurs when you import 'something' when your python file name is 'something.py'.

author
AdrieSentosa (author)DhityoY2016-05-31

yup it's the bug. i tried myself.

author
ibrar234 (author)2016-05-24

is arduino ide must be installed on raspberry pi to do serial communication?

author
AdrieSentosa (author)ibrar2342016-05-31

not really, you could use picocom to start the communication automatically. But, it is recommended to install arduino ide so you could troubleshoot your code.

author
ibrar234 (author)2016-05-24

is arduino ide must be installed on raspberry pi to do serial communication?

About This Instructable

134,152views

120favorites

License:

Bio: A candle loses nothing of its light when lighting another
More by AdrieSentosa:LED Acrylic Trophy / PlacardRaspberry Pi - Remote Desktop ConnectionIoT Service Robot
Add instructable to: