Analog Input and Output aka Dimming Lights! (sort of)

Posted: September 26th, 2010 | Author: genevieve | Filed under: PhysComp | 1 Comment »

Week 2 Lab Assignment in PComp was to dim and brighten and LED with an analog sensor. First, I set up my arduino and breadboard with a simple circuit where a potentiometer would control the brightness on an LED. This worked for me without any problems.

arduino and breadboard with potentiometer and LED

Here I am controlling the brightness of the LED with the pot sensor.

Next I wanted to see if I could do the same thing, but using a different analog sensor. I settled on an FSR, a force sensing resistor and wired up the same circuit.

arduino with breadboard and Force Sensing Resistor

Here I am controlling the LED’s brightness with an FSR:

So, the second part of the lab was a bit more complicated, and called for building a “voltage divider” circuit, where two analog sensors would control the brightness of two different LEDs. I used the FSR to control the brightness of a smaller green LED, while a photocell controlled the brightness of the large blue LED. Here’s my circuit ready to be programmed:

arduino, breadboard, fsr sensor, photocell and LEDs

here's where things got more complicated

I adjusted the code from the site to accommodate one more sensor and LED. Here’s my code:

 int photoPin = 0;    // Analog input pin that the photocell is attached to
 int photoSenValue = 0;   // value read from the photocell analog sensor
 int led = 9;    // PWM pin that the LED is on.  n.b. PWM 0 is on digital pin 9
 
 int fsrPin = 1; // Analog input pin that the FSR is attached to
 int fsrSenValue = 0; // value read from the photocell analog sensor
 int led2 = 10; // PWM pin that the second LED is on
 
 void setup() {
   // initialize serial communications at 9600 bps:
   Serial.begin(9600); 
   // declare the led pin as an output:
   pinMode(led, OUTPUT);
   pinMode(led2, OUTPUT);
 }
 
 void loop() {
   photoSenValue = analogRead(photoPin); // read the photocell value
   fsrSenValue = analogRead(fsrPin); // read the fsr value
 
   // map the sensor vaue from the input range (400 - 900, for example) 
   // to the output range (0-255). Change the values 400 and 900 below
   // to match the range your analog input gives:
   int brightness = map(photoSenValue, 400, 900, 0, 255); 
   int brightness2 = map(fsrSenValue, 400, 900, 0, 255);
 
   analogWrite(led, brightness);  // set the LED brightness with the result
   Serial.println("photoSenValue = " + photoSenValue);   // print the pot value back to the debugger pane
   delay(10);                     // wait 10 milliseconds before the next loop
 
   analogWrite(led2, brightness2);
   Serial.println("fsrSenValue = " + photoSenValue);
 }

At some point I also realized that I could stick my FSR into my breadboard, and did that so I wouldn’t have to worry about shorting my circuit if the alligator clips touched. Anyway, here I am running the code, which was sort of successful, but definitely needed to be finessed to make things look more responsive. I am thinking that I either needed to use different resistors for my sensors, and/or map the sensor values to the light output values differently in the code.


One Comment on “Analog Input and Output aka Dimming Lights! (sort of)”

  1. 1 Tom Gerhardt said at 3:55 am on September 27th, 2010:

    TRU,
    that is what often happens with things get more complicated.

    usually when you add another part to your circuit and what you had before stops working, its usually a wiring/circuit/logic error… so I’d check that; but I’d also make sure that the FSR is connecting correctly in the breadboard, they aren’t really made to be stuck in there and usually connect unpredictably.

    Nice though!


Leave a Reply