Raspberry Pi and Arduino Serial Communication

I must say that I am getting more and more enamored by my new Raspberry Pi.  As part of my new weather station project, I was needing to communicate data from my embedded Arduino to my Raspberry Pi where I would then store for later analysis.  Usually I expect ventures like these to take most of the evening, if not multiple evenings before finding success.  However, I was able to get successful 3-wire communication up and running in minutes!

Sometimes things just work on the first try!  Although, I have found that working with a Raspberry Pi is very straight forward and found lots of help in the RPi user community. This photo shows the initial test setup with 3 wires connecting the GPIO on the RPi to my embedded Arduino.

Sometimes things just work on the first try! Although, I have found that working with a Raspberry Pi is very straight forward and found lots of help in the RPi user community.
This photo shows the initial test setup with 3 wires connecting the GPIO on the RPi to my embedded Arduino.

Here are the steps involved in getting Arduino (TX) to Raspberry Pi (RX) configured:

Setup and Connections

  1. Connect the output of the Arduino TX pin to a voltage divider.  The output of the voltage divider is then connected to the RPi GPIO pin # 10 (GPIO 15).  The voltage divider drops Arduino TX voltage from 5V to 3.3V.  In my case, I used 2 10K resistors connected in parallel and 1 10K resistor attached to ground (See schematic below).
  2. The RPi TX GPIO pin #8 is attached to the base of an NPN transistor while the Arduino RX pin is attached to the collector (See schematic below).  The transistor takes the input signal from the RPi and increases the output voltage to 5V.
  3. Connect a wire from Arduino GND to RPi GPIO pin # 6.
  4. Remove console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 from /boot/cmdline.txt on the RPi.
  5. Comment out #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 in /etc/inittab on the RPi.
Since the Arduino transmits 5V signals, we need to drop the voltage to a more suitable 3.3V before attaching to the raspberry pi.

Since the Arduino transmits 5V signals, we need to drop the voltage to a more suitable 3.3V before attaching to the raspberry pi.

 

Since the Raspberry Pi transmits at 3.3V, we need to increase the voltage to 5V so the Arduino can properly receive it.

Since the Raspberry Pi transmits at 3.3V, we need to increase the voltage to 5V so the Arduino can properly receive it.

The Software – Raspberry Pi

I have seen a lot of examples of serial communication using Python and it seems VERY simple and straight forward.  However, since I happened to have spent WAY too much time coding a C program last year that did just that.  This program sends a byte of data to the Arduino setting the brightness of an LED.

In order to confirm that the receiving circuit is working OK, the Arduino sends the brightness back to the program and is displayed in the terminal.

To compile this program, simply enter the following at a command prompt:
gcc serial_set_led_brightness.c -o serial_set_led_brightness

The Software – Arduino

The Dimmer sketch can be found in the “Examples” folder.  I modified it slightly to send the brightness back to the Raspberry Pi by adding the following at the end of the loop:

Serial.println(brightness);

Make sure that the baud rate is set at 9600 in the Arduino sketch as well.

The LED Test

On a breadboard, set up an LED with the Anode connected to Digital 9 and the cathode to a low value resistor or directly to ground (there really isn’t much current in this test).

Once the LED is all ready to go, it is time to test out the communication.  At the command prompt of the Raspberry Pi, browse to the directory where you  compiled your serial_set_led_brightness program and enter the following:
./serial_set_led_brightness /dev/ttyAMA0 9600

This sets the serial device to /dev/ttyAMA0 and the baud rate to 9600.  you should see a prompt to enter a value between 0 and 255.  This will determine the LED brightness.  Here is an example of the expected output.  Note how the brightness from the Arduino is returned on the following line?

Set LED brightness (0 to 255): 100
100
Set LED brightness (0 to 255): 34
34
Set LED brightness (0 to 255): 255
255

Now that we have these two small-but-powerful gadgets communicating, the possibilities are endless!  Now on to putting the finishing touches on my weather data logging program.

Thanks for reading and good luck on your project!

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.


Speak Your Mind

Your email address will not be published. Required fields are marked *

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>