Potentiometer with LED
Introduction
In this guide, you will learn to combine a with a making a circuit that proportionally turns on the LED when the potentiometer is turned.
Before continuing this guide it is recommended that you have read- or have basic knowledge about and .
Supplies
- board
- Wires
- 220 Ohm
- (generic)
Circuit
Code
int analogPin = A0; //the analog input pin attach to
int ledPin = 9; //the led attach to
int inputValue = 0; //variable to store the value coming from sensor
int outputValue = 0; //variable to store the output value
void setup() {
pinMode(analogPin, INPUT);
pinMode(ledPin,OUTPUT);
Serial.begin(9600); //Opens the serial monitor so you can see what is happening
}
void loop() {
inputValue = analogRead(analogPin); //read the value from the potentiometer
Serial.print("Input: "); //prints "Input" in the serial monitor
Serial.println(inputValue); //print inputValue
outputValue = map(inputValue, 0, 1023, 0, 255); //Convert the signal that goes from 0-1023 proportional to the number of a number of from 0 to 255
Serial.print("Output: "); //print "Output"
Serial.println(outputValue); //print outputValue
analogWrite(ledPin, outputValue); //turn the LED on depending on the output value
delay(1000); // Pauses the script for 1 second before reading a new value
}
Challenges
Now that you have completed this guide on how to combine a LED with a push button, you can try to change your button circuit to light up one of three different LED’s at a time, every time you press the button. Only one LED should light up at a time. That way you cycle through all three colours with three button presses.