Thursday, October 3, 2013

Potentiometer Controlled LEDs

Another part of my Interactive Town Play Mat project is a series of "street lights" along a few of the streets. I originally thought about using a light sensor that would automatically turn on the streets lights when the ambient light level grew dim (like a night light), but I decided to scrap this idea after several prototypes. The main problem was that the light sensor that came with the Lilypad ProtoSnap is very fidgety, in that the reading from the sensor fluctuates even at a constant ambient light level. If I set a certain light reading level (say, <50) as the trigger for when the LEDs would turn on, when the ambient light dropped near that level, the light sensor would continually flip above and below that threshold, causing the lights to flicker on and off. The sensor works fine for gross changes in light (like when the sensor is covered) but I could not figure out a way to effectively apply it with gradually dimming ambient light.

So instead I decided to incorporate a manual "dimmer" control to control the lights. This is accomplished through the use of a potentiometer.

The circuit is represented by the diagram below, made using the open-source software from Fritzing.org.


A brief video of how it works:


The full sketch is included at the bottom of this post for reference. The neat new function was using map(potReading, 0, 1024, 0, 255) to convert the analog readings from the potentiometer (ranging from 0-1024) to the output range for the digital pins (ranging from 0-255).

So I thought this would be a very straightforward circuit and sketch to create. The two tutorials I reference at the end of this post helped me understand how to read a potentiometer using an Arduino, and then how to output the signal to a string of LEDs. However, when I applied power to the circuit the first time, no LEDs turned on. I had set up the serial monitor to indicate both the potentiometer reading as well as the brightness level being sent to the LED, but turning on the serial monitor indicated readings of zero across the board.

Some debugging and browsing Arduino reference sites revealed that I had declared the potPin variable incorrectly. One of the tutorials I referenced used "const byte" to declare the variable for the analog pin that was reading the potentiometer, but apparently this would not work for the Uno because it used "A1" for the analog pin rather than just "1". Or something. Anyway, all I know is that when I switched to "const int" to declare that variable it worked, and I started getting readings through the serial monitor.

Unfortunately, the serial monitor output seemed to lag considerably behind what the potentiometer was set to, as in I could turn the pot full on but the serial monitor would still display a low value for several more seconds. I figured out that this was due to a bug in Codebender, and when I used the Arduino IDE the serial monitor kept in sync perfectly.

And while the serial monitor was showing the right values, for some reason the LEDs were still not lighting. It took me awhile to figure out that I had forgotten to turn on the little on/off slider switch that I had added to the circuit. DUH! This just goes to show that so many little things can cause a circuit to fail, and it's part of the challenge of electronics to follow the trail and try to find and fix every single little thing.

References:

http://www.arduino.cc/en/Tutorial/Potentiometer
http://www.electroschematics.com/9009/led-brightness-fan-speed-arduino/

The Sketch:

/*
 Potentiometer Controlled Street Lights
 Sketch that adjusts the brightness of a string of LED lights
 based on analog readings of a potentiometer

 Created 9/30/13
 By Ken Yeh

*/

/*  Pin Definitions  */
const int potPin = A1;  // Potentiometer connected to analog input A1
const int ledPin = 3;     // LED connected to PWM pin 6
int potReading;   // Variable to hold the reading from the potentiometer
int ledBrightness;  // Variable that holds the LED brightness level

void setup()
{
 pinMode(ledPin, OUTPUT);
 pinMode(potPin, INPUT);
 Serial.begin(9600);
}

void loop()
{
 potReading = analogRead(potPin); // Read value from potentiometer
 ledBrightness = map(potReading, 0, 1024, 0, 255); // Converts analog reading from pot to 8-bit value  
 analogWrite(ledPin, ledBrightness); // Output at this level for the LED
 Serial.print("Potentiometer=");
 Serial.print(potReading);
 Serial.print("   LED Level=");
 Serial.println(ledBrightness); 
}

1 comment:

  1. Things are looking great, Ken. You have really been incorporating lots of things and extending the project into many new knowledge areas. I like your use of the part of the sewing dev board in your potentiometer video :)

    ReplyDelete