We have found a couple tutorials around the web to help with communication between Arduino and Raspberry Pi, but we really had to fill in a lot of the blanks. We figured that it would be useful to outline our process from start to finish to help anyone else out that may not be 100% familiar with Linux, Python, or using the Raspberry Pi’s GPIO pins.
Parts needed
- Raspberry Pi
- Arduino
- 8 jumper cables
- Breadboard
- logic level converter ( https://www.sparkfun.com/products/8745 )
- Raspberry Pi cobbler (optional) ( http://learn.adafruit.com/adafruit-pi-cobbler-kit/overview )
To start off, make sure that all of your devices are turned off
Setting up the Raspberry Pi for serial communication
Before you dig into the hardware connections, you need to set some software up first, so turn on and log into your Raspberry Pi. Once in there you need to make sure that your current libraries are all current, so update them:
sudo apt-get update sudo apt-get upgrade
Next, you need to make sure that you have a text editor of your choice installed. For the purposes of this tutorial we are going to be using nano, which we have found to be quick, easy and painless to work with. Using your text editor you are going to disable the need to login via serial port, open up /etc/initab and go to the bottom. You need to comment out the following:
change
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
to
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Next you are going to disable Raspberry Pi’s process of sending boot info to the serial port. We have heard that this isn’t necessary, but it is quick and easy and we would rather not have all the boot info being sent over serial (we like a little control over what gets sent). To do this you are going to use your trusty text editor again to edit /boot/cmdline.txt.
sudo nano /boot/cmdline.txt
and be careful to ONLY remove the following bit of text:
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
Now you can reboot your Raspberry Pi
That covers the changes that you have to make to your settings, next you will need to install the software packages needed for actual communication. The first (and only, if you aren’t going to be using python) is minicom. Once minicom is setup, you can run it and move onto physically connecting the Arduino and the Pi. Otherwise, check below for the python packages that we use to communicate.
sudo apt-get install minicom
The only package that you need to start with for python is pyserial. This will facilitate the communication between devices over the serial port.
sudo apt-get install python-serial
Wiring it:
- Connect one side of the pi cobbler to the raspberry pi board and the other side to a breadboard. If you don’t have the cobbler, it would be easiest to have female – male jumper cables to connect directly to the GPIO pins on the Raspberry Pi, if this is what you need to do just follow along the next couple steps to identify the pins that you need to work with. Also note that if you have the ribbon cable with no Pi Cobbler, you can use male – male jumpers to connect right into the ribbon.
- Connect the Logic Level Converter to your breadboard. We set it up across the center of the breadboard to keep the two power levels separate.
- From your Raspberry Pi, you need to connect the 3.3V first pin to the LV power of the Logic Level Converter.
- Next you can connect the ground, this is pin 6 (opposite the power and down three) and connect the other end to the LV ground pin of the logic level converter.
- Now you just need the serial pins connected to enable the communication. You are going to use the TX/RX GPIO pins 14/15, right next to the ground on the Pi / Cobbler. Those pins connect to their counterpart on the Logic Level Converter (RX – RX, TX – TX).
- That’s it for the wiring of the Pi, now it is time to move over to the Arduino. You need to connect the 5V power on the Arduino to the HV power pin on the converter.
- Next connect the ground from the Arduino to the HV ground on the converter.
- Finally, you need to connect you serial pins. Connect the RX on the Arduino to the TX on the HV side of the Logic Level Converter and the TX on the Arduino to the RX on the Logic Level converter (note that this is the opposite of the Raspberry Pi setup.
That is it for necessary hardware. Time to get a sketch going and begin communication. Something simple to start with, we are going control the pin 13 LED of the Arduino from your Raspberry Pi and return a message to the Pi to let it know that things are working.
So first step is the sketch:
#define BAUDRATE 9600 #define LEDPIN 13 char val = ' '; void setup() { // open the serial communication on 9600 baudrate Serial.begin(BAUDRATE); // ensure that pin 13 is set for output pinMode(LEDPIN, OUTPUT); } void loop() { if (Serial.available()) { val = Serial.read(); if (val == 'p') { Serial.print("POWER ON THE LIGHT"); digitalWrite(LEDPIN,HIGH); } else if (val == 'o') { Serial.print("TURNING OFF THE LIGHT"); } } }
Now to write the Python on the Raspberry Pi:
import serial, time arduino = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) arduino.open() try: while True: arduino.write('p') response = arduino.readline() print response time.sleep(1) arduino.write('o') response = arduino.readline() print response time.sleep(1) except KeyboardInterrupt: arduino.close()
Some links to help you out further:
http://www.hobbytronics.co.uk/raspberry-pi-serial-port
http://forums.trossenrobotics.com/tutorials/how-to-diy-128/complete-control-of-an-arduino-via-serial-3300/
http://justpushbuttons.com/blog/?p=376