Sub topic 1
Analog and Digital Signals
Prerequisites
This chapter assumes you can upload a sketch, that you know the setup() and loop() structure, and that you understand voltage, ground, and breadboard wiring. Familiarity with digitalRead() helps but is not required.
Analog and digital signals
A digital signal has two states: HIGH (about 5 V on an Uno) or LOW (0 V). An analog signal can take any value between those limits. A potentiometer's wiper, a temperature sensor, and a light sensor all produce analog voltages.
Digital pins read only HIGH or LOW. Analog pins (A0 to A5 on an Uno) measure a continuous voltage and convert it to a number.
How the ADC turns voltage into a number
The analog-to-digital converter (ADC) is the circuit inside the microcontroller that performs this conversion. The Uno's ADC has 10 bits of resolution, so analogRead() returns an integer from 0 to 1023. A reading of 0 means the input is at ground. A reading of 1023 means the input is at the reference voltage.
Reference voltage and converting readings to volts
The reference voltage is the top of the measuring scale. On an Uno it is 5 V by default. Each count represents 5.0 / 1023 volts, about 4.89 mV.
int raw = analogRead(A0);
float volts = raw * (5.0 / 1023.0);A raw reading of 512 gives 512 × 5.0 / 1023, which is about 2.50 V.
The voltage divider behind a potentiometer
A potentiometer is a resistor with a sliding contact called the wiper. Connect the two outer terminals to 5 V and GND. The wiper then outputs a fraction of 5 V:
Vout = Vin × Rbottom / (Rtop + R_bottom)
Turning the knob changes the ratio, so the wiper voltage moves from 0 V to 5 V.
Exercise
The reference is 5 V and the reading is 205. What is the voltage at the pin?
Show answer
205 × 5.0 / 1023 ≈ 1.00 V.