Chapterبەش 5 · Lectureوانە 5.3

Reading Analog Signals: the ADC and analogRead()خوێندنەوەی سیگناڵەکانی ئەنالۆگ: ADC و ⁦analogRead()⁩

How a board reads a smooth voltage: analog and digital outputs, the ADC inside the microcontroller, n bits = 2ⁿ levels, the step size and the formulas between readings and volts, analogRead() on the UNO R3, UNO R4 and MKR WAN 1310 with analogReadResolution(), the reference voltage and analogReference(), a TMP36 worked example, floating pins and averaging.

بۆرد چۆن نەرم دەخوێنێتەوە: و ، ADCـی ناو ، nn = 2n2^n ئاست، قەبارەی هەنگاو و نێوان خوێندنەوە و ، ⁦analogRead()⁩ لەسەر ⁦UNO R3⁩، ⁦UNO R4⁩ و ⁦MKR WAN 1310⁩ بە ⁦analogReadResolution()⁩، و ⁦analogReference()⁩، نموونەیەکی هەنگاوبەهەنگاوی ⁦TMP36⁩، و تێکڕا.

21 min read23 خولەک خوێندنەوە · 20 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Explain what an ADC does and why an analog sensor needs one.
  • Work out the number of levels and the step size for 8, 10, 12 and 14 bits.
  • Convert an analogRead() value to volts, and volts to the expected value.
  • Use analogRead() and analogReadResolution() on the UNO R3, UNO R4 and MKR WAN 1310 with the right reference voltage.
  • Explain why an unconnected analog pin reads noise, and smooth readings by averaging.
  • ڕوونی بکەیتەوە ADC چی دەکات و بۆچی پێویستی پێیەتی.
  • ژمارەی ئاستەکان و قەبارەی هەنگاو بۆ 8، ⁦10⁩، ⁦12⁩ و ⁦14⁩ بدۆزیتەوە.
  • نرخێکی ⁦analogRead()⁩ بگۆڕیت بۆ و بۆ نرخە چاوەڕوانکراوەکە.
  • ⁦analogRead()⁩ و ⁦analogReadResolution()⁩ لەسەر ⁦UNO R3⁩، ⁦UNO R4⁩ و ⁦MKR WAN 1310⁩ بە دروست بەکار بهێنیت.
  • ڕوونی بکەیتەوە بۆچی نەبەستراو دەخوێنێتەوە و خوێندنەوەکان بە تێکڕا ئارام بکەیتەوە.

From a smooth voltage to a numberلە ڤۆڵتیەیەکی نەرمەوە بۆ ژمارەیەک

Turn the knob of a potentiometer, and the voltage on its middle leg glides smoothly from 0 V to 5 V. A TMP36 gives 0.75 V at 25 °C and 0.76 V at 26 °C. digitalRead() cannot tell you any of that: it only answers HIGH or LOW. To read how much, the microcontroller uses a converter built into it, the ADC, through its analog input pins.

دەسکی (potentiometer) بسووڕێنە و قاچە ناوەڕاستەکەی بە نەرمی لە ⁦0 V⁩ـەوە بۆ ⁦5 V⁩ دەڕوات. ⁦TMP36⁩ لە ⁦25 °C⁩ـدا ⁦0.75 V⁩ و لە ⁦26 °C⁩ـدا ⁦0.76 V⁩ دەدات. digitalRead() ناتوانێت هیچ کام لەمانەت پێ بڵێت: تەنیا HIGH یان LOW وەڵام دەداتەوە. بۆ خوێندنەوەی چەندە، گۆڕەرێکی ناوخۆیی بەکار دەهێنێت، ⁦(ADC)⁩، لە ڕێی .

In this lesson the ADC turns volts into numbers: how many levels it has, how big one step is, what analogRead() returns on each board, why the reference voltage matters, and why a pin with nothing on it gives random numbers. How analog and digital signals differ in general is in Analog and Digital Signals; how sound becomes samples is in How sound becomes numbers.

لەم وانەیەدا ADC دەکاتە ژمارە: چەند ئاستی هەیە، یەک هەنگاو چەند گەورەیە، analogRead() لەسەر هەر بۆردێک چی دەگەڕێنێتەوە، بۆچی گرنگە و بۆچی پینێک کە هیچی پێوە نییە دەدات. جیاوازیی گشتیی و لە سیگناڵەکانی ئەنالۆگ و دیجیتاڵـدایە؛ چۆنیەتیی بوونی دەنگ بە نموونە لە دەنگ چۆن دەبێتە ژمارەـدایە.

A potentiometer knob set to 2.37 V sends that voltage to pin A0 of an UNO R3 microcontroller. Inside, the ADC with 10 bits compares it with 5 V; the sketch calls analogRead(A0). A green arrow labelled 485 goes to a Serial Monitor that shows 485, 485, 486. Footer: the voltage can be anything; the program gets a whole number from 0 to 1023.دەسکی بەرگرێکی گۆڕاو کە لەسەر 2.37 V دانراوە ئەو ڤۆڵتیەیە دەنێرێت بۆ پینی A0ـی مایکرۆکۆنترۆڵەری UNO R3. لە ناوەوە ADCـێکی 10 بیت بە 5 V بەراوردی دەکات؛ سکێچەکە analogRead(A0) بانگ دەکات. تیرێکی سەوز بە نووسینی 485 دەچێت بۆ Serial Monitor کە 485، 485، 486 پیشان دەدات. لە خوارەوە: ڤۆڵتیەکە دەتوانێت هەر شتێک بێت؛ بەرنامەکە ژمارەیەکی تەواو لە 0 تا 1023 وەردەگرێت.
A smooth voltage in, a whole number out. نەرم دێتە ژوورەوە، دێتە دەرەوە.CêIT

Two kinds of sensor outputدوو جۆری دەرخستەی هەستەوەر

Classified by their output signal, sensors are analog or digital (Analog or digital output?). An analog sensor gives an output that varies continuously with the quantity — for example a voltage anywhere from 0 to 5 V — so it needs analog-to-digital (A/D) conversion before a program can use it. A digital sensor's output moves in discrete steps, 0s and 1s, the form digital communication uses, so no A/D conversion is needed on the board.

بەپێی ، یان (دەرخستەی ئەنالۆگ یان دیجیتاڵ؟). دەدات کە بە بەردەوامی لەگەڵ بڕەکەدا دەگۆڕێت — بۆ نموونە لە هەر شوێنێکی نێوان 0 و ⁦5 V⁩ـدا — بۆیە پێش ئەوەی بەکاری بهێنێت پێویستی بە گۆڕینی بۆ ⁦(A/D)⁩ هەیە. بە هەنگاوی جیا دەجووڵێت، 0 و 1، ئەو شێوەیەی پەیوەندیی بەکاری دەهێنێت، بۆیە لەسەر بۆردەکە پێویستی بە گۆڕینی ⁦A/D⁩ نییە.

Both kinds need power. An outside source — wind speed, sunlight, light intensity — acts on the sensor; an excitation supply and ground feed it; and it gives out a voltage or a current: a smooth signal from an analog sensor, a square wave from a digital one.

هەردوو جۆرەکە پێویستیان بە وزەیە. سەرچاوەیەکی دەرەکی — خێرایی با، تیشکی خۆر، توندیی ڕووناکی — کار لە دەکات؛ ورووژاندن و وزەی دەدەنێ؛ و یان دەداتە دەرەوە: نەرم لە ، لە .

Two rows, analog sensor and digital sensor. In each: an outside source (wind, sunlight, light) goes into a sensor, which is fed by excitation from above and ground from below and gives a voltage or current output. The analog row ends in a smooth wave, labelled smooth: needs an ADC. The digital row ends in a square wave, labelled steps of 0 and 1: no ADC.دوو ڕیز، هەستەوەری ئەنالۆگ و هەستەوەری دیجیتاڵ. لە هەریەکەیاندا: سەرچاوەیەکی دەرەکی (با، تیشکی خۆر، ڕووناکی) دەچێتە هەستەوەرێکەوە کە لە سەرەوە ورووژاندن و لە خوارەوە زەوی وزەی دەدەنێ و ڤۆڵتیە یان تەزووی دەرخستە دەدات. ڕیزی ئەنالۆگ بە شەپۆلێکی نەرم کۆتایی دێت، بە نووسینی نەرم: پێویستی بە ADC هەیە. ڕیزی دیجیتاڵ بە شەپۆلی چوارگۆشەییەک کۆتایی دێت، بە نووسینی پلەی 0 و 1: ADC ناوێت.
Analog output needs the board's ADC; digital output does not. پێویستی بە ADCـی بۆردەکە هەیە؛ نا.CêIT

The ADC: a tiny measurer inside the chipADC: پێوەرێکی بچووک لە ناو چیپەکەدا

An analog signal can take any number of values; a digital signal has only two, HIGH and LOW. To measure an analog voltage, the microcontroller (MCU) — the tiny computer chip on the board that runs your sketch and controls sensors and circuits — has a built-in analog-to-digital converter (ADC): a tiny machine that takes a real-world voltage and turns it into a number that the code can understand.

دەتوانێت هەر ژمارەیەک نرخ وەربگرێت؛ تەنیا دوو نرخی هەیە، HIGH و LOW. بۆ پێوانی ، ⁦(MCU)⁩ — ئەو کۆمپیوتەرە بچووکەی سەر بۆردەکە کە جێبەجێ دەکات و و کۆنترۆڵ دەکات — ⁦(ADC)⁩ ناوخۆیی هەیە: ئامێرێکی بچووک کە جیهانی ڕاستەقینە وەردەگرێت و دەیکاتە ژمارەیەک کە کۆدەکە تێی بگات.

On an UNO the six analog pins A0–A5 share one ADC: a selector inside the chip, a multiplexer, connects one pin at a time. The ADC compares the pin's voltage with a reference voltage (5 V on an UNO) and answers one question: what fraction of the reference is this? — as a whole number.

لەسەر UNO شەش ⁦A0⁩ تا ⁦A5⁩ یەک ADC بەکار دەهێنن: هەڵبژێرێک لە ناو ، واتە ، لە هەر جارێکدا یەک پین دەبەستێتەوە. ADC پینەکە لەگەڵ (⁦5 V⁩ لەسەر UNO) بەراورد دەکات و وەڵامی یەک پرسیار دەداتەوە: ئەمە چ بەشێکی ؟ — وەک .

Inside the ATmega328P of an UNO R3: six pins A0 to A5 enter on the left and all lead to a selector, a multiplexer, which connects A0 to the ADC. The ADC, 10 bits, gets the reference voltage of 5 V from above and asks what fraction of the reference this is. Its result, 0 to 1023, goes to the CPU, which runs analogRead(). Footer: the selector connects one pin at a time to the ADC.لە ناو ATmega328Pـی UNO R3: شەش پینی A0 تا A5 لە چەپەوە دێنە ژوورەوە و هەموویان دەچن بۆ هەڵبژێرێک، واتە مەڵتیپلێکسەرێک، کە A0 بە ADCـەوە دەبەستێت. ADC، 10 بیت، ڤۆڵتیەی پێوەری 5 V لە سەرەوە وەردەگرێت و دەپرسێت ئەمە چ بەشێکی ڤۆڵتیەی پێوەرە. ئەنجامەکەی، 0 تا 1023، دەچێت بۆ CPU کە analogRead() جێبەجێ دەکات. لە خوارەوە: مەڵتیپلێکسەرەکە لە هەر جارێکدا یەک پین بە ADCـەوە دەبەستێت.
Six analog pins, one selector, one ADC.شەش ، یەک ، یەک ADC.CêIT

n bits, 2ⁿ levelsnn بیت، 2n2^n ئاست

An ADC with n bits sorts its input into 2n2^n levels, numbered from 0 to 2n−12^n - 1 (How many patterns do N bits give?). The largest number is always one less than the number of levels, because counting starts at 0.

ADCـێک بە nn دابەش دەکات بەسەر 2n2^n ئاستـدا کە لە 0ـەوە تا 2n−12^n - 1 ژمارە دەکرێن (NN بیت چەند شێوە دەدات؟). گەورەترین ژمارە هەمیشە یەکێک کەمترە لە ژمارەی ئاستەکان، چونکە ژماردن لە 0ـەوە دەست پێ دەکات.

BitsLevelsئاستەکانValuesنرخەکانWhereلە کوێ
380–7the staircase picture belowوێنەی پەیژەکەی خوارەوە
82560–255MKR boards can be set to 8 bitsبۆردەکانی MKR دەتوانن لەسەر 8 دابنرێن
101,0240–1023UNO R3, Nano, Mega; the default everywhere⁦UNO R3⁩، Nano، Mega؛ بنەڕەت لە هەموو شوێنێک
124,0960–4095MKR WAN 1310, Nano 33 IoT⁦MKR WAN 1310⁩، ⁦Nano 33 IoT⁩
1416,3840–16383UNO R4 Minima and WiFi⁦UNO R4 Minima⁩ و WiFi

So a 12-bit ADC gives 0 to 4095, not 0 to 4096: 4096 is the number of levels, not the top value. In the same way an 8-bit ADC gives 0 to 255.

بۆیە ADCـێکی ⁦12⁩ لە 0 تا ⁦4095⁩ دەدات، نەک 0 تا ⁦4096: 4096⁩ ژمارەی ئاستەکانە، نەک بەرزترین نرخ. بە هەمان شێوە ADCـێکی 8 لە 0 تا ⁦255⁩ دەدات.

n bits, 2ⁿ levelsnn بیت، 2n2^n ئاست

The staircase: 3 bits from 0 to 5 Vپەیژەکە: 3 بیت لە 0 تا ⁦5 V⁩

Picture a 3-bit ADC with a 5 V reference. Its 8 levels split 0–5 V into 8 steps of 5 ÷ 8 = 0.625 V. As the input rises smoothly, the output climbs a staircase: 0 to 0.625 V gives 000, 0.625 to 1.25 V gives 001, and so on, up to 111 for 4.375 V and above.

ADCـێکی 3 بە ⁦5 V⁩ وێنا بکە. 8 ئاستەکەی 0 تا ⁦5 V⁩ دەکەنە 8 هەنگاوی ⁦5 ÷ 8 = 0.625 V⁩. کاتێک بە نەرمی بەرز دەبێتەوە، بە پەیژەیەکدا دەچێتە سەرەوە: 0 تا ⁦0.625 V⁩ دەبێتە ⁦000⁩، ⁦0.625⁩ تا ⁦1.25 V⁩ دەبێتە ⁦001⁩ و بەم شێوەیە، تا ⁦111⁩ بۆ ⁦4.375 V⁩ و سەرووتر.

Every voltage inside one step gives the same code: 2.0 V and 2.4 V both give 011, which is 3. That lost detail is the price of turning a smooth voltage into a number. More bits make the steps smaller, never zero.

هەموو لە ناو یەک هەنگاودا هەمان کۆد دەدات: ⁦2.0 V⁩ و ⁦2.4 V⁩ هەردووکیان ⁦011⁩ دەدەن، کە 3ـە. ئەو وردەکارییە لەدەستچووە نرخی گۆڕینی نەرمە بۆ ژمارە. زیاتر هەنگاوەکان بچووکتر دەکەن، بەڵام هەرگیز سفریان ناکەن.

A graph with the input voltage Vin from 0 to 5 V across and the 3-bit codes 000 to 111 up the side. A green staircase climbs one code every 0.625 V, next to a grey dashed diagonal line for the ideal smooth value. Orange marks at 2.0 V and 2.4 V both reach the step of code 011: same code. A double arrow over one step says one step = 0.625 V.گرافێک کە ڤۆڵتیەی نێوخستە Vin لە 0 تا 5 V بە درێژیدایە و کۆدە 3 بیتییەکانی 000 تا 111 بە لای لاکەوە. پەیژەیەکی سەوز هەر 0.625 V کۆدێک بەرز دەبێتەوە، لە تەنیشت هێڵێکی خۆڵەمێشیی پچڕپچڕی لار بۆ نرخە نەرمە بێهەڵەکە. نیشانە پرتەقاڵییەکان لە 2.0 V و 2.4 V هەردووکیان دەگەنە هەنگاوی کۆدی 011: هەمان کۆد. تیرێکی دوولایەنە لەسەر یەک هەنگاو دەڵێت یەک هەنگاو = 0.625 V.
Inside one step, every voltage gives the same code.لە ناو یەک هەنگاودا هەموو هەمان کۆد دەدات.CêIT

n bits, 2ⁿ levelsnn بیت، 2n2^n ئاست

Watch analogRead() follow a knobسەیری ⁦analogRead()⁩ بکە کە بەدوای دەسکێکدا دەڕوات

On a real UNO the ADC has 10 bits, so the staircase has 1024 steps — too small to see — and the animation shows the number instead. A potentiometer on A0 is turned from 0 V to 5 V while a sketch prints analogRead(A0). The number follows the voltage in proportion: about half of 5 V gives about half of 1023.

لەسەر UNOـی ڕاستەقینە ⁦ADC 10⁩ هەیە، بۆیە پەیژەکە ⁦1024⁩ هەنگاوی هەیە — زۆر بچووکن بۆ بینین — بۆیە لەبری ئەوە ژمارەکە پیشان دەدات. لەسەر ⁦A0⁩ لە ⁦0 V⁩ـەوە بۆ ⁦5 V⁩ دەسووڕێنرێت لە کاتێکدا analogRead(A0) چاپ دەکات. ژمارەکە بە ڕێژە بەدوای دەڕوات: نزیکەی نیوەی ⁦5 V⁩ نزیکەی نیوەی ⁦1023⁩ دەدات.

An animation in six steps. Left, a knob and the voltage on A0; middle, a bar from 0 to 1023 that fills up; right, the code int reading = analogRead(A0); Serial.println(reading); and a Serial Monitor. The knob turns to 0.00 V, 1.10 V, 2.40 V, 3.70 V and 5.00 V; the readings are 0, 225, 491, 757 and 1023, and each one is added to the Serial Monitor. Under each step the sum is shown, for example 2.40 V × 1024 ÷ 5 V = 491.5, which becomes 491; at 5.00 V the result 1024 stops at the top value 1023. Last frame: about half of 5 V gives about half of 1023.
An animation in six steps. Left, a knob and the voltage on A0; middle, a bar from 0 to 1023 that fills up; right, the code int reading = analogRead(A0); Serial.println(reading); and a Serial Monitor. The knob turns to 0.00 V, 1.10 V, 2.40 V, 3.70 V and 5.00 V; the readings are 0, 225, 491, 757 and 1023, and each one is added to the Serial Monitor. Under each step the sum is shown, for example 2.40 V × 1024 ÷ 5 V = 491.5, which becomes 491; at 5.00 V the result 1024 stops at the top value 1023. Last frame: about half of 5 V gives about half of 1023.
The reading grows in proportion to the voltage, from 0 to 1023.خوێندنەوەکە بە ڕێژە لەگەڵ گەورە دەبێت، لە 0 تا ⁦1023⁩.CêIT

How big is one step?یەک هەنگاو چەند گەورەیە؟

One step — also called 1 LSB, the value of the least significant bit — is the reference voltage divided by the number of levels:

یەک هەنگاو — کە پێی دەوترێت ⁦1 LSB⁩ـیش، واتە نرخی کەمگرنگترین بیت — دابەش بە ژمارەی ئاستەکان:

step=Vref2n\text{step} = \frac{V_{\text{ref}}}{2^n}
UNO R3 (10 bits, 5 V):5 V1024=4.8828 mV≈4.9 mVMKR WAN 1310 (12 bits, 3.3 V):3.3 V4096=0.8057 mVUNO R4 (14 bits, 5 V):5 V16384=0.3052 mV\begin{aligned} \text{UNO R3 (10 bits, 5 V):}\quad & \frac{5\,\text{V}}{1024} = 4.8828\,\text{mV} \approx 4.9\,\text{mV} \\ \text{MKR WAN 1310 (12 bits, 3.3 V):}\quad & \frac{3.3\,\text{V}}{4096} = 0.8057\,\text{mV} \\ \text{UNO R4 (14 bits, 5 V):}\quad & \frac{5\,\text{V}}{16384} = 0.3052\,\text{mV} \end{aligned}

The Arduino reference rounds the UNO's step the same way: 5 volts / 1024 units, about 4.9 mV per unit. This step is the ADC's resolution: a change smaller than one step may not change the reading at all (Resolution: the smallest step you can see).

سەرچاوەی Arduino هەنگاوی UNO بە هەمان شێوە نزیک دەکاتەوە: 5 / ⁦1024⁩ یەکە، نزیکەی ⁦4.9 mV⁩ بۆ هەر یەکەیەک. ئەم هەنگاوە ADCـە: گۆڕانێکی بچووکتر لە یەک هەنگاو لەوانەیە هەرگیز خوێندنەوەکە نەگۆڕێت (توانای جیاکردنەوە: بچووکترین هەنگاو کە دەبینرێت).

A card with four formulas for n bits and the reference Vref, with an UNO R3 example (n = 10, 5 V) on the right. Levels: 2ⁿ, 1024. One step (1 LSB): Vref ÷ 2ⁿ, 4.88 mV. Reading R = ⌊Vin × 2ⁿ ÷ Vref⌋: 2.37 V gives 485. Volts from R: Vin ≈ R × Vref ÷ 2ⁿ: 485 gives 2.368 V. Footer: ⌊ ⌋ means round down; the top reading is 2ⁿ − 1, for example 1023.کارتێک بە چوار هاوکێشە بۆ n بیت و ڤۆڵتیەی پێوەر Vref، بە نموونەیەکی UNO R3 (n = 10، 5 V) لە لای ڕاست. ئاستەکان: 2ⁿ، 1024. یەک هەنگاو (1 LSB): Vref ÷ 2ⁿ، 4.88 mV. خوێندنەوەی R = ⌊Vin × 2ⁿ ÷ Vref⌋: 2.37 V دەبێتە 485. ڤۆڵت لە Rـەوە: Vin ≈ R × Vref ÷ 2ⁿ: 485 دەبێتە 2.368 V. لە خوارەوە: ⌊ ⌋ واتە نزیککردنەوە بەرەو خوار؛ بەرزترین خوێندنەوە 2ⁿ − 1ـە، بۆ نموونە 1023.
Four lines that cover every analogRead() calculation.چوار دێڕ کە هەموو هەژمارێکی ⁦analogRead()⁩ دەگرنەوە.CêIT

How big is one step?یەک هەنگاو چەند گەورەیە؟

What one step means for a thermometerیەک هەنگاو بۆ گەرماپێوێک واتای چییە؟

Imagine a temperature sensor whose output covers 0–50 °C across the ADC's whole range. With 8 bits, the 256 levels split 50 °C into steps of about 0.2 °C; with 10 bits, about 0.05 °C; with 12 bits, about 0.012 °C — the difference between seeing 37.1 °C and 37.12 °C.

وێنا بکە کە 0 تا ⁦50 °C⁩ بەسەر هەموو ئاستەکانی ADCـدا دابەش دەکات. بە 8 ، ⁦256⁩ ئاستەکە ⁦50 °C⁩ دەکەنە هەنگاوی نزیکەی ⁦0.2 °C⁩؛ بە ⁦10⁩ نزیکەی ⁦0.05 °C⁩؛ بە ⁦12⁩ نزیکەی ⁦0.012 °C⁩ — جیاوازیی نێوان بینینی ⁦37.1 °C⁩ و ⁦37.12 °C⁩.

50 ∘C256=0.195 ∘C50 ∘C1024=0.049 ∘C50 ∘C4096=0.012 ∘C\frac{50\,^\circ\text{C}}{256} = 0.195\,^\circ\text{C} \qquad \frac{50\,^\circ\text{C}}{1024} = 0.049\,^\circ\text{C} \qquad \frac{50\,^\circ\text{C}}{4096} = 0.012\,^\circ\text{C}

Even 8 bits is far finer than "cold, medium, hot", which needs only 2 bits (4 levels). And the ADC is often not the real limit: a sensor that is only accurate to ±2 °C gains nothing real from 0.012 °C steps (Many digits do not make a good sensor).

تەنانەت 8 زۆر وردترە لە «سارد، مامناوەند، گەرم» کە تەنیا 2 (⁦4⁩ ئاست) پێویستە. هەروەها زۆرجار ADC سنووری ڕاستەقینە نییە: کە تەنیا ⁦±2 °C⁩ـە هیچ سوودێکی ڕاستەقینە لە هەنگاوی ⁦0.012 °C⁩ وەرناگرێت (ژمارەی زۆری دوای فاریزە هەستەوەرێکی باش دروست ناکات).

Three strips for 8, 10 and 12 bits, each zoomed on 37.0 to 37.2 °C of a 0–50 °C range, with a line for every new reading value. 8 bits, 256 levels: one step about 0.2 °C, a single line. 10 bits, 1024 levels: one step about 0.05 °C, four lines. 12 bits, 4096 levels: one step about 0.012 °C, sixteen lines.سێ شریت بۆ 8، 10 و 12 بیت، هەریەکەیان لە 37.0 تا 37.2 °Cـی ناوچەیەکی 0 تا 50 °C نزیک کراوەتەوە، بە هێڵێک بۆ هەر نرخێکی نوێی خوێندنەوە. 8 بیت، 256 ئاست: یەک هەنگاو نزیکەی 0.2 °C، یەک هێڵ. 10 بیت، 1024 ئاست: یەک هەنگاو نزیکەی 0.05 °C، چوار هێڵ. 12 بیت، 4096 ئاست: یەک هەنگاو نزیکەی 0.012 °C، شانزە هێڵ.
Each extra 2 bits makes the steps four times smaller.هەر 2 زیادە هەنگاوەکان چوار جار بچووکتر دەکات.CêIT

From reading to volts, and backلە خوێندنەوەوە بۆ ڤۆڵت و بە پێچەوانەوە

The ADC counts how many whole steps fit under the input voltage, so a reading is the voltage divided by one step, rounded down; going back, multiply the reading by the step:

ADC دەژمێرێت چەند هەنگاوی تەواو لە ژێر نێوخستەدا جێ دەبنەوە، بۆیە خوێندنەوە دابەش بە یەک هەنگاو، بەرەو خوار نزیککراوە؛ بۆ گەڕانەوە، خوێندنەوەکە لێکبدە لە هەنگاوەکە:

R=⌊Vin×2nVref⌋ (at most 2n−1)Vin≈R×Vref2nR = \left\lfloor \frac{V_{\text{in}} \times 2^n}{V_{\text{ref}}} \right\rfloor \ (\text{at most } 2^n - 1) \qquad V_{\text{in}} \approx R \times \frac{V_{\text{ref}}}{2^n}
R=⌊2.37×10245⌋=⌊485.376⌋=485V≈485×5 V1024=2.368 V\begin{aligned} R &= \left\lfloor \frac{2.37 \times 1024}{5} \right\rfloor = \lfloor 485.376 \rfloor = 485 \\ V &\approx 485 \times \frac{5\,\text{V}}{1024} = 2.368\,\text{V} \end{aligned}

Worked example on an UNO R3. The true voltage lies somewhere from 2.368 V to 2.373 V: inside one step.

نموونەیەکی هەنگاوبەهەنگاو لەسەر ⁦UNO R3⁩. ڕاستەقینە لە شوێنێکی نێوان ⁦2.368 V⁩ و ⁦2.373 V⁩ـدایە: لە ناو یەک هەنگاودا.

A sensor that changes its resistance, such as an LDR, gives no voltage by itself: a voltage divider turns it into a voltage that these formulas can use (The voltage divider).

کە دەگۆڕێت، وەک ، خۆی هیچ نادات: دەیکات بە کە ئەم بەکاری بهێنن (دابەشکەری ڤۆڵتیە).

analogRead() on an UNO⁦analogRead()⁩ لەسەر UNO

analogRead(pin) reads the voltage on an analog input pin such as A0 and gives you a number that represents it — not the voltage itself. On a 10-bit board the number goes from 0 to 1023, relative to the reference voltage. By default the reference is the board's operating voltage: 5 V on an UNO, 3.3 V on MKR boards. The pin needs no pinMode() for this.

analogRead(pin) وەک ⁦A0⁩ دەخوێنێتەوە و ژمارەیەکت دەداتێ کە نوێنەرایەتیی دەکات — نەک خودی . لەسەر بۆردێکی ⁦10⁩ ژمارەکە لە 0 تا ⁦1023⁩ـە، بەپێی . بە شێوەی بنەڕەت کارکردنی بۆردەکەیە: ⁦5 V⁩ لەسەر UNO و ⁦3.3 V⁩ لەسەر بۆردەکانی MKR. پینەکە بۆ ئەمە پێویستی بە pinMode() نییە.

Lab: connect a 10 kΩ potentiometer — its outer legs to 5V and GND, its middle leg (the wiper) to A0 — and print the reading and the voltage twice a second:

تاقیگە: ⁦10 kΩ⁩ ببەستە — قاچە دەرەکییەکانی بە ⁦5V⁩ و GND، قاچە ناوەڕاستەکەی بە ⁦A0⁩ — و خوێندنەوەکە و چرکەی دوو جار چاپ بکە:

Read a voltage on A0 (UNO R3)خوێندنەوەی ڤۆڵتیەیەک لەسەر ⁦A0 (UNO R3)⁩read_voltage.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Read a voltage on A0 and print it (Arduino UNO R3: 10-bit ADC, 5 V reference)
const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int reading = analogRead(sensorPin);    // 0 to 1023
  float volts = reading * 5.0 / 1024.0;   // one step = 5 V / 1024
  Serial.print("reading = ");
  Serial.print(reading);
  Serial.print("   volts = ");
  Serial.println(volts);
  delay(500);
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 9

analogRead() returns a whole number from 0 to 1023.

⁦analogRead()⁩ لە 0 تا ⁦1023⁩ دەگەڕێنێتەوە.

Lineدێڕی 10

One step is 5 V / 1024; write 5.0 and 1024.0 so the sum is done with decimals.

یەک هەنگاو ⁦5 V / 1024⁩ـە؛ ⁦5.0⁩ و ⁦1024.0⁩ بنووسە بۆ ئەوەی هەژمارەکە بە بکرێت.

Lineدێڕی 11-14

print() keeps the line open; println() ends it.

⁦print()⁩ دێڕەکە کراوە دەهێڵێتەوە؛ ⁦println()⁩ کۆتایی پێ دەهێنێت.

An Arduino UNO R3 and a 10 kΩ potentiometer. A red wire joins the potentiometer's left leg to 5V, a black wire its right leg to GND, and a green wire its middle leg, the wiper, to A0.Arduino UNO R3 و بەرگری گۆڕاوێکی 10 kΩ. وایەرێکی سوور قاچی چەپی بەرگری گۆڕاوەکە بە 5Vـەوە دەبەستێت، وایەرێکی ڕەش قاچی ڕاستی بە GNDـەوە و وایەرێکی سەوز قاچە ناوەڕاستەکەی بە A0ـەوە.
Outer legs to 5V and GND, middle leg to A0.قاچە دەرەکییەکان بۆ ⁦5V⁩ و GND، قاچی ناوەڕاست بۆ ⁦A0⁩.CêIT

analogRead() on an UNO⁦analogRead()⁩ لەسەر UNO

How fast? About 10,000 readings a secondچەند خێرا؟ نزیکەی ⁦10,000⁩ خوێندنەوە لە چرکەیەکدا

On ATmega boards — the UNO R3, Nano and Mega — one analogRead() takes about 100 microseconds, so a sketch can read at most about 10,000 times a second: the board's sampling rate. That is plenty for temperature, light or a knob, which change slowly. Sound needs more: a CD stores 44,100 samples a second (How sound becomes numbers).

لەسەر بۆردەکانی ⁦ATmega — UNO R3⁩، Nano و Mega — هەر analogRead()ـێک نزیکەی ⁦100⁩ مایکرۆچرکە دەخایەنێت، بۆیە دەتوانێت لە چرکەیەکدا زۆرترین نزیکەی ⁦10,000⁩ جار بخوێنێتەوە: بۆردەکە. ئەمە بۆ پلەی گەرمی، ڕووناکی یان دەسکێک زۆرە، چونکە هێواش دەگۆڕێن. دەنگ پێویستی بە زیاترە: CD لە چرکەیەکدا ⁦44,100⁩ نموونە هەڵدەگرێت (دەنگ چۆن دەبێتە ژمارە).

Many sensors are slower than the ADC anyway: the DHT22 allows one reading every 2 seconds. Reading a sensor faster than it can measure only repeats old values.

زۆر هەر لە ADC هێواشترن: ⁦DHT22⁩ هەر 2 چرکە جارێک ڕێگەی خوێندنەوە دەدات. خوێندنەوەی خێراتر لەوەی دەتوانێت بیپێوێت تەنیا نرخی کۆن دووپاتە دەکاتەوە.

A timeline of 1 millisecond, from 0 to 1000 µs, divided into ten parts. Above it, a slowly changing sensor voltage is read ten times, once every 100 µs, shown as green dots. Footer: on the UNO R3, Nano and Mega one analogRead() takes about 100 µs, 0.0001 s.هێڵێکی کاتی 1 ملیچرکە، لە 0 تا 1000 µs، کە کراوە بە دە بەش. لە سەرووییەوە ڤۆڵتیەیەکی هێواش گۆڕاوی هەستەوەر دە جار دەخوێندرێتەوە، هەر 100 µs جارێک، بە خاڵی سەوز پیشان دراوە. لە خوارەوە: لەسەر UNO R3، Nano و Mega هەر analogRead()ـێک نزیکەی 100 µs، واتە 0.0001 s، دەخایەنێت.
Ten readings fit into one millisecond.دە خوێندنەوە لە یەک ملیچرکەدا جێ دەبنەوە.CêIT

analogRead on each boardanalogRead لەسەر هەر بۆردێک

Boards differ in ADC bits, reference voltage and analog pins. The course uses the UNO R3 and the MKR WAN 1310; the UNO R4 is the UNO sold today (Arduino Boards and the Arduino IDE).

بۆردەکان لە ADC، و جیاوازن. کۆرسەکە ⁦UNO R3⁩ و ⁦MKR WAN 1310⁩ بەکار دەهێنێت؛ ⁦UNO R4⁩ ئەو UNOـیە کە ئەمڕۆ دەفرۆشرێت (بۆردەکانی Arduino و Arduino IDE).

