Lab 3: Sensing — Potentiometer
This week’s lab had us explore analog inputs with potentiometers. Zeke was super helpful during office hours in helping explain how the potentiometers, resistors, and LEDs work together as a circuit. Potentiometers act somewhat like a resistor, but allows for more user inputted through twisting the knob, which controls the LEDs’ brightness and blinking rate. We had to solder three wires to the potentiometer to build the circuits.
Components Used
- Arduino
- Breadboard
- LED lights
- Wires
- 220 ohm resistors
- Potentiometers
Part 1: Soldering
I haven’t soldered in years, so it was super exciting to get back into it. Thank you Tonya for showing me the ropes and for being so attentive!
Part 2: Controlling One LED from the Potentiometer
During Zeke’s invention lab hours, we tested out the potentiometers I soldered to make sure they actually worked well. I connected the black wire to ground, yellow wire to pin A0, and red wire to power, or pin 5V.
I then extended my three LED circuits to include the potentiometer according to the lab instructions. It took a few tries to get the code working, then altered it so that it could blink three LEDs at the correct pins. I uploaded the code and was able to verify that the potentiometer was working.
Part 3: Multiple Pots Controlling Multiple LEDs
Option 1: Using two potentiometers
I used two potentiometers — one to control the brightness of all the LEDs together and the other potentiometer to control the blinking of the LEDs all at the same time. I wasn’t sure how to connect my second potentiometer at first because there’s only one 5V pin. I learned from Zeke that the two potentiometers can actually share both the 5V pin and the ground pin, which makes sense considering that the circuit still completes. I then connected the yellow wire to the A1 pin.
To alter the sample code, I added another variable to represent the second potentiometer. However, when I uploaded my code and twisted the potentiometers, the LEDs were super dim.
I then learned from both Tonya and Zeke that I could apply how I have connecting the ground wire through the rail to power as well. So, I connected my red wire (power) connected to the 5V pin to the rail, and also rewired the potentiometer wire to be on that rail.
Future work
I’m sad that I didn’t have time to work on extra credit, but I would definitely try to control each LED with a potentiometer and see how it would change how light diffuses through our previous lab.
Code
Controlling blinking with one potentiometer
/*
Original code
created by David Cuartielles
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInput
*/
int sensorPin = A0; // select the input pin for the potentiometer
int greenLED = 10;
int redLED = 11;
int blueLED = 9;
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(blueLED, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, HIGH);
digitalWrite(blueLED, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(blueLED, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
Fade and blink with two potentiometers
/* POT to LED test -> by Owen Mundy March 11, 2010
from: http://itp.nyu.edu/physcomp/Labs/AnalogIn
—————————————————————*/int sensorPin = A0; // Analog input pin that the potentiometer is attached to
int blinkPin = A1; //Second potentiometer that controls the blink rate
int potValue = 0; // value read from the potentiometer
int blueLED = 9; // PWM pin that the LED is on
int greenLED = 10;
int redLED = 11; int sensorValue = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the ledPin as an OUTPUT:
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(blueLED, OUTPUT);
}void loop() {
int potValue = analogRead(sensorPin); // reads the potentiometer value
Serial.println(potValue);
analogWrite(greenLED, potValue/4);
analogWrite(redLED, potValue/4);
analogWrite(blueLED, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
sensorValue = analogRead(blinkPin); //using the 2nd potentiometer to control the blink ratedelay(sensorValue);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(blueLED, LOW);// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}