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:
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200from /boot/cmdline.txt on the RPi.
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100in /etc/inittab on the RPi.
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 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.
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!