ENع
→Sensors
الدرس 22 · Analog: ADC, DAC and sensors

Sensors: potentiometer, thermistor and light

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة9 خطوات5 أسئلةIntermediate
عن هذا الدرس

The ADC behind an analog pin only measures voltage, so a sensor's job is to turn the world into a voltage. Many sensors are just resistors whose value follows something physical. On an Arduino Uno R3, read a 10 kΩ potentiometer, then put a 10 kΩ NTC thermistor and a light cell under a fixed 10 kΩ resistor to make a voltage divider, and follow the firmware as it runs the divider backwards to ohms and then to degrees. The Signals lessons on quantization and ADCs explain the converter itself, and “Reference voltage, resolution and scaling” covers the 0 to 1023 scale; here we stay with the sensors.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 22 · تفاعلي
01 / 09

The chip only reads voltage

Pin A0 has an ADC (analog-to-digital converter) behind it: it measures a voltage from 0 to 5 V and gives a whole number from 0 to 1023. It cannot feel heat or light. A sensor's job is to turn the world into that voltage.

board     Arduino Uno R3 (ATmega328P, 5 V)
line 1    int raw = analogRead(A0);
ADC       raw = Vin x 1024 / 5 V, top value 1023
one count 4.88 mV
the job   world -> resistance -> voltage -> number
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. The chip only reads voltage

    Pin A0 has an ADC (analog-to-digital converter) behind it: it measures a voltage from 0 to 5 V and gives a whole number from 0 to 1023. It cannot feel heat or light. A sensor's job is to turn the world into that voltage.

    board     Arduino Uno R3 (ATmega328P, 5 V)
    line 1    int raw = analogRead(A0);
    ADC       raw = Vin x 1024 / 5 V, top value 1023
    one count 4.88 mV
    the job   world -> resistance -> voltage -> number
  2. A potentiometer splits a resistor

    A potentiometer, or pot, is a 10 kΩ resistor track with a sliding contact, the wiper, on its middle pin. Turning the knob moves the wiper, so it splits the track into two parts. Together they always add up to 10 kΩ.

    pot       10 kΩ track, ends on 5 V and GND
    wiper     the middle pin, slides along the track
    upper     wiper to 5 V      lower   wiper to GND
    upper + lower = 10 kΩ, always
  3. Knob position becomes a number

    Turn the wiper 30 % of the way up: the lower part is 3 kΩ, the upper 7 kΩ. The pin gets that share of 5 V: 1.5 V. analogRead returns 307. Twice the turn, twice the number.

    lower     3 kΩ   upper  7 kΩ
    voltage   5 V x 3 / 10 = 1.5 V
    line 1    raw = 1.5 x 1024 / 5 = 307
    all the way down  0 V    reads 0
    all the way up    5 V    reads 1023
  4. Any resistor sensor: a divider

    Most sensors are just one resistor that changes. So pair it with a fixed 10 kΩ resistor: a voltage divider. The pin sits at 5 V × sensor ÷ (10 kΩ + sensor). Change the sensor and the voltage follows.

    top       fixed 10 kΩ, 5 V to A0
    bottom    the sensor, A0 to GND
    V(A0)     5 x Rs / (10k + Rs)
    Rs = 10 kΩ   ->  2.50 V
    Rs = 0       ->  0 V      Rs huge  ->  near 5 V
  5. A thermistor: warmer, lower

    A thermistor is a resistor made to change with heat. Ours is an NTC: warmer means less resistance. The datasheet gives 32.6 kΩ at 0 °C, 10 kΩ at 25 °C and 3.6 kΩ at 50 °C. So the pin voltage falls as it warms.

    part      Vishay NTCLE100E3103, 10 kΩ at 25 C
     0 C      32554 Ω   ->  3.83 V   raw 783
    15 C      15698 Ω   ->  3.05 V   raw 625
    25 C      10000 Ω   ->  2.50 V   raw 512
    35 C       6535 Ω   ->  1.98 V   raw 404
    50 C       3605 Ω   ->  1.32 V   raw 271
    NTC: negative temperature coefficient
  6. Run the divider backwards

    At 25 °C the thermistor is 10 kΩ, equal to the fixed one, so the pin sits at 2.50 V and reads 512. Line 2 turns the reading back into ohms: 10000 × 512 ÷ (1024 − 512) = 10000 Ω.

    25 C      Rs = 10000 Ω  ->  2.50 V
    line 1    raw = 512
    line 2    r = R0 * raw / (1024 - raw)
                = 10000 * 512 / 512 = 10000 Ω
    the divider formula, solved for the sensor
  7. From ohms to degrees

    Lines 3 and 4 use the thermistor's B value, 3977 from the datasheet, to turn ohms into kelvin (0 °C = 273.15 K), then Celsius: 25.0 °C. Warm it to 35 °C: it reads 404, which gives 34.9 °C. Close, not perfect.

    B         3977 K (B25/85 of the NTCLE100E3103)
    line 3    1/T = 1/T0 + ln(r/R0) / B     (kelvin)
    line 4    c = k - 273.15
    25 C      raw 512  r 10000 Ω  ->  25.0 C
    35 C      raw 404  r 6516 Ω   ->  34.9 C
    0 C       raw 783  r 32490 Ω  ->  0.8 C
    
    // Arduino Uno R3: 10 kΩ from 5 V to A0, sensor from A0 to GND
    const float R0 = 10000.0;  // fixed resistor = NTC at 25 C
    const float T0 = 298.15;   // 25 C in kelvin
    const float B  = 3977.0;   // NTCLE100E3103, B25/85
    
    void setup() { Serial.begin(9600); }
    
    void loop() {
      int raw = analogRead(A0);
      float r = R0*raw/(1024-raw);
      float k = 1/(1/T0+log(r/R0)/B);
      float c = k - 273.15;
      Serial.println(c, 1);     // one decimal place
      delay(1000);
    }
  8. A light cell in the slot

    Swap in a light cell (LDR, light-dependent resistor). More light means less resistance: at least 1 MΩ in the dark, and 8 to 20 kΩ at 10 lux, very dim light. Same divider, same line 1.

    part      GL5528 CdS cell
    dark      at least 1 MΩ  ->  4.95 V   raw 1013
    10 lux    8-20 kΩ, say 15 kΩ  ->  3.00 V   raw 614
    100 lux   about 3 kΩ  ->  1.15 V   raw 235
    brighter: lower resistance, lower voltage
  9. Light cells are rough

    One GL5528 can be 8 kΩ at 10 lux and the next 20 kΩ, so nobody turns it into exact lux. Use it for dark or bright: a night light switches on when raw is above 800. Dark reads about 1013.

    spread    8 to 20 kΩ at 10 lux, part to part
    rule      raw > 800  ->  dark, light on
    dark      raw 1013     10 lux  raw 614     100 lux  raw 235
    thermistor: precise    light cell: dark or bright

اختبار يتاح بعد الخطوة الأخيرة.