Introduction
Potentiometers are widely used in electronic circuits for various applications such as volume control, brightness control, and many more. However, interfacing them with microcontrollers like Arduino can enhance their functionality, allowing them to be integrated into more complex projects.
In this article, we will discuss the basics of interfacing a “Potentiometer“ with an Arduino and demonstrate how it can be used to control the brightness of an LED.
What is Potentiometer?
A potentiometer, often referred to as a “pot”, is a type of variable resistor that can be used to control the flow of electric current in a circuit. It consists of a resistive element that is connected to three terminals, two of which are connected to the ends of the resistive element and the third is connected to a movable contact that can slide along the resistive element. By adjusting the position of the movable contact, the resistance between the two end terminals can be varied, allowing for the control of the current flow in the circuit.
Hardware Components
To interface a Potentiometer with Arduino, you’ll need the following hardware components to get started:
Components | Value | Qty |
---|---|---|
Arduino UNO | – | 1 |
USB Cable Type A to B | – | 1 |
DC Power for Arduino | – | 1 |
Resistor | 220Ω | 1 |
LED | – | 1 |
Potentiometer | 10K | 1 |
Breadboard | – | 1 |
Jumper Wires | – | 1 |
Potentiometer Pinout
Pin No. | Pin Name | Description |
---|---|---|
1 | Ground | This end To connect the Ground |
2 | Output | This end is connected to the wiper to provide variable voltage |
3 | VCC | This end is required to power the module. |
Potentiometer Circuit
Make connections according to the circuit diagram given below.
Wiring / Connections
Arduino | LED | Potentiometer |
---|---|---|
5V | VCC | |
GND | GND | GND |
D10 | ANODE + | |
A0 | OUT |
Installing Arduino IDE
First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.
Code
Now copy the following code and upload it to Arduino IDE Software.
#define Blink_LED 10
#define POT_PIN A0
void setup() {
Serial.begin(9600);
pinMode(Blink_LED, OUTPUT);
}
void loop() {
// reads the input on analog pin A0 (value between 0 and 1023)
int In_POT_Value = analogRead(POT_PIN);
// scales it to brightness (value between 0 and 255)
int Brightness = map(In_POT_Value, 0, 1023, 0, 255);
// sets the brightness LED that connects to pin 3
analogWrite(Blink_LED, Brightness);
// print out the value
if((In_POT_Value >= 0 && In_POT_Value <=1023)|| (Brightness>=0 && Brightness<=255)) {
Serial.print("POT Value: ");
Serial.print(In_POT_Value);
Serial.print(", Brightness Value: ");
Serial.println(Brightness);
}
else
{
Serial.println("Undefined value");
}
delay(500);
Serial.println("*******************************************");
}
Code Explanation
This Arduino code defines two constants using the “#define” preprocessor directive. The first constant, “Blink_LED“, is assigned the value 10, which represents the digital pin to which an LED is connected. The second constant, “POT_PIN“, is assigned the value A0, which represents the analog input pin to which a potentiometer is connected.
In the “setup()” function, the serial communication is initiated with a baud rate of 9600, and the pin “Blink_LED” is set to output mode using the “pinMode()” function.
In the “loop()” function, the input value from the potentiometer is read using the “analogRead()” function and is stored in the “In_POT_Value” variable. This value is then mapped to a brightness level using the “map()” function, and the resulting value is stored in the “Brightness” variable.
The “analogWrite()” function is used to set the brightness of the LED connected to the “Blink_LED” pin based on the value of the “Brightness” variable.
The code then checks whether the input and brightness values are within the expected range and prints them to the serial monitor using the “Serial.print()” and “Serial.println()” functions. If the values are not within the expected range, it prints “Undefined value” to the serial monitor.
Finally, the “delay()” function is used to introduce a 500-millisecond delay before starting the next iteration of the loop.
Applications
Here are some applications of potentiometers:
- Volume control in audio equipment, such as amplifiers and speakers
- Brightness control in lamps and light fixtures
- Speed control in electric motors and fans
- Temperature control in ovens and thermostats
- Control of various parameters in electronic circuits, such as frequency, voltage, and resistance
- Control of user interfaces, such as knobs and sliders on electronic devices
- Measurement of physical quantities, such as position, displacement, and rotation
- Calibration of scientific instruments, such as oscilloscopes and signal generators
- Fine-tuning of musical instruments, such as guitars and synthesizers
Conclusion.
A potentiometer with Arduino can significantly improve the functionality of electronic circuits. It allows for more precise and flexible control over various parameters, making it a useful tool for hobbyists and professionals alike.