Lab #3: Sensing: Potentiometer
Shirley Zhang. INFO/NWMEDIA C262, Fall 2022.
Description
We worked on exploring analog input and were introduced to potentiometers in this lab session. By turning the “knobs” on the potentiometer, we can easily adjust the resistance of the device and therefore vary the input values for Arduino. I was fascinated when learning that the potentiometer was the mechanism behind so many electronic devices in our daily life, even my favorite game console: joy-cons.
Soldering
After several practices in different classes, I am now pretty used to the soldering process. I also came up with my personal trick for soldering the potentiometer, which is starting with the middle foot. Leaving the middle one till last will make it hard to be soldered. To enhance the durability of my devices, I added heat shrink tubes to protect the connection.
Controlling LED from one Potentiometer
During the lab, I started with the example code on the Arduino website to control the blinking rate of the LEDs with one potentiometer. Then I changed the “digitalWrite” function to the “analogWrite” function. So instead of the blinking rate, the brightness of the LED will be adjusted while turning the knob.
int sensorPin = A0; // the input pin for the first potentiometer
int redPin = 9; // select the pin for the LEDs
int greenPin = 10;
int bluePin = 11;
int sensorValue = 0; // variable to store the value coming from the sensor
int ledValue = 0;void setup() {
// declare the ledPin as an OUTPUT:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
ledValue = round ((sensorValue * 0.00097) * 255);
// turn the ledPin on
analogWrite(redPin, ledValue);
analogWrite(greenPin, ledValue);
analogWrite(bluePin, ledValue);
}
Controlling LEDs from three Potentiometer
For this part of the exercise, I installed three potentiometers and three LEDs and then used each potentiometer to control an individual LED.
Control Blinking Rate
Like the former attempt, I started by using the potentiometer to control the blinking rate of each LED. When the sensor value gets higher, the blinking rate will become faster with it.
int sensorPin1 = A2; // the input pin for the first potentiometer
int sensorPin2 = A1; // select the input pin for the second potentiometer
int sensorPin3 = A0; // select the input pin for the third potentiometer
int redLed = 9; // select the pin for the red LED
int greenLed = 10; // select the pin for the green LED
int blueLed = 11; // select the pin for the blue LED
int sensorValue1 = 0; // variable to store the value coming from the sensor
int sensorValue2 = 0;
int sensorValue3 = 0;
int redValue = 0;
int greenValue = 0;
int blueValue = 0;void setup() {
// declare the ledPin as an OUTPUT:
pinMode(redLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}void loop() {
// read the value from the sensor:
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
sensorValue3 = analogRead(sensorPin3);
digitalWrite(redLed, HIGH);
delay(sensorValue1);
digitalWrite(redLed, LOW);
delay(sensorValue1);
digitalWrite(greenLed, HIGH);
delay(sensorValue2);
digitalWrite(greenLed, LOW);
delay(sensorValue2);
digitalWrite(blueLed, HIGH);
delay(sensorValue3);
digitalWrite(blueLed, LOW);
delay(sensorValue3);
}
Control Brightness
I updated the one-potentiometer version code to the three-potentiometer one, and the code runs well. But I encountered a challenge when trying to turn the knob: the LED light was not so stable and flickered. That might be caused by the wires working loose on the breadboard.
int sensorPin1 = A2; // the input pin for the first potentiometer
int sensorPin2 = A1; // select the input pin for the second potentiometer
int sensorPin3 = A0; // select the input pin for the third potentiometer
int redLed = 9; // select the pin for the red LED
int greenLed = 10; // select the pin for the green LED
int blueLed = 11; // select the pin for the blue LED
int sensorValue1 = 0; // variable to store the value coming from the sensor
int sensorValue2 = 0;
int sensorValue3 = 0;
int redValue = 0;
int greenValue = 0;
int blueValue = 0;void setup() {
// declare the ledPin as an OUTPUT:
pinMode(redLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}void loop() {
// read the value from the sensors:
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
sensorValue3 = analogRead(sensorPin3);
//calculate the LED luminance;
redValue = round ((sensorValue1 * 0.00097) * 255);
greenValue = round ((sensorValue2 * 0.00097) * 255);
blueValue = round ((sensorValue3 * 0.00097) * 255);
// turn the LED on
analogWrite(redLed, redValue);
analogWrite(greenLed, greenValue);
analogWrite(blueLed, blueValue);
}