Skip to content

Lab 7: Serial Duplex using an Arduino

June 14, 2009

In the previous lab, I was only sending one sensor value (actually, a potentiometer’s value in that case).  In this lab, we’re learning to send multiple sensor values, which is trickier, as you need to track which value is coming from which sensor.

Here’s the setup:


The glove has an accelerometer attached to it, and is something Aaron and I made for the Midterm, soon to be unveiled here.  If you are sick of hearing me talk about it, and want to see some video right now, go to his blog:  Here’s the accelerometer up close:


An accelerometer reads acceleration in the x-, y- and z-axes, and is what is in your iPhone telling it to turn the picture to landscape or portrait.  Here is the breadboard and the Arduino up close:



My alligator clips make things a little difficult to read in the pictures, so here’s the schematic from the lab:


So that’s the hardware.  This is mostly a programming exercise, though:

1. Read and Send Serial Data

We start by just reading one sensor at Analog Pin 0, which is the y-axis on the accelerometer:

Y-Sensor 255

This is sending the value in decimal, which as I understand it is the default.  

2.  Send the Data in Many Formats

Here is a revised code, showing the signal in raw binary (showing up as ASCII equivalent), encoded binary, decimal, hexadecimal and octal:

Y-Sensor Multi-Out

The point of this exercise is that the raw binary is more efficient at data transfer, but pretty impossible to read compared to that old standby, decimal.

3.  Send the Values for More than One Sensor

Looking at the setup, you can see that two of the accelerometer pins are outputting to the Arduino: both the x- and y-axes.  The simplest way of serially outputting the data would to just have it send a string back to the monitor, with values interrupted by commas.  The problem with this of course is that it’s pretty hard to determine which is an x-value and which a y-value:

X-Y String Out

Not particularly useful.  To deal with this, you either use Punctuation or a Call-and-Response (Handshaking) Method:

4. Punctuation

The idea here is pretty simple.  Just put a line return at the end of the loop, and then you know which value is which.  There is something a little backwards about this setup as the x-value is mapped to pin 1, and the y-value to pin 0, so you get your read-out as y and then x, but that could be easily fixed.

X-Y Punctuated

Now here’s the code that finally takes full advantage of the hardware setup.  The accelerometer is outputting to pins 0 and 1 (y and x respectively), and the momentary switch is outputting to digital pin 2:

X-Y + D2 Punctuated

y- and x-values go down when you move the accelerometer in the direction of the arrow, and up when you move it backwards.  The 3rd digit registers a 1 when the momentary switch is depressed, and a 0 when it is released.

Again, the serial data can be outputted in many programs.  Here is a Processing sketch set up to read this output:

X-Y + D2 Punctuated Processing

I started getting some pretty wonky values in both Processing and Arduino, but I finally think I figured out that it was because I was not closing the serial connection to one program before starting it in another:

X-Y + D2 Processing WONKY


I will note though that I didn’t see the null values the lab noted I would see–unless they were the line space between data strings.  The following code didn’t seem to make any difference to output:

if (myString != null) {

The “whitespace”, or line spaces between the outputted strings have been

eliminated with the Trim command. The data has also been tabulated and labeled:

Processing Tab Switch

In this case, the momentary switch was depressed, but the accelerometer was at rest.Should be able to use it now to draw a circle, but haven’t gotten that to work yet. Once I do, I’ll update.

5. Call-and-Response (Handshake) Method

Serial data can also be kept organized by sending one string at a time, waiting for the receiver to request a new set.  I’m going to circle back to this once I’ve gotten the Processing portion of the Punctuation Method working.

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: