Chapterبەش 5 · Lectureوانە 5.4
Sensing Light with an LDRهەستکردن بە ڕووناکی بە LDR
The LDR (photoresistor): a passive part whose resistance falls as light rises, from about 1 MΩ in the dark to a few hundred ohms in sunlight. Turn it into a voltage with a voltage divider, read it with analogRead() and build the night-light lab on the MKR WAN 1310 and the UNO, with its two compile errors fixed and a version that does not flicker.
: کە بە زیادبوونی ڕووناکی کەم دەبێتەوە، لە نزیکەی 1 MΩ لە تاریکیدا تا چەند سەد لە تیشکی خۆردا. بە دەیکەیت بە ، بە analogRead() دەیخوێنیتەوە و تاقیگەی گڵۆپی شەو لەسەر MKR WAN 1310 و UNO دروست دەکەیت، دوای چاککردنی دوو و وەشانێک کە نالەرزێت.
What you will learnچی فێر دەبیت
- Explain what an LDR is, why it is a passive component and how its resistance changes with light, with real data-sheet numbers.
- Work out the voltage and the analogRead() number of an LDR voltage divider.
- Wire and program the night light on the MKR WAN 1310 (3.3 V) and on the UNO (5 V).
- Find and fix two compile errors: a curly quote and a brace hidden in a comment.
- Stop a night light from flickering with two thresholds (hysteresis).
- ڕوونی بکەیتەوە چییە، بۆچی و چۆن لەگەڵ ڕووناکیدا دەگۆڕێت، بە ژمارەی ڕاستەقینەی .
- و ژمارەی analogRead()ـی هەژمار بکەیت.
- گڵۆپی شەو لەسەر MKR WAN 1310 (3.3 V) و UNO (5 V) ببەستیت و پڕۆگرامی بکەیت.
- دوو بدۆزیتەوە و چاکیان بکەیت: چەماوە و کەوانەیەکی شاراوە لە .
- بە دوو () ڕێگە لە لەرزینی گڵۆپی شەو بگریت.
A resistor that can see lightبەرگرێک کە ڕووناکی دەبینێت
At dusk the street lights of a city switch themselves on, and nobody presses a button. On top of many lamps sits a small light sensor. In this lesson you meet the simplest one, the LDR: you see how its resistance follows the light, turn that resistance into a voltage, read it on a board and build a night light.
لە ئێوارەدا گڵۆپەکانی شەقامی شارێک خۆیان دادەگیرسێن و کەس دوگمەیەک داناگرێت. لەسەر زۆربەیان بچووک دانراوە. لەم وانەیەدا سادەترینیان دەناسیت، : دەبینیت چۆن بەدوای ڕووناکیدا دەڕوات، ئەو دەکەیت بە ، لەسەر بۆردێک دەیخوێنیتەوە و گڵۆپێکی شەو دروست دەکەیت.
You read analog voltages in Reading Analog Signals: the ADC and analogRead() and lit an LED from a pin in Lab: Blink an LED and Build a Traffic Light. This lab uses both.
لە خوێندنەوەی سیگناڵەکانی ئەنالۆگ: گۆڕەری ئەنالۆگ بۆ دیجیتاڵ و analogRead() خوێندەوە و لە تاقیگە: LEDـێک بترووسکێنە و گڵۆپی هاتوچۆ دروست بکە لە پینێکەوە داگیرساند. ئەم تاقیگەیە هەردووکیان بەکار دەهێنێت.
The LDR: a resistance that follows the lightLDR: بەرگرییەک کە بەدوای ڕووناکیدا دەڕوات
An LDR (light-dependent resistor), also called a photoresistor, a photocell or a photo-conductive cell, is a resistor whose value is set by light. The more light falls on its face, the lower its resistance; the less light, the higher it climbs. That is why it can sense light. Resistance is measured in ohms (Ω) (Resistance and resistors).
( هەستیار بە ڕووناکی)، کە پێی دەوترێت photoresistor، photocell یان photo-conductive cell، کە ڕووناکی نرخەکەی دیاری دەکات. تا ڕووناکیی زیاتر بکەوێتە سەر ڕووەکەی، نزمتر دەبێت؛ تا ڕووناکی کەمتر بێت، بەرزتر دەبێتەوە. بۆیە دەتوانێت هەست بە ڕووناکی بکات. بە (Ω) دەپێورێت (بەرگری و بەرگرەکان).
Why does light matter? The LDR's track is made of a semiconductor, a material that carries almost no current until something gives its electrons energy. Here the energy comes from light: light that is absorbed frees charges that can carry a current. More light, more free charges, less resistance; when the light goes, they disappear again. In short, it lets electricity pass more or less depending on the light.
بۆچی ڕووناکی گرنگە؟ ڕێڕەوەکەی لە دروست کراوە، ماددەیەک کە نزیکەی هیچ ناگەیەنێت تا شتێک وزە نەداتە ئەلیکترۆنەکانی. لێرەدا وزەکە لە ڕووناکییەوە دێت: ئەو ڕووناکییەی هەڵدەمژرێت هەندێک ئازاد دەکات کە دەتوانێت هەڵبگرێت. ڕووناکیی زیاتر، ئازادی زیاتر، کەمتر؛ کە ڕووناکی نەما، دیسان نامێنن. بە کورتی، بەپێی ڕووناکی کارەبا کەمتر یان زیاتر تێدەپەڕێنێت.


