Sensing - Potentiometers

Posted by karthik

karthik's picture

 

Objective

 Explore analog input by using Arduino to read pot values and control the LEDs as a function of the pot. 

Materials Required

Arduino, Potentiometer x 2, LEDs x 3 ,   220 ohm resistor x 3, connecting wires

Description

Part 1 - First, I soldered connecting wires onto the potentiometer.

Part 2 - One pot controls brightness of one LED (see attached pics)

Code:

 /*

* one pot fades one led

 * modified version of AnalogInput
 * by DojoDave <http://www.0j0.org>
 * http://www.arduino.cc/en/Tutorial/AnalogInput 
 */
int potPin = 2;   // select the input pin for the potentiometer
int ledPin = 9;   // select the pin for the LED
int val = 0;      // variable to store the value coming from the sensor
void setup() {
  Serial.begin(9600);
}
void loop() {
  val = analogRead(potPin);    // read the value from the sensor, between 0 - 1024
  Serial.println(val);
  analogWrite(ledPin, val/4); // analogWrite can be between 0-255
}
-------------------------------------------------------------------------------
Part 3 - One pot controls brightness, the other blinking

Code:

 

/*
* one pot dims, the other pot changes the blinking rate
* modification of the following
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int pot1Pin = 0;   // select the input pin for the potentiometer 1
int pot2Pin = 1;   // select the input pin for the potentiometer 2
int pot1Val = 0;   // variable to store the value coming from pot 1
int pot2Val = 0;   // variable to store the value coming from pot 2
int led1Pin = 9;   // select the pin for the LED 1
int led2Pin = 11;  // select the pin for the LED 2
void setup() {
   pinMode(led1Pin, OUTPUT);  // declare the led1Pin as an OUTPUT
   pinMode(led2Pin, OUTPUT);  // declare the led2Pin as an OUTPUT
}
void loop() {
   pot1Val = analogRead(pot1Pin);   // read the value from pot 1, between 0 - 1024, for dimming
   pot2Val = analogRead(pot2Pin);   // read the value from pot 2, between 0 - 1024, for blinking
   analogWrite(led2Pin, pot1Val/4); // dim LED to value from pot1
   delay(pot2Val);                  // stop the program for some time, meaning, LED is on for this time
   analogWrite(led2Pin, 0);         // dim LED to completely dark (zero)
   delay(pot2Val);                  // stop the program for some time, meaning, LED is OFF for this time
}

-------------------------------------------------------------------------------
Optional:

One pot acts as a counter, "counting" the number of LEDs to turn on. The other pot controls both brightness and rate of blinking of all 3 LEDs at the same time. Brightness is inversely related to blinking rate, i.e. when brighter, the LEDs blink slower.

Code:

 

/*
 * one pot counts the number of active pots, the other one controls both dimming and blinking
 */
int pot1Pin = 0;   // select the input pin for the potentiometer 1
int pot2Pin = 1;   // select the input pin for the potentiometer 2
int pot1Val = 0;   // variable to store the value coming from pot 1
int pot2Val = 0;   // variable to store the value coming from pot 2 
int led1Pin = 9;   // select the pin for the LED 1
int led2Pin = 10;  // select the pin for the LED 2
int led3Pin = 11;  // select the pin for the LED 3
void setup() {
   pinMode(led1Pin, OUTPUT);  // declare the led1Pin as an OUTPUT
   pinMode(led2Pin, OUTPUT);  // declare the led2Pin as an OUTPUT
}
void loop() {
   pot1Val = analogRead(pot1Pin);   // read the value from pot 1, between 0 - 1024, for counting
   pot2Val = analogRead(pot2Pin);   // read the value from pot 2, between 0 - 1024, for blinking+dimming
   if(pot1Val <= 341)
  {
    analogWrite(led1Pin,pot2Val/4);    
  }  
  else if(pot1Val <= 700)
  {
     analogWrite(led1Pin,pot2Val/4);    
     analogWrite(led2Pin,pot2Val/4);    
  }
  else
  {
     analogWrite(led1Pin,pot2Val/4);    
     analogWrite(led2Pin,pot2Val/4);     
     analogWrite(led3Pin,pot2Val/4);    
  }
   delay(pot2Val);                  // stop the program for some time, meaning, LED is on for this time
   analogWrite(led1Pin, 0);         // dim LED to completely dark (zero) 
   analogWrite(led2Pin, 0);         // dim LED to completely dark (zero) 
   analogWrite(led3Pin, 0);         // dim LED to completely dark (zero) 
   delay(pot2Val);                  // stop the program for some time, meaning, LED is OFF for this time
}
-------------------------------------------------------------------------------------------------------------------------------

 

Single Pot controls brightness (2)
Single Pot controls brightness (1)
Counter = 3, Blinking is slow and brightness is high
Counter = 1, Blinking is slow and brightness is high
2 Pots control brightness and blinking (2)
2 Pots control brightness and blinking (1)
0
Your rating: None
Tags: