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!
Here are the steps involved in getting Arduino (TX) to Raspberry Pi (RX) configured:
Setup and Connections
- 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).
- 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.
- Connect a wire from Arduino GND to RPi GPIO pin # 6.
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200from /boot/cmdline.txt on the RPi.
- Comment out
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100in /etc/inittab on the RPi.
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:
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!