The LDR: a resistance that follows the lightLDR: بەرگرییەک کە بەدوای ڕووناکیدا دەڕوات
What an LDR looks like insideناوەوەی LDR چۆنە
Look closely at an LDR and you see a zigzag line between two metal sides. The zigzag is the light-sensitive track, in cheap LDRs cadmium sulphide (CdS); the metal electrodes on each side lead to the two legs. A long, narrow zigzag fits a lot of track between the electrodes in a small disc, so the change with light is large. A clear coating protects it. An LDR has no + or − side: like every resistor, it works either way round.
ئەگەر لە نزیکەوە سەیری بکەیت، هێڵێکی زیگزاگ لە نێوان دوو لای کانزاییدا دەبینیت. زیگزاگەکە ڕێڕەوە هەستیارەکەیە بە ڕووناکی، لە هەرزاندا کادمیۆم سەلفاید (CdS)؛ ئەلیکترۆدە کانزاییەکانی هەردوو لا دەچن بۆ دوو قاچەکە. زیگزاگێکی درێژ و باریک ڕێڕەوێکی زۆر لە قرسێکی بچووکدا لە نێوان ئەلیکترۆدەکاندا جێ دەکاتەوە، بۆیە گۆڕانەکە لەگەڵ ڕووناکیدا گەورەیە. چینێکی ڕوون دەیپارێزێت. لای + و −ی نییە: وەک هەموو ، بە هەردوو ئاراستەدا کار دەکات.
A passive componentپارچەی ناچالاک
An LDR is a passive component. The word comes from the Latin passivus, "receiving, being acted on": such a part does not act, it is acted on. A passive component:
. وشەکە لە لاتینیی passivusـەوە هاتووە، بە واتای «وەرگرتن، کاری لەسەر کراو»: پارچەیەکی وا کار ناکات، بەڵکو کاری لەسەر دەکرێت. :
- does not produce energy;
- only receives, stores or slows down energy — it uses it, stores it or resists it;
- cannot control a current or a voltage by itself, as an active component such as a transistor or a chip can.
- وزە بەرهەم ناهێنێت؛
- تەنیا وزە وەردەگرێت، هەڵیدەگرێت یان هێواشی دەکاتەوە — بەکاری دەبات، هەڵیدەگرێت یان بەرگریی لێ دەکات؛
- ناتوانێت بە خۆی یان کۆنترۆڵ بکات، وەک ئەوەی وەک یان دەتوانێت.
A sponge can soak up water (store it) or slow its flow (resist it), but it can never make water. A resistor, a capacitor and an LDR are sponges for electricity. A transistor is more like a tap: a small hand on the handle controls a big flow.
ئیسفەنج دەتوانێت ئاو هەڵبمژێت (هەڵیبگرێت) یان ڕۆیشتنی هێواش بکاتەوە (بەرگریی لێ بکات)، بەڵام هەرگیز ناتوانێت ئاو دروست بکات. ، و ئیسفەنجن بۆ کارەبا. زیاتر وەک بۆری ئاوە: دەستێکی بچووک لەسەر دەسکەکە ڕۆیشتنێکی گەورە کۆنترۆڵ دەکات.
Where the analogy stops workingسنووری ئەم نموونەیە
Only a picture: a capacitor stores charge, and a resistor turns electrical energy into heat.
تەنیا وێنەیەکە: هەڵدەگرێت و دەکات بە گەرمی.
How big is the change? A real data sheetگۆڕانەکە چەند گەورەیە؟ پەڕەیەکی تایبەتمەندییەکان ڕاستەقینە
In the dark an LDR can reach several megohms (1 MΩ = 1,000,000 Ω); in bright light it drops to a few hundred ohms. The GL5528, the LDR in many Arduino kits, shows the real numbers in its maker's data sheet (Senba, GL55 series):
لە تاریکیدا دەتوانێت بگاتە چەند مێگائۆمێک (1 MΩ = 1,000,000 Ω)؛ لە ڕووناکیی بەهێزدا دادەبەزێت بۆ چەند سەد . GL5528، کە زۆربەی کیتەکانی Arduinoـیە، ژمارە ڕاستەقینەکان لە دروستکەرەکەیدا پیشان دەدات (Senba، زنجیرەی GL55):
| Property (GL5528)تایبەتمەندی (GL5528) | Valueنرخ |
|---|---|
| resistance at 10 lux لە 10 | 10–20 kΩ |
| dark resistance (10 s after the light is switched off) تاریکی (10 چرکە دوای کوژاندنەوەی ڕووناکی) | 1 MΩ or more1 MΩ یان زیاتر |
| γ: how fast it falls from 10 to 100 luxγ: لە 10 بۆ 100 چەند خێرا دادەبەزێت | 0.6: about 4 times smaller at 100 lux0.6: لە 100 نزیکەی 4 جار بچووکتر |
| response time: more light / less light: ڕووناکیی زیاتر / کەمتر | 20 ms / 30 ms |
| highest voltage, highest powerزۆرترین ، زۆرترین وزە | 150 V DC، 100 mW |
| most sensitive toزۆرترین بۆ | green light, 540 nmڕووناکیی سەوز، 540 nm |
Lux (lx) measures how much light falls on a surface: a full moon gives about 0.05–0.3 lx, a living room about 50 lx, an office 320–500 lx, a cloudy day about 1,000 lx and full daylight 10,000–25,000 lx. The graph puts the data sheet on these scales:
(lx) دەپێوێت چەند ڕووناکی دەکەوێتە سەر ڕووبەرێک: مانگی پڕ نزیکەی 0.05–0.3 lx دەدات، ژووری دانیشتن نزیکەی 50 lx، ئۆفیس 320–500 lx، ڕۆژی هەوری نزیکەی 1,000 lx و ڕووناکیی تەواوی ڕۆژ 10,000–25,000 lx. هێڵکارییەکە لەسەر ئەم پێوەرانە دادەنێت:
How big is the change? A real data sheetگۆڕانەکە چەند گەورەیە؟ پەڕەیەکی تایبەتمەندییەکان ڕاستەقینە
Slow, and made with cadmiumهێواش و لە کادمیۆم دروستکراو
LDRs are slow. The GL5528 takes about 20 ms to follow more light and 30 ms to follow less, and its full dark value is measured only 10 seconds after the light goes off; from bright to dark an LDR can lag by a second or more. For day and night that does not matter, but an LDR cannot see a fast flicker, such as light that carries data.
هێواشە. GL5528 نزیکەی 20 ms پێویستە تا بەدوای ڕووناکیی زیاتردا بڕوات و 30 ms بۆ ڕووناکیی کەمتر. نرخی تەواوی تاریکییەکەی تەنیا 10 چرکە دوای کوژانەوەی ڕووناکی دەپێورێت؛ لە ڕووناکەوە بۆ تاریک دەتوانێت چرکەیەک یان زیاتر دوا بکەوێت. بۆ ڕۆژ و شەو ئەمە گرنگ نییە، بەڵام ناتوانێت ترووسکەی خێرا ببینێت، وەک ئەو ڕووناکییەی هەڵدەگرێت.
Where LDRs sense lightLDR لە کوێ هەست بە ڕووناکی دەکات
LDRs are used wherever a device must know whether there is light, or how much:
لە هەر شوێنێکدا بەکار دێت کە ئامێرێک پێویستی بە زانینی هەبوونی ڕووناکی یان بڕەکەی بێت:
- Alarm clocks: check the room's light, dim the screen at night and wake you gently at sunrise.
- Street lights: on when it is dark, off when it is light.
- Light-intensity meters: measure how bright a place is; photographers use them to set the camera.
- Burglar alarms: sound when someone blocks a beam of light, like laser alarm systems.
- کاتژمێری زەنگدار: ڕووناکیی ژوورەکە دەپشکنێت، شەو شاشەکە کز دەکات و لە خۆرهەڵاتندا بە نەرمی ئاگات دەکاتەوە.
- گڵۆپی شەقام: لە تاریکیدا دادەگیرسێت، لە ڕووناکیدا دەکوژێتەوە.
- پێوەری ڕووناکی: دەپێوێت شوێنێک چەند ڕووناکە؛ وێنەگرەکان بۆ ڕێکخستنی کامێرا بەکاری دەهێنن.
- ئاگادارکەرەوەی دز: کاتێک کەسێک تیشکێکی ڕووناکی دەبڕێت زەنگ لێ دەدات، وەک سیستەمی ئاگادارکەرەوەی .
Connected street lights go further: they dim, brighten and report faults (Smart lighting). The project at the end of this course uses an LDR to notice sunrise and sunset (Project: a Smart Bird-Cage Cover).
گڵۆپی شەقامی بەستراو زیاتر دەکات: کز و گەش دەبێت و تێکچوون ڕاپۆرت دەکات (ڕووناکیی هۆشمەند). پڕۆژەکەی کۆتایی ئەم کۆرسە بەکار دەهێنێت بۆ هەستکردن بە خۆرهەڵاتن و خۆرئاوابوون (پڕۆژە: بەرگی هۆشمەندی قەفەزی باڵندە).
The voltage divider: from resistance to voltageدابەشکەری ڤۆڵتیە: لە بەرگرییەوە بۆ ڤۆڵتیە
A board cannot measure resistance directly: analogRead() measures a voltage (analogRead() on an UNO). The trick is a voltage divider: the LDR and a fixed resistor in series between the supply and GND. The same current flows through both, so they share the supply voltage in proportion to their resistances (Series and parallel circuits). The point where they meet goes to the analog pin.
بۆرد ناتوانێت ڕاستەوخۆ بپێوێت: analogRead() دەپێوێت (analogRead() لەسەر UNO). فێڵەکە : و جێگیر بە لە نێوان سەرچاوەکە و GNDـدا. هەمان بە هەردووکیاندا تێدەپەڕێت، بۆیە سەرچاوەکە بەپێی بەش دەکەن (بەستنی زنجیرەیی و هاوتەریب). ئەو خاڵەی یەکتر دەگرن دەچێت بۆ .
R2 is the fixed resistor to GND: 10 kΩ in the lab.
R2 ئەو جێگیرەیە کە دەچێت بۆ GND: لە تاقیگەکەدا 10 kΩ.
More light → a smaller R(LDR) → R2 gets a bigger share → a higher Vout. In the lab the 10 kΩ to GND is called a pull-down resistor: in the dark, when the LDR is almost an open switch, it pulls the pin towards 0 V instead of leaving it floating (Pull-up and pull-down resistors).
ڕووناکیی زیاتر ← R(LDR)ـی بچووکتر ← R2 بەشێکی گەورەتر وەردەگرێت ← Voutـی بەرزتر. لە تاقیگەکەدا ئەو 10 kΩـەی دەچێت بۆ GND پێی دەوترێت : لە تاریکیدا، کاتێک نزیکەی کراوەیە، پینەکە بەرەو 0 V دەکێشێت لە جیاتی ئەوەی بە هەڵواسراوی بیهێڵێتەوە (بەرگری بەرزکەرەوە و بەرگری نزمکەرەوە).
The voltage divider: from resistance to voltageدابەشکەری ڤۆڵتیە: لە بەرگرییەوە بۆ ڤۆڵتیە
Worked example: four light levelsنموونەی چارەسەرکراو: چوار ئاستی ڕووناکی
Put numbers in: a 5 V supply, R2 = 10 kΩ and the GL5528 at 10 lux, about 15 kΩ. The second line turns the voltage into the number analogRead() gives on a 10-bit ADC:
ژمارە دابنێ: سەرچاوەیەکی 5 V، R2 = 10 kΩ و GL5528 لە 10 ، نزیکەی 15 kΩ. دێڕی دووەم دەکات بەو ژمارەیەی analogRead() لەسەر 10 دەیدات:
| Lightڕووناکی | R(LDR) | Vout | analogRead() |
|---|---|---|---|
| darkتاریکی | 1 MΩ | 0.05 V | 10 |
| 1 lx | 60 kΩ | 0.71 V | 146 |
| 10 lx | 15 kΩ | 2.00 V | 409 |
| 100 lx | 3.8 kΩ | 3.62 V | 742 |
| 1,000 lx | 950 Ω | 4.57 V | 935 |


The rule value = ⌊Vout × 1024 ÷ Vref⌋ (⌊ ⌋: round down to a whole number) comes from From reading to volts, and back; a real board may differ by a step or two.
یاسای value = ⌊Vout × 1024 ÷ Vref⌋ (⌊ ⌋: بۆ خوارەوە دەکرێتە ) لە لە خوێندنەوەوە بۆ ڤۆڵت و بە پێچەوانەوە هاتووە؛ بۆردێکی ڕاستەقینە لەوانەیە هەنگاوێک یان دووان جیاواز بێت.
The voltage divider: from resistance to voltageدابەشکەری ڤۆڵتیە: لە بەرگرییەوە بۆ ڤۆڵتیە
5 V or 3.3 V: the same number5 V یان 3.3 V: هەمان ژمارە
On the MKR WAN 1310 the divider sits on 3.3 V, and its ADC also measures against 3.3 V. At 10 lux, Vout = 3.3 × 10 ÷ 25 = 1.32 V, and ⌊1.32 × 1024 ÷ 3.3⌋ = 409: the same number as on the UNO. Put the two formulas together and the supply cancels out:
لەسەر MKR WAN 1310، لەسەر 3.3 V دادەنرێت و بەرامبەر 3.3 V دەپێوێت. لە 10 Vout = 3.3 × 10 ÷ 25 = 1.32 V و ⌊1.32 × 1024 ÷ 3.3⌋ = 409: هەمان ژمارەی سەر UNO. دوو هاوکێشەکە پێکەوە دابنێ و سەرچاوەکە لادەچێت:
So one sketch with one threshold works on both boards, as long as the divider is fed from the board's own reference: 5 V on the UNO, 3.3 V on the MKR. Engineers call such a measurement ratiometric: only the ratio of the resistances counts.
کەواتە یەک بە یەک لەسەر هەردوو بۆرد کار دەکات، بە مەرجێک لە خودی بۆردەکەوە وەربگرێت: 5 V لەسەر UNO، 3.3 V لەسەر MKR. ئەندازیاران بە پێوانێکی وا دەڵێن ratiometric: تەنیا ڕێژەی دەژمێردرێت.
The voltage divider: from resistance to voltageدابەشکەری ڤۆڵتیە: لە بەرگرییەوە بۆ ڤۆڵتیە
Which way round? And why 10 kΩ?بە کام ئاراستەدا؟ و بۆچی 10 kΩ؟
Swap the two parts — 10 kΩ to the supply, the LDR to GND — and the numbers turn over: more light now gives a smaller number (100 lx: 281, 1 lx: 877). Both circuits are correct; the sketch must match. The lab puts the LDR on top, so a bigger number means more light.
دوو پارچەکە ئاڵوگۆڕ بکە — 10 kΩ بۆ سەرچاوەکە، بۆ GND — و ژمارەکان پێچەوانە دەبنەوە: ئێستا ڕووناکیی زیاتر ژمارەیەکی بچووکتر دەدات (100 lx: 281، 1 lx: 877). هەردوو دروستن؛ دەبێت لەگەڵیاندا بگونجێت. تاقیگەکە لە سەرەوە دادەنێت، بۆیە ژمارەی گەورەتر واتە ڕووناکیی زیاتر.
Why 10 kΩ? A divider changes most when R2 is close to the LDR's resistance in the light you care about. 10 kΩ is close to the GL5528 at dusk (10–20 kΩ at 10 lux), so the reading moves a lot exactly where a night light must decide.
بۆچی 10 kΩ؟ زۆرترین گۆڕانی دەبێت کاتێک R2 نزیکی بێت لەو ڕووناکییەدا کە گرنگە بۆت. 10 kΩ نزیکی GL5528ـە لە ئێوارەدا (10–20 kΩ لە 10 lx)، بۆیە خوێندنەوەکە ڕێک لەو شوێنەدا زۆر دەجووڵێت کە گڵۆپی شەو دەبێت بڕیار بدات.
Testing an LDR with metersتاقیکردنەوەی LDR بە پێوەر
Even without a board you can watch an LDR work: a battery drives a current through it, an ammeter in series shows the current and a voltmeter across it shows its voltage. Turn the lamp up with its dimmer and the current rises, because the resistance R = V ÷ I falls (Ohm's law, The multimeter).
بەبێ بۆردیش دەتوانیت سەیری کارکردنی بکەیت: بەناویدا دەنێرێت، بە پیشان دەدات و بە دوو سەرییەوە پیشان دەدات. گڵۆپەکە بە کزکەرەوەکەی گەش بکە و زیاد دەبێت، چونکە R = V ÷ I کەم دەبێتەوە (یاسای ئۆم، مەڵتیمیتەر).
A thermistor is tested the same way, with heat instead of light. In the common NTC kind, the resistance falls as it gets warmer, so the current rises when the heater is on. Both are resistive sensors, and both can sit in a voltage divider; the DHT11 in the next lesson has an NTC thermistor inside.
بە هەمان شێوە تاقی دەکرێتەوە، بە گەرمی لە جیاتی ڕووناکی. لە جۆری باوی NTCـدا کەم دەبێتەوە کاتێک گەرمتر دەبێت، بۆیە کە گەرمکەرەوەکە هەڵکرا زیاد دەبێت. هەردووکیان بەرگرییین و هەردووکیان دەتوانن لە دابنرێن؛ وانەی داهاتوو NTCـی لە ناودایە.
The lab: an LDR and an LED on the MKR WAN 1310تاقیگە: LDR و LED لەسەر MKR WAN 1310
The lab turns the LDR into a night light on the Arduino MKR WAN 1310, a 3.3 V board (5 V boards and 3.3 V boards). You need an LDR, a 10 kΩ resistor (R1), an LED and a 220 Ω resistor (R2).
تاقیگەکە دەکات بە گڵۆپێکی شەو لەسەر Arduino MKR WAN 1310، بۆردێکی 3.3 V (بۆردەکانی 5 V و بۆردەکانی 3.3 V). پێویستت بە ، 10 kΩ (R1)، و 220 Ω (R2) هەیە.
Connect the LDR from the VCC pin (3.3 V) to pin A1, and R1 = 10 kΩ from A1 to GND: the divider.
لە پینی VCC (3.3 V)ـەوە ببەستە بە پینی A1ـەوە و R1 = 10 kΩ لە A1ـەوە بۆ GND: .
Connect the LED with R2 = 220 Ω from pin 5 to GND, long leg towards pin 5. (3.3 V − 2 V) ÷ 220 Ω ≈ 5.9 mA, inside the MKR's 7 mA per pin (Choosing the LED's resistor).
بە R2 = 220 Ω لە پینی 5ـەوە بۆ GND ببەستە، قاچە درێژەکە بەرەو پینی 5. (3.3 V − 2 V) ÷ 220 Ω ≈ 5.9 mA، لە ناو 7 mAـی هەر پینێکی MKRـدا (هەڵبژاردنی بەرگری LED).
Read pin A1 with
analogRead()to measure the voltage.پینی A1 بە
analogRead()بخوێنەوە بۆ پێوانی .Copy the sketch, upload it and open the Serial Monitor.
کۆپی بکە، هەڵبار بکە و بکەرەوە.
The night-light sketchسکێچی گڵۆپی شەو
The sketch reads the light once a second, prints the number and sets the LED: on below 300, off at 300 or more. This is the corrected sketch; the next screen shows the two typing mistakes that stopped the first version from compiling.
چرکەی جارێک ڕووناکییەکە دەخوێنێتەوە، ژمارەکە چاپ دەکات و ڕێک دەخات: لە خوار 300ـەوە داگیرساو، لە 300 یان زیاتر کوژاوە. ئەمە چاککراوەکەیە؛ شاشەی داهاتوو ئەو دوو هەڵەی نووسینە پیشان دەدات کە نەیانهێشت یەکەم وەشان کۆمپایل بێت.
/* Interfacing an LDR and an LED with the Arduino MKR WAN 1310 */
int ldrPin = A1; // the analog pin A1, connected to the LDR divider
int ledPin = 5; // the digital pin 5, connected to the LED
int value = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
value = analogRead(ldrPin); // read the light level: 0 to 1023
Serial.print("Light Intensity: "); // print it to the Serial Monitor
Serial.println(value);
// Control the LED based on the light intensity
if (value < 300) {
digitalWrite(ledPin, HIGH); // dark: turn the LED on
} else {
digitalWrite(ledPin, LOW); // light: turn the LED off
}
delay(1000); // one reading a second is enough
}Light Intensity: 742 Light Intensity: 742 Light Intensity: 409 Light Intensity: 409 Light Intensity: 146 Light Intensity: 146 Light Intensity: 299 Light Intensity: 301 Light Intensity: 742
CêIT Arduino checker. A1 was fed the worked example's numbers: 742 (a lit room), 409 (dusk), 146 (night), then 299, 301 and 742 (morning), one value a second. The LED on pin 5 went on at 4 s (146) and off at 7 s (301).
پشکنەری Arduinoـی CêIT. ژمارەکانی نموونە چارەسەرکراوەکە درانە A1: 742 (ژوورێکی ڕووناک)، 409 (ئێوارە)، 146 (شەو)، پاشان 299، 301 و 742 (بەیانی)، چرکەی نرخێک. پینی 5 لە چرکەی 4 (146) داگیرسا و لە چرکەی 7 (301) کوژایەوە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 12
analogRead(A1): the divider's voltage as a number from 0 to 1023.
analogRead(A1): وەک ژمارەیەک لە 0 تا 1023.
- Lineدێڕی 16-20
Below 300 (dark): LED on; otherwise off. A level test: it is checked again every second.
خوار 300 (تاریک): داگیرساو؛ ئەگەرنا کوژاو. پشکنینی ئاست: هەموو چرکەیەک دیسان دەپشکنرێت.
- Lineدێڕی 21
One reading a second is plenty for daylight, and keeps the Serial Monitor readable.
چرکەی جارێک خوێندنەوە بۆ ڕووناکیی ڕۆژ بەسە و خوێندراوە دەهێڵێتەوە.
What is 300? On a 5 V board it is about 1.47 V on A1, on the MKR about 0.97 V, and on both an LDR of about 24 kΩ: for a GL5528, deep dusk of about 4–5 lux.
300 چییە؟ لەسەر بۆردێکی 5 V واتە نزیکەی 1.47 V لەسەر A1 و لەسەر MKR نزیکەی 0.97 V. لەسەر هەردووکیان واتە نزیکەی 24 kΩ: بۆ GL5528، ئێوارەیەکی تاریک بە نزیکەی 4–5 .
The night-light sketchسکێچی گڵۆپی شەو
Common mistake: two errors that stop the compilerهەڵەی باو: دوو هەڵە کە کۆمپایلەر ڕادەگرن
ldr_led.ino:13:14: warning: missing terminating '"' character
13 | Serial.print("Light Intensity: “); // Print the analog value to the Serial Monitor
| ^
ldr_led.ino:13:14: error: expected expression
ldr_led.ino:17:1: error: expected expression
17 | else { digitalWrite(ledPin, LOW); // Turn off the LED. }
| ^
ldr_led.ino:21:1: error: expected '}'
1 warning and 4 errors generated.The night-light sketchسکێچی گڵۆپی شەو
A night light, not a toggleگڵۆپی شەو، نەک گۆڕین
It is easy to read this lab as a toggle — cover the LDR, the LED switches; cover it again, it switches back. The code does something else. if (value < 300) is a level test: true while the reading is low. So the LED stays on as long as the LDR is covered and goes off when it is uncovered: a night light. A toggle must react to the moment the reading falls below 300 — an edge, as with buttons in Level or edge? React to the change.
ئاسانە ئەم تاقیگەیە وەک گۆڕین تێبگەیت — دابپۆشە، دەگۆڕێت؛ دیسان دایبپۆشە، دەگەڕێتەوە. کۆدەکە شتێکی تر دەکات. if (value < 300) پشکنینی ئاستـە: ڕاستە تا خوێندنەوەکە نزمە. بۆیە تا داپۆشراوە داگیرساو دەمێنێتەوە و کە لابرا دەکوژێتەوە: گڵۆپی شەو. گۆڕین دەبێت کاردانەوەی هەبێت بۆ ساتی دابەزینی خوێندنەوەکە بۆ خوار 300 — لێوارێک، وەک دوگمەکان لە ئاست یان لێوار؟ کاردانەوە بۆ گۆڕان.
The same night light on an UNOهەمان گڵۆپی شەو لەسەر UNO
On an UNO the circuit is the same with 5 V in place of 3.3 V: the LDR from 5V to A1, 10 kΩ from A1 to GND, and the LED with its resistor on pin 5. Pins A1 and 5 exist on both boards, so the sketch runs unchanged, and because the reading is ratiometric the threshold 300 means the same light. On an UNO R3, 220 Ω gives about 13.6 mA; on an UNO R4, whose pins allow 8 mA, use 470 Ω.
لەسەر UNO هەمان شتە بە 5 V لە جیاتی 3.3 V: لە 5Vـەوە بۆ A1، 10 kΩ لە A1ـەوە بۆ GND و بە لەسەر پینی 5. پینی A1 و 5 لەسەر هەردوو بۆرد هەن، بۆیە بێ گۆڕین کار دەکات. چونکە خوێندنەوەکە ratiometricـە، 300 هەمان ڕووناکی دەگەیەنێت. لەسەر UNO R3، 220 Ω نزیکەی 13.6 mA دەدات؛ لەسەر UNO R4، کە پینەکانی 8 mA ڕێگە دەدەن، 470 Ω بەکار بهێنە.
No flicker at dusk: two thresholdsبێ لەرزین لە ئێوارەدا: دوو ئاستانە
At dusk the light changes slowly and the reading wobbles by a few steps: a passing cloud, a car's lights, a little electrical noise. With one threshold the LED flickers every time the number crosses 300. The cure is hysteresis: switch on below 280, but off only above 320. Between the two nothing changes — the same idea as a thermostat's dead band (Closed-loop control).
لە ئێوارەدا ڕووناکی بە هێواشی دەگۆڕێت و خوێندنەوەکە چەند هەنگاوێک سەر و خوار دەکات: هەورێکی تێپەڕ، گڵۆپی ئۆتۆمبێلێک، کەمێک . بە یەک ، هەر جارێک ژمارەکە بە 300ـدا تێدەپەڕێت دادەگیرسێت یان دەکوژێتەوە: دەلەرزێت. چارەسەرەکە ـە: لە خوار 280ـەوە دایبگیرسێنە، بەڵام تەنیا لە سەرووی 320ـەوە بیکوژێنەوە. لە نێوان ئەو دووانەدا هیچ ناگۆڕێت — هەمان بیرۆکەی (کۆنترۆڵی خولی داخراو).
// A night light with two thresholds: no flicker at dusk
const int ldrPin = A1;
const int ledPin = 5;
const int onBelow = 280; // switch on only when clearly dark
const int offAbove = 320; // switch off only when clearly light
bool ledOn = false;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int value = analogRead(ldrPin);
if (!ledOn && value < onBelow) {
ledOn = true;
} else if (ledOn && value > offAbove) {
ledOn = false;
}
if (ledOn) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
Serial.print("Light: ");
Serial.print(value);
if (ledOn) {
Serial.println(" LED on");
} else {
Serial.println(" LED off");
}
delay(1000);
}Light: 330 LED off Light: 305 LED off Light: 296 LED off Light: 303 LED off Light: 289 LED off Light: 302 LED off Light: 270 LED on Light: 290 LED on Light: 310 LED on Light: 335 LED off
CêIT Arduino checker: the same noisy dusk readings were fed to both sketches. The one-threshold night light switched pin 5 six times; this one switched it twice, on at 6 s (270) and off at 9 s (335).
پشکنەری Arduinoـی CêIT: هەمان خوێندنەوە ژاوەژاوییەکانی ئێوارە درانە هەردوو . گڵۆپی شەوی یەک پینی 5ـی شەش جار گۆڕی؛ ئەمە دوو جار، لە چرکەی 6 (270) داگیرسا و لە چرکەی 9 (335) کوژایەوە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-5
Two thresholds 40 steps apart: the band between them is the dead band.
دوو بە جیاوازیی 40 هەنگاو: ناوچەی نێوانیان .
- Lineدێڕی 15-19
Switch on only when off and clearly dark; switch off only when on and clearly light.
تەنیا کاتێک کوژاوەیە و بە ڕوونی تاریکە دایبگیرسێنە؛ تەنیا کاتێک داگیرساوە و بە ڕوونی ڕووناکە بیکوژێنەوە.
The smart cage goes one step further: it waits until it has been dark for some minutes before it moves the cover, so a cloud or a room lamp cannot fool it (The light rule: no cover dance at dusk).
قەفەزە هۆشمەندەکە هەنگاوێکی تر دەڕوات: چاوەڕێ دەکات تا چەند خولەکێک تاریک بێت پێش ئەوەی بەرگەکە بجووڵێنێت، بۆیە هەورێک یان گڵۆپی ژوورێک ناتوانێت هەڵیبخەڵەتێنێت (یاسای ڕووناکی: بەرگەکە لە ئێوارەدا نالەرزێت).
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | Betterباشتر |
|---|---|
| The divider on the MKR's 5V pin. لەسەر پینی 5Vـی MKR. | VCC (3.3 V) on 3.3 V boards; 5V only on 5 V boards.VCC (3.3 V) لەسەر بۆردەکانی 3.3 V؛ 5V تەنیا لەسەر بۆردەکانی 5 V. |
| Code pasted from a word processor, with curly quotes.کۆدێک لە بەرنامەیەکی نووسینەوە کۆپی کراوە، بە چەماوە. | Type code in the IDE: straight quotes only.کۆد لە IDEـدا بنووسە: تەنیا ڕاست. |
A } written after //.}ێک دوای // نووسراوە. | Each brace on its own line, before any comment.هەر کەوانەیەک لە دێڕی خۆیدا، پێش هەر . |
Expecting value < 300 to toggle the LED.چاوەڕوانیی ئەوە کە value < 300 بگۆڕێت. | It is a level test (a night light); a toggle needs an edge.پشکنینی ئاستە (گڵۆپی شەو)؛ گۆڕین پێویستی بە لێوارە. |
| The LDR alone between 5 V and the pin, with no resistor to GND.تەنیا لە نێوان 5 V و پینەکەدا، بەبێ بۆ GND. | Add the 10 kΩ to GND: without it there is no divider, and in the dark the pin floats.10 kΩـەکە بۆ GND زیاد بکە: بەبێ ئەو نییە و لە تاریکیدا پینەکە هەڵدەواسرێت. |
Swapping the divider but keeping value < 300.ئاڵوگۆڕکردنی بەڵام هێشتنەوەی value < 300. | With the LDR to GND, dark is a big number: use value > 723.کە بۆ GND بێت، تاریکی ژمارەیەکی گەورەیە: value > 723 بەکار بهێنە. |
| One threshold at dusk: the LED flickers.یەک لە ئێوارەدا: دەلەرزێت. | Two thresholds (hysteresis), or wait until it stays dark.دوو ()، یان چاوەڕێ بکە تا تاریک بمێنێتەوە. |
| Reading an LDR's number as lux.خوێندنەوەی ژمارەی وەک . | Parts differ (10–20 kΩ at 10 lux): use it for light or dark; for real lux, use a calibrated light-sensor chip.پارچەکان جیاوازن (10–20 kΩ لە 10 ): بۆ ڕووناک یان تاریک بەکاری بهێنە؛ بۆ ڕاستەقینە، بەکار بهێنە کە کرابێت. |
Exercisesڕاهێنانەکان
At deep dusk a GL5528 reads about 30 kΩ. With R2 = 10 kΩ on a 5 V UNO, what are Vout and the reading? Is the night light on?
لە ئێوارەیەکی تاریکدا GL5528ـێک نزیکەی 30 kΩ دەخوێنێتەوە. بە R2 = 10 kΩ لەسەر UNOـی 5 V، Vout و خوێندنەوەکە چەندن؟ ئایا گڵۆپی شەو داگیرساوە؟
Hintئاماژە
Use Vout = V × R2 ÷ (R(LDR) + R2), then value = ⌊Vout × 1024 ÷ 5⌋.
Vout = V × R2 ÷ (R(LDR) + R2) بەکار بهێنە، پاشان value = ⌊Vout × 1024 ÷ 5⌋.
Show answerوەڵامەکە پیشان بدە
256 is below 300, so the LED is on.
256 لە خوار 300ـەوەیە، بۆیە داگیرساوە.
Print the light level as 0–100 % instead of 0–1023, once a second.
ئاستی ڕووناکی وەک 0–100% چاپ بکە لە جیاتی 0–1023، چرکەی جارێک.
Hintئاماژە
map(value, 0, 1023, 0, 100) keeps whole numbers.
map(value, 0, 1023, 0, 100) دەهێڵێتەوە.
Show answerوەڵامەکە پیشان بدە
// Show the light level as a percentage: 0 % = dark, 100 % = very bright
const int ldrPin = A1;
void setup() {
Serial.begin(9600);
}
void loop() {
int value = analogRead(ldrPin);
int percent = map(value, 0, 1023, 0, 100);
Serial.print("Light: ");
Serial.print(percent);
Serial.println(" %");
delay(1000);
}Light: 14 % Light: 39 % Light: 91 %
CêIT Arduino checker, A1 = 146, 409 and 935 (1, 10 and 1,000 lux).
پشکنەری Arduinoـی CêIT، A1 = 146، 409 و 935 (1، 10 و 1,000 ).
Make the LED change state each time the LDR is covered (the moment the reading falls below 300), and stay that way until the next covering.
وا بکە هەر جارێک دادەپۆشرێت دۆخی بگۆڕێت (ساتی دابەزینی خوێندنەوەکە بۆ خوار 300) و تا داپۆشینی داهاتوو وەک خۆی بمێنێتەوە.
Hintئاماژە
Keep wasDark from the last pass, as lastBtnState in the button lab.
wasDark لە خولی پێشووەوە بهێڵەوە، وەک lastBtnState لە تاقیگەی دوگمەکەدا.
Show answerوەڵامەکە پیشان بدە
// A real toggle: each time you cover the LDR, the LED changes state
const int ldrPin = A1;
const int ledPin = 5;
const int threshold = 300;
bool wasDark = false; // was the LDR covered at the last reading?
bool ledOn = false;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
bool isDark = analogRead(ldrPin) < threshold;
if (isDark && !wasDark) { // it has just been covered
ledOn = !ledOn;
if (ledOn) {
digitalWrite(ledPin, HIGH);
Serial.println("covered: LED on");
} else {
digitalWrite(ledPin, LOW);
Serial.println("covered: LED off");
}
}
wasDark = isDark;
delay(50);
}covered: LED on covered: LED off covered: LED on
CêIT Arduino checker: a hand covered the LDR at 0.5 s, 2.0 s and 3.2 s. The LED on pin 5 went on, off and on.
پشکنەری Arduinoـی CêIT: دەستێک لە چرکەی 0.5، 2.0 و 3.2 داپۆشی. پینی 5 داگیرسا، کوژایەوە و داگیرسایەوە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 15
A falling edge: dark now, light before.
: ئێستا تاریک، پێشتر ڕووناک.
Rewire the divider with 10 kΩ to the supply and the LDR to GND. Change the sketch so that the LED is still on in the dark.
دووپاتە ببەستەوە بە 10 kΩ بۆ سەرچاوەکە و بۆ GND. بگۆڕە تا هێشتا لە تاریکیدا داگیرساو بێت.
Show answerوەڵامەکە پیشان بدە
// The divider swapped: 10 kOhm to 3.3 V, the LDR to GND.
// Now more light gives a SMALLER number, so the test flips.
const int ldrPin = A1;
const int ledPin = 5;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int value = analogRead(ldrPin);
Serial.print("Light Intensity: ");
Serial.println(value);
if (value > 723) { // dark now means a big number
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(1000);
}Light Intensity: 281 Light Intensity: 614 Light Intensity: 877
CêIT Arduino checker, A1 = 281, 614, 877 (100, 10, 1 lux). The LED on pin 5 came on only at 877.
پشکنەری Arduinoـی CêIT، A1 = 281، 614، 877 (100، 10، 1 ). پینی 5 تەنیا لە 877ـدا داگیرسا.
You want the night light to switch at 10 lux. With the lab's divider and a GL5528 of 15 kΩ at 10 lux, which threshold do you write?
دەتەوێت گڵۆپی شەو لە 10 بگۆڕێت. بە تاقیگەکە و GL5528ـێکی 15 kΩ لە 10 ، کام دەنووسیت؟
Show answerوەڵامەکە پیشان بدە
value = ⌊1024 × 10 ÷ (15 + 10)⌋ = 409, so if (value < 409). Better still: print the readings at the moment you want, because each LDR is a little different.
value = ⌊1024 × 10 ÷ (15 + 10)⌋ = 409، بۆیە if (value < 409). باشتر لەوە: لەو ساتەی دەتەوێت خوێندنەوەکان چاپ بکە، چونکە هەر کەمێک جیاوازە.
Summaryپوختە
- An LDR (photoresistor) is a passive component: its resistance falls as light rises, from 1 MΩ or more in the dark to a few hundred ohms in sunlight (GL5528: 10–20 kΩ at 10 lux).
- A voltage divider turns its resistance into a voltage: Vout = V × R2 ÷ (R(LDR) + R2); analogRead() turns that into 0–1023.
- Feed the divider from the board's own voltage: VCC (3.3 V) on the MKR WAN 1310, 5V on an UNO. Then the reading is the same on both.
- The lab is a night light (a level test below 300); straight quotes and braces outside comments make it compile.
- Two thresholds (hysteresis) stop flicker at dusk.
- : بە زیادبوونی ڕووناکی کەم دەبێتەوە، لە 1 MΩ یان زیاتر لە تاریکیدا تا چەند سەد لە تیشکی خۆردا (GL5528: 10–20 kΩ لە 10 ).
- دەکات بە : Vout = V × R2 ÷ (R(LDR) + R2)؛ analogRead() ئەوە دەکات بە 0–1023.
- لە خودی بۆردەکەوە بدە: VCC (3.3 V) لەسەر MKR WAN 1310، 5V لەسەر UNO. ئینجا خوێندنەوەکە لەسەر هەردووکیان وەک یەکە.
- تاقیگەکە گڵۆپی شەوە (پشکنینی ئاست لە خوار 300)؛ ڕاست و کەوانە لە دەرەوەی وای لێ دەکەن کۆمپایل بێت.
- دوو () لەرزینی ئێوارە ڕادەگرن.
Why can a board not read an LDR without a second resistor?بۆچی بۆرد ناتوانێت بەبێ بەرگرێکی دووەم LDR بخوێنێتەوە؟
analogRead() measures voltage, not resistance. The fixed resistor makes a divider whose middle voltage follows the LDR.
analogRead() دەپێوێت، نەک . جێگیرەکە دروست دەکات کە ناوەڕاستی بەدوای دەڕوات.
What did the two compile errors have in common?دوو هەڵەی کاتی کۆمپایلکردنەکە چییان هاوبەش بوو؟
Both were typing slips the compiler cannot guess around: a curly quote that never closes the text, and a brace hidden inside a comment.
هەردووکیان هەڵەی نووسین بوون کە ناتوانێت مەزەندەیان بکات: چەماوە کە هەرگیز دەقەکە داناخات و کەوانەیەک کە لە ناو شاراوەیە.
Why do phones use photodiodes instead of LDRs?بۆچی مۆبایل لە جیاتی LDR چیپی هەستەوەری ڕووناکی بەکار دەهێنێت؟
They are faster and more accurate, and they contain no cadmium, which EU rules restrict.
خێراتر و وردترن و کادمیۆمیان تێدا نییە، کە یاساکانی یەکێتیی ئەورووپا سنوورداری دەکەن.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Temperature and Humidity: the DHT11 and a 16x2 LCDپلەی گەرمی و شێ: DHT11 و LCDـێکی 16x2
Related lecturesوانە پەیوەندیدارەکان
- Sensors: How Things Feel the Worldهەستەوەرەکان: شتەکان چۆن هەست بە جیهان دەکەن
- Accuracy, Precision, Range: Reading a Sensor's Data Sheetڕاستیی پێوان، وردیی پێوان و مەودای پێوان: خوێندنەوەی پەڕەی تایبەتمەندییەکانی هەستەوەرێک
- Temperature and Humidity: the DHT11 and a 16x2 LCDپلەی گەرمی و شێ: DHT11 و LCDـێکی 16x2
- Project: a Smart Bird-Cage Coverپرۆژە: بەرگی هۆشمەندی قەفەزی باڵندە
- Lab: Buttons, INPUT_PULLUP and Debouncingتاقیگە: دوگمەکانی پەستان، INPUT_PULLUP و لابردنی هەڵبەزینەوە
- Arduino Boards and the Arduino IDEبۆردەکانی Arduino و Arduino IDE
- Where IoT Is Used, and What Makes It HardIoT لە کوێ بەکار دێت و چی قورسی دەکات
- Embedded Systems at Work: a Bike Computer and a Car Engineسیستەمە چەسپێنراوەکان لە کاردا: کۆمپیوتەری پاسکیل و بزوێنەری ئۆتۆمبێل
- Components and Instruments on the Lab Benchپارچە و ئامێرەکانی سەر مێزی تاقیگە
- Electricity Basics: Voltage, Current and Resistanceبنەماکانی کارەبا: ڤۆڵتیە، تەزوو و بەرگری
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Senba Optical & Electronic: GL55 Series Photoresistor (GL5528 data sheet: 10–20 kΩ at 10 lux, 1 MΩ dark, γ 0.6, 20/30 ms)
- Arduino MKR WAN 1310 data sheet (ABX00029): 3.3 V I/O, not 5 V tolerant, the 5V pin
- Arduino reference: analogRead()
- Arduino reference: map()
- Wikipedia: Photoresistor (dark and light resistance, response lag, RoHS restriction of CdS)
- Wikipedia: Lux (illuminance examples)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.