A potentiometer is a knob you can turn to change a value. Reading it lets your Arduino know how much you turned the knob.
Reading a potentiometer in Arduino
Start learning this pattern below
Jump into concepts and practice - no test required
int sensorValue = analogRead(pin);analogRead(pin) reads the voltage on the pin and returns a number from 0 to 1023.
The pin must be an analog input pin, like A0, A1, etc.
potValue.int potValue = analogRead(A0);int potValue = analogRead(A1);
Serial.println(potValue);This program reads the potentiometer connected to pin A0 and prints its value every half second to the Serial Monitor. You can see how the number changes as you turn the knob.
int potPin = A0; // Analog pin where potentiometer is connected int potValue = 0; // Variable to store the potentiometer value void setup() { Serial.begin(9600); // Start serial communication } void loop() { potValue = analogRead(potPin); // Read the potentiometer Serial.print("Potentiometer value: "); Serial.println(potValue); // Print the value delay(500); // Wait half a second before reading again }
The value from analogRead ranges from 0 (0 volts) to 1023 (5 volts).
Make sure your potentiometer is connected correctly: one side to 5V, the other to GND, and the middle pin to the analog input.
Use the Serial Monitor in the Arduino IDE to see the printed values.
Use analogRead(pin) to get the potentiometer position as a number from 0 to 1023.
Connect the potentiometer's middle pin to an analog input pin on Arduino.
Print the value to Serial Monitor to see how it changes when you turn the knob.
Practice
analogRead(pin) function do when reading a potentiometer on Arduino?Solution
Step 1: Understand analogRead function
TheanalogRead(pin)reads the voltage on the specified analog pin and converts it to a number between 0 and 1023.Step 2: Relate to potentiometer reading
Since a potentiometer outputs a variable voltage depending on its position,analogReadreturns a value representing that voltage level.Final Answer:
It reads the voltage level on the analog pin and returns a value from 0 to 1023. -> Option AQuick Check:
analogRead() returns 0-1023 value [OK]
- Thinking analogRead sets voltage instead of reading it
- Confusing analogRead with digitalRead
- Assuming analogRead returns voltage in volts
sensorValue?Solution
Step 1: Identify correct function for analog input
The function to read analog input isanalogRead(pin), not digitalRead or analogWrite.Step 2: Check variable assignment syntax
Assigning the result ofanalogRead(A0)tosensorValueuses the syntax:sensorValue = analogRead(A0);Final Answer:
sensorValue = analogRead(A0); -> Option CQuick Check:
Use analogRead() to read analog pin [OK]
- Using digitalRead instead of analogRead
- Using analogWrite which is for output
- Using a non-existent function readAnalog
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1000);
}Solution
Step 1: Understand analogRead output range
The potentiometer at mid position outputs about half the voltage, soanalogRead(A0)returns around 512 (half of 1023).Step 2: Analyze Serial output
The code prints the sensorValue every 1000 milliseconds (1 second), so the Serial Monitor shows a value near 512 each second.Final Answer:
A value close to 512 printed every second -> Option DQuick Check:
Mid potentiometer = ~512 output [OK]
- Expecting 0 or 1023 at mid position
- Forgetting Serial.begin causes no output
- Confusing analogRead with digitalRead values
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue;
sensorValue = analogRead(0);
Serial.print(sensorValue);
delay(500);
}Solution
Step 1: Check Serial output method
The code usesSerial.print(sensorValue)without a newline, causing consecutive values to print on the same line and appear garbled or unreadable.Step 2: Use println for readability
Serial.println(sensorValue)adds a newline after each value, making the output clear on the Serial Monitor.Final Answer:
Missing Serial.println instead of Serial.print -> Option AQuick Check:
Serial.print vs println for newlines [OK]
- Using numeric 0 instead of A0 for analogRead
- Confusing Serial.print and Serial.println
- Ignoring delay causing fast output
Solution
Step 1: Read potentiometer value correctly
UseanalogRead(A0)to get a value from 0 to 1023.Step 2: Map value to 0-255 for PWM
Usemap(sensorValue, 0, 1023, 0, 255)to convert the range for LED brightness.Step 3: Write PWM value to LED pin
UseanalogWrite(9, brightness)to set LED brightness on pin 9.Final Answer:
int sensorValue = analogRead(A0); int brightness = map(sensorValue, 0, 1023, 0, 255); analogWrite(9, brightness); -> Option BQuick Check:
Read analog, map range, write PWM [OK]
- Using digitalRead instead of analogRead
- Writing PWM to analog pin or wrong pin
- Using digitalWrite for PWM control
