Description
I loved this week’s assignment! Having never explored many analog mechanisms before, I enjoyed playing with the potentiometers (like a DJ…you’ll see how this ties in later). After completing the soldering and basic set-up steps, I dove into the main lab activities.
Part 2: Single Potentiometer
I first created a simple circuit with three LEDs and a single potentiometer. I connected the red wire to the 5V pin, the yellow wire to A0, and the black wire to ground. I then ran two scripts: one for blinking and one for fade. One detail I noticed was that the code for controlling the blinking intensity used digitalWrite while the code for light intensity used analogWrite.
/* Blinking code */int sensorPin = A0; // select the input pin for the potentiometer
int redPin = 9; // select the pin for the LED
int greenPin = 10;
int bluePin = 11;
int sensorValue = 0; // variable to store the value coming from the sensorvoid 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);
// turn the ledPin on
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}/* Brightness code */int sensorPin = A0; // select the input pin for the potentiometer
int redPin = 9; // select the pin for the LED
int greenPin = 10;
int bluePin = 11;
int sensorValue = 0; // variable to store the value coming from the sensorvoid setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// 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);
analogWrite(redPin, sensorValue/4);
analogWrite(greenPin, sensorValue/4);
analogWrite(bluePin, sensorValue/4);
// stop the program for for <sensorValue> milliseconds:
delay(10);
}
Part 3: Two Potentiometers
The final step was to bring in multiple potentiometers. I chose option 1 since my third potentiometer’s wires accidentally detached 🥲. Using additional wires for the 5V and ground pins, I extended the circuit to include two potentiometers. As for the code, I slightly modified the variables sensorPin and sensorValue to account for both pots. In the loop, I merged the two separate scripts from earlier together so that instead of turning the LEDs on using digitalWrite (from the original blinking code), the program would read in the sensorValue from one of the pots.
/* Multiple Pots */int sensorPin1 = A0; // select the input pin for the potentiometer
int sensorPin2 = A1;
int redPin = 9; // select the pin for the LED
int greenPin = 10;
int bluePin = 11;
int sensorValue1 = 0; // variable to store the value coming from the sensor
int sensorValue2 = 0;void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the ledPin as an OUTPUT:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}void loop() {
// read the value from the sensor:
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
analogWrite(redPin, sensorValue1/4);
analogWrite(greenPin, sensorValue1/4);
analogWrite(bluePin, sensorValue1/4);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue2);
// turn the ledPin off:
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue2);
}
After making sure the code worked, I tried twisting the pots around to make the LEDs blink and fade in sync with music! Watch the video below to see the results (recommend starting from 0:40). ✨
Components Used
- Arduino
- Breadboard
- (3) LED lights (red, green, blue)
- (2) Potentiometers
- (6) Wires
- (3) 220 ohm resistors
Images
Reflection
I really enjoyed the feeling of twisting the potentiometers to change the states of the LEDs. Because it’s such a smooth motion, I would like to further explore this interaction in the future with the midterm project/final project.