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 دروست دەکەیت، دوای چاککردنی دوو و وەشانێک کە نالەرزێت.

24 min read24 خولەک خوێندنەوە · 21 stepsهەنگاو

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ـێک بترووسکێنە و گڵۆپی هاتوچۆ دروست بکە لە پینێکەوە داگیرساند. ئەم تاقیگەیە هەردووکیان بەکار دەهێنێت.

Two panels. Left, day: a street lamp with a small LDR on top, the sun shining; the lamp is off and the label says much light, LDR resistance low. Right, dusk: the moon and stars; the lamp shines a cone of light; the label says little light, LDR resistance high, lamp on.دوو بەش. چەپ، ڕۆژ: گڵۆپێکی شەقام کە LDRـێکی بچووکی لەسەرە، خۆر دەدرەوشێتەوە؛ گڵۆپەکە کوژاوەیە و نووسینەکە دەڵێت ڕووناکیی زۆر، بەرگریی LDR نزم. ڕاست، ئێوارە: مانگ و ئەستێرە؛ گڵۆپەکە ڕووناکی دەداتە خوارەوە؛ نووسینەکە دەڵێت ڕووناکیی کەم، بەرگریی LDR بەرز، گڵۆپ داگیرساوە.
The LDR on the lamp decides: light, stay off; dark, switch on. سەر گڵۆپەکە بڕیار دەدات: ڕووناک، کوژاوە بمێنەوە؛ تاریک، دابگیرسێ.CêIT

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.

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

Animation in five steps. A torch shines on an LDR whose two legs lead to points A and B. Three bars on the right show light, resistance and current. Torch off: 0 lux, resistance 1 megohm or more, almost no current. Then 10 lux and about 15 kilohms, 100 lux and about 3.8 kilohms, 1,000 lux and about 950 ohms; the current arrow from A to B grows each time. Torch off again: the resistance climbs back.
Animation in five steps. A torch shines on an LDR whose two legs lead to points A and B. Three bars on the right show light, resistance and current. Torch off: 0 lux, resistance 1 megohm or more, almost no current. Then 10 lux and about 15 kilohms, 100 lux and about 3.8 kilohms, 1,000 lux and about 950 ohms; the current arrow from A to B grows each time. Torch off again: the resistance climbs back.
Light up, resistance down, current up.ڕووناکی بەرز، نزم، بەرز.CêIT

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 large drawing of an LDR seen from above: a round disc with an orange-brown zigzag track between two silver electrodes, labelled light-sensitive track (CdS), metal electrode (one leg) and clear epoxy on top. On the right, a side view with two long legs, about 5 mm across, and the circuit symbol: a resistor zigzag with two arrows of light pointing at it.وێنەیەکی گەورەی LDR لە سەرەوە: قرسێکی خڕ کە ڕێڕەوێکی زیگزاگی پرتەقاڵی-قاوەیی لە نێوان دوو ئەلیکترۆدی زیوینیدایە، بە نووسینی ڕێڕەوی هەستیار بە ڕووناکی (CdS)، ئەلیکترۆدی کانزایی (قاچێک) و چینێکی ڕوونی ئیپۆکسی. لە لای ڕاستەوە، وێنەیەک لە لاوە بە دوو قاچی درێژ، نزیکەی 5 mm پانی و هێمای هێڵکاریی سووڕ: زیگزاگی بەرگر لەگەڵ دوو تیری ڕووناکی کە ئاماژەی بۆ دەکەن.
The zigzag of CdS between two electrodes; in circuits, a resistor with two arrows of light.زیگزاگی CdS لە نێوان دوو ئەلیکترۆددا؛ لە ، بە دوو تیری ڕووناکی.CêIT

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ئیسفەنج

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.

تەنیا وێنەیەکە: هەڵدەگرێت و دەکات بە گەرمی.

Two panels. Passive: use, store or resist energy — a resistor, a capacitor and an LDR; they cannot make a signal bigger. Active: a small signal controls a big current — a transistor and a chip; they can switch and amplify. Below: an LDR is passive; light changes its resistance, but it adds no energy.دوو بەش. ناچالاک: وزە بەکار دەبەن، هەڵدەگرن یان بەرگری دەکەن — بەرگر، کاپاسیتەر و LDR؛ ناتوانن سیگناڵێک گەورەتر بکەن. چالاک: سیگناڵێک بچووک تەزوویەک گەورە کۆنترۆڵ دەکات — ترانزیستۆر و چیپ؛ دەتوانن هەڵ و بکوژێننەوە و بەهێز بکەن. لە خوارەوە: LDR ناچالاکە؛ ڕووناکی بەرگرییەکەی دەگۆڕێت، بەڵام هیچ وزەیەک زیاد ناکات.
Passive parts can only take what the circuit gives; active parts can control it. تەنیا ئەوە وەردەگرن کە دەیدات؛ دەتوانن کۆنترۆڵی بکەن.CêIT

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 more⁦1 MΩ⁩ یان زیاتر
γ: how fast it falls from 10 to 100 luxγ: لە ⁦10⁩ بۆ ⁦100⁩ چەند خێرا دادەبەزێت0.6: about 4 times smaller at 100 lux⁦0.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⁩. هێڵکارییەکە لەسەر ئەم پێوەرانە دادەنێت:

A graph with ten-times scales on both axes: light from 0.1 to 100,000 lux, resistance from 10 ohms to 10 megohms. A falling straight line with a shaded band shows the GL5528: 10 to 20 kilohms at 10 lux, marked in orange; dark, 1 megohm or more, at the top left. Examples along the light axis: full moon, living room, office, cloudy day, daylight, sun. A label says: more light, less resistance.هێڵکارییەک کە هەردوو تەوەرەکەی پێوەری دە هێندەیین: ڕووناکی لە 0.1 تا 100,000 لوکس، بەرگری لە 10 ئۆم تا 10 مێگائۆم. هێڵێکی ڕاستی دابەزیو لەگەڵ پشتێنێکی ڕەنگکراو GL5528 پیشان دەدات: 10 تا 20 کیلۆئۆم لە 10 لوکس، بە پرتەقاڵی نیشانە کراوە؛ تاریکی، 1 مێگائۆم یان زیاتر، لە سەرەوەی چەپ. نموونەکان لەسەر تەوەری ڕووناکی: مانگی پڕ، ژووری دانیشتن، ئۆفیس، ڕۆژی هەور، ڕووناکیی ڕۆژ، خۆر. نووسینێک دەڵێت: ڕووناکیی زیاتر، بەرگری کەمتر.
Ten times more light: about four times less resistance.دە هێندە ڕووناکیی زیاتر: نزیکەی چوار هێندە کەمتر.CêIT

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).

گڵۆپی شەقامی بەستراو زیاتر دەکات: کز و گەش دەبێت و تێکچوون ڕاپۆرت دەکات (ڕووناکیی هۆشمەند). پڕۆژەکەی کۆتایی ئەم کۆرسە بەکار دەهێنێت بۆ هەستکردن بە خۆرهەڵاتن و خۆرئاوابوون (پڕۆژە: بەرگی هۆشمەندی قەفەزی باڵندە).

Four cards. Alarm clock with a small LDR: dims its screen at night, wakes you at sunrise. Street light: on at dusk, off at dawn. Light meter with a needle: tells a photographer how bright it is. Burglar alarm: a red beam from a box to an LDR, a person crossing it; someone blocks the beam, the alarm sounds.چوار کارت. کاتژمێری زەنگدار بە LDRـێکی بچووکەوە: شەو شاشەکەی کز دەکات، لە خۆرهەڵاتندا ئاگات دەکاتەوە. گڵۆپی شەقام: لە ئێوارەدا دادەگیرسێت، لە بەیانیدا دەکوژێتەوە. پێوەری ڕووناکی بە دەرزییەکەوە: بە وێنەگر دەڵێت چەند ڕووناکە. ئاگادارکەرەوەی دز: تیشکێکی سوور لە سندووقێکەوە بۆ LDRـێک، کەسێک بەناویدا تێدەپەڕێت؛ کەسێک تیشکەکە دەبڕێت، زەنگەکە لێ دەدات.
Four jobs, one idea: notice light, or its absence.چوار کار، یەک بیرۆکە: هەستکردن بە ڕووناکی، یان نەبوونی.CêIT

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ـدا. هەمان بە هەردووکیاندا تێدەپەڕێت، بۆیە سەرچاوەکە بەپێی بەش دەکەن (بەستنی زنجیرەیی و هاوتەریب). ئەو خاڵەی یەکتر دەگرن دەچێت بۆ .

Vout=Vsupply×R2RLDR+R2V_{out} = V_{supply} \times \frac{R_2}{R_{LDR} + R_2}

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⁩ دەکێشێت لە جیاتی ئەوەی بە هەڵواسراوی بیهێڵێتەوە (بەرگری بەرزکەرەوە و بەرگری نزمکەرەوە).

A circuit: 5 V at the top, then the LDR with two light arrows, then a junction labelled Vout that goes to pin A1, then a 10 kilohm resistor to ground; a current arrow I runs down beside them. A card on the right: Vout = 5 V × 10 kΩ / (R(LDR) + 10 kΩ); example at 10 lux, R(LDR) = 15 kΩ, Vout = 5 × 10 / 25 = 2.00 V, analogRead(A1) = ⌊2.00 × 1024 / 5⌋ = 409; more light, smaller R(LDR), higher Vout, bigger number.سووڕێک: 5 V لە سەرەوە، پاشان LDR بە دوو تیری ڕووناکییەوە، پاشان خاڵێکی پەیوەندی بە ناوی Vout کە دەچێت بۆ پینی A1، پاشان بەرگرێکی 10 کیلۆئۆم بۆ زەوی؛ تیری تەزووی I لە تەنیشتیانەوە دێتە خوارەوە. کارتێک لە لای ڕاستەوە: Vout = 5 V × 10 kΩ / (R(LDR) + 10 kΩ)؛ نموونە لە 10 لوکس، R(LDR) = 15 kΩ، Vout = 5 × 10 / 25 = 2.00 V، analogRead(A1) = ⌊2.00 × 1024 / 5⌋ = 409؛ ڕووناکیی زیاتر، R(LDR)ـی بچووکتر، Voutـی بەرزتر، ژمارەی گەورەتر.
Two resistors in series share the supply; the LDR decides the share.دوو زنجیرەیی سەرچاوەکە بەش دەکەن؛ بەشەکە دیاری دەکات.CêIT

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⁩ دەیدات:

Vout=5 V×10 kΩ15 kΩ+10 kΩ=5×0.4=2.00 Vvalue=⌊2.00 V×10245 V⌋=⌊409.6⌋=409\begin{aligned} V_{out} &= 5\,\text{V} \times \frac{10\,\text{k}\Omega}{15\,\text{k}\Omega + 10\,\text{k}\Omega} = 5 \times 0.4 = 2.00\,\text{V} \\ \text{value} &= \left\lfloor \frac{2.00\,\text{V} \times 1024}{5\,\text{V}} \right\rfloor = \lfloor 409.6 \rfloor = 409 \end{aligned}
LightڕووناکیR(LDR)VoutanalogRead()
darkتاریکی1 MΩ0.05 V10
1 lx60 kΩ0.71 V146
10 lx15 kΩ2.00 V409
100 lx3.8 kΩ3.62 V742
1,000 lx950 Ω4.57 V935
Animation: the same divider at 1, 10, 100 and 1,000 lux. Each frame shows R(LDR), the sum for Vout and the analogRead() number on a bar from 0 to 1023 with a mark at 300: 60 kilohms, 0.71 V, 146, below 300 so the night light is on; 15 kilohms, 2.00 V, 409; 3.8 kilohms, 3.62 V, 742; 950 ohms, 4.57 V, 935. The last frame is a table of all four.
Animation: the same divider at 1, 10, 100 and 1,000 lux. Each frame shows R(LDR), the sum for Vout and the analogRead() number on a bar from 0 to 1023 with a mark at 300: 60 kilohms, 0.71 V, 146, below 300 so the night light is on; 15 kilohms, 2.00 V, 409; 3.8 kilohms, 3.62 V, 742; 950 ohms, 4.57 V, 935. The last frame is a table of all four.
From light to a number in three steps.لە ڕووناکییەوە بۆ ژمارە لە سێ هەنگاودا.CêIT

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 number⁦5 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. دوو هاوکێشەکە پێکەوە دابنێ و سەرچاوەکە لادەچێت:

value=⌊1024×VsupplyVref×R2RLDR+R2⌋=⌊1024×R2RLDR+R2⌋(Vsupply=Vref)\text{value} = \left\lfloor 1024 \times \frac{V_{supply}}{V_{ref}} \times \frac{R_2}{R_{LDR} + R_2} \right\rfloor = \left\lfloor 1024 \times \frac{R_2}{R_{LDR} + R_2} \right\rfloor \quad (V_{supply} = V_{ref})

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: تەنیا ڕێژەی دەژمێردرێت.

Two cards. UNO, 5 V: divider on 5 V, R(LDR) = 15 kilohms at 10 lux, Vout = 5 × 10 / 25 = 2.00 V, the ADC compares it with 5 V, ⌊2.00 × 1024 / 5⌋ = 409. MKR WAN 1310, 3.3 V: Vout = 3.3 × 10 / 25 = 1.32 V, compared with 3.3 V, ⌊1.32 × 1024 / 3.3⌋ = 409. Below: reading = ⌊1024 × 10 kΩ / (R(LDR) + 10 kΩ)⌋; the supply cancels out, as long as the divider uses the board's own reference.دوو کارت. UNO، 5 V: دابەشکەری ڤۆڵتیە لەسەر 5 V، R(LDR) = 15 کیلۆئۆم لە 10 لوکس، Vout = 5 × 10 / 25 = 2.00 V، گۆڕەری ئەنالۆگ بۆ دیجیتاڵ بەراوردی دەکات لەگەڵ 5 V، ⌊2.00 × 1024 / 5⌋ = 409. MKR WAN 1310، 3.3 V: Vout = 3.3 × 10 / 25 = 1.32 V، بەراورد لەگەڵ 3.3 V، ⌊1.32 × 1024 / 3.3⌋ = 409. لە خوارەوە: خوێندنەوە = ⌊1024 × 10 kΩ / (R(LDR) + 10 kΩ)⌋؛ ڤۆڵتیەی سەرچاوەکە لادەچێت، بە مەرجێک دابەشکەری ڤۆڵتیەکە ڤۆڵتیەی پێوەری خودی بۆردەکە بەکار بهێنێت.
Different voltages, the same reading: the ratio is what counts. جیاواز، هەمان خوێندنەوە: ڕێژەکەیە کە دەژمێردرێت.CêIT

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⁩)، بۆیە خوێندنەوەکە ڕێک لەو شوێنەدا زۆر دەجووڵێت کە گڵۆپی شەو دەبێت بڕیار بدات.

Two dividers side by side. Left: 5 V, the LDR on top, the junction to A1, 10 kilohms to ground; 100 lux gives 742 and 1 lux gives 146; LDR on top, more light, bigger number. Right: 5 V, 10 kilohms on top, the junction to A1, the LDR to ground; 100 lux gives 281 and 1 lux gives 877; LDR at the bottom, more light, smaller number.دوو دابەشکەری ڤۆڵتیە لە تەنیشت یەک. چەپ: 5 V، LDR لە سەرەوە، خاڵی پەیوەندی بۆ A1، 10 کیلۆئۆم بۆ زەوی؛ 100 لوکس 742 دەدات و 1 لوکس 146؛ LDR لە سەرەوە، ڕووناکیی زیاتر، ژمارەی گەورەتر. ڕاست: 5 V، 10 کیلۆئۆم لە سەرەوە، خاڵی پەیوەندی بۆ A1، LDR بۆ زەوی؛ 100 لوکس 281 دەدات و 1 لوکس 877؛ LDR لە خوارەوە، ڕووناکیی زیاتر، ژمارەی بچووکتر.
Swap the parts and the numbers turn over.پارچەکان ئاڵوگۆڕ بکە و ژمارەکان پێچەوانە دەبنەوە.CêIT

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ـی لە ناودایە.

Two circuits. Left, LDR + light: a battery, an ammeter A in series and an LDR, with a voltmeter V across the LDR and a lamp with a dimmer shining on it; more light, R falls, the ammeter reads more; R = V ÷ I from the two meters. Right, thermistor (NTC) + heat: the same loop with a thermistor and a heater beside it; warmer, R of an NTC falls, the ammeter reads more.دوو سووڕ. چەپ، LDR + ڕووناکی: پاترییەک، ئەمپێرمیتەرێکی A بە بەستنی زنجیرەیی و LDRـێک، لەگەڵ ڤۆڵتمیتەرێکی V بە دوو سەری LDRەکەوە و گڵۆپێکی بە کزکەرەوە کە ڕووناکیی دەخاتە سەری؛ ڕووناکیی زیاتر، R دادەبەزێت، ئەمپێرمیتەر زیاتر دەخوێنێتەوە؛ R = V ÷ I لە دوو پێوەرەکەوە. ڕاست، تێرمیستۆر (NTC) + گەرمی: هەمان بازنە بە تێرمیستۆرێک و گەرمکەرەوەیەک لە تەنیشتی؛ گەرمتر، Rـی NTC دادەبەزێت، ئەمپێرمیتەر زیاتر دەخوێنێتەوە.
Two resistive sensors, one test: watch the current change.دوو بەرگریی، یەک تاقیکردنەوە: سەیری گۆڕانی بکە.CêIT

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)⁩ هەیە.

  1. 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: .

  2. 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).

  3. Read pin A1 with analogRead() to measure the voltage.

    پینی ⁦A1⁩ بە analogRead() بخوێنەوە بۆ پێوانی .

  4. Copy the sketch, upload it and open the Serial Monitor.

    کۆپی بکە، هەڵبار بکە و بکەرەوە.

