Reading an analog pin
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A digital pin sees only HIGH or LOW; an analog pin measures a voltage. Turn a 10 kΩ potentiometer on an Arduino Uno R3 to 3.20 V and follow analogRead(A0) inside the ATmega328P: the multiplexer picks the pin, a 14 pF capacitor samples and holds the voltage, and a 10-bit successive-approximation converter finds 1010001111, which is 655. Then scale it back to volts and see what one step (4.88 mV) and one reading (104 µs) cost. The Signals lessons “Quantization and bit depth” and “Inside an ADC: flash and successive approximation” go deeper into the converter; the Circuits lesson “Voltage and current dividers” explains the potentiometer.
A pin that measures
A digital pin only sees HIGH or LOW. An analog input pin measures any voltage from 0 to 5 V and turns it into a number. The Arduino Uno R3 has six: A0 to A5. We read A0.
digital pin HIGH or LOW, nothing between analog pin any voltage 0 .. 5 V -> a number 0 .. 1023 Arduino Uno R3: A0 A1 A2 A3 A4 A5 inside the ATmega328P: one converter, shared by all six
اقرأ الدرس خطوة بخطوة
A pin that measures
A digital pin only sees HIGH or LOW. An analog input pin measures any voltage from 0 to 5 V and turns it into a number. The Arduino Uno R3 has six: A0 to A5. We read A0.
digital pin HIGH or LOW, nothing between analog pin any voltage 0 .. 5 V -> a number 0 .. 1023 Arduino Uno R3: A0 A1 A2 A3 A4 A5 inside the ATmega328P: one converter, shared by all sixTurn the knob to 3.20 V
A potentiometer is a 10 kΩ resistor with a sliding contact, the wiper. Its ends sit on 5 V and GND, so the wiper picks a voltage between them. At 64% of the way up it gives 3.20 V.
potentiometer 10 kΩ, ends on 5 V and GND wiper slides along it: a voltage divider position 64% up from GND wiper voltage 0.64 x 5 V = 3.20 V -> pin A0analogRead(A0) starts
Line 2 calls analogRead(A0). The Arduino core writes the channel number into a register called ADMUX, then sets the ADSC bit to start a conversion. The CPU then waits until the converter clears ADSC.
line 2 int raw = analogRead(A0); core ADMUX = reference | channel 0 set ADSC start converting while ADSC is 1, wait return ADC the 10-bit resultPick one pin
The chip has six analog pins but only one converter. A multiplexer, a selector switch, connects just the chosen pin to it. ADMUX says channel 0, so 3.20 V from A0 goes through and the other five wait.
multiplexer 6 pins in, 1 out ADMUX channel 0 -> A0 connected A0 3.20 V passes through A1 A2 A3 A4 A5 not connected this timeSample, then hold
A switch lets the voltage charge a tiny 14 pF capacitor to 3.20 V. 1.5 ADC clock ticks after the start, the switch opens. The capacitor now holds 3.20 V steady, even if the knob moves, while the bits are worked out.
sample switch closed: C(S/H) = 14 pF charges to 3.20 V hold 1.5 ADC clocks in (12 µs), the switch opens the capacitor keeps 3.20 V for the rest of the conversion source should be about 10 kΩ or less, to charge it in timeTen bits, top bit first
Successive approximation: test one bit per clock tick. Is 3.20 V at least 2.50 V, half of 5 V? Yes: bit 9 is 1. At least 3.75 V? No: bit 8 is 0. Ten tests later, the ADC register holds 1010001111.
try each bit from the top; keep it if Vin >= the trial level bit 9 try 512 = 2.500 V yes -> 1 bit 8 try 768 = 3.750 V no -> 0 bit 7 try 640 = 3.125 V yes -> 1 bit 6 try 704 = 3.438 V no -> 0 bit 5 try 672 = 3.281 V no -> 0 bit 4 try 656 = 3.203 V no -> 0 bit 3 try 648 = 3.164 V yes -> 1 bit 2 try 652 = 3.184 V yes -> 1 bit 1 try 654 = 3.193 V yes -> 1 bit 0 try 655 = 3.198 V yes -> 1 result 1010001111The number: 655
Binary 1010001111 is 655. The datasheet's rule agrees: 3.20 × 1024 / 5 = 655.4, rounded down to 655. analogRead returns it, and line 2 stores it in raw.
register 1010001111 = 655 rule ADC = Vin x 1024 / Vref 3.20 x 1024 / 5 = 655.36 -> 655 line 2 raw = 655Back to volts
Line 3 undoes the rule: 655 × 5.0 / 1024 = 3.198 V, and println shows 3.20. One step is 5 V / 1024 = 4.88 mV, so every voltage from 3.198 V up to 3.203 V reads 655.
line 3 v = 655 x 5.0 / 1024 = 3.1982 line 4 Serial.println(v) -> 3.20 (2 decimals by default) step 5 V / 1024 = 4.88 mV (resolution) 655 means 3.198 V <= Vin < 3.203 V104 µs per reading
The ADC clock is 16 MHz / 128 = 125 kHz: one tick every 8 µs. A conversion takes 13 ticks, 104 µs, so at most about 9,615 readings a second. Our delay(500) asks for only two.
ADC clock 16 MHz / 128 = 125 kHz (must be 50..200 kHz for 10 bits) one tick 8 µs conversion 13 ticks = 104 µs (the very first: 25 ticks) fastest about 9,615 readings per second line 5 delay(500) -> 2 readings per second
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01On an Arduino Uno R3, analogRead(A0) returns 0 to 1023. Which voltage on A0 gives a reading of about 512?
ADC = Vin × 1024 / 5, so 2.5 V gives 2.5 × 1024 / 5 = 512: half the range for half the reference.
02Why does the ADC sample the voltage onto a small capacitor and then disconnect it before converting?
Successive approximation tests one bit per clock tick. If the input moved during those ticks, the bits would describe different voltages. The sample-and-hold freezes it first.
03The converter holds 3.20 V. Its first test asks: is it at least 2.50 V (half of 5 V)? Its second asks: at least 3.75 V? What are the first two bits?
3.20 ≥ 2.50, so bit 9 is 1. 3.20 < 3.75, so bit 8 is 0. The full answer is 1010001111 = 655.
04With a 5 V reference and 10 bits, how big is one step of the ADC?
5 V / 1024 = 4.88 mV. Any two voltages closer together than that can read the same number.
05The Arduino core runs the ADC clock at 16 MHz / 128, and a normal conversion takes 13 ADC clock ticks. About how long is one analogRead()?
16 MHz / 128 = 125 kHz, or 8 µs per tick; 13 × 8 µs = 104 µs. That is why Arduino's reference says "about 100 microseconds", or roughly 10,000 readings a second at most.
المسرد
- Analog input
- A pin that measures a voltage anywhere in a range (0 to 5 V on the Uno R3) instead of only HIGH or LOW.
- ADC (analog-to-digital converter)
- The circuit that turns a voltage into a whole number. The ATmega328P has one 10-bit ADC shared by its analog pins.
- Potentiometer
- A resistor with a sliding contact (the wiper). With its ends on 5 V and GND, the wiper gives any voltage in between: a voltage divider you can turn.
- Multiplexer
- A selector switch that connects one of several inputs to a single output. It lets six pins share one ADC.
- Sample and hold
- A switch and a small capacitor (14 pF on the ATmega328P) that capture the input voltage and keep it steady during the conversion.
- Successive approximation
- Finding the number one bit at a time, most significant first: set the bit, compare with the held voltage, keep it or clear it.
- Resolution (LSB)
- The size of one step: the reference voltage divided by the number of codes, 5 V / 1024 = 4.88 mV here.
- Reference voltage (Vref)
- The voltage that the top of the range stands for. On the Uno R3 it is the 5 V supply unless you choose another.
الملاحظات والمصادر
- The card shows only loop(). The whole sketch also calls Serial.begin(9600) in setup(). The variable raw is an int and v a float; println prints a float with two decimals unless you ask for more.
- The datasheet's rule is ADC = Vin × 1024 / Vref, and 1023 (0x3FF) means "the reference minus one step", so this lesson divides by 1024 to go back. Arduino's ReadAnalogVoltage example divides by 1023.0 instead; the two differ by less than one step (4.88 mV).
- Real readings are not exact: the datasheet gives ±2 LSB absolute accuracy, and the reference is the board's 5 V supply, which is rarely exactly 5.00 V. Expect the last digit or two to jitter; later lessons in this module cover the reference voltage, smoothing and calibration.
- The figure shows the bit tests one after another, as successive approximation works. The exact clock-by-clock timing inside the 13 cycles (sample at 1.5, then the bits, then writing the result) is simplified.
- The Uno R3's DIP-package ATmega328P brings out six analog inputs (A0 to A5). The multiplexer inside has more channels: two more on the TQFP and VQFN packages, plus an internal temperature sensor.
- Microchip ATmega48A/PA/88A/PA/168A/PA/328/P datasheet (DS40002061B), ch. 24 Analog-to-Digital Converter
- Arduino language reference: analogRead() (source)
- Arduino language reference: Serial.println() (source)
- Arduino AVR core: wiring.c (ADC prescaler, 16 MHz / 128 = 125 kHz)
- Arduino AVR core: wiring_analog.c (analogRead: ADMUX, ADSC, ADC)
- Arduino AVR core: Print.h (floats print with 2 decimals)
- Arduino example: ReadAnalogVoltage
- J. W. Valvano, Embedded Systems: Shape the World, ch. 14 ADC and data acquisition (UT Austin)
اختبار يتاح بعد الخطوة الأخيرة.