analogRead() on four boards (Arduino reference and tech specs, checked November 2026)⁦analogRead()⁩ لەسەر چوار بۆرد (سەرچاوە و تایبەتمەندییە تەکنیکییەکانی Arduino، پشکنراو لە تشرینی دووەمی ⁦2026⁩)
BoardبۆردADCanalogRead() by default⁦analogRead()⁩ بە شێوەی بنەڕەتReferenceAnalog pinsOne stepیەک هەنگاو
UNO R3, Nano10 bits⁦10⁩ 0–10235 VA0–A5 (Nano A0–A7)4.88 mV
Mega 256010 bits⁦10⁩ 0–10235 V16 (A0–A15)⁦16⁩ پین ⁦(A0–A15)⁩4.88 mV
UNO R4 Minima / WiFiup to 14 bitsتا ⁦14⁩ 0–1023 (10 bits)⁦0–1023 (10⁩ )5 VA0–A50.31 mV at 14 bits⁦0.31 mV⁩ بە ⁦14⁩
MKR WAN 1310up to 12 bitsتا ⁦12⁩ 0–1023 (10 bits)⁦0–1023 (10⁩ )3.3 VA0–A60.81 mV at 12 bits⁦0.81 mV⁩ بە ⁦12⁩

Two surprises. First, the newer boards start at 10 bits too, for compatibility, until you ask for more with analogReadResolution(). Second, on a 3.3 V board the top of the range is 3.3 V: never put 5 V on its pins (5 V boards and 3.3 V boards).

دوو شتی چاوەڕواننەکراو. یەکەم، بۆردە نوێیەکانیش بۆ گونجان لەگەڵ کۆنەکان بە ⁦10⁩ دەست پێ دەکەن، تا بە analogReadResolution() داوای زیاتر دەکەیت. دووەم، لەسەر بۆردێکی ⁦3.3 V⁩ بەرزترین کە دەخوێندرێتەوە ⁦3.3 V⁩ـە: هەرگیز ⁦5 V⁩ مەخە سەر پینەکانی (بۆردەکانی ⁦5 V⁩ و بۆردەکانی ⁦3.3 V⁩).

Four cards. UNO R3 and Nano: 10-bit ADC, analogRead() 0–1023, reference 5 V, pins A0–A5 (Nano A0–A7), one step 4.88 mV. Mega 2560: 10-bit, 0–1023, 5 V, 16 pins A0–A15, 4.88 mV. UNO R4 Minima and WiFi: 14-bit ADC, 0–1023 by default, 5 V, A0–A5, 0.31 mV at 14 bits. MKR WAN 1310: 12-bit ADC, 0–1023 by default, 3.3 V, A0–A6, 0.81 mV at 12 bits. Footer: newer boards start at 10 bits too; call analogReadResolution() to get more.چوار کارت. UNO R3 و Nano: ADCـی 10 بیت، analogRead() 0–1023، ڤۆڵتیەی پێوەر 5 V، پینەکانی A0–A5 (Nano A0–A7)، یەک هەنگاو 4.88 mV. Mega 2560: 10 بیت، 0–1023، 5 V، 16 پینی A0–A15، 4.88 mV. UNO R4 Minima و WiFi: ADCـی 14 بیت، بە شێوەی بنەڕەت 0–1023، 5 V، A0–A5، 0.31 mV بە 14 بیت. MKR WAN 1310: ADCـی 12 بیت، بە شێوەی بنەڕەت 0–1023، 3.3 V، A0–A6، 0.81 mV بە 12 بیت. لە خوارەوە: بۆردە نوێیەکانیش بە 10 بیت دەست پێ دەکەن؛ بۆ زیاتر analogReadResolution() بانگ بکە.
Know your board's bits and reference before you convert a reading.پێش گۆڕینی خوێندنەوەیەک و بۆردەکەت بزانە.CêIT

analogRead on each boardanalogRead لەسەر هەر بۆردێک

More bits: analogReadResolution()بیتەکانی زیاتر: ⁦analogReadResolution()⁩

On boards with a bigger ADC, analogReadResolution(bits) sets how many bits analogRead() returns: analogReadResolution(12) gives 0–4095 on the MKR WAN 1310, and analogReadResolution(14) gives 0–16383 on the UNO R4. Ask for more bits than the chip has and the extra bits are filled with zeros; ask for fewer and the lowest bits are dropped. On an UNO R3 the function does not exist, so the sketch will not compile.

لەسەر بۆردەکان کە ADCـی گەورەتریان هەیە، analogReadResolution(bits) دیاری دەکات analogRead() چەند بگەڕێنێتەوە: analogReadResolution(12) لەسەر ⁦MKR WAN 1310 0–4095⁩ دەدات و analogReadResolution(14) لەسەر ⁦UNO R4 0–16383⁩. داوای زیاتر لەوەی هەیەتی بکە، زیادەکان بە سفر پڕ دەکرێنەوە؛ داوای کەمتر بکە، نزمەکان لا دەبرێن. لەسەر ⁦UNO R3⁩ ئەم نییە، بۆیە کۆمپایل نابێت.

10 and 12 bits on the MKR WAN 1310⁦10⁩ و ⁦12⁩ بیت لەسەر ⁦MKR WAN 1310⁩adc_12bit.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Arduino MKR WAN 1310: read A1 with 10 and with 12 bits (3.3 V reference)
void setup() {
  Serial.begin(9600);
  while (!Serial) { }          // MKR boards: wait until the Serial Monitor is open
}

