How to connect a Raspberry pi and a Arduino via serial communication
You want to connect an Arduino board to a raspberry pi board. There are several different ways to do it. It can be done with I2C communication, SPI or even the GPIO port on the Raspberry pi. But in this tutorial, we'll use a serial comunication.
But why do we want to connect a raspberry pi 2 B+ and an Arduino uno Rev 3 together? As maybe some of you already know, both of them got their advantages, misadvantages and each of themopen source and open hardware
Arduino strengths:
Low current consumption
Easy to plug in sensors and analog sensors without additionnal hardwares
excellent for IOT
and more
Arduino flaws:
Doesn't have a processor for powerful calculation
Needs additionnal boards and extensions to connect it to internet, USB, Wfi, bluetooth, etc
Raspberry pi strengths:
Runs with an operating system "raspbian: a linux derivation"
Have a bunch of extension (USB slot, Wifi, HDMI slot, RJ45 port, etc)
Can be used for IOT "Internet of things"
Can make higher calculation than a Arduino board
Raspberry pi flaws:
Need to add additionnal hardware to plug analog sensors
Consume more energy than an arduino". Not to good for mobile application"
So to connect our 2 boards and exploit their strengths for our future projects, we need to make them communicate and share data (it could be data from sensors ''Arduino, from internet ''Raspberry pi'', a smartphone, etc).
To start, you need to:
Buy a raspberry pi 2 B+ or 3 (Those are lastest one)
Buy a Arduino Uno, Mega or another one
Get an usb cable (A Male to B male)
Connect them with the USB cable and test the serial communication on the raspberry pi board the following command on the raspberry pi terminal:
ls /dev/tty
And the result should be this : "/dev/ttyACM0'' or something like this depending of your board. That allows you to know that everything is okay and the raspberry pi board is connected and find it's name. :-)))
Send data from the Arduino to the raspberry pi:
Upload the following program to your Arduino:
Program:
void setup(){ Serial.begin(9600); }
void loop(){ Serial.println(“Hello Pi”); delay(2000); }
Run Python on your Raspberry Pi and type this command in your terminal:
Program:
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
/dev/ttyACM0: being the port name, It may be different for you depending of your
Raspberry Pi board
9600: Represents the baud on the serial port. It must bethe sqme on both side (Arduino and raspberry pi).
Now type the following code to add a listener to serial port on the raspberry pi
Program
while 1 : ser.readline()
We should see appearing on our terminal screen: "Hi"
This is only a beginning with what you can do with both of them working side by side.
Yoiu can improve the montage and make a weather station, a small computer, a virtual reality helmet and more.
You can check out the following for more details on this project: How to connect a raspberry pi 2 B+ and an Arduino uno rev 3
Don't forget to share with us on our social medias (Facebook, Twitter, Google+, Instagram, Youtube) and subcribe to our newsletter for our last news and tutorials.
Come by soon for other tutorials and news.
Peace!!! ;-)