what's Potentiometer and how it works?
Experienced Full-Stack Developer with a strong background in React.js, MongoDB, Nest.js, and Laravel. Proficient in building scalable applications and RESTful APIs, ensuring optimal performance and scalability. Skilled in implementing responsive design principles and maintaining code integrity through rigorous testing practices. Adept at collaborating with diverse teams to deliver innovative solutions and meeting project deadlines.
A potentiometer is a type of variable resistor that is commonly used to control the electrical resistance in a circuit.

How it works?
A potentiometer typically has three pins. The first pin, often labeled VCC, is where you connect the power source, such as the 5V pin on an Arduino board. The second pin, referred to as the output pin, is where the variable voltage is obtained. Finally, the third pin, labeled GND or ground, is connected to the ground reference of the circuit.
Inside a potentiometer, there are typically two resistors connected in series. By turning the potentiometer, you can adjust the position of a wiper between these resistors, allowing you to control the amount of voltage passed through.
When the wiper is turned fully to the left, the resistance between the output pin and ground is minimized, resulting in no voltage being passed (0V), as the resistance becomes effectively 0. Conversely, when the wiper is turned fully to the right, the resistance between the output pin and ground is maximized, allowing the full input voltage (usually 5V in Arduino circuits) to pass through.
This way, you can precisely control the voltage output between 0V and the maximum input voltage (e.g., 5V) by adjusting the position of the wiper.
Simple example about how u can read the changing voltage:

// Define constants for maximum analog value and maximum voltage
const int MAX_ANALOG_VALUE = 1023;
const float MAX_VOLTAGE = 5.0;
float analogValue;
void setup() {
// Set A2 pin as input
pinMode(A2, INPUT);
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Read analog value from A2 pin
analogValue = analogRead(A2);
// Convert analog value to voltage
float voltage = analogValue * MAX_VOLTAGE / MAX_ANALOG_VALUE;
// Print voltage to serial monitor
Serial.println(voltage);
}
Output on Serial monitor when potentiometer turn to left :

Let's dive into a real project:

int ledPin = 3;
int readPin = A5;
int readValue;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(readPin, INPUT);
}
void loop() {
// Read the analog value from the pin
readValue = analogRead(readPin);
// Map the analog value to the range of 0-255
int brightness = map(readValue, 0, 1023, 0, 255);
// Print the mapped value to Serial monitor
Serial.println(brightness);
// Set the brightness of the LED using analogWrite
analogWrite(ledPin, brightness);
// Add a short delay to stabilize readings
delay(10);
}

