Bird
Raised Fist0
Arduinoprogramming~10 mins

analogRead() and ADC conversion 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 - analogRead() and ADC conversion
Start analogRead(pin)
ADC starts conversion
Sample analog voltage
Convert voltage to digital value
Return digital value (0-1023)
End
The analogRead function starts an ADC conversion on a pin, samples the voltage, converts it to a digital number, and returns that value.
Execution Sample
Arduino
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
Reads the analog voltage on pin A0 and prints the digital value (0-1023).
Execution Table
StepActionAnalog Voltage (V)ADC ConversionDigital Value Returned
1Start analogRead(A0)N/AStart ADC conversionN/A
2Sample voltage on A02.5Convert 2.5V to digital512
3Return digital valueN/AN/A512
4Print valueN/AN/A512
💡 analogRead completes after conversion and returns digital value between 0 and 1023
Variable Tracker
VariableStartAfter analogReadFinal
sensorValueundefined512512
Key Moments - 3 Insights
Why does analogRead return a number between 0 and 1023?
Because the Arduino ADC converts the analog voltage into a 10-bit digital number, which ranges from 0 (0V) to 1023 (5V reference). See execution_table step 2.
Does analogRead return the actual voltage value?
No, it returns a digital number representing the voltage level, not the voltage itself. You can convert it back to voltage if needed.
What happens if the analog voltage is higher than the reference voltage?
The ADC will saturate and return the maximum value 1023, because it cannot measure above the reference voltage.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what digital value does analogRead return when the analog voltage is 2.5V?
A1023
B512
C256
D0
💡 Hint
Check execution_table row 2 under 'Digital Value Returned'
At which step does the ADC convert the analog voltage to a digital value?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at execution_table step 2 under 'ADC Conversion'
If the analog voltage was 0V, what digital value would analogRead return?
A0
B512
C1023
D256
💡 Hint
Recall ADC maps 0V to 0 digital value as per key_moments explanation
Concept Snapshot
analogRead(pin) reads analog voltage on pin
ADC converts voltage (0-5V) to 10-bit number (0-1023)
Returns digital value representing voltage level
Use returned value to measure sensors or inputs
Conversion is automatic and fast
Value can be converted back to voltage if needed
Full Transcript
The analogRead() function in Arduino reads the voltage on an analog pin. It starts an ADC conversion that samples the voltage and converts it into a digital number between 0 and 1023. This number represents the voltage level relative to the Arduino's reference voltage, usually 5 volts. For example, if the voltage is about half the reference (2.5V), analogRead returns about 512. The function returns this digital value, which can be printed or used in the program. The ADC cannot measure voltages above the reference voltage, so it returns the maximum value 1023 in that case. This process happens quickly and automatically when you call analogRead. You can convert the digital value back to voltage by multiplying by the reference voltage divided by 1023.

Practice

(1/5)
1. What does the analogRead() function do on an Arduino?
easy
A. It writes a voltage level to an analog pin.
B. It sends a digital signal to an output pin.
C. It resets the Arduino board.
D. It reads an analog voltage and converts it to a number between 0 and 1023.

Solution

  1. Step 1: Understand the purpose of analogRead()

    The function analogRead() reads the voltage on an analog pin and converts it to a number.
  2. Step 2: Know the range of values returned

    The returned value ranges from 0 (0 volts) to 1023 (maximum reference voltage, usually 5V or 3.3V).
  3. Final Answer:

    It reads an analog voltage and converts it to a number between 0 and 1023. -> Option D
  4. Quick Check:

    analogRead() returns 0-1023 [OK]
Hint: Remember: analogRead() maps voltage to 0-1023 [OK]
Common Mistakes:
  • Thinking analogRead() writes voltage
  • Confusing analogRead() with digitalWrite()
  • Assuming analogRead() returns voltage directly
  • Believing analogRead() resets the board
2. Which of the following is the correct syntax to read an analog value from pin A0?
easy
A. int sensorValue = analogRead(A0);
B. int sensorValue = digitalRead(A0);
C. analogRead(int A0);
D. int sensorValue = analogWrite(A0);