A circuit diagram. On the left, the MKR WAN 1310 with pins VCC 3.3 V, 5V, A1, 5 and GND. VCC goes to the LDR, the LDR to a junction that returns to A1 (labelled Vout), and R1 10 kilohms from the junction to ground. Pin 5 goes through R2 220 ohms and an LED to ground. The 5V pin has a red cross: not this pin, USB 5 V. Note: the MKR's pins take 3.3 V at most; feed the divider from VCC, never from 5V.نەخشەی سووڕێک. لە چەپەوە MKR WAN 1310 بە پینەکانی VCC 3.3 V، 5V، A1، 5 و GND. VCC دەچێت بۆ LDR، LDR بۆ خاڵێکی پەیوەندی کە دەگەڕێتەوە بۆ A1 (بە ناوی Vout) و R1ـی 10 کیلۆئۆم لە خاڵەکەوە بۆ زەوی. پینی 5 بە R2ـی 220 ئۆم و LEDـێکدا دەچێت بۆ زەوی. پینی 5V خاچێکی سووری لەسەرە: ئەم پینە نا، 5 Vـی USB. تێبینی: پینەکانی MKR زۆرترین 3.3 V وەردەگرن؛ دابەشکەری ڤۆڵتیەکە لە VCCـەوە بدە، هەرگیز لە 5Vـەوە نا.
The divider on VCC (3.3 V), the LED on pin 5. لەسەر ⁦VCC (3.3 V)⁩، لەسەر پینی 5.CêIT

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⁩ یان زیاتر کوژاوە. ئەمە چاککراوەکەیە؛ شاشەی داهاتوو ئەو دوو هەڵەی نووسینە پیشان دەدات کە نەیانهێشت یەکەم وەشان کۆمپایل بێت.

