Skip to content

Lab 5: Analog Out with an Arduino

June 13, 2009

First off, an aside: the reading, The User Illusion, Chapter 6: The Bandwidth of Consciousness by Norretranders, is pretty eye-opening.  The premise is this: our senses take in something on the order of 40 million bits of information per second, but we are only conscious of perhaps 1 million. We discard a tremendous amount of information to arrive at a state we call consciousness.  It is a remarkable act of ordering, like cutting a clearing in the midst of a jungle to build a house.  

It got me thinking about Matt Ruff’s Set This House in Order, a novel about multiple personality disorder.  This is not really the place to explore this, and I really don’t know much about it, but it struck me that in the case of this disorder, consciousness might be split, and different sensory input would be discarded by each.

Onto the lab.  

Lab 5: Servo Motor Control with an Arduino

So this time we’re going to use the Arduino to control a device that needs an analog signal (variable voltage), a little servo motor.  A servo is a motor that has an arm that can be set to a particular location, rather than simply spinning continuously.  I used the pot again to generate the signal, and should really be having more fun with sensors, so I’ll circle back to these labs again later.  I will in the meantime unveil some sensor work when I discuss the Midterm project.

We controlled the servo in two ways, requiring two different programs: the first is called a “pulse” method, which controls the servo by the length of time it receives a digital pulse.  The second uses a prefab servo “library” written for the Arduino, and it controls the exact position of the servo in degrees from 0 to 180.  The first method requires more trial and error to get the positions right, but I could imagine it being useful in some applications, for example, where you want to have an indirect correlation between the sensor and servo.  I’ll go into this a little more below.

The Pulse Method

The board gets set up the same way as Lab 4, and then the servo is connected to the Arduino at digital output pin 2.  The schematic from the lab is below:

P6010173

servo_schNote that the schematic is showing a voltage divider, which would be necessary if I was using a sensor, but the pot already is a voltage divider.

And here’s the code:

No Serial Output

The serial monitor should be displaying the output as it did in the previous lab, but I realized I forgot to tell the Arduino to do that in the program.  Here’s the revised program with the Serial.println() command added to the loop:

Serial Output

In a nutshell, the Arduino maps the range of the pot between min and max to the length of the pulse it sends to the servo.  When the pot is set to 0, the Arduino sends half-second pulses to the servo, with 20 milliseconds between.  At full open (1023), the Arduino sends 2 1/2-second pulses to the servo.  The short pulse sets the servo to 0 degrees, and the long pulse to 180 degrees.  That’s reasonably correlated to the amount of time it takes to turn the pot through its full arc.  You can alter this relationship by changing the length of the pulses: a longer pulse will take the servo longer to go to 180 degrees.

The Arduino Servo Library

By utilizing the Arduino servo library, you can set the servo to exactly the degree setting you wish:

Arduino Servo Library

The output pin has also been moved to 9, which is a Pulse Width Modulation (PWM) pin.  The Arduino servo library does the work of figuring out the relationship between pulse length and servo position.

P6020175

I don’t have a lot to say about this one, except that it works very well.

 

Advertisements
2 Comments
  1. Ralph permalink

    Same comment as for Lab 4. I have difficulty reading the code from the screen captures, although the captures are a little bigger (slightly easier to read) than in Lab 4.

    Thanks for your work. This stuff makes me feel young again.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: