We use analogRead() to measure real-world signals like light or temperature. It changes an electric voltage into a number the Arduino can understand.
analogRead() and ADC conversion in Arduino
int sensorValue = analogRead(pin);pin is the analog input pin number (like A0, A1).
The function returns a number from 0 to 1023 representing voltage from 0V to 5V (or 3.3V depending on board).
lightLevel.int lightLevel = analogRead(A0);int tempSensor = analogRead(A1);int joystickX = analogRead(A2);This program reads the analog voltage on pin A0 every second and prints the number to the Serial Monitor.
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.print("Sensor value: ");
Serial.println(sensorValue);
delay(1000);
}The number from analogRead() is called a digital value from the ADC (Analog to Digital Converter).
The ADC converts voltage into a number between 0 and 1023 (10-bit resolution).
Voltage range depends on your board (usually 0-5V or 0-3.3V).
analogRead() turns a voltage into a number from 0 to 1023.
Use it to read sensors that output analog voltages.
It helps your Arduino understand real-world signals.
Practice
analogRead() function do on an Arduino?Solution
Step 1: Understand the purpose of analogRead()
The functionanalogRead()reads the voltage on an analog pin and converts it to a number.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).Final Answer:
It reads an analog voltage and converts it to a number between 0 and 1023. -> Option DQuick Check:
analogRead() returns 0-1023 [OK]
- Thinking analogRead() writes voltage
- Confusing analogRead() with digitalWrite()
- Assuming analogRead() returns voltage directly
- Believing analogRead() resets the board
Solution
Step 1: Identify the correct function for analog input
The function to read analog input isanalogRead(), not digitalRead() or analogWrite().Step 2: Check the syntax for reading from pin A0
The correct syntax isint sensorValue = analogRead(A0);which stores the read value in an integer variable.Final Answer:
int sensorValue = analogRead(A0); -> Option AQuick Check:
Use analogRead(pin) to read analog input [OK]
- Using digitalRead() for analog pins
- Calling analogRead() with wrong syntax
- Using analogWrite() instead of analogRead()
- Trying to pass pin as int inside analogRead()
int sensorValue = analogRead(A1); Serial.println(sensorValue);
Solution
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.Step 2: Calculate the expected value for 2.5V input
value = (2.5 / 5) * 1023 = 0.5 * 1023 = 511.5, which rounds to 512.Final Answer:
512 -> Option BQuick Check:
Half of 1023 is about 512 [OK]
- Using 1023 directly without scaling
- Confusing digitalRead() output with analogRead()
- Rounding errors ignoring half values
- Assuming analogRead() returns voltage in volts
void setup() {
Serial.begin(9600);
}
void loop() {
int val = analogRead(0);
Serial.print(val);
delay(1000);
}Solution
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.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.Final Answer:
analogRead() should use A0 instead of 0 for clarity and correctness. -> Option CQuick Check:
Use A0 for analog pin 0 [OK]
- 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
analogRead() with a 5V reference. How can you correctly convert the analogRead() value to the actual voltage?Solution
Step 1: Understand the reference voltage and sensor range
The Arduino ADC uses 5V as reference, so analogRead() maps 0-5V to 0-1023.Step 2: Calculate voltage from analogRead() value
To get voltage, multiply the fraction (analogRead()/1023) by 5.0 (the reference voltage), not 3.3.Final Answer:
Voltage = (analogRead() / 1023.0) * 5.0 -> Option AQuick Check:
Use reference voltage (5V) in conversion formula [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