The night light on the MKR WAN 1310گڵۆپی شەو لەسەر ⁦MKR WAN 1310⁩ldr_led.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
/* 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
}
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⁩ .

RLDR=R2×(1024300−1)=10 kΩ×2.41=24.1 kΩR_{LDR} = R_2 \times \left(\frac{1024}{300} - 1\right) = 10\,\text{k}\Omega \times 2.41 = 24.1\,\text{k}\Omega
A graph of the reading over 9 seconds: 742 in a lit room, 409 at dusk, 146 at night, then 299 and 301 at dawn and 742 in the morning, one dot a second. A dashed orange line marks the threshold 300. Below, an LED strip is ON from 4 to 7 seconds. Note: 146 and 299 are below 300, on; 301 is not, off; the test is value < 300.هێڵکارییەکی خوێندنەوەکە لە ماوەی 9 چرکەدا: 742 لە ژوورێکی ڕووناکدا، 409 لە ئێوارەدا، 146 لە شەودا، پاشان 299 و 301 لە بەرەبەیاندا و 742 لە بەیانیدا، چرکەی خاڵێک. هێڵێکی پچڕپچڕی پرتەقاڵی ئاستانەی 300 نیشان دەدات. لە خوارەوە، شریتی LED لە چرکەی 4 تا 7 داگیرساوە. تێبینی: 146 و 299 لە خوار 300ـەوەن، داگیرساو؛ 301 وا نییە، کوژاو؛ پشکنینەکە value < 300ـە.
The threshold turns a number into a yes or no. ژمارەیەک دەکات بە بەڵێ یان نەخێر.CêIT

The night-light sketchسکێچی گڵۆپی شەو

Common mistake: two errors that stop the compilerهەڵەی باو: دوو هەڵە کە کۆمپایلەر ڕادەگرن

What the compiler said (CêIT checker; the Arduino IDE words it a little differently)ئەوەی کۆمپایلەر گوتی (پشکنەری CêIT؛ Arduino IDE کەمێک بە جیاوازی دەیڵێت)
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.
Two code panels. As typed, does not compile: Serial.print("Light Intensity: “); with the curly quote circled in red; the if and else lines with their comments shaded grey and the closing brace at the end of each circled in red. Notes: 1, a curly quote is not a C++ quote; 2, after // the rest of the line is a comment, so the brace never counts. Fixed: straight quotes, and each brace on its own line before any comment.دوو بەشی کۆد. وەک نووسراوە، کۆمپایل نابێت: Serial.print("Light Intensity: “); کە نیشانەی گوتەی چەماوەکەی بە سوور دەوری دراوە؛ دێڕەکانی if و else کە لێدوانەکانیان خۆڵەمێشی کراون و کەوانەی داخستنی کۆتایی هەریەکەیان بە سوور دەوری دراوە. تێبینی: 1، نیشانەی گوتەی چەماوە نیشانەی گوتەی ++C نییە؛ 2، دوای // تەواوی دێڕەکە لێدوانە، بۆیە کەوانەکە هەرگیز ناژمێردرێت. چاککراو: نیشانەی گوتەی ڕاست و هەر کەوانەیەک لە دێڕی خۆیدا پێش هەر لێدوانێک.
Straight quotes, and braces outside comments. ڕاست و کەوانە لە دەرەوەی .CêIT

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⁩ — لێوارێک، وەک دوگمەکان لە ئاست یان لێوار؟ کاردانەوە بۆ گۆڕان.

Three strips over 4 seconds. Top: when the LDR is covered (three dark blocks). Middle, night light (level): the LED is on exactly during each covered block. Bottom, toggle (edge): the LED turns on at the first covering, off at the second and on at the third, staying that way in between. Dashed lines mark the moments the LDR becomes covered. Caption: level, on while dark; edge, flip at the moment it becomes dark.سێ شریت لە ماوەی 4 چرکەدا. سەرەوە: کەی LDR داپۆشراوە (سێ پارچەی تاریک). ناوەڕاست، گڵۆپی شەو (ئاست): LEDـەکە ڕێک لە کاتی هەر پارچەیەکی داپۆشراودا داگیرساوە. خوارەوە، گۆڕین (لێوار): LEDـەکە لە یەکەم داپۆشیندا دادەگیرسێت، لە دووەمدا دەکوژێتەوە و لە سێیەمدا دادەگیرسێت؛ لە نێوانیاندا وەک خۆی دەمێنێتەوە. هێڵی پچڕپچڕ ساتی داپۆشینی LDR نیشان دەدەن. ڕوونکردنەوە: ئاست، داگیرساو تا تاریکە؛ لێوار، گۆڕین لە ساتی تاریکبووندا.
Same hand, same LDR: a level test or an edge test gives a different LED.هەمان دەست، هەمان : پشکنینی ئاست یان لێوار جیاواز دەدات.CêIT

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 Ω⁩ بەکار بهێنە.

An Arduino UNO and a breadboard. A red wire takes 5V to the top + rail and on to the LDR; a black wire takes GND to the top − rail. The LDR and a 10 kilohm resistor meet in one column, and a yellow wire from that column goes to A1. A blue wire from pin 5 goes to a 220 ohm resistor and a red LED, whose other leg goes to the − rail. Notes: UNO R3, 220 ohms is fine, about 13.6 mA; UNO R4, use 470 ohms, 8 mA per pin.Arduino UNO و بریدبۆردێک. وایەرێکی سوور 5V دەبات بۆ هێڵی +ی سەرەوە و لەوێوە بۆ LDR؛ وایەرێکی ڕەش GND دەبات بۆ هێڵی −ی سەرەوە. LDR و بەرگرێکی 10 کیلۆئۆم لە یەک ستووندا یەک دەگرن و وایەرێکی زەرد لەو ستوونەوە دەچێت بۆ A1. وایەرێکی شین لە پینی 5ـەوە دەچێت بۆ بەرگرێکی 220 ئۆم و LEDـێکی سوور، کە قاچەکەی تری دەچێت بۆ هێڵی −. تێبینی: UNO R3، 220 ئۆم باشە، نزیکەی 13.6 mA؛ UNO R4، 470 ئۆم بەکار بهێنە، 8 mA بۆ هەر پینێک.
The same circuit on 5 V; the sketch does not change.هەمان لەسەر ⁦5 V⁩؛ ناگۆڕێت.CêIT

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گڵۆپی شەو بە دوو ئاستانەnight_light_hysteresis.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// 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);
}
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.

تەنیا کاتێک کوژاوەیە و بە ڕوونی تاریکە دایبگیرسێنە؛ تەنیا کاتێک داگیرساوە و بە ڕوونی ڕووناکە بیکوژێنەوە.

A graph of ten readings near dusk, from 330 down to 270 and up to 335, wobbling around 300. Dashed lines at 280 (on below) and 320 (off above), and a dotted line at 300. Two strips below: one threshold (300) turns the LED on and off three times; two thresholds (280 / 320) turn it on once, at the reading 270, and off once, at 335.هێڵکارییەکی دە خوێندنەوە نزیکەی ئێوارە، لە 330ـەوە دادەبەزێت بۆ 270 و بەرز دەبێتەوە بۆ 335، بە دەوری 300ـدا سەر و خوار دەکات. هێڵی پچڕپچڕ لە 280 (داگیرساندن لە خوارەوە) و 320 (کوژاندنەوە لە سەرەوە) و هێڵێکی خاڵخاڵ لە 300. دوو شریت لە خوارەوە: یەک ئاستانە (300) LEDـەکە سێ جار هەڵ و دەکوژێنێتەوە؛ دوو ئاستانە (280 / 320) یەک جار دایدەگیرسێنێت (لە خوێندنەوەی 270ـدا) و یەک جار دەیکوژێنێتەوە (لە 335ـدا).
A gap between on and off stops the flicker.بۆشاییەک لە نێوان داگیرساندن و کوژاندنەوەدا لەرزینەکە ڕادەگرێت.CêIT

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ڕاهێنانەکان

ExerciseڕاهێنانA divider by handدابەشکەری ڤۆڵتیەیەک بە دەست

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وەڵامەکە پیشان بدە
Vout=5×1030+10=1.25 Vvalue=⌊1.25×10245⌋=256\begin{aligned} V_{out} &= 5 \times \frac{10}{30 + 10} = 1.25\,\text{V} \\ \text{value} &= \left\lfloor \frac{1.25 \times 1024}{5} \right\rfloor = 256 \end{aligned}

256 is below 300, so the LED is on.

⁦256⁩ لە خوار ⁦300⁩ـەوەیە، بۆیە داگیرساوە.

ExerciseڕاهێنانThe light as a percentageڕووناکی وەک ڕێژەی سەدی

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وەڵامەکە پیشان بدە
Answer: map() scales 0–1023 to 0–100وەڵام: ⁦map() 0–1023⁩ دەگۆڕێت بۆ ⁦0–100⁩light_percent.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// 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);
}
ExerciseڕاهێنانA real toggleگۆڕینێکی ڕاستەقینە

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وەڵامەکە پیشان بدە
Answer: remember the last state, act on the changeوەڵام: دوایین دۆخ لەبیر بکە، کاردانەوە بۆ گۆڕانەکەldr_toggle.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// 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);
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 15

A falling edge: dark now, light before.

: ئێستا تاریک، پێشتر ڕووناک.

ExerciseڕاهێنانThe divider the other way roundدابەشکەری ڤۆڵتیەکە بە پێچەوانەوە

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وەڵامەکە پیشان بدە
Answer: dark is now a big numberوەڵام: ئێستا تاریکی ژمارەیەکی گەورەیەldr_swapped.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// 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);
}
ExerciseڕاهێنانYour own thresholdئاستانەی خۆت

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⁩)؛ ڕاست و کەوانە لە دەرەوەی وای لێ دەکەن کۆمپایل بێت.
  • دوو () لەرزینی ئێوارە ڕادەگرن.
Review questionsپرسیارەکانی پێداچوونەوە
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زاراوە سەرەکییەکان

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

Temperature and Humidity: the DHT11 and a 16x2 LCDپلەی گەرمی و شێ: DHT11 و LCDـێکی ⁦16x2⁩

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.تەنیا لەسەر ئەم ئامێرە پاشەکەوت دەکرێت.