void loop() {
  analogReadResolution(10);
  int r10 = analogRead(A1);    // 0 to 1023
  analogReadResolution(12);
  int r12 = analogRead(A1);    // 0 to 4095
  Serial.print("10-bit: ");
  Serial.print(r10);
  Serial.print("   12-bit: ");
  Serial.print(r12);
  Serial.print("   volts: ");
  Serial.println(r12 * 3.3 / 4096.0, 3);
  delay(1000);
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

MKR boards talk over native USB: wait until the Serial Monitor is open.

بۆردەکانی MKR بە USBـی ڕاستەوخۆ قسە دەکەن: چاوەڕێ بکە تا دەکرێتەوە.

Lineدێڕی 8-11

Switch between 10 and 12 bits and read the same pin twice.

لە نێوان ⁦10⁩ و ⁦12⁩ بگۆڕە و هەمان پین دوو جار بخوێنەوە.

Lineدێڕی 17

At 12 bits one step is 3.3 V / 4096; the 3 asks for three decimal places.

بە ⁦12⁩ یەک هەنگاو ⁦3.3 V / 4096⁩ـە؛ 3 داوای سێ دەکات.

The reference voltage: the top of the rulerڤۆڵتیەی پێوەر: سەری ڕاستەکە

Every reading is a fraction of the reference voltage, so the reference is the length of the ADC's ruler. On an UNO R3, analogReference() can change it: DEFAULT (5 V), INTERNAL (a built-in 1.1 V) or EXTERNAL (the voltage on the AREF pin, 0 to 5 V only). The same 1.00 V reads 204 with the 5 V ruler but 930 with the 1.1 V ruler: a shorter ruler gives finer steps (1.1 V ÷ 1024 ≈ 1.07 mV) — but anything above 1.1 V then reads 1023.

هەموو خوێندنەوەیەک بەشێکە لە ، بۆیە درێژیی ڕاستەکەی ADCـە. لەسەر ⁦UNO R3⁩، analogReference() دەتوانێت بیگۆڕێت: DEFAULT ⁦(5 V)⁩، INTERNAL (⁦1.1 V⁩ـی ناوخۆیی) یان EXTERNAL ( پینی AREF، تەنیا 0 تا ⁦5 V⁩). هەمان ⁦1.00 V⁩ بە ڕاستەی ⁦5 V⁩ ⁦204⁩ دەخوێندرێتەوە، بەڵام بە ڕاستەی ⁦1.1 V⁩ ⁦930⁩: ڕاستەیەکی کورتتر هەنگاوی وردتر دەدات ⁦(1.1 V ÷ 1024 ≈ 1.07 mV)⁩ — بەڵام هەر شتێک لە سەرووی ⁦1.1 V⁩ـەوە دەبێتە ⁦1023⁩.

Other boards name the options differently: the UNO R4 has AR_DEFAULT (5 V) and AR_INTERNAL (1.5 V); SAMD boards such as the MKR family have AR_DEFAULT (3.3 V), AR_INTERNAL1V0, AR_INTERNAL1V65, AR_INTERNAL2V23 and AR_EXTERNAL.

بۆردەکانی تر بژاردەکان بە ناوی جیاواز دەنێن: ⁦UNO R4⁩ AR_DEFAULT ⁦(5 V)⁩ و AR_INTERNALـی ⁦(1.5 V)⁩ هەیە؛ بۆردەکانی SAMD وەک خێزانی MKR AR_DEFAULT ⁦(3.3 V)⁩، AR_INTERNAL1V0، AR_INTERNAL1V65، AR_INTERNAL2V23 و AR_EXTERNALـیان هەیە.

Two vertical rulers side by side, each from 0 V, which reads 0, to the top, which reads 1023. Left, DEFAULT, 5 V, 4.88 mV per step: a red line at 1.00 V reads 204. Right, INTERNAL, 1.1 V, 1.07 mV per step: the same 1.00 V is near the top and reads 930.دوو ڕاستەی ستوونی لە تەنیشت یەکەوە، هەریەکەیان لە 0 Vـەوە، کە 0 دەخوێندرێتەوە، تا سەرەوە، کە 1023 دەخوێندرێتەوە. چەپ، DEFAULT، 5 V، 4.88 mV بۆ هەر هەنگاوێک: هێڵێکی سوور لە 1.00 Vـدا 204 دەخوێندرێتەوە. ڕاست، INTERNAL، 1.1 V، 1.07 mV بۆ هەر هەنگاوێک: هەمان 1.00 V نزیکی سەرەوەیە و 930 دەخوێندرێتەوە.
A shorter ruler: finer steps, smaller range.ڕاستەیەکی کورتتر: هەنگاوی وردتر، سەرێکی نزمتر.CêIT

The reference voltage: the top of the rulerڤۆڵتیەی پێوەر: سەری ڕاستەکە

Worked example: a TMP36 on A0نموونەیەکی هەنگاوبەهەنگاو: ⁦TMP36⁩ لەسەر ⁦A0⁩

Put the pieces together on an UNO R3. With its flat face towards you, the TMP36's left leg goes to 5V, its middle leg (the output) to A0 and its right leg to GND. The sketch turns the reading into volts and the volts into °C with the line from Sensitivity: T = (V − 0.5 V) ÷ 0.010 V/°C. Hold the sensor between two fingers and watch it warm:

پارچەکان لەسەر ⁦UNO R3⁩ پێکەوە دابنێ. کاتێک ڕووە تەختەکەی بەرەو تۆیە، قاچی چەپی ⁦TMP36⁩ دەچێت بۆ ⁦5V⁩، قاچە ناوەڕاستەکەی () بۆ ⁦A0⁩ و قاچی ڕاستی بۆ GND. خوێندنەوەکە دەکاتە و دەکاتە °C بە هێڵەکەی هەستیاری: ⁦T = (V − 0.5 V) ÷ 0.010 V/°C⁩. لە نێوان دوو پەنجەدا بگرە و سەیری گەرمبوونەکەی بکە:

TMP36 on A0: reading → volts → °C (UNO R3)⁦TMP36⁩ لەسەر ⁦A0⁩: خوێندنەوە ← ڤۆڵت ← °⁦C (UNO R3)⁩tmp36.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// TMP36 temperature sensor on A0 (Arduino UNO R3, 5 V reference)
const int tmpPin = A0;
const float CAL_OFFSET = 0.0;   // correction in °C from a calibration

void setup() {
  Serial.begin(9600);
}

void loop() {
  int reading = analogRead(tmpPin);
  float volts = reading * 5.0 / 1024.0;
  float celsius = (volts - 0.5) / 0.010 + CAL_OFFSET;
  Serial.print(reading);
  Serial.print(" -> ");
  Serial.print(volts, 3);
  Serial.print(" V -> ");
  Serial.print(celsius, 1);
  Serial.println(" C");
  delay(1000);
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

A correction found by calibration, for example −1.2 after an ice-water test.

ڕاستکردنەوەیەک کە بە دۆزراوەتەوە، بۆ نموونە ⁦−1.2⁩ دوای تاقیکردنەوەی ئاوی سەهۆڵاوی.

Lineدێڕی 12

The TMP36 line turned around: 0.5 V at 0 °C, 10 mV per °C.

هێڵی ⁦TMP36⁩ بە پێچەوانەوە: ⁦0.5 V⁩ لە ⁦0 °C⁩، ⁦10 mV⁩ بۆ هەر °C.

Lineدێڕی 15

print(value, 3): three decimal places.

⁦print(value, 3)⁩: سێ .

The readings jump in steps of about 0.5 °C: one ADC step of 4.88 mV is half a degree for a 10 mV/°C sensor. That is still finer than the TMP36's ±2 °C accuracy, so the sensor, not the ADC, limits the result. If you calibrate it (Drift and calibration), put the correction in CAL_OFFSET.

خوێندنەوەکان بە هەنگاوی نزیکەی ⁦0.5 °C⁩ باز دەدەن: یەک هەنگاوی ADC کە ⁦4.88 mV⁩ـە بۆ ⁦10 mV/°C⁩ نیو پلەیە. ئەمە هێشتا لە ⁦±2 °C⁩ـی ⁦TMP36⁩ وردترە، بۆیە ، نەک ADC، ئەنجامەکە سنووردار دەکات. ئەگەر بکەیت (لادانی هەستەوەر و ڕێکخستنی پێوان)، ڕاستکردنەوەکە بخەرە ناو CAL_OFFSET.

Left: a TMP36 seen with its flat face towards you, three legs: +VS to 5V, VOUT to A0, GND to GND. Right: a chain of five boxes, 24.7 °C, 0.747 V, 153, 0.747 V and 24.7 °C, joined by arrows labelled sensor: 0.5 V + 10 mV/°C; ADC: × 1024 ÷ 5 V; sketch: × 5 V ÷ 1024; sketch: (V − 0.5) ÷ 0.010. Note: one ADC step = 4.88 mV = about 0.5 °C for this sensor; its accuracy of ±2 °C is still the bigger limit.چەپ: TMP36 کە ڕووە تەختەکەی بەرەو تۆیە، سێ قاچ: +VS بۆ 5V، VOUT بۆ A0، GND بۆ GND. ڕاست: زنجیرەیەک لە پێنج خانە، 24.7 °C، 0.747 V، 153، 0.747 V و 24.7 °C، کە بە تیر بەیەکەوە بەستراون و نووسراوە: هەستەوەر: 0.5 V + 10 mV/°C؛ ADC: × 1024 ÷ 5 V؛ سکێچ: × 5 V ÷ 1024؛ سکێچ: (V − 0.5) ÷ 0.010. تێبینی: یەک هەنگاوی ADC = 4.88 mV = نزیکەی 0.5 °C بۆ ئەم هەستەوەرە؛ ڕاستیی پێوانەکەی، ±2 °C، هێشتا سنوورە گەورەترەکەیە.
From °C to a number in the chip, and back to °C in the sketch.لە °Cـەوە بۆ ژمارە لە و دیسان بۆ °C لە .CêIT

A pin with nothing on it reads noiseپینێک کە هیچی پێوە نییە ژاوەژاو دەخوێنێتەوە

Pull the wire out of A0 and the numbers do not fall to 0: they wander. An analog pin with nothing connected is a floating input: nothing sets its voltage, so it picks up noise from nearby wires, the other analog inputs and even your hand. The Arduino reference warns about exactly this. Digital pins float in the same way (A pin connected to nothing floats).

وایەرەکە لە ⁦A0⁩ دەربهێنە و ژمارەکان بۆ 0 دانابەزن: دەسووڕێنەوە. کە هیچی پێوە نەبەسترابێت ـە: هیچ شتێک دیاری ناکات، بۆیە لە وایەرە نزیکەکان، تر و تەنانەت دەستی تۆوە وەردەگرێت. سەرچاوەی Arduino ڕێک ئاگاداری ئەمە دەکاتەوە. پینە بە هەمان شێوە هەڵدەواسرێن (پینێک کە بە هیچەوە نەبەسترابێت هەڵدەواسرێت).

An unconnected analog pin (UNO R3)پینی نێوخستەی ئەنالۆگێکی نەبەستراو ⁦(UNO R3)⁩floating_a1.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Nothing is connected to A1: what does analogRead() give? (Arduino UNO R3)
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A1));
  delay(300);
}
A graph of analogRead() over 1.7 seconds. A flat green line at 491: A0 with a potentiometer connected reads 491, 491, 491. An orange line jumps up and down between about 270 and 410: A1 with nothing connected reads 312, 287, 355, 301, 410, 268.گرافی analogRead() بە درێژایی 1.7 چرکە. هێڵێکی سەوزی ڕێک لە 491: A0 کە بەرگری گۆڕاوی پێوە بەستراوە 491، 491، 491 دەخوێنێتەوە. هێڵێکی پرتەقاڵی لە نێوان نزیکەی 270 و 410ـدا سەر و خوار دەکات: A1 کە هیچی پێوە نییە 312، 287، 355، 301، 410، 268 دەخوێنێتەوە.
Only a connected pin gives a meaningful reading.تەنیا پینێکی بەستراو خوێندنەوەیەکی واتادار دەدات.CêIT

A pin with nothing on it reads noiseپینێک کە هیچی پێوە نییە ژاوەژاو دەخوێنێتەوە

Calmer readings: take an averageخوێندنەوەی ئارامتر: تێکڕا وەربگرە

