Chapterبەش 4 · Lectureوانە 4.4

Lab: Dimming with PWM and Beeping with tone()تاقیگە: کزکردن بە PWM و بیپ بە ⁦tone()⁩

Dim an LED with PWM: duty cycle, the ~ pins of the UNO R3, UNO R4 and MKR boards, analogWrite() 0–255, the fade and brightness-button labs with constrain(); then buzzers, active or passive, their uses, and tone()/noTone() with their limits.

کزکردنی بە : ، پینەکانی ~ـی ⁦UNO R3⁩، ⁦UNO R4⁩ و بۆردەکانی MKR، ⁦analogWrite() 0–255⁩، تاقیگەکانی کزکردن و دوگمەکانی ڕووناکی بە ⁦constrain()⁩؛ پاشان ، چالاک یان ناچالاک، بەکارهێنانەکانی و ⁦tone()/noTone()⁩ لەگەڵ سنوورەکانیان.

19 min read19 خولەک خوێندنەوە · 20 stepsهەنگاو

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

  • Explain PWM, duty cycle and the average voltage it gives.
  • Choose a PWM pin on the UNO R3, UNO R4 and MKR boards and use analogWrite() with values 0–255.
  • Fix a sketch whose value leaves 0–255, using constrain().
  • Tell an active buzzer from a passive one and wire a buzzer safely.
  • Play tones with tone() and noTone(), and know their limits.
  • ، و ئەو تێکڕای دەیدات ڕوون بکەیتەوە.
  • پینێکی PWM لەسەر ⁦UNO R3⁩، ⁦UNO R4⁩ و بۆردەکانی MKR هەڵبژێریت و ⁦analogWrite()⁩ بە بەهای ⁦0–255⁩ بەکار بهێنیت.
  • چاک بکەیتەوە کە بەهاکەی لە ⁦0–255⁩ دەردەچێت، بە ⁦constrain()⁩.
  • چالاک لە ناچالاک جیا بکەیتەوە و بە سەلامەتی ببەستیت.
  • بە ⁦tone()⁩ و ⁦noTone()⁩ دەنگ لێ بدەیت و سنوورەکانیان بزانیت.

Between on and offلە نێوان هەڵکراو و کوژاودا

digitalWrite() knows only two states: fully on or fully off. Yet a lamp can be dimmed and a buzzer can play different notes. Both tricks use the same idea: switch the pin on and off very fast. If you change how long it stays on in each round, an LED looks brighter or dimmer; if you change how many rounds happen per second, a buzzer sounds higher or lower.

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

This lab fades an LED with analogWrite(), sets its brightness with two buttons and plays tones with tone(). The buttons are read exactly as in Lab: Buttons, INPUT_PULLUP and Debouncing.

ئەم تاقیگەیە بە analogWrite() کز و گەش دەکات، بە دوو دوگمە ڕووناکییەکەی دیاری دەکات و بە tone() دەنگ لێ دەدات. دوگمەکان ڕێک وەک تاقیگە: دوگمەکانی پەستان، ⁦INPUT_PULLUP⁩ و لابردنی هەڵبەزینەوە دەخوێنرێنەوە.

Left: three LEDs glowing at 25, 50 and 100 percent above square waves whose HIGH parts get wider. Right: a round buzzer beside two square waves, 1000 Hz and 2000 Hz; the 2000 Hz wave has twice as many cycles.چەپ: سێ LED کە بە 25، 50 و 100 لە سەد دەدرەوشێنەوە لە سەرووی شەپۆلە چوارگۆشەییەکانەوە کە بەشی HIGHـیان پانتر دەبێت. ڕاست: بەزەرێکی خڕ لە تەنیشت دوو شەپۆلی چوارگۆشەییەوە، 1000 Hz و 2000 Hz؛ شەپۆلی 2000 Hz دوو هێندە خولی هەیە.
Wider HIGH parts: a brighter LED. More waves per second: a higher note.بەشی HIGHـی پانتر: گەشتر. شەپۆلی زیاتر لە چرکەیەکدا: دەنگێکی بەرزتر.CêIT

PWM: on and off, faster than the eyeمۆدیولەیشنی پانایی تڕپە: هەڵکردن و کوژاندنەوە، لە چاو خێراتر

PWM (pulse-width modulation) switches a pin fully on and fully off hundreds of times a second: 490 times on most UNO pins. The duty cycle is the share of each period the pin is HIGH. At 25 % the LED is on a quarter of the time; our eyes cannot follow the flashes and see a dim light. The motor or LED feels the average voltage:

پینێک لە چرکەیەکدا سەدان جار تەواو هەڵدەکات و تەواو دەیکوژێنێتەوە: لەسەر زۆربەی پینەکانی UNO، ⁦490⁩ جار. ئەو بەشەی هەر کە پینەکە تێیدا HIGHـە. لە ⁦25%⁩ـدا چارەکێکی کاتەکە هەڵکراوە؛ چاومان ناتوانێت بەدوای داگیرسانەکاندا بڕوات و ڕووناکییەکی کز دەبینێت. یان تێکڕای هەست پێ دەکات:

Vˉ=D×5 VD=25 %⇒Vˉ=1.25 V\bar{V} = D \times 5\,\text{V} \qquad D = 25\,\% \Rightarrow \bar{V} = 1.25\,\text{V}

The average voltage is the duty cycle times the HIGH voltage (5 V on an UNO, 3.3 V on 3.3 V boards).

تێکڕای دەکاتە جارانی ⁦HIGH (5 V⁩ لەسەر UNO، ⁦3.3 V⁩ لەسەر بۆردەکانی ⁦3.3 V⁩).

At 490 Hz one period lasts about 2 ms (period and frequency). A servo motor also reads pulses, but there the width of each pulse sets an angle: Pulse width sets the angle.

لە ⁦490 Hz⁩ـدا یەک نزیکەی ⁦2 ms⁩ دەخایەنێت (کاتی خولی و لەرەلەر). دەخوێنێتەوە، بەڵام لەوێ پانایی هەر گۆشەیەک دیاری دەکات: پانایی تڕپە گۆشەکە دیاری دەکات.

Three square waves with five periods each: 25, 50 and 75 percent, labelled value 64, 128 and 191. A dashed orange line shows each average: 1.25 V, 2.5 V and 3.75 V. LEDs on the right glow brighter for higher duty cycles.سێ شەپۆلی چوارگۆشەیی هەریەکە بە پێنج خول: 25، 50 و 75 لە سەد، بە بەهای 64، 128 و 191. هێڵێکی پچڕپچڕی پرتەقاڵی تێکڕای هەریەکە پیشان دەدات: 1.25 V، 2.5 V و 3.75 V. LEDـەکانی لای ڕاست بۆ ڕێژەی کارکردنی بەرزتر گەشتر دەدرەوشێنەوە.
Same frequency, different duty cycles: a different average.هەمان ، جیاواز: تێکڕای جیاواز.CêIT
The garden hoseشلنگی باخچە

Imagine a hose that can only be fully open or shut. Open it for one second in every four and the garden gets a quarter of the water, on average. PWM does the same with electricity, about 490 times a second.

شلنگێک وێنا بکە کە تەنیا دەتوانێت تەواو کراوە یان داخراو بێت. لە هەر چوار چرکەدا یەک چرکە بیکەرەوە، باخچەکە بە تێکڕا چارەکی ئاوەکە وەردەگرێت. هەمان کار بە کارەبا دەکات، نزیکەی ⁦490⁩ جار لە چرکەیەکدا.

Where the analogy stops workingسنووری ئەم نموونەیە

The LED never glows at half strength: it is fully on or off, and our eyes do the averaging.

هەرگیز بە نیوەی هێز ناگەشێتەوە: یان تەواو هەڵکراوە یان کوژاو و چاومان تێکڕاکە دەکات.

PWM: on and off, faster than the eyeمۆدیولەیشنی پانایی تڕپە: هەڵکردن و کوژاندنەوە، لە چاو خێراتر

Which pins can do PWM? The ~ pinsکام پینانە دەتوانن PWM بکەن؟ پینەکانی ~

PWM needs a hardware timer behind the pin, so only some pins have it. On the board they carry a ~ next to the number. The table shows the official pins and frequencies of the boards in this course, as of November 2026:

پێویستی بە ڕەقەواڵە لە پشت پینەکەوە هەیە، بۆیە تەنیا هەندێک پین هەیانە. لەسەر بۆردەکە ~یان لە تەنیشت ژمارەکەوەیە. خشتەکە پینە فەرمییەکان و بۆردەکانی ئەم کۆرسە پیشان دەدات، تا تشرینی دووەمی ⁦2026⁩:

BoardبۆردPWM pinsپینەکانی PWMFrequency
UNO R33, 5, 6, 9, 10, 11490 Hz (pins 5 and 6: 980 Hz)⁦490 Hz⁩ (پینەکانی 5 و ⁦6: 980 Hz⁩)
UNO R4 Minima / WiFi3, 5, 6, 9, 10, 11490 Hz
MKR boards, such as the MKR WAN 1310بۆردەکانی MKR، وەک ⁦MKR WAN 1310⁩0–8, 10, A3, A4732 Hz
The digital header of an UNO, pins 13 to 0. Pins ~11, ~10, ~9, ~6, ~5 and ~3 are outlined in orange. Below, a table: UNO R3 pins 3, 5, 6, 9, 10, 11 at 490 Hz (5 and 6 at 980 Hz); UNO R4 the same pins at 490 Hz; MKR boards pins 0 to 8, 10, A3, A4 at 732 Hz.سەرپینی دیجیتاڵی UNO، پینەکانی 13 تا 0. پینەکانی ~11، ~10، ~9، ~6، ~5 و ~3 بە پرتەقاڵی دەوری دراون. لە خوارەوە خشتەیەک: UNO R3 پینەکانی 3، 5، 6، 9، 10 و 11 بە 490 Hz (5 و 6 بە 980 Hz)؛ UNO R4 هەمان پینەکان بە 490 Hz؛ بۆردەکانی MKR پینەکانی 0 تا 8، 10، A3 و A4 بە 732 Hz.
Look for the ~ before you wire an LED you want to dim.پێش ئەوەی ببەستیت کە دەتەوێت کزی بکەیت، بەدوای ~دا بگەڕێ.CêIT

analogWrite(pin, value): 0 to 255⁦analogWrite(pin, value): 0⁩ تا ⁦255⁩

analogWrite(pin, value) starts PWM on a ~ pin. The value is the duty cycle: 0 is always off, 255 is always on, 64 is about 25 % and 128 about 50 %. Why 255? The classic boards use 8 bits, and 8 bits give 28=2562^8 = 256 values, 0 to 255 (How many patterns do N bits give?). The pin keeps pulsing until the next analogWrite(), digitalWrite() or digitalRead() on it.

analogWrite(pin, value) لەسەر پینێکی ~ دەست پێدەکات. بەهاکە : 0 هەمیشە کوژاوە، ⁦255⁩ هەمیشە هەڵکراو، ⁦64⁩ نزیکەی ⁦25%⁩ و ⁦128⁩ نزیکەی ⁦50%⁩. بۆچی ⁦255⁩؟ بۆردە کلاسیکییەکان 8 بەکار دەهێنن و 8 28=2562^8 = 256 بەها دەدات، 0 تا ⁦255⁩ (N بیت چەند نەخش دەدات؟). پینەکە بەردەوام تڕپە دەدات تا ⁦analogWrite()⁩، ⁦digitalWrite()⁩ یان ⁦digitalRead()⁩ـی داهاتوو لەسەری.

A number line from 0 to 255 with marks at 0, 64, 128, 191 and 255, labelled 0, 25, 50, 75 and 100 percent. Above each mark an LED glows a little brighter. Note: 256 steps, because 8 bits give 2⁸ = 256 values.هێڵێکی ژمارە لە 0 تا 255 بە نیشانە لە 0، 64، 128، 191 و 255، بە 0، 25، 50، 75 و 100 لە سەد. لە سەرووی هەر نیشانەیەکەوە LEDـێک کەمێک گەشتر دەدرەوشێتەوە. تێبینی: 256 پلە، چونکە 8 بیت 2⁸ = 256 بەها دەدات.
The number you write is the share of time the pin is HIGH.ئەو ژمارەیەی دەینووسیت بەشی ئەو کاتەیە کە پینەکە HIGHـە.CêIT

analogWrite(pin, value): 0 to 255⁦analogWrite(pin, value): 0⁩ تا ⁦255⁩

More steps: analogWriteResolution()پلەی زیاتر: ⁦analogWriteResolution()⁩

256 steps is the classic Arduino range. Newer boards can use finer steps: on the UNO R4, analogWriteResolution(12); makes analogWrite() take 0 to 4095, and the MKR boards' PWM pins can also go up to 12 bits. The UNO R3 always uses 8 bits. The UNO R4 and the MKR boards also have one true analog output on pin A0, a digital-to-analog converter (12-bit on the UNO R4, 10-bit on the MKR boards), which gives a steady voltage instead of pulses.

⁦256⁩ پلە ڕێگای کلاسیکی . بۆردە نوێیەکان دەتوانن پلەی وردتر بەکار بهێنن: لەسەر ⁦UNO R4⁩، analogWriteResolution(12); دەکات ⁦analogWrite()⁩ لە 0 تا ⁦4095⁩ وەربگرێت و پینەکانی PWMـی بۆردەکانی MKRـیش دەتوانن تا ⁦12⁩ بڕۆن. ⁦UNO R3⁩ هەمیشە 8 بەکار دەهێنێت. ⁦UNO R4⁩ و بۆردەکانی MKR هەروەها یەک ڕاستەقینەیان لەسەر پینی ⁦A0⁩ هەیە، (⁦12⁩ لەسەر ⁦UNO R4⁩، ⁦10⁩ لەسەر بۆردەکانی MKR)، کە جێگیر دەدات لە جیاتی تڕپە.

Two staircases climbing the same height. Left, 8 bits, 0 to 255: big steps (256 steps, every board). Right, 12 bits, 0 to 4095: many small steps (4096 steps, UNO R4 and MKR).دوو پلیکانە کە بۆ هەمان بەرزی سەردەکەون. چەپ، 8 بیت، 0 تا 255: پلەی گەورە (256 پلە، هەموو بۆردێک). ڕاست، 12 بیت، 0 تا 4095: پلەی بچووکی زۆر (4096 پلە، UNO R4 و MKR).
Same range of brightness, finer control.هەمان سنووری ڕووناکی، کۆنترۆڵی وردتر.CêIT

Lab 4: fade an LED up and downتاقیگەی 4: LEDـێک گەش و کز بکە

Wire an LED with its resistor to pin 3, a ~ pin, as in the LED lab. The sketch climbs from 0 to 255 in steps of 1, waiting 10 ms at each step, then walks back down: 256×10 ms≈2.56 s256 \times 10\,\text{ms} \approx 2.56\,\text{s} each way, about 5 seconds for a full breath.

بە بە پینی 3ـەوە ببەستە، کە پینێکی ~ـە، وەک تاقیگەی . بە پلەی 1 لە 0ـەوە بۆ ⁦255⁩ سەردەکەوێت، لە هەر پلەیەکدا ⁦10 ms⁩ چاوەڕێ دەکات، پاشان بۆ خوارەوە دەگەڕێتەوە: 256×10 ms≈2.56 s256 \times 10\,\text{ms} \approx 2.56\,\text{s} بۆ هەر لایەک، نزیکەی 5 چرکە بۆ هەناسەیەکی تەواو.

Lab 4: fade the LED on pin 3تاقیگەی 4: کزکردن و گەشکردنی LEDـی پینی 3fade.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
const int ledPin = 3;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  for (int brightness = 0; brightness <= 255; brightness++) {
    analogWrite(ledPin, brightness);
    delay(10);
  }

  for (int brightness = 255; brightness >= 0; brightness--) {
    analogWrite(ledPin, brightness);
    delay(10);
  }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 8-11

Up: brightness 0, 1, 2 … 255, one step every 10 ms.

سەرکەوتن: ڕووناکی 0، 1، ⁦2 … 255⁩، هەر ⁦10 ms⁩ پلەیەک.

Lineدێڕی 13-16

Down: 255 … 0, then loop() starts again.

دابەزین: ⁦255 … 0⁩، پاشان ⁦loop()⁩ دیسان دەست پێدەکاتەوە.

Animation. A square wave on pin 3 whose HIGH part grows from 0 to 25, 50, 75 and 100 percent and back to 50 and 0, with the code analogWrite(3, value) below; an LED on the right brightens and dims with it.
Animation. A square wave on pin 3 whose HIGH part grows from 0 to 25, 50, 75 and 100 percent and back to 50 and 0, with the code analogWrite(3, value) below; an LED on the right brightens and dims with it.
The wider the HIGH part, the brighter the LED.بەشی HIGH تا پانتر بێت، گەشترە.CêIT

Lab 5: brightness buttonsتاقیگەی 5: دوگمەکانی ڕووناکی

Two buttons with INPUT_PULLUP, UP on pin 2 and DOWN on pin 4, change a number from 0 to 255 in steps of 5, and analogWrite() sends it to the LED on pin 3. Holding a button repeats the step every 200 ms, like the volume button of a TV remote. This is the corrected sketch:

دوو دوگمە بە ⁦INPUT_PULLUP⁩، سەرەوە لەسەر پینی 2 و خوارەوە لەسەر پینی 4، ژمارەیەک لە 0 تا ⁦255⁩ بە پلەی 5 دەگۆڕن و ⁦analogWrite()⁩ دەینێرێت بۆ پینی 3. گرتنی دوگمەیەک هەر ⁦200 ms⁩ جارێک پلەکە دووپاتە دەکاتەوە، وەک دوگمەی دەنگی ڕیمۆتی تەلەفزیۆن. ئەمە چاککراوەکەیە:

Lab 5, corrected: constrain() and analogWrite()تاقیگەی 5، چاککراو: ⁦constrain()⁩ و ⁦analogWrite()⁩bright_fixed.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Lab 5: brightness up and down with two buttons
const int upBtnPin = 2;
const int downBtnPin = 4;
const int ledPin = 3;                 // a ~ (PWM) pin

int brightness = 128;                 // start at half

void setup() {
  pinMode(upBtnPin, INPUT_PULLUP);
  pinMode(downBtnPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  analogWrite(ledPin, brightness);
}

void loop() {
  if (digitalRead(upBtnPin) == LOW) {
    brightness = brightness + 5;
    brightness = constrain(brightness, 0, 255);   // never above 255
    analogWrite(ledPin, brightness);              // the LED follows the number
    Serial.print("Brightness: ");
    Serial.println(brightness);
    delay(200);                                   // held: one step every 200 ms
  }

  if (digitalRead(downBtnPin) == LOW) {
    brightness = brightness - 5;
    brightness = constrain(brightness, 0, 255);   // never below 0
    analogWrite(ledPin, brightness);
    Serial.print("Brightness: ");
    Serial.println(brightness);
    delay(200);
  }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 19

Never above 255, whatever the sum was.

هەرگیز لە ⁦255⁩ زیاتر نا، کۆکردنەوەکە هەرچی بێت.

Lineدێڕی 20

The LED follows the number: the line the first version forgot.

بەدوای ژمارەکەدا دەڕوات: ئەو دێڕەی یەکەم وەشان لەبیری کرد.

Lineدێڕی 23

Held: one step every 200 ms.

گیراو: هەر ⁦200 ms⁩ پلەیەک.

An Arduino UNO box with three pins: pin 2 (INPUT_PULLUP) to an UP +5 button and then ground, pin 4 (INPUT_PULLUP) to a DOWN −5 button and ground, and ~3 (PWM out) through a 330 Ω resistor to an LED and ground. The LED starts at 128 (50 %).سندووقێکی Arduino UNO بە سێ پینەوە: پینی 2 (INPUT_PULLUP) بۆ دوگمەی سەرەوە +5 و پاشان زەوی، پینی 4 (INPUT_PULLUP) بۆ دوگمەی خوارەوە −5 و زەوی؛ ~3 (دەرچووی PWM) لە ڕێی بەرگرێکی 330 Ωـەوە بۆ LEDـێک و زەوی. LEDـەکە لە 128 (50%) دەست پێدەکات.
Two buttons in, one PWM pin out.دوو دوگمە بۆ ناوەوە، یەک پینی PWM بۆ دەرەوە.CêIT

Lab 5: brightness buttonsتاقیگەی 5: دوگمەکانی ڕووناکی

Common mistake: the number changes, the LED does notهەڵەی باو: ژمارەکە دەگۆڕێت، LEDـەکە ناگۆڕێت

Left: a Serial Monitor panel printing Brightness: 133, 138, 143, 148, 153 and 158. Right: a dashed line with a question mark leads to an unlit LED, labelled pin 3 never changes.چەپ: پانێڵێکی Serial Monitor کە Brightness: 133، 138، 143، 148، 153 و 158 دەنووسێت. ڕاست: هێڵێکی پچڕپچڕ بە نیشانەی پرسیارەوە دەچێت بۆ LEDـێکی کوژاو، بە ناوی پینی 3 هەرگیز ناگۆڕێت.
Printing a number is not the same as setting the pin.نووسینی ژمارەیەک هەمان ڕێکخستنی پینەکە نییە.CêIT

Lab 5: brightness buttonsتاقیگەی 5: دوگمەکانی ڕووناکی

Common mistake: 258 and −2هەڵەی باو: ⁦258⁩ و ⁦−2⁩

This little test shows what an 8-bit register keeps of each number; it is the same overflow as in When the answer does not fit:

ئەم تاقیکردنەوە بچووکە پیشان دەدات تۆمارێکی 8 چی لە هەر ژمارەیەک دەهێڵێتەوە؛ هەمان وەک کاتێک وەڵامەکە جێی نابێتەوە:

What an 8-bit register keepsتۆمارێکی 8 بیتی چی دەهێڵێتەوەbyte_wrap.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// What an 8-bit register keeps of a number
void setup() {
  Serial.begin(9600);
  Serial.println((byte)253);   // fits: 253
  Serial.println((byte)258);   // too big: 258 - 256
  Serial.println((byte)3);     // fits: 3
  Serial.println((byte)-2);    // below zero: 256 - 2
}

void loop() {
}
A number line with the valid range 0 to 255 shaded. Outside it, 258 on the right and −2 on the left, in orange. A dashed arrow carries 258 over the top to 2: UNO R3 keeps 8 bits, almost off. Another carries −2 under the line to 254: almost full. Below: constrain(258, 0, 255) gives 255 and constrain(-2, 0, 255) gives 0.هێڵێکی ژمارە کە سنووری دروستی 0 تا 255ـی ڕەنگ کراوە. لە دەرەوەی، 258 لە ڕاست و −2 لە چەپ، بە پرتەقاڵی. تیرێکی پچڕپچڕ 258 لە سەرەوە دەباتە 2: UNO R3 تەنیا 8 بیت دەهێڵێتەوە، نزیکەی کوژاو. یەکێکی تر −2 لە ژێر هێڵەکەوە دەباتە 254: نزیکەی تەواو. لە خوارەوە: constrain(258, 0, 255) 255 دەدات و constrain(-2, 0, 255) 0 دەدات.
Wrap-around on the UNO R3, and the cure.سووڕانەوە لەسەر ⁦UNO R3⁩ و چارەسەرەکەی.CêIT

Lab 5: brightness buttonsتاقیگەی 5: دوگمەکانی ڕووناکی

constrain(): keep a number inside its range⁦constrain()⁩: ژمارەیەک لەناو سنوورەکەیدا بهێڵەوە

constrain(x, a, b) returns x when it lies between a and b, a when x is smaller and b when x is larger. Do the sum first and constrain the result in its own line: Arduino's reference warns against putting other function calls inside constrain().

constrain(x, a, b) کاتێک x لە نێوان a و bـدا بێت x دەگەڕێنێتەوە، کاتێک x بچووکتر بێت a و کاتێک گەورەتر بێت b. سەرەتا کۆکردنەوەکە بکە و ئەنجامەکە لە دێڕێکی جیادا constrain بکە: سەرچاوەی ئاگادار دەکاتەوە کە بانگکردنی تر نەخرێتە ناو ⁦constrain()⁩ـەوە.

xconstrain(x, 0, 255)Whyبۆچی
258255above the range: the topسەرووی سنوورەکە: بەرزترین بەها
130130inside: unchangedلەناویدا: نەگۆڕاو
-20below the range: the bottomخوارووی سنوورەکە: نزمترین بەها

The buzzer: the simplest way to make a soundبەزەر: سادەترین ڕێگا بۆ دروستکردنی دەنگ

A buzzer is the easiest and cheapest way to add sound to a project: alarms, bells, sirens, even simple music. It runs on a low DC voltage. You find buzzers in timers, alarm devices, printers and computers, and they confirm what you did: a beep for a key press or a mouse click.

ئاسانترین و هەرزانترین ڕێگایە بۆ زیادکردنی دەنگ بۆ پڕۆژەیەک: ئاگادارکەرەوە، زەنگ، سیرەن، تەنانەت مۆسیقای سادە. بە DCـی نزم کار دەکات. لە ، ئامێرەکانی ئاگادارکردنەوە، و دەبینیت و ئەوەی کردووتە پشتڕاستی دەکەنەوە: بیپێک بۆ پەستانی دوگمەیەکی یان کرتەیەکی .

It has two legs. The positive one, marked + or simply the longer leg, goes to the board's pin; the negative one, marked − or the shorter leg, goes to GND.

دوو قاچی هەیە. قاچی پۆزەتیڤ، کە + ـی لەسەرە یان تەنیا قاچە درێژەکەیە، بۆ پینی بۆردەکە دەچێت؛ قاچی نێگەتیڤ، کە − ـی لەسەرە یان قاچە کورتەکەیە، بۆ GND دەچێت.

A round black buzzer seen from above with a + mark near the edge and a small sound hole in the middle; beside it, a side view with two legs: the longer + leg goes to the pin, the shorter − leg to GND.بەزەرێکی خڕی ڕەش لە سەرەوە بە نیشانەی + لە نزیک لێوارەکەی و کونێکی بچووکی دەنگ لە ناوەڕاستدا؛ لە تەنیشتیەوە، وێنەیەک لە لاوە بە دوو قاچ: قاچی درێژی + بۆ پین دەچێت، قاچی کورتی − بۆ GND.
Long leg + to the pin, short leg − to GND.قاچی درێژی + بۆ پین، قاچی کورتی − بۆ GND.CêIT

The buzzer: the simplest way to make a soundبەزەر: سادەترین ڕێگا بۆ دروستکردنی دەنگ

Active or passive buzzer?بەزەری چالاک یان ناچالاک؟

Buzzers come in two kinds. An active buzzer has its own little oscillator inside: give it a steady DC voltage and it beeps at one fixed pitch, so digitalWrite() is enough. A passive buzzer is only the sound disc: it needs a changing signal, and tone() makes the square wave, so it can play any pitch. Only a passive one can play 1000 Hz and then 2000 Hz, a siren or a tune. Quick test: on a steady 5 V an active buzzer beeps; a passive one only clicks.

دوو جۆرە. چالاک لەرزێنەرێکی بچووکی خۆی لەناودایە: DCـی جێگیری بدەیتێ بە یەک دەنگی جێگیر بیپ دەکات، بۆیە ⁦digitalWrite()⁩ بەسە. ناچالاک تەنیا قرسی دەنگەکەیە: پێویستی بە گۆڕاو هەیە و tone() دروست دەکات، بۆیە دەتوانێت هەر دەنگێک لێ بدات. تەنیا جۆری ناچالاک دەتوانێت ⁦1000 Hz⁩ و پاشان ⁦2000 Hz⁩، سیرەنێک یان ئاوازێک لێ بدات. تاقیکردنەوەی خێرا: لەسەر ⁦5 V⁩ـی جێگیر چالاک بیپ دەکات؛ ناچالاک تەنیا کرتەیەک دەکات.

An active buzzer needs only digitalWrite()بەزەری چالاک تەنیا ⁦digitalWrite()⁩ـی پێویستەactive_beep.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// An ACTIVE buzzer on pin 4: HIGH = beep, LOW = quiet
const int buzzerPin = 4;

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  digitalWrite(buzzerPin, HIGH);   // its own oscillator makes the sound
  delay(200);
  digitalWrite(buzzerPin, LOW);
  delay(800);
}
Two panels. Active: a steady HIGH line goes into a box with an oscillator symbol and a sound disc; one fixed pitch; digitalWrite(4, HIGH);. Passive: a square wave goes into a box with only the disc; any pitch tone() asks for; tone(4, 1000);. Notes: steady DC gives a beep, or only a click.دوو پانێڵ. چالاک: هێڵێکی HIGHـی جێگیر دەچێتە ناو سندووقێک بە هێمای لەرزێنەر و قرسی دەنگ؛ یەک دەنگی جێگیر؛ digitalWrite(4, HIGH);. ناچالاک: شەپۆلی چوارگۆشەییەک دەچێتە ناو سندووقێک کە تەنیا قرسەکەی تێدایە؛ هەر دەنگێک tone() بیەوێت؛ tone(4, 1000);. تێبینی: تەزووی جێگیر بیپ دەدات، یان تەنیا کرتەیەک.
The oscillator decides: built in (active) or made by tone() (passive).لەرزێنەرەکە بڕیار دەدات: ناوخۆیی (چالاک) یان ⁦tone()⁩ دروستی دەکات (ناچالاک).CêIT

The buzzer: the simplest way to make a soundبەزەر: سادەترین ڕێگا بۆ دروستکردنی دەنگ

Inside: piezo or magneticلە ناوەوە: پیزۆ یان موگناتیسی

A piezo buzzer holds a thin ceramic disc glued to a metal plate; the disc bends a little when voltage is put across it, and the bending makes the sound. It needs very little current. A magnetic buzzer works like a tiny speaker: a coil pulls a metal disc. It runs on lower voltages but draws more current, often over 20 mA.

پیزۆ قرسێکی سێرامیکیی تەنکی هەیە کە بە پلێتێکی کانزاییەوە نووسێنراوە؛ کاتێک دەخرێتە سەری قرسەکە کەمێک دەچەمێتەوە و ئەم چەمانەوەیە دەنگەکە دروست دەکات. زۆر کەمی پێویستە. موگناتیسی وەک بچووک کار دەکات: کۆیلێک قرسێکی کانزایی ڕادەکێشێت. بە نزمتر کار دەکات، بەڵام زیاتر دەکێشێت، زۆرجار زیاتر لە ⁦20 mA⁩.

Where buzzers beepبەزەر لە کوێ بیپ دەکات

  • cars: the seat belt, reverse gear or an open door;
  • alarm circuits: a loud noise for smoke, fire or theft;
  • annunciator panels: a sound that tells operators about a fault;
  • communication devices: beeps for keys and calls on phones and radios;
  • electronic metronomes: regular ticks that keep musicians in time;
  • game shows: a beep when a player presses the button;
  • household appliances: the microwave or the washing machine is done;
  • portable devices: calculators and hand tools;
  • security systems: a noise when an intruder is detected;
  • sport: the start and the end of a match or a round;
  • timers: ovens and stopwatches beep when time is up.
  • ئۆتۆمبێل: پشتێنی کورسی، گێڕی پاشەوە یان دەرگایەکی کراوە؛
  • ئاگادارکردنەوە: دەنگێکی بەرز بۆ دووکەڵ، ئاگر یان دزی؛
  • تابلۆکانی ئاگادارکردنەوە: دەنگێک کە کارمەندان لە هەڵەیەک ئاگادار دەکاتەوە؛
  • : بیپ بۆ دوگمە و پەیوەندی لە مۆبایل و ڕادیۆدا؛
  • مێترۆنۆمی ئەلیکترۆنی: لێدانی ڕێک کە مۆسیقاژەنەکان لەسەر ڕیتم دەهێڵێتەوە؛
  • پێشبڕکێی تەلەفزیۆنی: بیپێک کاتێک یاریزانێک دوگمەکە دەپەستێنێت؛
  • ئامێرەکانی ماڵ: مایکرۆوەیڤ یان جلشۆر تەواو بووە؛
  • ئامێرە هەڵگرەکان: و ئامرازی دەستی؛
  • سیستەمەکانی پاراستن: دەنگێک کاتێک دزێک دەدۆزرێتەوە؛
  • وەرزش: دەستپێک و کۆتایی یارییەک یان گەڕێک؛
  • : فڕن و کاتژمێری وەرزشی کاتێک کات تەواو دەبێت بیپ دەکەن.

A metronome ticks or flashes at a steady beat chosen in BPM, beats per minute: 60 BPM is one beat a second, 120 BPM two beats a second. In a sketch the time between beats is 60000÷BPM60000 \div \text{BPM} milliseconds (an exercise below).

مێترۆنۆم بە ڕیتمێکی جێگیر لێدەدات یان دەدرەوشێتەوە کە بە BPM، لێدان لە خولەکێکدا، هەڵدەبژێردرێت: ⁦60 BPM⁩ واتە یەک لێدان لە چرکەیەکدا، ⁦120 BPM⁩ دوو لێدان لە چرکەیەکدا. لە کاتی نێوان دوو لێدان 60000÷BPM60000 \div \text{BPM} میلیچرکەیە (ڕاهێنانێک لە خوارەوە).

Eleven tiles with simple icons: a car (seat belt, reverse), a bell (smoke, fire, theft), a control panel (fault), a phone (keys and calls), a metronome (the beat), a red game-show button, a microwave (done), a calculator (hand tools), a shield (intruder alarm), a whistle (start and end of a match) and a timer (oven timer, stopwatch).یازدە خانە بە وێنۆچکەی سادە: ئۆتۆمبێل (پشتێنی کورسی، پاشەوە)، زەنگ (دووکەڵ، ئاگر، دزی)، تابلۆی کۆنترۆڵ (هەڵە)، مۆبایل (دوگمە و پەیوەندی)، مێترۆنۆم (ڕیتم)، دوگمەیەکی سووری پێشبڕکێ، مایکرۆوەیڤ (تەواو)، حاسیبە (ئامرازی دەستی)، قەڵغان (ئاگادارکەرەوەی دزی)، فیکە (دەستپێک و کۆتایی یاری) و تایمەر (تایمەری فڕن، کاتژمێری وەرزشی).
One small part, eleven jobs.یەک پارچەی بچووک، یازدە کار.CêIT

tone() and noTone(): a square wave of any pitch⁦tone()⁩ و ⁦noTone()⁩: شەپۆلی چوارگۆشەییەک بە هەر بەرزییەک

tone(pin, frequency) makes a square wave with a 50 % duty cycle on any digital pin and keeps it going until noTone(pin). tone(pin, frequency, duration) stops by itself after duration milliseconds. The frequency is the number of waves per second, in hertz (Hz): a higher frequency sounds higher. 2000 Hz is one octave above 1000 Hz, because it has twice the frequency.

tone(pin, frequency) لەسەر هەر پینێکی بە ⁦50%⁩ دروست دەکات و بەردەوامی دەکات تا noTone(pin). tone(pin, frequency, duration) دوای duration میلیچرکە خۆی دەوەستێت. ژمارەی شەپۆلەکانە لە چرکەیەکدا، بە ⁦(Hz)⁩: بەرزتر دەنگی بەرزتر دەدات. ⁦2000 Hz⁩ ئۆکتاڤێک لە سەرووی ⁦1000 Hz⁩ـەوەیە، چونکە دوو هێندەیە.

Two square waves over 3 ms. 1000 Hz in blue: three periods, one period marked 1 ms. 2000 Hz in orange: six periods, one marked 0.5 ms. Note: both are square waves with 50 % duty cycle.دوو شەپۆلی چوارگۆشەیی لە 3 msـدا. 1000 Hz بە شین: سێ خول، یەک کاتی خولی بە 1 ms نیشان دراوە. 2000 Hz بە پرتەقاڵی: شەش خول، یەکێکیان بە 0.5 ms نیشان دراوە. تێبینی: هەردووکیان شەپۆلی چوارگۆشەیین بە ڕێژەی کارکردنی 50%.
Twice the frequency, half the period, a higher note.دوو هێندە ، نیوەی ، دەنگێکی بەرزتر.CêIT

Lab: two tones and a pause on the MKR WAN 1310تاقیگە: دوو دەنگ و وەستانێک لەسەر ⁦MKR WAN 1310⁩

  1. Wireببەستە

    Connect the buzzer's positive (red) wire to pin 4 and the negative (black) wire to GND.

    وایەری پۆزەتیڤی (سوور) بە پینی 4 و وایەری نێگەتیڤی (ڕەش) بە GNDـەوە ببەستە.

  2. Uploadهەڵبارکردن

    Upload the sketch to the MKR WAN 1310 from the Arduino IDE (Tools → Board, Port).

    لە Arduinoـەوە بۆ ⁦MKR WAN 1310⁩ هەڵبار بکە (Tools → Board، Port).

  3. Listenگوێ بگرە

    Listen: 1000 Hz for a second, 2000 Hz for a second, one second of silence, again and again.

    گوێ بگرە: یەک چرکە ⁦1000 Hz⁩، یەک چرکە ⁦2000 Hz⁩، یەک چرکە بێدەنگی، دووپاتە و دووپاتە.

Two tones and a pause (buzzer on pin 4)دوو دەنگ و وەستانێک (بەزەر لەسەر پینی ⁦4⁩)tone_lab.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Define the digital pin connected to the buzzer
int buzzerPin = 4;

void setup() {
  // Set the buzzer pin as an output
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  // Play a tone with a frequency of 1000 Hz for 1 second
  tone(buzzerPin, 1000);
  delay(1000);

  // Play a tone with a frequency of 2000 Hz for 1 second
  tone(buzzerPin, 2000);
  delay(1000);

  // Stop the buzzer
  noTone(buzzerPin);
  // Wait for 1 second before repeating the loop
  delay(1000);
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 11-12

1000 Hz, and wait 1 s while it plays.

⁦1000 Hz⁩ و 1 چرکە چاوەڕێ تا لێدەدرێت.

Lineدێڕی 15-16

The same pin again: tone() just changes the pitch.

دیسان هەمان پین: ⁦tone()⁩ تەنیا دەنگەکە دەگۆڕێت.

Lineدێڕی 19-21

Silence for 1 s.

1 چرکە بێدەنگی.

It needs a passive buzzer, because it changes the pitch. The MKR WAN 1310 is a 3.3 V board, which is fine for a small piezo; the same sketch runs unchanged on an UNO.

پێویستی بە ناچالاک هەیە، چونکە دەنگەکە دەگۆڕێت. ⁦MKR WAN 1310⁩ بۆردێکی ⁦3.3 V⁩ـە، کە بۆ پیزۆیەکی بچووک باشە؛ هەمان بەبێ گۆڕان لەسەر UNOـش کار دەکات.

A time strip from 0 to 6 s: blue 1000 Hz from 0 to 1 s, orange 2000 Hz from 1 to 2 s, grey silence from 2 to 3 s, and the same again from 3 to 6 s. Below: tone(4, 1000) · tone(4, 2000) · noTone(4).شریتێکی کات لە 0 تا 6 چرکە: شینی 1000 Hz لە 0 تا 1 چرکە، پرتەقاڵیی 2000 Hz لە 1 تا 2 چرکە، بێدەنگیی خۆڵەمێشی لە 2 تا 3 چرکە و هەمان شت دیسان لە 3 تا 6 چرکە. لە خوارەوە: tone(4, 1000) · tone(4, 2000) · noTone(4).
A 3-second pattern that repeats for ever.نەخشەیەکی 3 چرکەیی کە بۆ هەمیشە دووپاتە دەبێتەوە.CêIT

What tone() cannot do⁦tone()⁩ چی ناتوانێت بکات

  • Only one tone at a time. A tone() on another pin does nothing while a tone plays; a tone() on the same pin only changes the pitch. To move to another pin, call noTone() first.
  • Nothing below 31 Hz.
  • On boards other than the Mega, tone() disturbs PWM on pins 3 and 11. On the UNO R3 the reason is visible in its core: tone() borrows Timer 2, the timer that makes the PWM of pins 3 and 11. Fading an LED on pin 3 while a buzzer plays does not work; move the LED to pin 9 or 10.
  • تەنیا یەک دەنگ لە یەک کاتدا. تا دەنگێک لێدەدرێت، ⁦tone()⁩ لەسەر پینێکی تر هیچ ناکات؛ ⁦tone()⁩ لەسەر هەمان پین تەنیا دەنگەکە دەگۆڕێت. بۆ چوون بۆ پینێکی تر، سەرەتا ⁦noTone()⁩ بانگ بکە.
  • هیچ شتێک لە خوارووی ⁦31 Hz⁩ نا.
  • لەسەر بۆردەکان جگە لە Mega، ⁦tone() PWM⁩ـی پینەکانی 3 و ⁦11⁩ تێک دەدات. لەسەر ⁦UNO R3⁩ هۆکارەکە لە ناوەکیی دیارە: ⁦tone()⁩ تایمەری 2 دەبات، ئەو PWMـی پینەکانی 3 و ⁦11⁩ دروست دەکات. کزکردنی لەسەر پینی 3 تا لێدەدات کار ناکات؛ بۆ پینی 9 یان ⁦10⁩ بگوازەوە.
Three rows for the UNO R3. Timer 0 drives millis() and delay() and PWM on ~5 and ~6. Timer 1 drives the Servo library and PWM on ~9 and ~10. Timer 2, in orange, drives tone() and PWM on ~3 and ~11. Note: while a tone plays, analogWrite() on pins 3 and 11 does not work as expected.سێ ڕیز بۆ UNO R3. تایمەری 0 millis() و delay() و PWMـی ~5 و ~6 ڕادەپەڕێنێت. تایمەری 1 کتێبخانەی Servo و PWMـی ~9 و ~10. تایمەری 2، بە پرتەقاڵی، tone() و PWMـی ~3 و ~11. تێبینی: تا دەنگێک لێدەدرێت، analogWrite() لەسەر پینەکانی 3 و 11 وەک پێویست کار ناکات.
Three timers, shared jobs: tone() and pins 3 and 11 cannot both have Timer 2.سێ ، کاری هاوبەش: ⁦tone()⁩ و پینەکانی 3 و ⁦11⁩ ناتوانن هەردووکیان تایمەری 2یان هەبێت.CêIT

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

MistakeهەڵەBetterباشتر
analogWrite() on a pin without ~: the LED only switches on or off.⁦analogWrite()⁩ لەسەر پینێک بەبێ ~: تەنیا هەڵ یان دەکوژێتەوە.Use a PWM pin: 3, 5, 6, 9, 10 or 11 on an UNO.پینێکی PWM بەکار بهێنە: 3، 5، 6، 9، ⁦10⁩ یان ⁦11⁩ لەسەر UNO.
Changing the variable but never calling analogWrite().گۆڕینی بەبێ بانگکردنی ⁦analogWrite()⁩.Write the new value to the pin after every change.دوای هەر گۆڕانێک بەها نوێیەکە بۆ پینەکە بنووسە.
Checking the limit before adding: 258 and −2.پشکنینی سنوور پێش زیادکردن: ⁦258⁩ و ⁦−2⁩.Add, then constrain(value, 0, 255).زیاد بکە، پاشان constrain(value, 0, 255).
A function call inside constrain().بانگکردنی لەناو ⁦constrain()⁩ـدا.Compute first, then constrain the variable.سەرەتا هەژمار بکە، پاشان constrain بکە.
tone() on an active buzzer: always the same beep.⁦tone()⁩ لەسەر چالاک: هەمیشە هەمان بیپ.Use a passive buzzer for different pitches.بۆ دەنگی جیاواز ناچالاک بەکار بهێنە.
A big magnetic buzzer straight on a pin. موگناتیسیی گەورە ڕاستەوخۆ لەسەر پینێک.Use a transistor or a buzzer module. یان مۆدیولی بەکار بهێنە.
Two tones on two pins at the same time.دوو دەنگ لەسەر دوو پین لە یەک کاتدا.One tone at a time; noTone() before moving pins.یەک دەنگ لە یەک کاتدا؛ پێش گواستنەوەی پین ⁦noTone()⁩.
Fading on pin 3 or 11 of an UNO R3 while tone() plays.کزکردن لەسەر پینی 3 یان ⁦11⁩ـی ⁦UNO R3⁩ تا ⁦tone()⁩ لێدەدات.Use pin 9 or 10 for the LED.بۆ پینی 9 یان ⁦10⁩ بەکار بهێنە.
Expecting tone() to wait for its duration.چاوەڕوانی ئەوە کە ⁦tone()⁩ چاوەڕێی duration بکات.Add a delay() after each note.دوای هەر نۆتەیەک ⁦delay()⁩ زیاد بکە.

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

ExerciseڕاهێنانA fade with one variableکزکردن و گەشکردن بە یەک گۆڕاو

Rewrite Lab 4 without for loops: one variable brightness, a step of +5 that turns into −5 at the ends, and 30 ms between steps.

تاقیگەی 4 بەبێ دووپاتە بنووسەوە: یەک brightness، پلەیەکی ⁦+5⁩ کە لە کۆتاییەکاندا دەبێتە ⁦−5⁩ و ⁦30 ms⁩ لە نێوان پلەکاندا.

Hintئاماژە

When brightness reaches 0 or 255, set step = -step.

کاتێک brightness دەگاتە 0 یان ⁦255⁩، ⁦step = -step⁩.

Show answerوەڵامەکە پیشان بدە
Answer: the official Fade ideaوەڵام: بیرۆکەی Fadeـی فەرمیfade_one.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// The fade with one variable and no for loops
const int ledPin = 3;
int brightness = 0;
int step = 5;                  // +5 going up, -5 going down

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  analogWrite(ledPin, brightness);
  brightness = brightness + step;
  if (brightness <= 0 || brightness >= 255) {
    step = -step;              // turn round at the ends
  }
  delay(30);
}

loop() now stays short, so it could also read buttons between steps.

ئێستا ⁦loop()⁩ کورت دەمێنێتەوە، بۆیە دەتوانێت لە نێوان پلەکاندا دوگمەش بخوێنێتەوە.

ExerciseڕاهێنانA metronomeمێترۆنۆمێک

Make a passive buzzer click at 120 BPM: a 50 ms click at 1500 Hz on every beat.

ناچالاک بە ⁦120 BPM⁩ لێ بدە: کرتەیەکی ⁦50 ms⁩ بە ⁦1500 Hz⁩ لە هەر لێدانێکدا.

Hintئاماژە

tone() with a duration returns at once; delay() sets the beat.

⁦tone()⁩ بە duration یەکسەر دەگەڕێتەوە؛ ⁦delay()⁩ ڕیتمەکە دیاری دەکات.

Show answerوەڵامەکە پیشان بدە
Answer: 60000 ÷ 120 = 500 ms per beatوەڵام: ⁦60000 ÷ 120 = 500 ms⁩ بۆ هەر لێدانێکmetronome.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// A metronome: a short click at 120 beats per minute
const int buzzerPin = 4;
const int bpm = 120;
const unsigned long beat = 60000UL / bpm;   // 500 ms between beats

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  tone(buzzerPin, 1500, 50);   // a 50 ms click; tone() returns at once
  delay(beat);                 // so delay() sets the time between beats
}
ExerciseڕاهێنانA two-tone sirenسیرەنێکی دوو دەنگ

Make the buzzer alternate between 700 Hz and 1000 Hz, half a second each, for ever.

بە نۆرە لە نێوان ⁦700 Hz⁩ و ⁦1000 Hz⁩ـدا لێ بدە، هەریەکە نیو چرکە، بۆ هەمیشە.

Show answerوەڵامەکە پیشان بدە
Answer: one pin, two pitchesوەڵام: یەک پین، دوو دەنگsiren.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// A two-tone siren: 700 Hz and 1000 Hz, half a second each
const int buzzerPin = 4;

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  tone(buzzerPin, 700);
  delay(500);
  tone(buzzerPin, 1000);    // same pin: tone() just changes the pitch
  delay(500);
}
ExerciseڕاهێنانRead the tableخشتەکە بخوێنەوە

An MKR WAN 1310 must dim an LED. Pin 9 or pin 5? And at what PWM frequency?

⁦MKR WAN 1310⁩ دەبێت کز بکات. پینی 9 یان پینی 5؟ و بە چ PWM؟

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

Pin 5: the MKR boards have PWM on 0–8, 10, A3 and A4, so pin 9 has none. Their PWM runs at 732 Hz.

پینی 5: بۆردەکانی MKR لەسەر ⁦0–8⁩، ⁦10⁩، ⁦A3⁩ و ⁦A4 PWM⁩ـیان هەیە، بۆیە پینی ⁦9 PWM⁩ـی نییە. PWMـەکەیان بە ⁦732 Hz⁩ کار دەکات.

Summaryپوختە

  • PWM switches a pin fully on and off very fast; the duty cycle (% of each period HIGH) sets the average voltage and the brightness.
  • Only ~ pins do PWM: 3, 5, 6, 9, 10, 11 on the UNO R3 and R4 (490 Hz; 980 Hz on pins 5 and 6 of the R3); 0–8, 10, A3, A4 on the MKR boards (732 Hz).
  • analogWrite(pin, 0…255): 8 bits; the UNO R4 can use analogWriteResolution(12) for 0…4095.
  • Keep the value in range with constrain(); past 255 an UNO R3 wraps around (258 → 2).
  • An active buzzer beeps on DC at one pitch; a passive buzzer needs tone(), which can play any pitch from 31 Hz up.
  • tone() plays one tone at a time, returns at once and, on boards other than the Mega, disturbs PWM on pins 3 and 11.
  • پینێک زۆر بە خێرایی تەواو هەڵدەکات و دەیکوژێنێتەوە؛ (ئەو %ـەی هەر خولێک کە HIGHـە) تێکڕای و ڕووناکی دیاری دەکات.
  • تەنیا پینەکانی ~ PWM دەکەن: 3، 5، 6، 9، ⁦10⁩ و ⁦11⁩ لەسەر ⁦UNO R3⁩ و ⁦R4 (490 Hz⁩؛ ⁦980 Hz⁩ لەسەر پینەکانی 5 و 6ـی ⁦R3⁩)؛ ⁦0–8⁩، ⁦10⁩، ⁦A3⁩ و ⁦A4⁩ لەسەر بۆردەکانی ⁦MKR (732 Hz)⁩.
  • ⁦analogWrite(pin, 0…255): 8⁩ ؛ ⁦UNO R4⁩ دەتوانێت ⁦analogWriteResolution(12)⁩ بۆ ⁦0…4095⁩ بەکار بهێنێت.
  • بە ⁦constrain()⁩ بەهاکە لەناو سنوورەکەدا بهێڵەوە؛ لە دوای ⁦255⁩، ⁦UNO R3⁩ دەسووڕێتەوە (⁦258⁩ دەبێتە ⁦2⁩).
  • چالاک لەسەر DC بە یەک دەنگ بیپ دەکات؛ ناچالاک پێویستی بە ⁦tone()⁩ هەیە، کە دەتوانێت لە ⁦31 Hz⁩ـەوە بەرەو سەرەوە هەر دەنگێک لێ بدات.
  • ⁦tone()⁩ لە یەک کاتدا یەک دەنگ لێدەدات، یەکسەر دەگەڕێتەوە و لەسەر بۆردەکان جگە لە Mega، PWMـی پینەکانی 3 و ⁦11⁩ تێک دەدات.
