Reading a potentiometer in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When reading a potentiometer with Arduino, we want to know how the time to read changes as we do more readings.
We ask: How does the number of readings affect the total time the program takes?
Analyze the time complexity of the following code snippet.
int sensorPin = A0;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
delay(100);
}
This code reads the potentiometer value once every loop and prints it, pausing briefly each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop() function runs repeatedly, reading the sensor once each time.
- How many times: It runs indefinitely, but if we consider n readings, it runs n times.
Each reading takes about the same time, so total time grows directly with number of readings.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sensor reads and prints |
| 100 | 100 sensor reads and prints |
| 1000 | 1000 sensor reads and prints |
Pattern observation: Doubling the number of readings doubles the total work.
Time Complexity: O(n)
This means the total time grows in direct proportion to how many times we read the potentiometer.
[X] Wrong: "Reading the potentiometer once or many times takes the same total time."
[OK] Correct: Each reading takes time, so more readings add up and increase total time.
Understanding how repeated sensor readings affect program time helps you write efficient Arduino code and shows you can think about how programs grow.
"What if we read the potentiometer inside a nested loop that runs m times inside the main loop? How would the time complexity change?"
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