Solution

  1. Step 1: Identify the correct function for analog input

    The function to read analog input is analogRead(), not digitalRead() or analogWrite().
  2. Step 2: Check the syntax for reading from pin A0

    The correct syntax is int sensorValue = analogRead(A0); which stores the read value in an integer variable.
  3. Final Answer:

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

    Use analogRead(pin) to read analog input [OK]
Hint: Use analogRead(pin) to get analog input value [OK]
Common Mistakes:
  • Using digitalRead() for analog pins
  • Calling analogRead() with wrong syntax
  • Using analogWrite() instead of analogRead()
  • Trying to pass pin as int inside analogRead()
3. What will be the output of this Arduino code snippet if the analog voltage on pin A1 is 2.5V and the reference voltage is 5V?
int sensorValue = analogRead(A1);
Serial.println(sensorValue);
medium
A. 1023
B. 512
C. 256
D. 0

Solution

  1. Step 1: Understand the ADC conversion formula

    The analogRead() converts voltage to a value between 0 and 1023 based on the formula: value = (input voltage / reference voltage) * 1023.
  2. Step 2: Calculate the expected value for 2.5V input

    value = (2.5 / 5) * 1023 = 0.5 * 1023 = 511.5, which rounds to 512.
  3. Final Answer:

    512 -> Option B
  4. Quick Check:

    Half of 1023 is about 512 [OK]
Hint: Multiply voltage ratio by 1023 for analogRead() value [OK]
Common Mistakes:
  • Using 1023 directly without scaling
  • Confusing digitalRead() output with analogRead()
  • Rounding errors ignoring half values
  • Assuming analogRead() returns voltage in volts
4. Identify the error in this Arduino code that reads an analog value and prints it:
void setup() {
  Serial.begin(9600);
}

void loop() {
  int val = analogRead(0);
  Serial.print(val);
  delay(1000);
}
medium
A. Serial.begin() must be called inside loop(), not setup().
B. delay() cannot be used with Serial.print().
C. analogRead() should use A0 instead of 0 for clarity and correctness.
D. The variable val must be declared as float, not int.

Solution

  1. Step 1: Check the analogRead() argument

    Using 0 instead of A0 can work but is discouraged; A0 is the correct constant for analog pin 0.
  2. Step 2: Verify other code parts

    Serial.begin() is correctly in setup(), delay() works fine with Serial.print(), and int is suitable for analogRead() values.
  3. Final Answer:

    analogRead() should use A0 instead of 0 for clarity and correctness. -> Option C
  4. Quick Check:

    Use A0 for analog pin 0 [OK]
Hint: Use A0 constant for analog pin 0 [OK]
Common Mistakes:
  • Using numeric 0 instead of A0 for analogRead()
  • Moving Serial.begin() to loop() causing repeated starts
  • Thinking delay() stops Serial.print()
  • Declaring analogRead() result as float unnecessarily
5. You want to measure a sensor voltage that ranges from 0V to 3.3V using Arduino's analogRead() with a 5V reference. How can you correctly convert the analogRead() value to the actual voltage?
hard
A. Voltage = (analogRead() / 1023.0) * 5.0
B. Voltage = (analogRead() / 1023.0) * 3.3
C. Voltage = (analogRead() / 5.0) * 3.3
D. Voltage = analogRead() * 3.3 / 1023

Solution

  1. Step 1: Understand the reference voltage and sensor range

    The Arduino ADC uses 5V as reference, so analogRead() maps 0-5V to 0-1023.
  2. Step 2: Calculate voltage from analogRead() value

    To get voltage, multiply the fraction (analogRead()/1023) by 5.0 (the reference voltage), not 3.3.
  3. Final Answer:

    Voltage = (analogRead() / 1023.0) * 5.0 -> Option A
  4. Quick Check:

    Use reference voltage (5V) in conversion formula [OK]
Hint: Multiply analogRead ratio by reference voltage (5V) [OK]
Common Mistakes:
  • Using sensor max voltage (3.3V) instead of reference voltage (5V)
  • Dividing by 5 instead of 1023
  • Multiplying analogRead() directly without division
  • Confusing sensor voltage range with ADC reference