Intro: How to Make a BeagleBone and an Arduino Communicate
Say you've got this nice Ardunio project that provides serial data, but it needs a FTDI and you don't want to tie up your BeagleBone's (or BB) USB port or use a hub. Well, it's not as hard as you think, and you don't need a FTDI!
The BB has 4 TTL serial ports available, and this how-to will show you how to use one to talk to an Arduino. I'm going to show this by using a minimal Arduino on a breadboard, but you can do this with a normal Arduino as well, just use an external power adapter and don't plug in the USB. The "gotcha" to this is that the BB's ports run at 3.3VDC and the Arduino runs at 5VDC. We'll solve that using a logic level shifter.
Things you'll need:
- Breadboard and some jumper wires
- a BeagleBone (any revision including black should work the same)
- an Ardunio
- a Logic level shifter that supports 5v-3.3v and bi-directional on the TX line. I'm using Sparkfun's
- you know how to program and do some basic wiring with an Ardunio
- you know how to install python packages and run scripts on your BeagleBone
Step 1: Program the Ardunio
First we'll make a simple test program on the Ardunio. This test program (attached) is fairly simple, but provides some visual feedback via pin13 LED so that we know it is working. This sample program uses a serial read function that supports up to a 20 character string, so it should be pretty usable for more than testing.
the basic logic is this:
- in setup(), turn on LED for 2 seconds and wait, this gives us some warning in case were not ready for serial data yet.
- write an "A" to the serial port
- turn on LED for .2 second
- read serial port and if it reads an "A" , turn on LED for 1 second.
program the Arduino in whatever method you are accustomed to. when programmed and powered on, the led should blink about 5 times a second.
Step 2: Program the BeagleBone
I've come to like a toolkit called PyBBIO. it adds a framework to python that's very Arduino/Wiring like, but still lets you easily use any python libraries and features you want. The github page has some good instructions, though if you're using a distro other than Angstrom on it, the python package name may be different.
I'm just using the PyBBIO serial echo example as is, it simply takes whatever it receives on Serial2, prints it, and sends it back. This should give our Arduino program back the "A" it sends, which will cause it to then turn the LED on for 1 second. If there's any bad wiring or external noise corrupting the signal, the Arduino wont' see the "A".
You can also write your own serial echo script in whatever language you want.
Step 3: Wire It All Up!
The Sparkfun level shifter has a low voltage side and a high voltage side, and supports 2 channels of data. a simple voltage divider won't work for this as it's not bidirectional, and we need the 3.3v TX data to be raised to the 5V on the Arduino. We're only going to use one channel for this demonstration. It needs to be powered from the BB's 3.3v on the LV side, and from the Arduino's 5V on the HV side. There also needs to be a common Ground connection between the BB and Arduino or the data can be corrupted on the wire. I'm using the bread board's blue rail as the common ground, and both rails are connected together. I also have 5V on both red rails because the minimal Arduino needs it.
I've included a Fritzing showing what this might look like with a normal Arduino.
Start with everything powered off, just in case.
First, lets put power to the level shifter:
- Connect both GND pins to the ground,
- Connect the HV pin to the bread board 5V rail
- the LV pin will get connected to the BB later
Next, connect the BeagleBone:
- P9 pin 1 (GND) to the breadboard ground rail
- P9 pin 3 (3.3v) to the level shifter LV pin
- P9 pin 21 (Serial2 TX) to one of the level shifters low side TX pin (TXI)
- P9 pin 22 (Serial2 RX) to one of the level shifters low side RX pin (RXO)
- in the photo, ignore the wire going to the Arduno's reset pin, that's for a later Instructible.
Finally, connect the Ardunio. we're making what's known as a "null modem cable" in PC land. The BB's TX data needs to end up on the Arduino's RX pin:
- D0 (RX) to level shifters high side TX pin (TXO).
- D1 (TX) to level shifters high side RX pin (RXI)
- LED on pin 13 as you normally would
NOTE: If you have a 3.3V Arduino, you can connect the BB and Ardunio together directly, without the level shifter.
NOTE2: I've found communication to be more reliable if the RX and TX wires are twisted around each other like this. The twisting causes EMF interference to be reduced.
Power up the Arduino and the LED should start blinking as before.
Boot up the BB and move to the next step
Step 4: Make It So
Now, open a terminal (ssh, usb serial, or local GUI) and find the serial_echo.py script and run it with python.
You should see the "A" received, and the Arduino should change to a short-blink--long-blink pattern. If it doesn't, you have something off in the wiring. Make sure the RX's and TX's are right, if you get the backwards it won't hurt anything, just don't connect the Ardunio's 5V and BeagleBone bone directly together anywhere because it will damage the BeagleBone. Only the GND can be connected directly in this demonstration.
make a framework to get your data back and forth! If you're working from scratch, the Ardunio SerCommand library is very easy to use.
Check out my follow up Instructible that will make use of that reset wire to program the Ardunio from the BeageBone! no more pulling chips or connecting FTDI in awkward installations.