Sensors: potentiometer, thermistor and light
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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
اقرأ الدرس خطوة بخطوة
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 -> numberA 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Ω, alwaysKnob 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 1023Any 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 VA 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 coefficientRun 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 sensorFrom 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); }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 voltageLight 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
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A 10 kΩ potentiometer has its ends on 5 V and GND. The wiper is 60 % of the way up from GND. What does analogRead return on the Uno?
The wiper gets 60 % of 5 V, which is 3 V. The ADC gives 3 × 1024 ÷ 5 = 614.4, rounded down to 614: twice the 307 you get at 30 %.
02Why do we put a thermistor in a voltage divider instead of joining it straight to A0?
The ADC only measures a voltage. With a fixed resistor above it, the thermistor's changing resistance changes how the 5 V is shared, so the voltage at A0 follows the temperature.
03The thermistor is at the bottom of the divider, under a fixed 10 kΩ. What happens to the reading as the room warms?
NTC means less resistance at higher temperature. The pin voltage is 5 V × Rs ÷ (10 kΩ + Rs), so a smaller Rs gives a smaller voltage: 783 at 0 °C, 512 at 25 °C, 271 at 50 °C.
04analogRead returns 512. What does line 2, r = R0*raw/(1024-raw), give with R0 = 10000?
10000 × 512 ÷ (1024 − 512) = 10000 Ω. Halfway up the scale means the sensor equals the fixed resistor, which for this thermistor means 25 °C.
05The GL5528 datasheet gives 8 to 20 kΩ at 10 lux. What is the sensible way to use it?
Two cells of the same type can differ by more than two times, so exact lux would need calibrating each one. A threshold that separates dark from bright works on all of them.
المسرد
- ADC (analog-to-digital converter)
- The circuit behind an analog pin that measures a voltage and gives a whole number; on the Uno, 0 to 1023 for 0 to 5 V.
- Potentiometer (pot)
- A resistor track with a sliding contact, the wiper, on its middle pin. Turning the knob changes how the track is split on either side of the wiper.
- Voltage divider
- Two resistors in series across a supply. The point between them sits at a share of the supply set by the two values: V = Vs × Rbottom ÷ (Rtop + Rbottom).
- Thermistor (NTC)
- A resistor made to change with temperature. NTC (negative temperature coefficient) means its resistance falls as it warms.
- B value
- A single number from a thermistor's datasheet that describes how steeply its resistance changes with temperature; used in the B equation 1/T = 1/T0 + ln(R/R0)/B.
- Light cell (LDR)
- A light-dependent resistor, often made of cadmium sulphide (CdS). Its resistance falls as light grows.
- Lux
- The unit of illuminance: how much light falls on a surface. Very dim light is around 10 lux.
الملاحظات والمصادر
- The Uno's ADC gives Vin × 1024 / 5 V, rounded down, so one count is 4.88 mV and 5 V itself would read 1023, the top value. The lesson "Reference voltage, resolution and scaling" explains this in depth.
- The B equation is a simple fit through 25 °C and 85 °C. With the datasheet's real resistances it gives 0.8 °C at 0 °C, under one degree off. Vishay's datasheet gives a longer formula for better accuracy.
- The Uno has no hardware for decimals: float maths is done in software, 32 bits with 6 to 7 digits, and is much slower than whole numbers. For one reading a second that does not matter.
- The ATmega328P's ADC works best when the source has 10 kΩ or less behind it. A divider with a 10 kΩ resistor on top never has more than 10 kΩ, even with a light cell at 1 MΩ.
- The divider always draws a little current: 5 V ÷ (10 kΩ + 10 kΩ) = 250 µA at 25 °C. That also warms the thermistor slightly, which is one reason not to use a much smaller fixed resistor.
- 15 kΩ at 10 lux is an example inside the datasheet's 8 to 20 kΩ. Other light levels use the datasheet's gamma of 0.7: ten times the light gives about 5.0 times less resistance.
- Microchip ATmega328P datasheet (DS40002061B), §23.6.1 Analog Input Circuitry and §23.7 ADC Conversion Result
- Arduino language reference: analogRead() (source)
- Arduino built-in example: AnalogReadSerial (potentiometer, source)
- Arduino language reference: float (source)
- Vishay NTCLE100E3 NTC thermistor datasheet (doc 29049)
- GL5528 CdS photoconductive cell datasheet (SparkFun mirror)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 14: ADC, data acquisition and control (UT Austin)
- Thermistor: the B (β) parameter equation
اختبار يتاح بعد الخطوة الأخيرة.