Connecting Raspbery Pi to Arduino over serial pins / GPIO

Warning ! the Raspberry Pi operates on 3.3v and the Arduino on 5v. Connecting the TX RX pins directly will damage the Pi! 

Today we will be connecting a Raspberry Pi ZeroW to an Arduino Nano (Elegoo Clone) over serial interface to enable two way communication. We will test this using the previous voltage divider project.


 To accomplish this we will need the following:

  • Raspberry Pi
  • Arduino Nano
  • Logic level converter
  • Breadboard + jumper wires
  • Power supplies for each

The wiring diagram is as follows :


 I have set it out on a bread board as follows using the 3.3v and ground of the Arduino instead of a battery for the divider measurement.


 The GPIO serial pins on the Pi need to be configured before we can use them. To do this connect / ssh into your Pi and run "sudo raspi-config". Select "3 Interface Options", then "P6 Serial Port".

Would you like a login shell to be accessible over serial, select "No".

Would you like serial port hardware to be enabled, select "Yes".

Select "OK", "Finish" then "sudo reboot".

Log back in to the Pi and run "dmesg | grep tty".


The part we are interested in is "ttyS0", it should be similar on your Pi. Take a note of it because we will need it soon.

Create a new python script eg. "nano serialtest.py" and enter the following code :


import serial

ser = serial.Serial("/dev/ttyS0", baudrate = 9600)

while 1:
    value = ser.readline()
    print(value)

Run it with "python serialtest.py", Reset the Nano using the reset switch and you should receive the following :

Thats a good start, lets change the code to request the battery voltage reading.


 


import serial
import time

#set up the serial connection
ser = serial.Serial("/dev/ttyS0", baudrate = 9600, timeout = 1)

if ser.inWaiting() > 0:  #Checks if there is anything in the buffer
    rx = ser.readlines() #It should be empty.
    print(rx)            #Prints buffer to screeen

ser.write("b")           #Writes "b" to the serial interface
time.sleep(1)            #Give it chance to send
rx = ser.readline()      #Read any reply and save it to a variable
print(rx)                #Print it to the screen



Thats pretty much it but now that we can retrieve the value on our Pi in Python we can do a lot more with it, such as calculating the voltage correctly.


 Our output.

Conclusions.

The cheap logic level converter works really well, I bought a pack of 10 for £7.99 KeeYees 10pcs 4 Channels IIC I2C Logic Level Converter Bi-Directional Module 3.3V to 5V Shifter for Arduino (Pack of 10)
Requesting the values via serial is very simple and because we now have two way communication we can also use the more powerful 5v outputs on the Arduino. I have been using Python 2.x in this project, it will need updating for Python 3 :/

 

PySerial Documentation


 

Comments

Popular posts from this blog

Intro and Welcome

How to stop Outlook Meeting Updates Going Directly to Deleted Items

ScreenGrab