Friday, December 28, 2012

Day 2: Getting to know the Potentiometer

I think the last time I actually built any kind of circuit was in my freshman year physics class in college (8 years ago). Unsurprisingly I've forgotten almost everything. Today as I started circuit number 2 in my SparkFun Inventor's Kit I was faced with a component called a potentiometer (or variable resistor).


This little fella here basically has three pins and a little knob on the top that you can rotate and is described in the SparkFun guide as a knob that can raise and lower resistance, like a dimmer switch or a volume knob. The two outer pins connect to the power and the ground, and the central pin which is the output. It seems that in general, the way that these things work is that a connection is made between some sort of resistive material (apparently graphite in cheap potentiometers) organized in an arc and the power. By turning the knob on the potentiometer, you adjust the amount of resistive material between the power and the output by moving the second contact to a position further on the arc. This site explains it far more clearly than me. It's a surprisingly simple and elegant design... and I always thought that the inner workings of any of these things would be impossible to understand!

The second project in the kit involves setting up the potentiometer so that it can adjust the speed of the blinking of an LED, and getting this to work was surprisingly simple. Basically I made two circuits. One of the circuits is between the arduino and the LED, and the other is between the arduino and the potentiometer.


It's at this point that the manual discusses some crucial aspects of the arduino board: analog and digital pins. Basically, the analog pins take a value between 0 and 5 volts and translates them into a number between 0 and 1023 (more on this later). The digital pin is for dealing with values of things that are either off (0 volts) or on (5 volts) like say a blinking LED.

Now the code:

int sensorPin = 0;
int ledPin = 13;

void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
int sensorValue;
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH);
delay(sensorValue);
digitalWrite(ledPin, LOW);
delay(sensorValue);
}

If you are unfamiliar with Arduino code, this might look complicated but it's actually pretty straightforward, especially if you know some programming. Here are the key lines:


sensorValue = analogRead(sensorPin);
This reads the value from the potentiometer and sets it to a variable called sensorValue

digitalWrite(ledPin, HIGH); This turns on the LED
delay(sensorValue);  This keeps the next line from running for sensorValue milliseconds 
digitalWrite(ledPin, LOW); This turns off the LED
delay(sensorValue);

That's it. Just read in the value of the potentiometer and turn the light on and off delaying by the returned value from the potentiometer. It's really surprising to me that such a simple set of code could get this done. I was expecting it to be much more complicated, but perhaps that is why arduino is so popular.

No comments:

Post a Comment