Arduino and Raspberry Pi serial communciation

Want to hook up your Arduino to your Raspberry Pi and send data between the two?  Read on!

So you have your Arduino robot, or weather station and you want to send data back and forth with a Raspberry Pi?  The hardware hookup is really easy, but we have to configure a few things on the Pi.

Hardware connections

We'll use the serial UART pins on the Raspberry Pi Zero GPIO for this example, and transfer data from an Arduino to a Raspberry Pi zero, though all Raspberry Pis and Arduinos are capable of this feat.  The UART is a common serial communication protocol comprised of two pins labelled, RX (receive data pin) and "TX" (transmit data pin).  Wiring is easy - hook up the desired RX pin on the Arduino to the Raspberry Pi TX pin, and the Arduino TX to the Raspberry Pi RX pin.  As always, make sure you connect the ground line between the Arduino and the Raspberry Pi.  If the particular Arduino model you are using is powered by 5V (find out by measuring the voltage on the TX pin!), you'll need a couple of resistors to bring the voltage down to 3.3V.  If you connect 5V from the Arduino to the Raspberry Pi without the resistor voltage divider, you will damage your Pi, possible permanently!


Above:  Hook your Arduino and Raspberry Pi together so they can talk over the Serial port!

In the hookup diagram above I have configured a SoftwareSerial on the Arduino using the D3 and D2 pin like so:
Don't get confused with the above code, two Serial interfaces are invoked; one is "mySerial" which is the software UART to communicate with the Pi, and the other is "Serial", which is the hardware UART on D0 and D1, and can be accessed over the USB cable on your computer.

Configuring the Raspberry Pi

Okay!  It turns out that we can hook up wiring to the Raspberry Pi as shown above and actually control a console session.  There is some out of date information on the web, some suggesting this is disabled by default, others claiming the contrary.  We need to disable this to use the GPIO serial on the Pi.  As of writing (June 2019), it seemed to be active and requires disabling:

Log in on the Pi and type:

dmesg | grep tty

The last line will indicate whether the hardware console is enabled or not.  In order to disable it, type:

sudo raspi-config

Go to Connections --> Serial --> Disable console, Enable hardware serial

reboot the Pi:

sudo reboot

Some guides recommend editing /boot/config.txt.  The above method should suffice but nevertheless we can check boot/config.txt by typing:

sudo nano /boot/config.txt

Confirm that there is a line that reads enable_uart = 1.  If not, you can add it to the end and save the file.

Raspberry Pi Zero W

Note to get this working using the Raspberry Pi Zero W we have to disable the bluetooth controller that would otherwise map onto this UART.  We can do this by opening /boot/config.txt again by typing:

sudo nano /boot/config.txt

and adding at the end of the file:

dtoverlay=pi3-miniuart-bt

And then reboot:
sudo reboot

Raspberry Python comunication

We have already looked at some code to run on the Arduino for two-way serial communication with your Pi.  For the Raspberry Pi, I've provided a brief script to read a line from the Arduino and print it out to the console.
Now you should have all you need to get your Arduno projects talking to each and to your Raspberry Pi without getting involved in wireless solutions!  Happy hobbying!

References

https://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/
https://spellfoundry.com/2016/05/29/configuring-gpio-serial-port-raspbian-jessie-including-pi-3/
https://www.abelectronics.co.uk/kb/article/1035/raspberry-pi-3--4-and-zero-w-serial-port-usage

Comments

Popular posts from this blog

Getting started with the Pro Micro Arduino Board