Bird
Raised Fist0
Arduinoprogramming~10 mins

Reading a potentiometer in Arduino - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Reading a potentiometer
Start
Setup pin modes
Loop start
Read analog value from potentiometer
Store value in variable
Use or print value
Loop repeats
The program sets up the input pin, then repeatedly reads the potentiometer's analog value, stores it, and uses or prints it.
Execution Sample
Arduino
int potPin = A0;
int val = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  val = analogRead(potPin);
  Serial.println(val);
  delay(500);
}
This code reads the potentiometer value from pin A0 and prints it every half second.
Execution Table
StepActionVariable 'val'OutputNotes
1Setup serial communication0Serial starts at 9600 baud
2Read analog value from A0e.g. 512Potentiometer middle position approx.
3Print value to Serial512512Value sent to serial monitor
4Wait 500 ms512Pause before next read
5Read analog value from A0e.g. 600Potentiometer moved slightly
6Print value to Serial600600Value sent to serial monitor
7Wait 500 ms600Pause before next read
...Loop continues reading and printingvariesvariesRepeats indefinitely
💡 Arduino loop runs forever; no exit condition
Variable Tracker
VariableStartAfter Step 2After Step 5...final
val0512600varies with potentiometer position
Key Moments - 3 Insights
Why does 'val' change each time in the execution table?
Because the potentiometer position changes, analogRead reads a new voltage level each loop (see steps 2 and 5).
Why is there a delay after printing the value?
The delay(500) pauses the loop so the readings and prints happen every half second, making output readable (step 4).
What does analogRead actually return?
It returns a number from 0 to 1023 representing voltage from 0V to 5V on the analog pin (step 2 shows example values).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'val' after step 5?
A600
B512
C0
D1023
💡 Hint
Check the 'Variable val' column at step 5 in the execution_table.
At which step does the program print the potentiometer value to the Serial monitor?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the 'Print value to Serial' action in the execution_table.
If the potentiometer is turned to maximum, what is the expected range of 'val'?
AAround 0
BAround 1023
CAround 512
DNegative values
💡 Hint
Recall analogRead returns 0 to 1023 based on voltage; max position means max voltage.
Concept Snapshot
Reading a potentiometer:
- Connect potentiometer middle pin to analog input (e.g., A0).
- Use analogRead(pin) to get value 0-1023.
- Store value in variable.
- Use Serial.println() to print value.
- Use delay() to control reading speed.
- Loop repeats to update reading continuously.
Full Transcript
This Arduino program reads a potentiometer connected to analog pin A0. It starts by setting up serial communication at 9600 baud. In the loop, it reads the analog value from the potentiometer using analogRead, which returns a number between 0 and 1023 depending on the knob position. This value is stored in the variable 'val'. The program then prints this value to the serial monitor using Serial.println. After printing, it waits for 500 milliseconds before reading again. This loop repeats forever, updating the potentiometer reading continuously. The variable 'val' changes as the potentiometer is turned, reflecting the voltage level on the analog pin. The delay helps make the output readable by spacing out the readings.

Practice

(1/5)
1. What does the analogRead(pin) function do when reading a potentiometer on Arduino?
easy
A. It reads the voltage level on the analog pin and returns a value from 0 to 1023.
B. It sets the output voltage of the pin to control the potentiometer.
C. It converts a digital signal to an analog voltage.
D. It resets the potentiometer to zero position.

Solution

  1. Step 1: Understand analogRead function

    The analogRead(pin) reads the voltage on the specified analog pin and converts it to a number between 0 and 1023.
  2. Step 2: Relate to potentiometer reading

    Since a potentiometer outputs a variable voltage depending on its position, analogRead returns a value representing that voltage level.
  3. Final Answer:

    It reads the voltage level on the analog pin and returns a value from 0 to 1023. -> Option A
  4. Quick Check:

    analogRead() returns 0-1023 value [OK]
Hint: Remember analogRead returns 0-1023 for voltage levels [OK]
Common Mistakes:
  • Thinking analogRead sets voltage instead of reading it
  • Confusing analogRead with digitalRead
  • Assuming analogRead returns voltage in volts
