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

Circuit

The Arduino circuit created in TinkerCAD
This circuit looks similar to the one that was done in the guide but now with a potentiometer added. Let's start by connecting the button to the circuit: Connect three wires to the board. Two of these wires should connect the 5 volts (5V) and ground (GND) to one side of the button. Now insert a 10k Ohm resistor in the connection between the button and the wire that goes to the ground. The third wire should be connected to one of the legs on the other side of the button, to one of the pins on the board. This will be the wire that reads if the button is pushed or not. Next, let's connect a LED to the circuit: Start by connecting a wire from a pin to the positive side of the LED. Then from the negative side, connect a 220 Ohm resister from the LED to the ground. When you are finished your circuit should look similar to the picture above.

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.

Articles mentioning this page

Guides