Fritz-Hut

Connecting an Arduino and Raspberry Pi

The use case of the GPIO pins on the Raspberry Pi are rather limited and since I have an Arduino Mega 2560 lying around I can easily connect the two. In the next couple of weeks I’ll explain my process of connecting the Arduino with the Raspberry Pi and creating some sort of Library so the Arduino can be controlled from a web browser using a Raspberry Pi.

Connection options for and Arduino to Raspberry Pi connection

There are a couple of options for connecting an Arduino with a Raspberry Pi. The first and obvious one is of course plugging the USB port in the Raspberry Pi. As easy as it sound there are some disadvantages. First you sacrifice a precious USB port and second you now need to do your Arduino development on your Raspberry Pi. Not that easy or handy after all.

There are also bridge boards like Ponte (most popular one) that allow to connect Arduino shields with a Raspberry Pi. There are also extension board for the Raspberry Pi that actually include an Arduino chip (forgot the name). ## Say hello to my little friend UART The Raspberry Pi has a UART port and the Arduino has a Serial port, match made in heaven! My Arduino works with 5V logic and the Raspberry Pi with 3.3V, whatever you do, never and never connect the two without a logic level conversion circuit!

I decided to build a circuit (its more fun!), Sparkfun Logic Level Converter is another option. I used a Bi-Directional MOSFET Level Converter, based on the 2N7000. The schematics are really easy. Edit: thanks Scott for pointing out that R3 should be 10k instead of 10.

Level Converter Circuit
Level Converter Circuit

When the low side (3.3V) transmits a logic one the MOSFET is tied high (off) and the high side sees 5V through the pull-up resistor. When the low side transmits a logic zero the MOSFET source pin is grounded and the MOSFET is switched on and the high side is pulled down to 0V.

When the high side (5V) transmits a logic one the MOSFET substrate diode conducts pulling the low side down 0.7V, which in turns the MOSFET on. And so on. This circuit can also be used with I2C. We need two level converting circuits, one for RX and the other for TX.

Configuring the Raspberry Pi

When I started my Pi and loaded the pass through script in my Arduino, nothing happened. Normally the Raspberry Pi sends all the terminal output over the UART at a baud of 115200, but I didn’t receive anything on my serial monitor of my Arduino. I found out that my connection diagram was lacking one important connection. The Raspberry Pi ground pin also needs to be connected to the Arduino ground.

After connecting the Arduino Ground with the Raspberry Pi ground I got the following output on my Serial console.

Successful Communication
Successful Communication

Great, the Raspberry Pi uses the UART to send out all the console information, let’s change that.

First in /etc/inittab comment out (near the end) the following line (adding # before it)

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

Now the terminal stops listening on the UART pins but the debug information is still send, to change this we change /boot/cmdline.txt and remove the following line:

console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

Now save and reboot and no debugging information should be send. I now can send some text from my Raspberry Pi to my Arduino by using the following command:

echo -e "This is the text I want to display on my Arduino \r\n" > /dev/ttyAMA0

The /dev/ttyAMA0 is the UART device of the Raspberry Pi. My setup is quite complicated since I use SSH to use my Raspberry Pi and the Arduino sends the information from the Raspberry Pi to my serial port.

Next time I talk about different ways to send data from the Raspberry Pi to the Arduino. Possible solutions, PHP, node.js, Python,…

Comments