Even a connected sensor's readings jitter by a step or two. The simplest cure is to take several readings and use their average: random jumps up and down cancel out (Which error can you fix?). Arduino's Smoothing example keeps a running average of the last 10 readings; this shorter sketch averages 16 readings at once:

تەنانەت خوێندنەوەکانی بەستراویش بە هەنگاوێک یان دوو دەلەرزن. سادەترین چارەسەر ئەوەیە چەند خوێندنەوەیەک وەربگریت و تێکڕاکەیان () بەکار بهێنیت: بازدانە هەڕەمەکییەکانی سەر و خوار یەکتر لا دەبەن (کام هەڵە چاک دەکرێت؟). نموونەی Smoothingـی Arduino تێکڕای جووڵاوی دوایین ⁦10⁩ خوێندنەوە دەهێڵێتەوە؛ ئەم کورتترە تێکڕای ⁦16⁩ خوێندنەوە پێکەوە وەردەگرێت:

Average 16 readings (UNO R3)تێکڕای ⁦16⁩ خوێندنەوە ⁦(UNO R3)⁩average16.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Average 16 readings of A0 to calm the noise (Arduino UNO R3)
const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  long sum = 0;                       // long: a big sum never overflows
  for (int i = 0; i < 16; i++) {
    sum += analogRead(sensorPin);
    delay(2);
  }
  int average = sum / 16;
  Serial.print("one reading: ");
  Serial.print(analogRead(sensorPin));
  Serial.print("   average of 16: ");
  Serial.println(average);
  delay(500);
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 9

long: an UNO int stops at 32,767, and 64 readings of 1023 would pass it.

long: intـی UNO لە ⁦32,767⁩ـدا دەوەستێت و ⁦64⁩ خوێندنەوەی ⁦1023⁩ لێی تێدەپەڕن.

Lineدێڕی 10-13

Read 16 times, 2 ms apart, and add them up.

⁦16⁩ جار بخوێنەوە، ⁦2 ms⁩ لە یەکتر و کۆیان بکەرەوە.

Lineدێڕی 14

Integer division: the average is rounded down.

دابەشکردنی : تێکڕاکە بەرەو خوار نزیک دەکرێتەوە.

A graph of 64 readings over time. An orange zigzag jumps between 488 and 494: one reading at a time. A thick green line stays within 490 to 492: the average of the last 16.گرافی 64 خوێندنەوە بە درێژایی کات. هێڵێکی پرتەقاڵیی زیگزاگ لە نێوان 488 و 494ـدا باز دەدات: خوێندنەوە بە تەنیا. هێڵێکی سەوزی ئەستوور لە نێوان 490 و 492ـدا دەمێنێتەوە: تێکڕای دوایین 16.
Averaging trades a little speed for a much calmer value.تێکڕا کەمێک خێرایی دەدات بە نرخێکی زۆر ئارامتر.CêIT

Common mistakesهەڵە باوەکان

MistakeهەڵەBetterباشتر
Writing the 12-bit range as 0–4096 (or 8 bits as 0–256).نووسینی نرخەکانی ⁦12⁩ وەک ⁦0–4096⁩ (یان هی 8 وەک ⁦0–256⁩).n bits give 2ⁿ levels, numbered 0 to 2ⁿ − 1: 0–4095, 0–255.nn 2n2^n ئاست دەدات، لە 0 تا 2n−12^n - 1: ⁦0–4095⁩، ⁦0–255⁩.
Expecting 0–4095 on an MKR board without asking.چاوەڕوانیی ⁦0–4095⁩ لەسەر بۆردێکی MKR بەبێ داواکردن.Call analogReadResolution(12); the default is 10 bits.⁦analogReadResolution(12)⁩ بانگ بکە؛ بنەڕەت ⁦10⁩ .
Assuming every board has a 3.3 V reference (or 5 V).وا بزانیت هەموو بۆردێک ⁦3.3 V⁩ (یان ⁦5 V⁩)ـی هەیە.UNO: 5 V; MKR, Nano 33 and other 3.3 V boards: 3.3 V.⁦UNO: 5 V⁩؛ MKR، ⁦Nano 33⁩ و بۆردەکانی تری ⁦3.3 V: 3.3 V⁩.
Converting with the old numbers after changing board.گۆڕین بە ژمارە کۆنەکان دوای گۆڕینی بۆرد.Change both: the reference (5.0 or 3.3) and the levels (1024, 4096…).هەردووکیان بگۆڕە: (⁦5.0⁩ یان ⁦3.3⁩) و ئاستەکان (⁦1024⁩، ⁦4096⁩…).
Calling analogReadResolution() on an UNO R3.بانگکردنی ⁦analogReadResolution()⁩ لەسەر ⁦UNO R3⁩.It does not exist there; the UNO R3 is always 10 bits.لەوێ نییە؛ ⁦UNO R3⁩ هەمیشە ⁦10⁩ .
Treating analogRead() as volts.وا مامەڵە لەگەڵ ⁦analogRead()⁩ بکەیت وەک .It returns a number; volts ≈ reading × Vref ÷ 2ⁿ.ژمارەیەک دەگەڕێنێتەوە؛ ≈ خوێندنەوە × ⁦Vref ÷ 2ⁿ⁩.
Trusting a reading from an unconnected pin.متمانەکردن بە خوێندنەوەی پینێکی نەبەستراو.A floating pin reads noise; connect the sensor first. دەخوێنێتەوە؛ سەرەتا ببەستە.
More than 5 V (or 3.3 V) on an analog pin.زیاتر لە ⁦5 V⁩ (یان ⁦3.3 V⁩) لەسەر .Never above the board's voltage; bring it down with a voltage divider.هەرگیز لە بۆردەکە زیاتر مەبێت؛ بە دایبەزێنە.
A voltage on AREF while the reference is DEFAULT. لەسەر AREF لە کاتێکدا DEFAULTـە.Call analogReference(EXTERNAL) first.سەرەتا ⁦analogReference(EXTERNAL)⁩ بانگ بکە.
Using int for a big sum of readings.بەکارهێنانی int بۆ کۆی زۆری خوێندنەوەکان.Use long: an UNO int stops at 32,767.long بەکار بهێنە: intـی UNO لە ⁦32,767⁩ـدا دەوەستێت.

Exercisesڕاهێنانەکان

ExerciseڕاهێنانLevels and stepsئاست و هەنگاو

A 10-bit ADC has a 3.3 V reference. How many levels does it have, how big is one step, and what reading does 1.50 V give?

ADCـێکی ⁦10⁩ ⁦3.3 V⁩ـی هەیە. چەند ئاستی هەیە، یەک هەنگاو چەند گەورەیە و ⁦1.50 V⁩ چ خوێندنەوەیەک دەدات؟

Show answerوەڵامەکە پیشان بدە
210=1024 levels (0 to 1023)step=3.3 V1024=3.22 mVR=⌊1.50×10243.3⌋=⌊465.45⌋=465\begin{aligned} 2^{10} &= 1024 \text{ levels (0 to 1023)} \\ \text{step} &= \frac{3.3\,\text{V}}{1024} = 3.22\,\text{mV} \\ R &= \left\lfloor \frac{1.50 \times 1024}{3.3} \right\rfloor = \lfloor 465.45 \rfloor = 465 \end{aligned}
ExerciseڕاهێنانA TMP36 on the MKR WAN 1310⁦TMP36⁩ لەسەر ⁦MKR WAN 1310⁩

A TMP36 runs from 3.3 V on an MKR WAN 1310 with analogReadResolution(12). analogRead() gives 930. What is the voltage, and the temperature?

⁦TMP36⁩ بە ⁦3.3 V⁩ لەسەر ⁦MKR WAN 1310⁩ کار دەکات بە ⁦analogReadResolution(12). analogRead() 930⁩ دەدات. و پلەی گەرمییەکە چەندن؟

Show answerوەڵامەکە پیشان بدە
V≈930×3.3 V4096=0.749 VT=0.749−0.50.010=24.9 ∘C\begin{aligned} V &\approx 930 \times \frac{3.3\,\text{V}}{4096} = 0.749\,\text{V} \\ T &= \frac{0.749 - 0.5}{0.010} = 24.9\,^\circ\text{C} \end{aligned}

The TMP36 works from 2.7 V, so 3.3 V is fine; keep its output below 3.3 V (it stays under 2.0 V).

⁦TMP36⁩ لە ⁦2.7 V⁩ـەوە کار دەکات، بۆیە ⁦3.3 V⁩ باشە؛ لە خوار ⁦3.3 V⁩ـەوە بهێڵەرەوە (لە خوار ⁦2.0 V⁩ـەوە دەمێنێتەوە).

ExerciseڕاهێنانFix the lineدێڕەکە چاک بکەرەوە

