Potentiometers are variable resistors commonly used in electronic circuits to provide adjustable control over analog signals. When connected to an Arduino board, a potentiometer can be used to read analog values using one of the Arduino’s analog-to-digital conversion (ADC) pins. This allows us to incorporate user-adjustable input into our Arduino projects. In this article, we will focus on using a potentiometer to control the blinking rate of an LED.
Understanding Potentiometer Input
A potentiometer has three terminals: the outer two are fixed resistors, while the middle terminal is connected to a movable contact called the wiper. By adjusting the wiper position, the resistance between the middle terminal and either of the outer terminals can be varied. When connected to an Arduino’s analog input pin, the potentiometer’s resistance value can be read and converted into an analog value ranging from 0 to 1023 (10-bit resolution for most Arduino boards).
Circuit Diagram
Pot Pin is connected to Pin number 0 and Led pin 13 is directly connected to led in arduino uno
Example Code
The following code snippet demonstrates how to use a potentiometer to control the blinking rate of an LED:
int potPin = 0; // Input pin for the potentiometer
int ledPin = 13; // Output pin for the LED
void setup() {
pinMode(ledPin, OUTPUT); // Declare ledPin as an output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turns ledPin on
delay(analogRead(potPin)); // Pause program based on potentiometer value
digitalWrite(ledPin, LOW); // Turns ledPin off
delay(analogRead(potPin)); // Pause program based on potentiometer value
}
Explanation of the Code
In the provided code, we use the analogRead()
function to read the value of the potentiometer connected to the potPin
. This value is then used to determine the duration of the delay()
between turning the LED on and off. By adjusting the position of the potentiometer’s wiper, the delay duration can be varied, resulting in a change in the blinking rate of the LED.
Conclusion
Potentiometers provide a versatile way to introduce adjustable analog input to Arduino projects. By connecting a potentiometer to an Arduino board and utilizing the analog-to-digital conversion capabilities, we can read variable resistance values and convert them into analog values.