Raspberry Pi and Arduino Connected Over Serial GPIO

Raspberry Pi Serial GPIO Configuration

0. if you have not seen my article on how to remote access your Raspberry Pi, take a look here:

http://blog.oscarliang.net/setup-raspberry-pi-for-remote-access/

1. In order to use the Raspberry Piā€™s serial port, we need to disable getty (the program that displays login screen) by find this line in fileĀ /etc/inittab

T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

And comment it out by adding # in front of it

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Raspberry Pi and Arduino Connected Over Serial GPIO

2. To prevents the Raspberry Pi from sending out data to the serial ports when it boots, go to fileĀ /boot/cmdline.txtĀ and find the line and remove it

console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

3. reboot the Raspberry Pi using this command:Ā sudo reboot

4. Now, InstallĀ minicom

sudo apt-get install minicom

And thatā€™s the end of the software configuration.

Connect Serial Pins and GPIO with a Voltage Level Converter

Load this program on your Arduino first:

[sourcecode language=ā€cppā€]
byte number = 0;

void setup(){
Serial.begin(9600);}

void loop(){
if (Serial.available()) {
number = Serial.read();
Serial.print(ā€œcharacter recieved: ā€œ);
Serial.println(number, DEC);}}
[/sourcecode]

A Simple Example with Minicom

Now to connect to the Arduino via serial port using this command in putty or terminal

minicom -b 9600 -o -D /dev/ttyAMA0

When you type a character into the console, it will received by the Arduino, and it will send the corresponding ASCII code back.Ā Check here forĀ ASCII Table. And there it is, the Raspberry Pi is talking to the Arduino over GPIO serial port.

Raspberry Pi and Arduino Connected Over Serial GPIO Schematic

Example with Python Program

Using Python programming language, you can make Raspberry Pi do manyĀ fascinatingĀ stuff with the Arduino when they are connected.Ā Install Py-Serial first:

sudo apt-get install python-serial

Hereā€™s a simple application that sends the string ā€˜testingā€™ over the GPIO serial interface

[sourcecode language=ā€pythonā€]

import serial
ser = serial.Serial(ā€˜/dev/ttyAMA0ā€², 9600, timeout=1)
ser.open()

ser.write(ā€œtestingā€)
try:
while 1:
response = ser.readline()
print response
except KeyboardInterrupt:
ser.close()

[/sourcecode]

Then connect your Arduino, Raspberry Pi and Logic Level Converter like this:

This is how the wires are connected.

And this is the GPIO pins on the Raspberry Pi. Make sure you connect the correct pin otherwise you might damage your Pi.

A Simple Example with Minicom

Now to connect to the Arduino via serial port using this command inĀ puttyĀ or terminal

minicom -b 9600 -o -D /dev/ttyAMA0

When you type a character into the console, it will received by the Arduino, and it will send the corresponding ASCII code back.Ā Check here forĀ ASCII Table. And there it is, the Raspberry Pi is talking to theĀ ArduinoĀ over GPIO serial port.

To exit, press CTRL + A release then press Q

Example with Python Program

Using Python programming language, you can makeĀ Raspberry PiĀ do manyĀ fascinatingĀ stuff with theĀ ArduinoĀ when they are connected.Ā Install Py-Serial first:

sudo apt-get install python-serial

Hereā€™s a simple application that sends the string ā€˜testingā€™ over the GPIO serial interface

[sourcecode language=ā€pythonā€]

import serial
ser = serial.Serial(ā€˜/dev/ttyAMA0ā€™, 9600, timeout=1)
ser.open()

ser.write(ā€œtestingā€)
try:
while 1:
response = ser.readline()
print response
except KeyboardInterrupt:
ser.close()

[/sourcecode]

To exit, press CTRL + C

Connect Raspberry Pi and Arduino with a Voltage Divider

Apart from replacing the Login Level Converter with a voltage divider, the way it works is the same as above. Anyway, I will show you a different example to demonstrate this. A voltage divider is basically just two resistors.

There is something you should be aware of before we continue. The RX pin on the Arduino is held at 5 VoltsĀ even when it is not initialized. The reason could be that the Arduino is flashed from the Arduino IDEĀ through these pins when you program it, and there are weak external pull-ups to keep the lines to 5 Volts at other times.Ā So this method might be risky.Ā I recommend using a proper level converter, if you insist on doing it this way, tryĀ adding a resistor in series to the RX pin, andĀ never connect the Raspberry Pi to Arduino RX pin before you flash the program to Arduino, otherwise you may end up with a damaged Pi!

The ArduinoĀ serial pin is held at 5 volts and Raspberry Piā€™s at 3.3 volts. Therefore a voltage divider would be required, itā€™s basically just two resistors.

Here is the program you need to write to the Arduino board.

[sourcecode language=ā€cppā€]

void setup() {
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
int incoming = Serial.read();
Serial.print(ā€œcharacter recieved: ā€œ)
Serial.print(incoming, DEC);
}
}
[/sourcecode]

Now you can connect directly from your computer to theĀ Raspberry PiĀ on the tty-device of the Arduino, just like we described above. (type below into your putty)

minicom -b 9600 -o -D /dev/ttyAMA0

And as you type in characters in the console, you should see something like this:

And thatā€™s the end of this Raspberry Pi and Arduino article.Ā There are other ways of connecting, if you donā€™t already know, you can also use a Micro USB cable. Check out this post:Ā https://oscarliang.com/connect-raspberry-pi-and-arduino-usb-cable/

For more detail: Raspberry Pi and Arduino Connected Over Serial GPIO

 

About The Author

Scroll to Top
Read previous post:
Raspberry Pi Solar Weather Station
Raspberry Pi Solar Weather Station

spurred on by the completion of my two previous projects, the Compact Camera and Portable Games Console, I wanted to...

Close