A student moves a sketch from an UNO R3 to an MKR WAN 1310 and adds analogReadResolution(12). The pin is at 2.0 V and reads 2482, but the sketch prints 12.12 V. Which line is wrong, and what should it be?

قوتابییەک لە ⁦UNO R3⁩ـەوە دەگوازێتەوە بۆ ⁦MKR WAN 1310⁩ و ⁦analogReadResolution(12)⁩ زیاد دەکات. پینەکە لەسەر ⁦2.0 V⁩ـە و ⁦2482⁩ دەخوێنێتەوە، بەڵام ⁦12.12 V⁩ چاپ دەکات. کام دێڕ هەڵەیە و دەبێت چی بێت؟

Hintئاماژە

Two numbers change when the board changes.

کاتێک بۆرد دەگۆڕێت دوو ژمارە دەگۆڕێن.

Show answerوەڵامەکە پیشان بدە

The conversion still uses the UNO's numbers: reading * 5.0 / 1024.0 gives 2482 × 5 ÷ 1024 = 12.12 V. Use the MKR's reference and levels: reading * 3.3 / 4096.0, which gives 2.00 V.

گۆڕینەکە هێشتا ژمارەکانی UNO بەکار دەهێنێت: reading * 5.0 / 1024.0 دەدات ⁦2482 × 5 ÷ 1024 = 12.12 V⁩. و ئاستەکانی MKR بەکار بهێنە: reading * 3.3 / 4096.0، کە ⁦2.00 V⁩ دەدات.

ExerciseڕاهێنانWhich reference?کام ڤۆڵتیەی پێوەر؟

A sensor's output stays between 0 and 1.0 V. On an UNO R3, compare analogReference(DEFAULT) with analogReference(INTERNAL): the step, and how many different readings 0–1.0 V can give.

لە نێوان 0 و ⁦1.0 V⁩ـدا دەمێنێتەوە. لەسەر ⁦UNO R3⁩، ⁦analogReference(DEFAULT)⁩ لەگەڵ ⁦analogReference(INTERNAL)⁩ بەراورد بکە: هەنگاو و ئەوەی 0 تا ⁦1.0 V⁩ چەند خوێندنەوەی جیاواز دەدات.

Show answerوەڵامەکە پیشان بدە
  • DEFAULT (5 V): step 4.88 mV; 1.0 V reads 204, so only readings 0–204 (205 values) are used.
  • INTERNAL (1.1 V): step 1.07 mV; 1.0 V reads 930, so readings 0–930 (931 values): more than four times finer.
  • ⁦DEFAULT (5 V)⁩: هەنگاو ⁦4.88 mV⁩؛ ⁦1.0 V⁩ دەبێتە ⁦204⁩، بۆیە تەنیا خوێندنەوەکانی 0 تا ⁦204 (205⁩ نرخ) بەکار دێن.
  • ⁦INTERNAL (1.1 V)⁩: هەنگاو ⁦1.07 mV⁩؛ ⁦1.0 V⁩ دەبێتە ⁦930⁩، بۆیە خوێندنەوەکانی 0 تا ⁦930 (931⁩ نرخ): زیاتر لە چوار جار وردتر.
ExerciseڕاهێنانNoise as a seedژاوەژاو وەک تۆو

Explain to a friend why randomSeed(analogRead(A0)) gives different random numbers after each reset only when A0 is unconnected.

بۆ هاوڕێیەکت ڕوون بکەرەوە بۆچی randomSeed(analogRead(A0)) تەنیا کاتێک ⁦A0⁩ نەبەستراوە دوای هەر سەرلەنوێ دەستپێکردنێک جیاواز دەدات.

Show answerوەڵامەکە پیشان بدە

A floating pin reads noise, so each start gives a different seed and random() starts at a different point. With a sensor connected, the reading is steady, the seed is almost the same every time and the 'random' numbers repeat.

دەخوێنێتەوە، بۆیە هەر دەستپێکردنێک تۆوێکی جیاواز دەدات و ⁦random()⁩ لە خاڵێکی جیاوازەوە دەست پێ دەکات. کاتێک بەسترابێت، خوێندنەوەکە جێگیرە، تۆوەکە هەموو جارێک نزیکەی هەمانە و دووپاتە دەبنەوە.

Summaryپوختە

  • Analog sensors give a continuous voltage and need A/D conversion; digital sensors send bits.
  • The MCU's built-in ADC turns the voltage on an analog pin into a number; on an UNO six pins share one ADC.
  • n bits give 2ⁿ levels, 0 to 2ⁿ − 1: 0–255, 0–1023, 0–4095, 0–16383.
  • One step = Vref ÷ 2ⁿ: 4.88 mV on an UNO R3; reading = ⌊Vin × 2ⁿ ÷ Vref⌋; volts ≈ reading × Vref ÷ 2ⁿ.
  • analogRead() returns a number, about 100 µs per read on ATmega boards; 10 bits by default on every board, more with analogReadResolution() (MKR 12, UNO R4 14).
  • The reference is the ruler: 5 V on the UNO, 3.3 V on MKR boards; analogReference() changes it, carefully with AREF.
  • A floating pin reads noise; averaging several readings calms a jittery sensor.
  • بەردەوام دەدەن و پێویستیان بە گۆڕینی ⁦A/D⁩ هەیە؛ دەنێرن.
  • ADCـی ناوخۆیی MCU دەکاتە ژمارە؛ لەسەر UNO شەش پین یەک ADC بەکار دەهێنن.
  • nn 2n2^n ئاست دەدات، 0 تا 2n−12^n - 1: ⁦0–255⁩، ⁦0–1023⁩، ⁦0–4095⁩، ⁦0–16383⁩.
  • یەک هەنگاو = ⁦Vref ÷ 2ⁿ: 4.88 mV⁩ لەسەر ⁦UNO R3⁩؛ خوێندنەوە = ⌊⁦Vin × 2ⁿ ÷ Vref⁩⌋؛ ≈ خوێندنەوە × ⁦Vref ÷ 2ⁿ⁩.
  • ⁦analogRead()⁩ ژمارەیەک دەگەڕێنێتەوە، نزیکەی ⁦100 µs⁩ بۆ هەر خوێندنەوەیەک لەسەر بۆردەکانی ATmega؛ لەسەر هەموو بۆردێک بە شێوەی بنەڕەت ⁦10⁩ ، زیاتر بە ⁦analogReadResolution() (MKR 12⁩، ⁦UNO R4 14⁩).
  • ڕاستەکەیە: ⁦5 V⁩ لەسەر UNO و ⁦3.3 V⁩ لەسەر بۆردەکانی MKR؛ ⁦analogReference()⁩ دەیگۆڕێت، بە وریاییەوە لەگەڵ AREF.
  • دەخوێنێتەوە؛ تێکڕای چەند خوێندنەوەیەک لەرزۆک ئارام دەکاتەوە.
Review questionsپرسیارەکانی پێداچوونەوە
Why does a 10-bit ADC top out at 1023 and not 1024?بۆچی بەرزترین نرخی ADCـێکی ⁦10⁩ بیتی ⁦1023⁩ـە نەک ⁦1024⁩؟

It has 1024 levels, and counting starts at 0, so the last one is 1023.

⁦1024⁩ ئاستی هەیە و ژماردن لە 0ـەوە دەست پێ دەکات، بۆیە دوایینیان ⁦1023⁩ـە.

What two numbers must you change when a sketch moves from an UNO to an MKR board with 12 bits?کاتێک سکێچێک لە UNOـەوە دەچێت بۆ بۆردێکی MKR بە ⁦12⁩ بیت، دەبێت چ دوو ژمارەیەک بگۆڕیت؟

The reference (5.0 to 3.3) and the levels (1024 to 4096). Also remember analogReadResolution(12).

(⁦5.0⁩ بۆ ⁦3.3⁩) و ئاستەکان (⁦1024⁩ بۆ ⁦4096⁩). هەروەها ⁦analogReadResolution(12)⁩ لەبیر مەکە.

Is a 12-bit reading of a ±2 °C sensor accurate to 0.012 °C?ئایا خوێندنەوەیەکی ⁦12⁩ بیتیی هەستەوەرێکی ⁦±2 °C⁩ تا ⁦0.012 °C⁩ ڕاستە؟

No. 0.012 °C is only the resolution; the sensor's accuracy, ±2 °C, is the real limit.

نەخێر. ⁦0.012 °C⁩ تەنیا ؛ ، ⁦±2 °C⁩، سنوورە ڕاستەقینەکەیە.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Sensing Light with an LDRهەستکردن بە ڕووناکی بە LDR

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.

Appearanceڕووکار

Modeدۆخ
Styleشێواز
Coloursڕەنگەکان

Saved on this device only.تەنیا لەسەر ئەم ئامێرە پاشەکەوت دەکرێت.