Review questionsپرسیارەکانی پێداچوونەوە
Why does an LED look dim at 25 % duty cycle although it only ever gets 0 V or 5 V?بۆچی LEDـێک لە ڕێژەی کارکردنی ⁦25%⁩ـدا کز دەردەکەوێت، لە کاتێکدا تەنیا ⁦0 V⁩ یان ⁦5 V⁩ وەردەگرێت؟

It flashes about 490 times a second, far too fast for the eye, which sees the average: a quarter of full brightness.

لە چرکەیەکدا نزیکەی ⁦490⁩ جار دەدرەوشێتەوە، زۆر خێراتر لەوەی چاو بەدوایدا بڕوات، کە تێکڕاکە دەبینێت: چارەکی ڕووناکیی تەواو.

What two bugs did the first brightness sketch have?یەکەم سکێچی ڕووناکی چ دوو هەڵەیەکی هەبوو؟

It never called analogWrite(), and it checked the limit before adding, so it reached 258 and −2.

هەرگیز ⁦analogWrite()⁩ـی بانگ نەدەکرد و سنوورەکەی پێش زیادکردن دەپشکنی، بۆیە گەیشتە ⁦258⁩ و ⁦−2⁩.

How do you tell an active buzzer from a passive one?چۆن بەزەری چالاک لە ناچالاک جیا دەکەیتەوە؟

Put a steady 5 V on it: an active buzzer beeps, a passive one only clicks. Or read its datasheet.

⁦5 V⁩ـی جێگیری بخەرە سەر: چالاک بیپ دەکات، ناچالاک تەنیا کرتەیەک دەکات. یان بخوێنەوە.

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

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

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

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

Lab: The Watchdog Timer — Restarting a Frozen Deviceتاقیگە: تایمەری پاسەوان — دەستپێکردنەوەی ئامێرێکی گیرخواردوو

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