2. Which of the following is the correct syntax to read a potentiometer connected to analog pin A0 and store the value in a variable named sensorValue?
easy
A. sensorValue = analogWrite(A0);
B. sensorValue = digitalRead(A0);
C. sensorValue = analogRead(A0);
D. sensorValue = readAnalog(A0);

Solution

  1. Step 1: Identify correct function for analog input

    The function to read analog input is analogRead(pin), not digitalRead or analogWrite.
  2. Step 2: Check variable assignment syntax

    Assigning the result of analogRead(A0) to sensorValue uses the syntax: sensorValue = analogRead(A0);
  3. Final Answer:

    sensorValue = analogRead(A0); -> Option C
  4. Quick Check:

    Use analogRead() to read analog pin [OK]
Hint: Use analogRead(pin) to read analog sensors [OK]
Common Mistakes:
  • Using digitalRead instead of analogRead
  • Using analogWrite which is for output
  • Using a non-existent function readAnalog
3. What will be printed on the Serial Monitor when the following Arduino code runs and the potentiometer is turned to mid position?
void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(1000);
}
medium
A. A value close to 0 printed every second
B. A value close to 1023 printed every second
C. No output because Serial.begin is missing
D. A value close to 512 printed every second

Solution

  1. Step 1: Understand analogRead output range

    The potentiometer at mid position outputs about half the voltage, so analogRead(A0) returns around 512 (half of 1023).
  2. 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.
  3. Final Answer:

    A value close to 512 printed every second -> Option D
  4. Quick Check:

    Mid potentiometer = ~512 output [OK]
Hint: Mid potentiometer gives about half max value 512 [OK]
Common Mistakes:
  • Expecting 0 or 1023 at mid position
  • Forgetting Serial.begin causes no output
  • Confusing analogRead with digitalRead values
4. The following code is intended to read a potentiometer and print its value, but it does not work correctly. What is the error?
void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue;
  sensorValue = analogRead(0);
  Serial.print(sensorValue);
  delay(500);
}
medium
A. Missing Serial.println instead of Serial.print
B. Serial.begin baud rate is too low
C. No delay after Serial.print
D. Using analogRead(0) instead of analogRead(A0)

Solution

  1. Step 1: Check Serial output method

    The code uses Serial.print(sensorValue) without a newline, causing consecutive values to print on the same line and appear garbled or unreadable.
  2. Step 2: Use println for readability

    Serial.println(sensorValue) adds a newline after each value, making the output clear on the Serial Monitor.
  3. Final Answer:

    Missing Serial.println instead of Serial.print -> Option A
  4. Quick Check:

    Serial.print vs println for newlines [OK]
Hint: Use Serial.println() not print() for readable output [OK]
Common Mistakes:
  • Using numeric 0 instead of A0 for analogRead
  • Confusing Serial.print and Serial.println
  • Ignoring delay causing fast output
5. You want to read a potentiometer and map its 0-1023 value to a 0-255 range to control LED brightness using PWM on pin 9. Which code snippet correctly does this?
hard
A. int sensorValue = analogRead(9); int brightness = map(sensorValue, 0, 255, 0, 1023); analogWrite(A0, brightness);
B. int sensorValue = analogRead(A0); int brightness = map(sensorValue, 0, 1023, 0, 255); analogWrite(9, brightness);
C. int sensorValue = digitalRead(A0); int brightness = sensorValue * 255; analogWrite(9, brightness);
D. int sensorValue = analogRead(A0); int brightness = sensorValue / 4; digitalWrite(9, brightness);

Solution

  1. Step 1: Read potentiometer value correctly

    Use analogRead(A0) to get a value from 0 to 1023.
  2. Step 2: Map value to 0-255 for PWM

    Use map(sensorValue, 0, 1023, 0, 255) to convert the range for LED brightness.
  3. Step 3: Write PWM value to LED pin

    Use analogWrite(9, brightness) to set LED brightness on pin 9.
  4. Final Answer:

    int sensorValue = analogRead(A0); int brightness = map(sensorValue, 0, 1023, 0, 255); analogWrite(9, brightness); -> Option B
  5. Quick Check:

    Read analog, map range, write PWM [OK]
Hint: Use map() to convert 0-1023 to 0-255 for PWM [OK]
Common Mistakes:
  • Using digitalRead instead of analogRead
  • Writing PWM to analog pin or wrong pin
  • Using digitalWrite for PWM control