Wednesday, 23 June 2010

Two Open Source Worlds Collide: Using Arduino Sensors with the BeagleBoard

Some of the comments from the Wired and Gizmodo articles were from folks asking what they could possibly do with open source hardware that they couldn't do with something finished, like an iPad.

I've never been one to compare myself to Apple, mostly because they make it hard to hack. But being able to hack the hardware means a lot of things. Most of the time I need the hardware to do something pretty specific that no handheld computer really comes with, like sensors.

It could really be any sensor, because as long as I had input/output pins I could tap into, I could modify the hardware to do all sorts of things. Since I had a bunch of sensors I had already been using with the Arduino, something simple I could do would be to take my sensor setup, and integrate into the BeagleBoard computer.

The part I really liked is that I could swap out whatever sensors I wanted, and still have it talk to my BeagleBoard computer. Of course, I also got a kick out of being able two use both of my favorite open source hardware platforms at the same time…

For this project, I used an Arduino Duemilanove, a 3-axis accelerometer, BeagleBoard C4 and a BeagleTouch. When connecting anything to the BeagleBoard via USB, a 4-port USB hub is useful too.

P1010076
Setting up the Arduino + Sensors + BeagleBoard on the BeagleTouch was actually pretty straightforward. The Arduino and Sensors can start outputting data with just a few lines of code found on a PDF cheatsheet, in this case for the accelerometer. Then, I got a couple drivers onto the BeagleBoard, and plugged the Arduino into the BeagleBoard over USB, and voila :-)

When connecting the BeagleBoard to the Arduino for the first time, I needed to install some USB drivers.

Step 1 (on Desktop): Download USB Serial and FTDI Linux packages

Before the Arduino could talk to the BeagleBoard, as with many computers, the BeagleBoard needed to have the right FTDI and USB serial drivers. I’ve uploaded them to the Liquidware server here:

usbserial.ipk
ftdi.ipk

Step 2: Copy these files to an BeagleBoard  boot SD card

To put these on the BeagleBoard, I copied the USB Serial and FTDI packages to my BeagleBoard boot SD card (root directory).

Step 3 (on BeagleBoard): Plug SD card into BeagleBoard and BeagleTouch, and power on

P1010075

Once booted, the BeagleTouch will show two storage spaces in the upper right corner: the BeagleBoard flash memory, and the SD card.

Open up both, then drag and drop usbserial.ipk and ftdi.ipk to the BeagleBoard flash.

P1010077
Step 4: Install packages on BeagleBoard

Open up Terminal on the BeagleBoard, and type the following commands:

cd Desktop
opkg install usbserial.ipk
opkg install ftdi.ipk

P1010091 
The BeagleBoard is now capable of talking to the Arduino.

NOTE: I only needed to do this the very first time I connect the Arduino to the BeagleBoard, and I can start at Step 5 the next time I do it.

Step 5: Attach a sensor to the Arduino

Before I plug in my Arduino and sensor to the BeagleBoard, I want to wire it up and pre-program it. I decided to use an accelerometer for this demo, because it’s really easy to see the response.

I wired up the accelerometer using the instructions in the accelerometer cheatsheet (PDF). A mini breadboard and some jumper wires comes in handy here.

Accelerometer pins –> Arduino pins
GND -> Ground
Vin -> 5V
Gs1 -> Ground
Gs2 -> 5V
X0 -> Analog 0
Y0 -> Analog 1
Z0 –> Analog 2

P1010092

Step 6: Upload accelerometer code to the Arduino

First, I connected the Arduino + Accelerometer setup to the computer with a standard Type B USB cable.

Switching over to the Antipasto Arduino IDE (IDE install instructions here, if you don’t have it already), I copied and pasted the code into the IDE, then pushed upload.

void setup() {
    Serial.begin(9600);
}

void loop() {
    int valueX = analogRead(0);
    Serial.print("X: ");
    Serial.print(valueX);
    Serial.println(" ");
    int valueY = analogRead(1);
    Serial.print("Y: ");
    Serial.print(valueY);
    Serial.println(" ");
    int valueZ = analogRead(2);
    Serial.print("Z: ");
    Serial.print(valueZ);
    Serial.println(" ");
    Serial.println(" ");
    delay(250);
}
imageNow, the Arduino is ready to be connected to the BeagleBoard.

Step 7 (on BeagleBoard): Start reading sensor information on the BeagleBoard

To do this, I unplugged the Arduino + Accelerometer setup from my computer, and plugged it into the USB hub attached to my BeagleBoard.

Then I went back to Terminal on the BeagleBoard and typed:

cd
insmod lib/modules/2.6.34-rc3/kernel/drivers/usb/serial/usbserial.ko insmod lib/modules/2.6.34-rc3/kernel/drivers/usb/serial/ftdi_sio.ko

This loads the USB drivers that I installed earlier. To start reading data from the Arduino over serial, I typed the following in Terminal:

cd
screen dev/ttyUSB0 9600

P1010106

And that’s it! The accelerometer is now writing data directly to my BeagleBoard and BeagleTouch.

BeagleTouch Accelerometer
P1010112 Here’s a video of everything in action:

This can also work with a light sensor, compass module, or humidity sensor…or just about any sensor I can run off my Arduino. I wonder if I can do that with my iPad :-)

0 comments:

Post a Comment