Analog Input: Reading Voltages with the ADC
The Arduino's analog pins are not true analog inputs — they use an Analog-to-Digital Converter (ADC) to measure voltage and convert it to a digital value you can read in code.
analogRead() returns values from 0 to 1023 (10-bit resolution), representing a voltage between 0V and the reference voltage (typically 5V on a 5V Arduino).
How the ADC Works
The Arduino's ADC samples the analog voltage and converts it to a 10-bit binary number:
- 0 = 0V (0% of reference)
- 512 = 2.5V (50% of reference)
- 1023 = ~5V (100% of reference)
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0); // Read pin A0
float voltage = sensorValue * (5.0 / 1023.0); // Convert to voltage
Serial.print("ADC value: ");
Serial.print(sensorValue);
Serial.print(" Voltage: ");
Serial.println(voltage);
delay(100);
}Voltage Divider
A voltage divider reduces a higher voltage to a lower one using two resistors:
V_in ──┬── R1 ──┬── R2 ── GND
│ │
└── V_out ── analog pin
The output voltage is:
V_out = V_in × (R2 / (R1 + R2))
For example, to read a 12V signal with a 5V Arduino, use R1 = 10kΩ and R2 = 2.2kΩ:
- V_out = 12 × (2.2 / 12.2) = 2.16V (safe for the Arduino)
Potentiometer Circuit
A potentiometer (rotary knob) is essentially a variable voltage divider. Connect the outer pins to 5V and GND, and the middle pin (wiper) to an analog input:
const int potPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int potValue = analogRead(potPin);
Serial.println(potValue);
delay(100);
}Reading Sensors
Most analog sensors output a voltage proportional to the measured value. For example, a photoresistor (LDR) changes its resistance based on light intensity. By measuring the voltage across it, you can determine light levels.
Noise Reduction
Sensor readings can be noisy. Simple averaging helps:
int readAnalog(int pin) {
const int samples = 10;
long sum = 0;
for (int i = 0; i < samples; i++) {
sum += analogRead(pin);
delay(5);
}
return sum / samples;
}Taking multiple readings and averaging them reduces random noise and improves accuracy. This is especially important for sensors with small output variations.
Key Takeaways
analogRead()returns 0–1023 (10-bit resolution)- Convert to voltage:
value × (5.0 / 1023.0) - Voltage dividers scale high voltages down to safe levels
- Potentiometers are variable voltage dividers
- The Serial Plotter visualizes sensor data in real time
- Averaging multiple readings reduces noise
Resources
- Arduino.cc - analogRead: https://www.arduino.cc/reference/en/language/functions/analog/analogread/
- Video: Reading Analog Sensors - https://www.youtube.com/watch?v=Y5uiy13Rk5A
- TinkerCAD Circuits: https://www.tinkercad.com/circuits
- Arduino.cc - Serial Plotter: https://www.arduino.cc/en/Tutorial/BuiltInExamples/SerialPlotter