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

Lab: Buttons, INPUT_PULLUP and Debouncingتاقیگە: دوگمەکانی پەستان، ⁦INPUT_PULLUP⁩ و لابردنی هەڵبەزینەوە

Read push buttons on an Arduino: floating inputs, pull-up and pull-down resistors, INPUT_PULLUP (pressed = LOW), edge detection, contact bounce and a 50 ms debounce with millis(), and a three-button code lock.

خوێندنەوەی لەسەر : ، و ، ⁦INPUT_PULLUP⁩ (پەستێنراو = LOW)، ، و ⁦50 ms⁩ بە ⁦millis()⁩ و قفڵێکی کۆددار بە سێ دوگمە.

25 min read24 خولەک خوێندنەوە · 26 stepsهەنگاو

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

  • Explain why an unconnected input floats and how pull-up and pull-down resistors fix it.
  • Wire a button with INPUT_PULLUP and read it correctly (pressed = LOW).
  • Use edge detection so that one press causes one action.
  • Debounce a button with millis() and a 50 ms wait.
  • Build a three-button code lock and judge how safe it is.
  • ڕوون بکەیتەوە بۆچی نەبەستراو هەڵدەواسرێت و و چۆن چارەسەری دەکەن.
  • دوگمەیەک بە ⁦INPUT_PULLUP⁩ ببەستیت و بە دروستی بیخوێنیتەوە (پەستێنراو = LOW).
  • بەکار بهێنیت بۆ ئەوەی یەک پەستان یەک کردەوە دروست بکات.
  • بە ⁦millis()⁩ و چاوەڕێی ⁦50 ms⁩ بۆ دوگمەیەک بکەیت.
  • قفڵێکی کۆددار بە سێ دوگمە دروست بکەیت و هەڵیبسەنگێنیت چەند سەلامەتە.

Buttons: the board starts to listenدوگمە: بۆردەکە دەست بە گوێگرتن دەکات

So far the board has only talked: it switched LEDs on and off and printed text. Now it listens. A push button is the simplest input: a person presses, a pin reads it and the sketch decides what to do. It is the same sense, decide, act loop that robots and AI agents follow (Sensors).

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

You wire a button to pin 2 and an LED to pin 3 of an Arduino UNO (R3 or R4) and build three sketches: the LED follows the button, one press flips the LED, and a three-button code lock. On the way you meet floating pins, pull-up resistors, edges and the bouncing contacts that every real button has. Everything runs on the 5 V of the USB cable.

دوگمەیەک بە پینی 2 و بە پینی 3ـی ⁦Arduino UNO (R3⁩ یان ⁦R4⁩)ـەوە دەبەستیت و سێ دروست دەکەیت: بەدوای دوگمەکەدا دەڕوات، یەک پەستان دەگۆڕێت و قفڵێکی کۆددار بە سێ دوگمە. لە ڕێگادا ، ، لێوارەکان و ئەو خاڵە هەڵبەزیوانەی پەیوەندی دەناسیت کە هەموو دوگمەیەکی ڕاستەقینە هەیەتی. هەمووی بە ⁦5 V⁩ـی کێبڵی کار دەکات.

Three cards. 1, the LED follows: a pressed button and a lit red LED. 2, one press, one flip: a button and three LEDs, off, on, off. 3, a code lock: three buttons numbered 1, 2, 3 and a lit green LED.سێ کارت. 1، LED لەگەڵ دوگمەکە: دوگمەیەکی پەستێنراو و LEDـێکی سووری هەڵکراو. 2، یەک پەستان، یەک گۆڕان: دوگمەیەک و سێ LED، کوژاو، هەڵکراو، کوژاو. 3، قفڵی کۆددار: سێ دوگمەی ژمارە 1، 2، 3 و LEDـێکی سەوزی هەڵکراو.
The three sketches of this lab, from the simplest to the code lock.سێ ئەم تاقیگەیە، لە سادەترینەوە بۆ قفڵە کۆددارەکە.CêIT

What you needچیت پێویستە

  • an Arduino UNO R3 or UNO R4 and a USB data cable (an MKR WAN 1310 works too: the buttons go to GND, which is safe on a 3.3 V board);
  • three 4-leg push buttons (6 mm or 12 mm);
  • two or three LEDs, each with its resistor: 330 Ω on an UNO R3, 470 Ω on an UNO R4, as in the LED lab;
  • a breadboard and jumper wires; for one exercise, a 10 kΩ resistor.
  • ⁦Arduino UNO R3⁩ یان ⁦UNO R4⁩ و کێبڵێکی کە دەگوازێتەوە (⁦MKR WAN 1310⁩ـیش کار دەکات: دوگمەکان بۆ GND دەچن، کە لەسەر بۆردێکی ⁦3.3 V⁩ سەلامەتە)؛
  • سێ چوار قاچ (⁦6 mm⁩ یان ⁦12 mm⁩)؛
  • دوو یان سێ ، هەریەکە بە خۆیەوە: ⁦330 Ω⁩ لەسەر ⁦UNO R3⁩، ⁦470 Ω⁩ لەسەر ⁦UNO R4⁩، وەک تاقیگەی ؛
  • و وایەری پەیوەندی؛ بۆ ڕاهێنانێک، ⁦10 kΩ⁩.

Why those resistor values is explained in Choosing the LED's resistor with Ohm's law. No board yet? Tinkercad Circuits and Wokwi simulate buttons too (No board? Simulate it).

بۆچی ئەو بەهایانەی لە هەڵبژاردنی بەرگری LED بە یاسای ئۆمدا ڕوون کراوەتەوە. هێشتا بۆردت نییە؟ Tinkercad Circuits و Wokwiـش دوگمە هاوشێوە دەکەن (بۆردت نییە؟ هاوشێوەی بکە).

The push button: two pairs of legsدوگمەی پەستان: دوو جووت قاچ

A push button joins two metal contacts only while you press it; a spring pushes it back when you let go. The common 4-leg button has its legs in two pairs. The two legs of a pair are always joined inside, and pressing joins the two pairs. Not sure which legs pair? Use two legs at opposite corners: they always belong to different pairs.

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

On the breadboard, put the two pairs in different columns. If one column holds both pairs, they are joined all the time and the button seems pressed for ever (The breadboard).

لەسەر دوو جووتەکە لە دوو ستوونی جیاوازدا دابنێ. ئەگەر یەک ستوون هەردوو جووتەکەی تێدا بێت، هەمیشە بەیەکەوە بەستراون و دوگمەکە وا دەردەکەوێت کە بۆ هەمیشە پەستێنراوە (بریدبۆرد).

Two drawings of a 4-leg push button seen from above. Dashed blue lines join the two left legs and the two right legs: always joined. Released, the pairs are apart; pressed, an orange double arrow joins the left pair to the right pair.دوو وێنەی دوگمەی پەستانی چوار قاچ لە سەرەوە. هێڵی پچڕپچڕی شین دوو قاچی چەپ و دوو قاچی ڕاست دەبەستێتەوە: هەمیشە بەستراون. بەردراو، دوو جووتەکە جیان؛ پەستێنراو، تیرێکی دوولایەنەی پرتەقاڵی جووتی چەپ بە جووتی ڕاستەوە دەبەستێت.
Each pair is always joined; pressing joins the two pairs.هەر جووتێک هەمیشە بەستراوە؛ پەستان دوو جووتەکە بەیەکەوە دەبەستێت.CêIT

A pin connected to nothing floatsپینێک کە بە هیچەوە نەبەسترابێت هەڵدەواسرێت

An input pin is like an open ear: it only listens and takes almost no current. Arduino's documentation compares it to a 100 MΩ resistor in front of the pin. With nothing connected, nothing decides its level: the pin floats. It picks up noise from the air and from nearby wires like an antenna, so digitalRead() returns HIGH and LOW at random and the sketch sees presses that never happened. Analog pins float in the same way: A pin with nothing on it reads noise.

پینێکی وەک گوێیەکی کراوەیە: تەنیا گوێ دەگرێت و زۆر کەم وەردەگرێت. بەڵگەنامەکانی بە ⁦100 MΩ⁩ لەبەردەم پینەکەدا دەیشوبهێنن. کاتێک هیچی پێوە نەبەسترابێت، هیچ شتێک ئاستەکەی دیاری ناکات: پینەکە هەڵدەواسرێت (). وەک لە هەواوە و لە وایەرە نزیکەکانەوە وەردەگرێت، بۆیە ⁦digitalRead()⁩ بە هەڕەمەکی HIGH و LOW دەگەڕێنێتەوە و پەستانێک دەبینێت کە هەرگیز نەبووە. پینە بە هەمان شێوە هەڵدەواسرێن: پینێک کە هیچی پێوە نییە ژاوەژاو دەخوێنێتەوە.

A voltage line wanders up and down between 0 V and 5 V, through the HIGH band, the uncertain middle band and the LOW band. Eight dotted lines sample it; the boxes below read 1, 0, 1, 1, 0, 1, 0, 1.هێڵێکی ڤۆڵتیە لە نێوان 0 V و 5 Vـدا سەر و خوار دەکات، بە ناوچەی HIGH، ناوچەی ناڕوونی ناوەڕاست و ناوچەی LOWـدا. هەشت هێڵی خاڵخاڵ نموونەی لێ وەردەگرن؛ سندووقەکانی خوارەوە دەخوێننەوە 1، 0، 1، 1، 0، 1، 0، 1.
Nobody touches the button, but a floating pin still reads 1s and 0s.کەس دەست لە دوگمەکە نادات، بەڵام پینێکی هەڵواسراو هێشتا 1 و 0 دەخوێنێتەوە.CêIT
A balloon without a stringپاڵۆنێک بەبێ دەزوو

An untied balloon floats wherever the air pushes it: that is a floating pin. Tie its string to the ceiling and it stays up until someone pulls it down: that is a pull-up resistor holding the pin HIGH until a press pulls it to GND.

پاڵۆنێکی نەبەستراو بۆ هەر لایەک هەوا پاڵی پێوە بنێت دەڕوات: ئەوە پینێکی هەڵواسراوە. دەزووەکەی بە بنمیچەوە ببەستە، لە سەرەوە دەمێنێتەوە تا کەسێک بۆ خوارەوەی دەکێشێت: ئەوە کە پینەکە لە HIGHـدا ڕادەگرێت تا پەستانێک بۆ GNDـی دەکێشێت.

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

A balloon drifts slowly; a floating pin can flip between HIGH and LOW thousands of times a second.

پاڵۆن هێواش دەڕوات؛ پینێکی هەڵواسراو دەتوانێت لە چرکەیەکدا هەزاران جار لە نێوان HIGH و LOWـدا بگۆڕێت.

A pin connected to nothing floatsپینێک کە بە هیچەوە نەبەسترابێت هەڵدەواسرێت

See it: a pin that presses itselfببینە: پینێک کە خۆی دەپەستێنێت

This sketch reads pin 2 with plain INPUT and no resistor. Nobody touches the button, yet some lines say "pressed?":

ئەم پینی 2 بە INPUTـی سادە و بەبێ دەخوێنێتەوە. کەس دەست لە دوگمەکە نادات، کەچی هەندێک دێڕ دەڵێن «⁦pressed?⁩»:

Pin 2 with no pull-up: the input floatsپینی 2 بەبێ بەرگری بەرزکەرەوە: نێوخستەکە هەڵدەواسرێتfloating.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Pin 2 with a loose wire and NO pull-up resistor: a floating input
const int btnPin = 2;

void setup() {
  Serial.begin(9600);
  pinMode(btnPin, INPUT);   // plain INPUT: nothing holds the pin
}

void loop() {
  if (digitalRead(btnPin) == LOW) {
    Serial.println("pressed?");
  } else {
    Serial.println("not pressed");
  }
  delay(500);
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6

Plain INPUT: nothing holds the pin at HIGH or at LOW.

INPUTـی سادە: هیچ شتێک پینەکە لە HIGH یان LOWـدا ڕاناگرێت.

Lineدێڕی 10

This test is sometimes true and sometimes false without any press.

ئەم تاقیکردنەوەیە هەندێک جار ڕاستە و هەندێک جار هەڵە، بەبێ هیچ پەستانێک.

Pull-up and pull-down resistorsبەرگری بەرزکەرەوە و بەرگری نزمکەرەوە

The cure is a resistor that gives the pin a resting level. A pull-up resistor joins the pin to the supply, so the pin reads HIGH until the button, wired to GND, pulls it LOW. A pull-down resistor joins the pin to GND, so it reads LOW until the button, wired to 5 V, lifts it HIGH. Arduino suggests 10 kΩ for either.

چارەسەر کە ئاستێکی ئاسایی بە پینەکە دەدات. پینەکە بە سەرچاوەی دەبەستێت، بۆیە پینەکە HIGH دەخوێنێتەوە تا دوگمەکە، کە بە GNDـەوە بەستراوە، بۆ LOWـی دەکێشێت. پینەکە بە GNDـەوە دەبەستێت، بۆیە LOW دەخوێنێتەوە تا دوگمەکە، کە بە ⁦5 V⁩ـەوە بەستراوە، بەرزی دەکاتەوە بۆ HIGH. بۆ هەردووکیان ⁦10 kΩ⁩ پێشنیار دەکات.

Why a resistor and not a plain wire? With a wire from the pin to 5 V, pressing the button would join 5 V straight to GND: a short circuit. The resistor lets only a tiny current flow while the button is pressed (Ohm's law):

بۆچی و نەک وایەرێکی سادە؟ بە وایەرێک لە پینەکەوە بۆ ⁦5 V⁩، پەستانی دوگمەکە ⁦5 V⁩ ڕاستەوخۆ بە GNDـەوە دەبەستێت: . تا دوگمەکە پەستێنراوە تەنیا زۆر کەم دەهێڵێت بڕوات (یاسای ئۆم):

I=VR=5 V10 000 Ω=0.0005 A=0.5 mAI = \frac{V}{R} = \frac{5\,\text{V}}{10\,000\,\Omega} = 0.0005\,\text{A} = 0.5\,\text{mA}
Two circuit diagrams. Left, pull-up: 5 V, a 10 kΩ resistor, the junction to pin 2, then the button to ground; released reads HIGH, pressed reads LOW. Right, pull-down: 5 V, the button, the junction to pin 2, then a 10 kΩ resistor to ground; released reads LOW, pressed reads HIGH.دوو هێڵکاریی سووڕی کارەبایی. چەپ، بەرگری بەرزکەرەوە: 5 V، بەرگرێکی 10 kΩ، خاڵی پەیوەندی بۆ پینی 2، پاشان دوگمەکە بۆ زەوی؛ بەردراو HIGH و پەستێنراو LOW دەخوێنێتەوە. ڕاست، بەرگری نزمکەرەوە: 5 V، دوگمەکە، خاڵی پەیوەندی بۆ پینی 2، پاشان بەرگرێکی 10 kΩ بۆ زەوی؛ بەردراو LOW و پەستێنراو HIGH دەخوێنێتەوە.
The resistor decides the resting level; the button changes it. ئاستی ئاسایی دیاری دەکات؛ دوگمەکە دەیگۆڕێت.CêIT
A loose switch in a windy houseسویچێکی شل لە ماڵێکی باداردا

A loose light switch in a windy house flips on and off by itself. Add a spring that holds it at one position until somebody presses it firmly, and it only moves when it should. The pull-up or pull-down resistor is that spring.

شلی گڵۆپ لە ماڵێکی باداردا خۆی هەڵدەبێت و دەکوژێتەوە. سپرینگێکی بۆ دابنێ کە لە یەک شوێندا ڕایبگرێت تا کەسێک بە توندی دەیپەستێنێت، ئەوسا تەنیا کاتی پێویست دەجووڵێت. یان ئەو سپرینگەیە.

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

The resistor does not stop strong interference on long cables completely; it only gives the pin a firm default.

بەهێز لەسەر کێبڵی درێژ بە تەواوی ناوەستێنێت؛ تەنیا ئاستێکی بنەڕەتیی پتەو بە پینەکە دەدات.

INPUT_PULLUP: the pull-up inside the chip⁦INPUT_PULLUP⁩: بەرگری بەرزکەرەوە لەناو چیپەکەدا

You do not even need to buy the resistor. Every digital pin has a pull-up resistor inside the microcontroller, switched on by software with pinMode(2, INPUT_PULLUP);, the third mode of pinMode(): give each pin its job. Then wire the button from pin 2 to GND: released, the pin reads HIGH; pressed, it reads LOW.

پێویست ناکات بکڕیت. هەموو پینێکی لەناو هەیە کە بە و بە pinMode(2, INPUT_PULLUP); هەڵدەکرێت، سێیەم دۆخی ⁦pinMode()⁩: کاری هەر پینێک دیاری بکە. پاشان دوگمەکە لە پینی 2ـەوە بۆ GND ببەستە: بەردراو، پینەکە HIGH دەخوێنێتەوە؛ پەستێنراو، LOW دەخوێنێتەوە.

Inside a blue box marked inside the microcontroller: VCC, a 20 to 50 kΩ resistor (UNO R3) and pin 2, with the line pinMode(2, INPUT_PULLUP);. Outside: a pressed button from pin 2 to GND and a dashed orange arrow for the small current. A box reads: released HIGH (1), pressed LOW (0).لەناو سندووقێکی شیندا بە ناوی لەناو مایکرۆکۆنترۆڵەردا: VCC، بەرگرێکی 20 تا 50 kΩ (UNO R3) و پینی 2، لەگەڵ دێڕی pinMode(2, INPUT_PULLUP);. لە دەرەوە: دوگمەیەکی پەستێنراو لە پینی 2ـەوە بۆ GND و تیرێکی پچڕپچڕی پرتەقاڵی بۆ تەزووە کەمەکە. سندووقێک دەڵێت: بەردراو HIGH (1)، پەستێنراو LOW (0).
One line of code switches on the resistor; the button only needs a wire to GND.یەک دێڕ کۆد هەڵدەکات؛ دوگمەکە تەنیا وایەرێکی بۆ GND پێویستە.CêIT

INPUT_PULLUP: the pull-up inside the chip⁦INPUT_PULLUP⁩: بەرگری بەرزکەرەوە لەناو چیپەکەدا

How strong is the built-in pull-up?بەرگری بەرزکەرەوەی ناوخۆیی چەند بەهێزە؟

Checked in November 2026; for the exact value, read the datasheet of your board's microcontroller.لە تشرینی دووەمی ⁦2026⁩ـدا پشکنراوە؛ بۆ بەهای ورد، بۆردەکەت بخوێنەوە.
Board (chip)بۆرد (چیپ)Built-in pull-up ناوخۆیی
UNO R3 (ATmega328P)20–50 kΩ (Arduino documentation)⁦20–50 kΩ⁩ (بەڵگەنامەکانی )
MKR WAN 1310 (SAMD21)about 40 kΩ, between 20 and 60 kΩ (chip datasheet)نزیکەی ⁦40 kΩ⁩، لە نێوان ⁦20⁩ و ⁦60 kΩ⁩ـدا ( چیپەکە)
UNO R4 (RA4M1)built in as well; its datasheet gives it as a current, not in ohmsئەمیش ناوخۆییە؛ وەک دەیدات، نەک بە ئۆم

These pull-ups are weak: tens of kilohms. For a button on a breadboard that is plenty. A long cable picks up more noise, so projects with long wires often add a stronger external pull-up, such as 10 kΩ.

ئەم لاوازن: دەیان کیلۆئۆم. بۆ دوگمەیەک لەسەر بەسە. کێبڵی درێژ زیاتر وەردەگرێت، بۆیە پڕۆژەکانی وایەری درێژ زۆرجار دەرەکیی بەهێزتر، وەک ⁦10 kΩ⁩، زیاد دەکەن.

The spring of a doorسپرینگی دەرگا

INPUT_PULLUP is like the spring on a door: it keeps the door shut (HIGH). Push the door, that is press the button, and it opens (LOW); let go and the spring shuts it again.

⁦INPUT_PULLUP⁩ وەک سپرینگی دەرگایە: دەرگاکە داخراو ⁦(HIGH)⁩ ڕادەگرێت. دەرگاکە پاڵ بنێ، واتە دوگمەکە بپەستێنە، دەکرێتەوە ⁦(LOW)⁩؛ بەری بدە و سپرینگەکە دووپاتە دایدەخاتەوە.

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

A door can stay half open; a digital pin is always read as one of two levels.

دەرگا دەتوانێت نیوەکراوە بمێنێتەوە؛ پینێکی هەمیشە وەک یەکێک لە دوو ئاست دەخوێنرێتەوە.

Words for this labوشەکانی ئەم تاقیگەیە

WordوشەWhat it means hereلێرەدا واتای چییە
inputthe pin listens; pinMode(pin, INPUT)پینەکە گوێ دەگرێت؛ pinMode(pin, INPUT)
pull-uppulling the pin up to HIGH with a resistorکێشانی پینەکە بۆ HIGH بە
resistorlimits the current; for INPUT_PULLUP it is built into the chip; for the LED it protects LED and pin سنووردار دەکات؛ بۆ ⁦INPUT_PULLUP⁩ لەناو چیپەکەدایە؛ بۆ و پینەکە دەپارێزێت
internalinside the chipلەناو چیپەکەدا
floatingconnected to nothing, so the reading is randomبە هیچەوە نەبەستراوە، بۆیە خوێندنەوەکە هەڕەمەکییە
noiseunwanted signals from the air or nearby electronics نەخوازراو لە هەواوە یان لە ئامێرە ئەلیکترۆنییە نزیکەکانەوە
unreliablenot steady; you cannot trust itنەگۆڕ نییە؛ ناتوانیت متمانەی پێ بکەیت
HIGH / LOW1 = 5 V (3.3 V on 3.3 V boards) / 0 = 0 V⁦1 = 5 V⁩ (لەسەر بۆردەکانی ⁦3.3 V⁩، ⁦3.3 V) / 0 = 0 V⁩
GNDground, 0 V: the way back for every currentزەوی، ⁦0 V⁩: ڕێگای گەڕانەوەی هەموو
digitalRead(pin)reads HIGH or LOW from an input pinHIGH یان LOW لە پینێکی دەخوێنێتەوە
digitalWrite(pin, v)sets an output pin HIGH (on) or LOW (off)پینێکی دەکاتە HIGH (هەڵکراو) یان LOW (کوژاو)
loop()runs again and again, for everدووپاتە و دووپاتە کار دەکات، بۆ هەمیشە

The lab circuit: a button on pin 2, an LED on pin 3سووڕی کارەبایی تاقیگەکە: دوگمەیەک لەسەر پینی 2، LED لەسەر پینی 3

  1. Buttonدوگمە

    One leg of the button to pin 2; a leg of the other pair to GND.

    یەکێک لە قاچەکانی دوگمەکە بۆ پینی 2؛ قاچێکی جووتەکەی تر بۆ GND.

  2. LEDLED

    The LED's long leg (anode) to pin 3; its short leg (cathode) to a resistor, and the resistor to GND.

    قاچی درێژی () بۆ پینی 3؛ قاچە کورتەکەی () بۆ و بۆ GND.

  3. GNDGND، زەوی

    One wire from the board's GND to the breadboard's GND row.

    وایەرێک لە GNDـی بۆردەکەوە بۆ ڕیزی GNDـی .

  4. No resistor for the buttonبەرگر بۆ دوگمەکە نییە

    The button needs no resistor: INPUT_PULLUP switches on the internal one.

    دوگمەکە پێویستی بە نییە: ⁦INPUT_PULLUP⁩ ناوخۆییەکە هەڵدەکات.

The lab circuit in Tinkercad. The brown, black and red bands mean 1 kΩ: the LED is a little dimmer than with 330 Ω, and safe on every board. تاقیگەکە لە Tinkercadـدا. هێڵە قاوەیی، ڕەش و سوورەکان واتە ⁦1 kΩ⁩: کەمێک لە کاتی ⁦330 Ω⁩ کزترە و لەسەر هەموو بۆردێک سەلامەتە.Course material

The LED follows the buttonLEDـەکە بەدوای دوگمەکەدا دەڕوات

The first sketch reads pin 2 on every pass of loop() and copies the answer to pin 3. Step by step: INPUT_PULLUP holds pin 2 HIGH; not pressed, it stays HIGH and the else switches the LED off; pressed, pin 2 is joined to GND and reads LOW, so the if is true and the LED goes on.

یەکەم لە هەموو جارێکی ⁦loop()⁩ـدا پینی 2 دەخوێنێتەوە و وەڵامەکە بۆ پینی 3 دەگوازێتەوە. هەنگاو بە هەنگاو: ⁦INPUT_PULLUP⁩ پینی 2 لە HIGHـدا ڕادەگرێت؛ نەپەستێنراو، HIGH دەمێنێتەوە و else دەکوژێنێتەوە؛ پەستێنراو، پینی 2 بە GNDـەوە دەبەسترێت و LOW دەخوێنێتەوە، بۆیە if ڕاستە و هەڵدەبێت.

The LED is on exactly while the button is heldLEDـەکە ڕێک تا دوگمەکە گیراوە هەڵکراوەled_follows.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
void setup() {
  pinMode(2, INPUT_PULLUP); // use internal pull-up resistor
  pinMode(3, OUTPUT);       // LED pin as output
}

void loop() {
  if (digitalRead(2) == LOW) { // button is pressed (connected to GND)
    digitalWrite(3, HIGH);     // turn LED on
  } else {
    digitalWrite(3, LOW);      // turn LED off
  }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Pin 2 listens, with the internal pull-up.

پینی 2 گوێ دەگرێت، بە ناوخۆیی.

Lineدێڕی 7

LOW = the button joins pin 2 to GND = pressed.

LOW = دوگمەکە پینی 2 بە GNDـەوە دەبەستێت = پەستێنراو.

Timing diagram from 0 to 2000 ms. Pin 2 (button) is HIGH, drops to LOW at 1000 ms and returns to HIGH at 1500 ms. Pin 3 (LED) is LOW, rises to HIGH at 1000 ms and falls at 1500 ms. The pressed time is shaded.هێڵکاریی کاتبەندی لە 0 تا 2000 ms. پینی 2 (دوگمە) HIGHـە، لە 1000 msـدا دادەبەزێت بۆ LOW و لە 1500 msـدا دەگەڕێتەوە بۆ HIGH. پینی 3 (LED) LOWـە، لە 1000 msـدا بەرز دەبێتەوە بۆ HIGH و لە 1500 msـدا نزم دەبێتەوە. کاتی پەستان ڕەنگ کراوە.
The two pins are mirror images: pin 2 goes LOW, pin 3 goes HIGH.دوو پینەکە وەک وێنەی ئاوێنەن: پینی 2 دەبێتە LOW، پینی 3 دەبێتە HIGH.CêIT

Lab 1: a message for each press?تاقیگەی 1: نامەیەک بۆ هەر پەستانێک؟

Lab 1 prints a message while the button is down and then waits two seconds, so that one press does not fill the Serial Monitor.

تاقیگەی 1 تا دوگمەکە پەستێنراوە نامەیەک دەنووسێت و پاشان دوو چرکە چاوەڕێ دەکات، بۆ ئەوەی یەک پەستان پڕ نەکات.

Lab 1: read a button with INPUT_PULLUPتاقیگەی 1: خوێندنەوەی دوگمەیەک بە ⁦INPUT_PULLUP⁩lab1_print.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Lab 1: a message when the button on pin 2 is pressed
const int btnPin = 2;

void setup() {
  pinMode(btnPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(btnPin) == LOW) {   // LOW = pressed
    Serial.println("Button pressed");
    delay(2000);                      // wait 2 s before looking again
  }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 11

One line per press, and at most one line every 2 seconds.

یەک دێڕ بۆ هەر پەستانێک؛ لە هەر 2 چرکەدا زیاتر لە یەک دێڕ نا.

Lineدێڕی 12

While delay() waits, the sketch does not look at the button at all.

تا ⁦delay()⁩ چاوەڕێ دەکات، هەر سەیری دوگمەکە ناکات.

The long press prints twice (at 1 s and 3 s), and the two quick presses are lost: they come and go while delay(2000) waits. The sketch reacts to the level (is it down now?) instead of the moment of pressing.

پەستانە درێژەکە دوو جار دەنووسێت (لە 1 و 3 چرکەدا) و دوو پەستانە خێراکە لەدەست دەچن: دێن و دەڕۆن تا delay(2000) چاوەڕێ دەکات. کاردانەوەی بۆ ئاست هەیە (ئایا ئێستا پەستێنراوە؟) نەک بۆ ساتی پەستان.

Timeline from 0 to 6 s. The button is held from 1.0 to 3.5 s, then pressed twice quickly at 4.2 and 4.5 s. Two grey bars labelled delay(2000) cover 1 to 3 s and 3 to 5 s. Button pressed is printed at 1 s and 3 s; the quick presses fall inside the second bar and are marked missed.هێڵی کات لە 0 تا 6 چرکە. دوگمەکە لە 1.0 تا 3.5 چرکە گیراوە، پاشان لە 4.2 و 4.5 چرکەدا دوو جار بە خێرایی پەستێنراوە. دوو شریتی خۆڵەمێشی بە ناوی delay(2000) لە 1 تا 3 و لە 3 تا 5 چرکە دادەپۆشن. Button pressed لە 1 و 3 چرکەدا دەنووسرێت؛ پەستانە خێراکان دەکەونە ناو شریتی دووەمەوە و وەک لەدەستچوو نیشان دراون.
delay(2000) repeats a long press and hides quick ones.⁦delay(2000)⁩ پەستانی درێژ دووپاتە دەکاتەوە و پەستانی خێرا دەشارێتەوە.CêIT

Lab 2: the ternary operator and a flooded Serial Monitorتاقیگەی 2: کارپێکەری سێیانی و Serial Monitorـی پڕ

Lab 2 makes the LED follow the button in two lines. bool pressed = digitalRead(btnPin) == LOW; stores the answer as a boolean, true or false. pressed ? HIGH : LOW then chooses a value: HIGH if pressed is true, LOW if it is false. This ?: is the ternary operator (a choice in one line).

تاقیگەی 2 بە دوو دێڕ بەدوای دوگمەکەدا دەبات. bool pressed = digitalRead(btnPin) == LOW; وەڵامەکە وەک ، true یان false، هەڵدەگرێت. پاشان pressed ? HIGH : LOW بەهایەک هەڵدەبژێرێت: HIGH ئەگەر pressed ڕاست بێت، LOW ئەگەر هەڵە بێت. ئەم ?:ـە (هەڵبژاردن بە یەک دێڕ).

Lab 2: the LED follows the buttonتاقیگەی 2: LEDـەکە بەدوای دوگمەکەدا دەڕواتlab2_follow.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Lab 2: the LED follows the button
const int btnPin = 2;
const int ledPin = 3;

void setup() {
  pinMode(btnPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  bool pressed = digitalRead(btnPin) == LOW;
  digitalWrite(ledPin, pressed ? HIGH : LOW);
  Serial.println(pressed ? "LED ON" : "LED OFF");
  delay(200);
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 12

A boolean: true while the button is pressed.

: تا دوگمەکە پەستێنراوە true.

Lineدێڕی 13-14

Two ternary choices: one for the pin, one for the text.

دوو هەڵبژاردنی سێیانی: یەکێک بۆ پینەکە، یەکێک بۆ دەقەکە.

It works, but it floods the Serial Monitor with five lines a second for ever. Printing only when the state changes is better: that is an exercise below.

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

The code pressed ? HIGH : LOW above a box asking is pressed true?. An arrow labelled yes leads to HIGH, an arrow labelled no leads to LOW.کۆدی pressed ? HIGH : LOW لە سەرووی سندووقێک کە دەپرسێت ئایا pressed ڕاستە (true)؟. تیرێک بە ناوی بەڵێ بۆ HIGH دەچێت، تیرێک بە ناوی نەخێر بۆ LOW.
condition ? value if true : value if falseمەرج ? بەها ئەگەر ڕاست بێت : بەها ئەگەر هەڵە بێتCêIT

Level or edge? React to the changeئاست یان لێوار؟ کاردانەوە بۆ گۆڕانەکە

A level is what the pin reads now: HIGH or LOW. An edge is the moment it changes. With INPUT_PULLUP the press is a falling edge, HIGH → LOW, and the release is a rising edge, LOW → HIGH. These are the same edges that clock signals have in Logic Design (Pulses, period and frequency).

ئاست ئەوەیە کە پینەکە ئێستا دەیخوێنێتەوە: HIGH یان LOW. لێوار ساتی گۆڕانەکەیە. بە ⁦INPUT_PULLUP⁩ پەستان (لە HIGHـەوە بۆ LOW) و بەردان (لە LOWـەوە بۆ HIGH). ئەمانە هەمان ئەو لێوارانەن کە لە دیزاینی هەیەتی (تڕپەکان، کاتی خولی و لەرەلەر).

To catch an edge, the sketch keeps the last reading in a variable and compares: if it was HIGH last time and is LOW now, the button has just been pressed. This is edge detection, also called state change detection. A light-sensor lab later meets the same difference between a level and a change: A night light, not a toggle.

بۆ گرتنی لێوارێک، کۆتا خوێندنەوە لە هەڵدەگرێت و بەراورد دەکات: ئەگەر جاری پێشوو HIGH بووبێت و ئێستا LOW بێت، دوگمەکە ئێستا پەستێنرا. ئەمە ، کە پێی دەوترێت دۆزینەوەی گۆڕانی دۆخیش. دواتر تاقیگەیەکی ڕووناکی هەمان جیاوازیی نێوان ئاست و گۆڕان دەبینێت: گڵۆپی شەو، نەک گۆڕین.

A waveform: HIGH, a falling edge circled in orange and labelled the press, a long LOW part labelled level: LOW all the time it is held, then a rising edge circled in blue and labelled the release. Below: the code lastState == HIGH && state == LOW.شێوەی شەپۆلێک: HIGH، لێواری نزمبوونەوەیەک کە بە پرتەقاڵی بازنەی لێ دراوە و ناوی پەستانە، بەشێکی درێژی LOW بە ناوی ئاست: هەتا گیراوە هەر LOWـە، پاشان لێواری بەرزبوونەوەیەک کە بە شین بازنەی لێ دراوە و ناوی بەردانە. لە خوارەوە: کۆدی lastState == HIGH && state == LOW.
One press has two edges and a level in between.یەک پەستان دوو لێوار و ئاستێکی لە نێوانیاندا هەیە.CêIT

Lab 3: one press, one toggleتاقیگەی 3: یەک پەستان، یەک گۆڕان

Lab 3 flips the LED on each press. Two variables keep its memory: ledState (is the LED on?) and lastBtnState (what pin 2 read on the last pass). !ledState means "the opposite", as in the toggles of the Serial lab (Lab 5: toggles with switch and !).

تاقیگەی 3 لە هەر پەستانێکدا دەگۆڕێت. دوو یادەوەرییەکەی هەڵدەگرن: ledState (ئایا هەڵکراوە؟) و lastBtnState (پینی 2 لە جاری پێشوودا چی خوێندەوە). !ledState واتە «پێچەوانەکەی»، وەک گۆڕینەکانی تاقیگەی Serial (تاقیگەی 5: گۆڕین بە switch و !).

Lab 3 with edge detectionتاقیگەی 3 بە دۆزینەوەی لێوارlab3_fixed.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Lab 3: toggle the LED on each press (edge detection)
const int btnPin = 2;
const int ledPin = 3;

bool ledState = false;      // is the LED on?
bool lastBtnState = HIGH;   // the reading from the last pass of loop()

void setup() {
  pinMode(btnPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  bool currentBtnState = digitalRead(btnPin);
  if (lastBtnState == HIGH && currentBtnState == LOW) {  // HIGH -> LOW: a press
    ledState = !ledState;
    digitalWrite(ledPin, ledState ? HIGH : LOW);
    Serial.println(ledState ? "LED ON" : "LED OFF");
    delay(200);
  }
  lastBtnState = currentBtnState;   // remember it for the next pass
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 16

HIGH last time and LOW now: a falling edge, so a press.

جاری پێشوو HIGH و ئێستا LOW: ، واتە پەستان.

Lineدێڕی 22

Remember this reading for the next pass. Lab 3 first forgot this line.

ئەم خوێندنەوەیە بۆ جاری داهاتوو لەبیر بهێڵە. تاقیگەی 3 سەرەتا ئەم دێڕەی لەبیر کردبوو.

Two circles: LED off and LED on. Curved arrows go both ways, each labelled press (HIGH → LOW). Note: holding the button or letting it go changes nothing.دوو بازنە: LED کوژاوە و LED هەڵکراوە. تیری چەماوە بە هەردوو ئاراستەدا دەڕۆن، هەریەکە بە ناوی پەستان (HIGH بۆ LOW). تێبینی: گرتن یان بەردانی دوگمەکە هیچ ناگۆڕێت.
Only the falling edge moves the LED to its other state.تەنیا دەباتە دۆخەکەی تری.CêIT

Lab 3: one press, one toggleتاقیگەی 3: یەک پەستان، یەک گۆڕان

Common mistake: the reading that is never savedهەڵەی باو: ئەو خوێندنەوەیەی هەرگیز هەڵناگیرێت

Three rows over time. The button is pressed from 1000 to 1700 ms. With the bug, the LED line flips at 1000, 1200, 1400 and 1600 ms: ON OFF ON OFF. Fixed, the LED goes on at 1000 ms and stays on.سێ ڕیز بە درێژایی کات. دوگمەکە لە 1000 تا 1700 ms پەستێنراوە. بە هەڵەکەوە، هێڵی LEDـەکە لە 1000، 1200، 1400 و 1600 msـدا دەگۆڕێت: ON OFF ON OFF. چاککراو، LEDـەکە لە 1000 msـدا هەڵدەبێت و هەڵکراو دەمێنێتەوە.
Holding the button should not keep flipping the LED.گرتنی دوگمەکە نابێت هەر بگۆڕێت.CêIT

Real buttons bounceدوگمەی ڕاستەقینە هەڵدەبەزێتەوە

Inside a button, a springy metal part hits the contacts. Like a dropped ball it bounces: for a few milliseconds the contacts touch, part and touch again. This is contact bounce. Most switches settle within 1 to 20 ms, some take longer; measurements of many switches found about 1.5 ms on average.

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

A person sees one press. loop() runs thousands of times a second and sees every bounce as a new press.

مرۆڤ یەک پەستان دەبینێت. ⁦loop()⁩ لە چرکەیەکدا هەزاران جار کار دەکات و هەر هەڵبەزینەوەیەک وەک پەستانێکی نوێ دەبینێت.

A voltage graph from 0 to 12 ms. The line is at 5 V, drops to 0 V at 2 ms, then jumps up and down several times until about 7.5 ms, and stays at 0 V. An orange bracket marks bounce: about 5 ms here. Below, 17 sample boxes read 1 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0.هێڵکارییەکی ڤۆڵتیە لە 0 تا 12 ms. هێڵەکە لە 5 Vـدایە، لە 2 msـدا دادەبەزێت بۆ 0 V، پاشان چەند جارێک سەر و خوار باز دەدات تا نزیکەی 7.5 ms و لە 0 Vـدا دەمێنێتەوە. کەوانەیەکی پرتەقاڵی هەڵبەزینەوە نیشان دەدات: لێرەدا نزیکەی 5 ms. لە خوارەوە 17 سندووقی نموونە دەخوێننەوە 1 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0.
Zoomed in, one press is a burst of presses.بە نزیکبوونەوە، یەک پەستان کۆمەڵێک پەستانە.CêIT

Real buttons bounceدوگمەی ڕاستەقینە هەڵدەبەزێتەوە

See it in the checker: bounce fools Lab 3لە پشکنەردا ببینە: هەڵبەزینەوە تاقیگەی 3 فریو دەدات

Inside a tactile switch, a thin metal dome snaps down onto the contacts and shakes for a moment. Here the fixed Lab 3 meets two presses whose contacts bounce for 5 to 8 ms at the press and again at the release:

لەناو هەستپێکراودا، گومبەزێکی کانزایی تەنک بە توندی دەکەوێتە سەر خاڵەکانی پەیوەندی و بۆ ساتێک دەلەرزێت. لێرەدا تاقیگەی 3ـی چاککراو دوو پەستان دەبینێت کە خاڵەکانیان لە کاتی پەستان و دیسان لە کاتی بەرداندا 5 تا ⁦8 ms⁩ هەڵدەبەزنەوە:

Serial Monitor (Lab 3, two bouncy presses at 1000 and 3000 ms)Serial Monitor (تاقیگەی 3، دوو پەستانی هەڵبەزیو لە ⁦1000⁩ و ⁦3000 ms⁩)
LED ON
LED OFF
LED ON
LED OFF

Each press switches the LED on, and the bounce of the release switches it off again (at 1701 and 3401 ms). The checker runs loop() once per millisecond; a real UNO runs it far more often and sees even more bounces.

هەر پەستانێک هەڵدەکات و هەڵبەزینەوەی بەردانەکە دیسان دەیکوژێنێتەوە (لە ⁦1701⁩ و ⁦3401 ms⁩). پشکنەرەکە ⁦loop()⁩ لە هەر میلیچرکەیەکدا جارێک جێبەجێ دەکات؛ UNOـی ڕاستەقینە زۆر زیاتر جێبەجێی دەکات و هەڵبەزینەوەی زیاتریش دەبینێت.

Debouncing: wait until the button is stillلابردنی هەڵبەزینەوە: چاوەڕێ بکە تا دوگمەکە ئارام دەبێت

The cure is debouncing: believe a new reading only when it has stayed the same for a while. Arduino's official Debounce example waits 50 ms. Every change restarts a 50 ms timer; when the timer runs out with no new change, the reading becomes the accepted state, and only a change of that accepted state from HIGH to LOW counts as a press.

چارەسەر : تەنیا باوەڕ بە خوێندنەوەیەکی نوێ بکە کە بۆ ماوەیەک وەک خۆی مابێتەوە. نموونەی فەرمیی Debounceـی ⁦50 ms⁩ چاوەڕێ دەکات. هەر گۆڕانێک ⁦50 ms⁩ لە سەرەتاوە دەست پێدەکاتەوە؛ کاتێک بەبێ گۆڕانێکی نوێ تەواو دەبێت، خوێندنەوەکە دەبێتە دۆخی وەرگیراو و تەنیا گۆڕانی ئەو دۆخە وەرگیراوە لە HIGHـەوە بۆ LOW وەک پەستان دەژمێردرێت.

The timer is millis(), the clock from millis(): check the clock instead of waiting. Waiting with delay() would make the board deaf; millis() keeps loop() running, so it can watch other buttons and sensors at the same time.

millis()ـە، ئەو کاتژمێرەی لە ⁦millis()⁩: لە جیاتی چاوەڕێکردن سەیری کاتژمێر بکەدا ناسیت. چاوەڕێکردن بە ⁦delay()⁩ بۆردەکە کەڕ دەکات؛ ⁦millis() loop()⁩ بە کارکردن دەهێڵێتەوە، بۆیە دەتوانێت لە هەمان کاتدا سەیری دوگمە و تریش بکات.

Animation. Left: a button, released then pressed, and an LED. Right: the raw reading bounces between 0 and 8 ms while a 50 ms timer bar keeps restarting; after the last bounce the bar fills to 50 / 50 ms at 58 ms, the accepted state drops to LOW once and the LED turns on.
Animation. Left: a button, released then pressed, and an LED. Right: the raw reading bounces between 0 and 8 ms while a 50 ms timer bar keeps restarting; after the last bounce the bar fills to 50 / 50 ms at 58 ms, the accepted state drops to LOW once and the LED turns on.
Bounces restart the timer; 50 ms of stillness makes one press.هەڵبەزینەوەکان تایمەرەکە لە سەرەتاوە دەست پێدەکەنەوە؛ ⁦50 ms⁩ ئارامی یەک پەستان دروست دەکات.CêIT

Debouncing: wait until the button is stillلابردنی هەڵبەزینەوە: چاوەڕێ بکە تا دوگمەکە ئارام دەبێت

Lab 3, debouncedتاقیگەی 3، بە لابردنی هەڵبەزینەوە

Lab 3 with a 50 ms debounce (millis(), no delay)تاقیگەی 3 بە لابردنی هەڵبەزینەوەی ⁦50 ms (millis()⁩، بەبێ delay)debounce.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Lab 3, debounced: one press = one toggle
const int btnPin = 2;
const int ledPin = 3;
const unsigned long debounceDelay = 50;  // ms the reading must stay still

bool ledState = false;
int lastReading = HIGH;          // the raw reading from the last pass
int stableState = HIGH;          // the accepted (debounced) state
unsigned long lastChange = 0;    // when the raw reading last moved

void setup() {
  pinMode(btnPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int reading = digitalRead(btnPin);

  if (reading != lastReading) {     // it moved: a bounce or a real change
    lastChange = millis();          // start the 50 ms wait again
    lastReading = reading;
  }

  if (millis() - lastChange > debounceDelay && reading != stableState) {
    stableState = reading;          // still for 50 ms: accept it
    if (stableState == LOW) {       // the new state is LOW: a real press
      ledState = !ledState;
      digitalWrite(ledPin, ledState ? HIGH : LOW);
      Serial.print(millis());
      Serial.print(" ms: ");
      Serial.println(ledState ? "LED ON" : "LED OFF");
    }
  }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

The debounce time; raise it if the LED still flickers.

کاتی ؛ ئەگەر هێشتا دەلەرزێت زیادی بکە.

Lineدێڕی 20-23

Any movement, bounce or real, restarts the wait.

هەر جووڵەیەک، هەڵبەزینەوە بێت یان ڕاستەقینە، چاوەڕێکردنەکە لە سەرەتاوە دەست پێدەکاتەوە.

Lineدێڕی 25-27

Still for more than 50 ms and different from the accepted state: accept it; LOW means a real press.

زیاتر لە ⁦50 ms⁩ ئارام بووە و جیاوازە لە دۆخی وەرگیراو: وەریبگرە؛ LOW واتە پەستانێکی ڕاستەقینە.

Why 50 ms? It is longer than nearly every bounce and shorter than people notice: a 100 ms wait is noticeable, 50 ms feels instant. Debouncing is only for contacts that move: a password typed in the Serial Monitor needs none (A password over Serial).

بۆچی ⁦50 ms⁩؟ لە نزیکەی هەموو هەڵبەزینەوەیەک درێژترە و لەوە کورتترە کە خەڵک هەستی پێ بکەن: چاوەڕێی ⁦100 ms⁩ هەستی پێ دەکرێت، ⁦50 ms⁩ وەک ساتی دەردەکەوێت. تەنیا بۆ خاڵی پەیوەندیی جووڵاوە: کە لە دەنووسرێت پێویستی پێی نییە (تێپەڕەوشەیەک لە ڕێی Serialـەوە).

Three rows from minus 10 to 80 ms. Raw reading: bounces between 0 and 8 ms, then LOW. 50 ms timer: orange dots mark restarts at each bounce; a bar starts at 8 ms and fills to 50 / 50 ms at 58 ms. Accepted state: HIGH until 58 ms, then LOW, marked still for 50 ms: one press.سێ ڕیز لە کەم 10 تا 80 ms. خوێندنەوەی خاو: هەڵبەزینەوە لە نێوان 0 و 8 msـدا، پاشان LOW. تایمەری 50 ms: خاڵی پرتەقاڵی دەستپێکردنەوە لە هەر هەڵبەزینەوەیەکدا نیشان دەدەن؛ شریتێک لە 8 msـدا دەست پێدەکات و لە 58 msـدا پڕ دەبێت بۆ 50 / 50 ms. دۆخی وەرگیراو: HIGH تا 58 ms، پاشان LOW، بە نیشانەی 50 ms ئارام بوو: یەک پەستان.
The accepted state changes once, 50 ms after the last bounce.دۆخی وەرگیراو یەک جار دەگۆڕێت، ⁦50 ms⁩ دوای کۆتا هەڵبەزینەوە.CêIT

The idea: a three-button code lockبیرۆکەکە: قفڵێکی کۆددار بە سێ دوگمە

The last sketch turns three buttons into a lock. Each press adds a digit to a String; after three digits the sketch compares the String with the code "132": the green LED for two seconds if it is right, the red one if it is wrong, then it starts again.

کۆتا سێ دوگمە دەکاتە قفڵ. هەر پەستانێک ژمارەیەک بۆ زیاد دەکات؛ دوای سێ ژمارە لەگەڵ کۆدی «⁦132⁩» بەراورد دەکات: ئەگەر دروست بێت سەوزەکە بۆ دوو چرکە، ئەگەر هەڵە بێت سوورەکە، پاشان لە سەرەتاوە دەست پێدەکاتەوە.

A three-button code lock (button 3 on pin 7)قفڵێکی کۆددار بە سێ دوگمە (دوگمەی 3 لەسەر پینی ⁦7⁩)lock.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// A three-button code lock: the code is 1, 3, 2
const int btn1 = 3;
const int btn2 = 2;
const int btn3 = 7;          // not pin 1: on an UNO R3, pin 1 is the Serial TX line

const int redLedPin = 5;
const int greenLedPin = 4;

String correctPass = "132";
String input = "";

void setup() {
  pinMode(btn1, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
  pinMode(btn3, INPUT_PULLUP);

  pinMode(redLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  if (digitalRead(btn1) == LOW) {
    input += "1";
    Serial.println("Pressed: 1");
    waitForRelease(btn1);
  }
  if (digitalRead(btn2) == LOW) {
    input += "2";
    Serial.println("Pressed: 2");
    waitForRelease(btn2);
  }
  if (digitalRead(btn3) == LOW) {
    input += "3";
    Serial.println("Pressed: 3");
    waitForRelease(btn3);
  }

  if (input.length() == 3) {
    if (input == correctPass) {
      digitalWrite(redLedPin, LOW);
      digitalWrite(greenLedPin, HIGH);
      Serial.println("Correct password");
    } else {
      digitalWrite(redLedPin, HIGH);
      digitalWrite(greenLedPin, LOW);
      Serial.println("Incorrect password");
    }

    delay(2000);
    input = "";
    digitalWrite(redLedPin, LOW);
    digitalWrite(greenLedPin, LOW);
  }
}

void waitForRelease(int pin) {
  while (digitalRead(pin) == LOW) {   // wait while the button is held
    delay(10);
  }
  delay(200);                         // let the contacts settle
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

Pin 7, not pin 1: see the common mistake below.

پینی 7، نەک پینی 1: هەڵە باوەکەی خوارەوە ببینە.

Lineدێڕی 24-38

Each press adds a digit, then waits for the release.

هەر پەستانێک ژمارەیەک زیاد دەکات، پاشان چاوەڕێی بەردان دەکات.

Lineدێڕی 40-55

Three digits: compare, light green or red for 2 s, start again.

سێ ژمارە: بەراوردکردن، 2 چرکە سەوز یان سوور، دەستپێکردنەوە.

Lineدێڕی 58-63

Wait while held, then 200 ms more: a simple, blocking debounce.

تا گیراوە چاوەڕێ بکە، پاشان ⁦200 ms⁩ـی تر: سادە و ڕاگر.

A panel with three push buttons numbered 1 (pin 3), 2 (pin 2) and 3 (pin 7). Beside it, a box typed so far shows 1 3 2; a lit green LED (pin 4) and an unlit red LED (pin 5); note: right code, green for 2 s.پانێڵێک بە سێ دوگمەی پەستانی ژمارە 1 (پینی 3)، 2 (پینی 2) و 3 (پینی 7). لە تەنیشتیەوە سندووقێکی تا ئێستا لێدراوە 132 پیشان دەدات؛ LEDـێکی سەوزی هەڵکراو (پینی 4) و LEDـێکی سووری کوژاو (پینی 5)؛ تێبینی: کۆدی دروست، 2 چرکە سەوز.
Three buttons, one String, two LEDs.سێ دوگمە، یەک ، دوو .CêIT

The idea: a three-button code lockبیرۆکەکە: قفڵێکی کۆددار بە سێ دوگمە

Common mistake: a button on pin 1هەڵەی باو: دوگمەیەک لەسەر پینی 1

The digital header of an UNO R3, pins 7 to 0. Pins TX 1 and RX 0 are circled in orange with an arrow to a box USB-to-serial chip, then computer (Serial Monitor). Pins 2 to 7 are marked free for buttons and LEDs.سەرپینی دیجیتاڵی UNO R3، پینەکانی 7 تا 0. پینەکانی TX 1 و RX 0 بە پرتەقاڵی بازنەیان لێ دراوە و تیرێک بۆ سندووقی چیپی USB بۆ زنجیرەیی، پاشان کۆمپیوتەر (Serial Monitor) دەچێت. پینەکانی 2 تا 7 وەک ئازاد بۆ دوگمە و LED نیشان دراون.
Keep buttons off pins 0 and 1 when the sketch uses Serial.کاتێک Serial بەکار دەهێنێت دوگمە لەسەر پینەکانی 0 و 1 دامەنێ.CêIT

The idea: a three-button code lockبیرۆکەکە: قفڵێکی کۆددار بە سێ دوگمە

How safe is this lock?ئەم قفڵە چەند سەلامەتە؟

Three presses of three buttons give 3×3×3=273 \times 3 \times 3 = 27 codes, by the multiplication principle (Counting passwords). A try takes about three and a half seconds (three presses and the 2-second answer), so trying every code, a brute-force attack (How passwords are broken), takes under two minutes.

سێ پەستانی سێ دوگمە 3×3×3=273 \times 3 \times 3 = 27 کۆد دەدات، بەپێی بنەمای لێکدان (ژماردنی تێپەڕەوشەکان). هەر هەوڵێک نزیکەی سێ چرکە و نیو دەخایەنێت (سێ پەستان و وەڵامی 2 چرکەیی)، بۆیە تاقیکردنەوەی هەموو کۆدەکان، (تێپەڕەوشەکان چۆن دەشکێنرێن)، لە دوو خولەک کەمتر دەخایەنێت.

33=27106=1 000 0003^3 = 27 \qquad 10^6 = 1\,000\,000

Three buttons, three presses; ten keys, six digits.

سێ دوگمە، سێ پەستان؛ دە دوگمە، شەش ژمارە.

Real locks use more keys and longer codes, slow down after wrong tries and never ship one default code for every device. IoT devices follow the same rules (Keeping IoT devices safe).

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

Three panels, first press 1, 2 and 3, each with a 3 by 3 grid of codes: 111 to 133, 211 to 233 and 311 to 333. The code 132 is highlighted. Note: a thief who tries them all needs only a few minutes.سێ پانێڵ، پەستانی یەکەم 1، 2 و 3، هەریەکە بە خانەبەندییەکی 3 لە 3ـی کۆد: 111 تا 133، 211 تا 233 و 311 تا 333. کۆدی 132 ڕەنگی تر کراوە. تێبینی: دزێک کە هەموویان تاقی بکاتەوە تەنیا چەند خولەکێکی پێویستە.
All 27 codes fit on one small picture.هەموو ⁦27⁩ کۆدەکە لە یەک وێنەی بچووکدا جێیان دەبێتەوە.CêIT

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

MistakeهەڵەBetterباشتر
A button input with plain INPUT and no resistor: random presses. دوگمە بە INPUTـی سادە و بەبێ : پەستانی هەڕەمەکی.Use INPUT_PULLUP, or an external 10 kΩ pull-up or pull-down.⁦INPUT_PULLUP⁩ بەکار بهێنە، یان یان دەرەکیی ⁦10 kΩ⁩.
Testing == HIGH for a press with INPUT_PULLUP.تاقیکردنەوەی == HIGH بۆ پەستان بە ⁦INPUT_PULLUP⁩.With a pull-up, pressed = LOW.بە ، پەستێنراو = LOW.
Two legs of the same pair: the button is always "pressed".دوو قاچی هەمان جووت: دوگمەکە هەمیشە «پەستێنراو»ـە.Use two legs at opposite corners.دوو قاچی دوو گۆشەی بەرامبەر بەکار بهێنە.
A wire instead of a resistor from the pin to 5 V.وایەرێک لە جیاتی لە پینەکەوە بۆ ⁦5 V⁩.Pressing would short 5 V to GND; use a resistor.پەستان ⁦5 V⁩ بە GNDـەوە کورت دەبەستێت؛ بەکار بهێنە.
Reacting to the level when you want one action per press.کاردانەوە بۆ ئاست کاتێک بۆ هەر پەستانێک یەک کردەوەت دەوێت.Detect the falling edge: compare with the last reading. بدۆزەوە: لەگەڵ کۆتا خوێندنەوەدا بەراوردی بکە.
Forgetting lastBtnState = currentBtnState;.لەبیرکردنی lastBtnState = currentBtnState;.Save the reading at the end of every loop().لە کۆتایی هەموو ⁦loop()⁩ێکدا خوێندنەوەکە هەڵبگرە.
No debounce: one press counts two or three times.بەبێ : یەک پەستان دوو یان سێ جار دەژمێردرێت.Accept a change only after 50 ms without a new change (millis()).گۆڕان تەنیا دوای ⁦50 ms⁩ بەبێ گۆڕانی نوێ وەربگرە ⁦(millis())⁩.
Long delay() calls to hide bounce.بانگکردنی ⁦delay()⁩ـی درێژ بۆ شاردنەوەی هەڵبەزینەوە.They miss quick presses and freeze the board; use millis().پەستانی خێرا لەدەست دەدەن و بۆردەکە ڕادەگرن؛ ⁦millis()⁩ بەکار بهێنە.
A button on pin 0 or 1 of an UNO R3 while Serial runs.دوگمەیەک لەسەر پینی 0 یان 1ـی ⁦UNO R3⁩ تا Serial کار دەکات.Use pins 2 to 7.پینەکانی 2 تا 7 بەکار بهێنە.
Printing on every pass of loop().نووسین لە هەموو جارێکی ⁦loop()⁩ـدا.Print only when the state changes.تەنیا کاتێک دۆخەکە دەگۆڕێت بنووسە.

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

ExerciseڕاهێنانCount the pressesژماردنی پەستانەکان

Change the debounced Lab 3 so that it counts presses and prints Presses: 1, Presses: 2, … instead of toggling an LED.

تاقیگەی 3ـی بە بگۆڕە بۆ ئەوەی پەستانەکان بژمێرێت و Presses: 1، Presses: 2، … بنووسێت لە جیاتی گۆڕینی .

Hintئاماژە

Keep the debounce part; replace the toggle with presses++.

بەشی بهێڵەوە؛ گۆڕینەکە بە presses++ بگۆڕە.

Show answerوەڵامەکە پیشان بدە
Answer: a debounced press counterوەڵام: ژمێرەری پەستان بە لابردنی هەڵبەزینەوەex_count.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Count the presses: one press = one count, with a 50 ms debounce
const int btnPin = 2;
const unsigned long debounceDelay = 50;

int presses = 0;
int lastReading = HIGH;
int stableState = HIGH;
unsigned long lastChange = 0;

void setup() {
  pinMode(btnPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int reading = digitalRead(btnPin);
  if (reading != lastReading) {
    lastChange = millis();
    lastReading = reading;
  }
  if (millis() - lastChange > debounceDelay && reading != stableState) {
    stableState = reading;
    if (stableState == LOW) {
      presses++;
      Serial.print("Presses: ");
      Serial.println(presses);
    }
  }
}
ExerciseڕاهێنانThe pull-down versionوەشانی بەرگری نزمکەرەوە

Rewire Lab 2 with an external 10 kΩ pull-down: the resistor from pin 2 to GND and the button from pin 2 to 5 V. Change the sketch so the LED still follows the button.

تاقیگەی 2 بە دەرەکیی ⁦10 kΩ⁩ دووپاتە ببەستەوە: لە پینی 2ـەوە بۆ GND و دوگمەکە لە پینی 2ـەوە بۆ ⁦5 V⁩. بگۆڕە بۆ ئەوەی هێشتا بەدوای دوگمەکەدا بڕوات.

Hintئاماژە

With a pull-down the logic is the natural one: pressed = HIGH.

بە لۆجیکەکە ئاساییە: پەستێنراو = HIGH.

Show answerوەڵامەکە پیشان بدە
Answer: plain INPUT and pressed = HIGHوەڵام: INPUTـی سادە و پەستێنراو = HIGHex_pulldown.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Lab 2 with an external 10 kOhm pull-down: the button goes to 5 V
const int btnPin = 2;
const int ledPin = 3;

void setup() {
  pinMode(btnPin, INPUT);        // the resistor on the breadboard holds it LOW
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  bool pressed = digitalRead(btnPin) == HIGH;   // pressed = HIGH now
  digitalWrite(ledPin, pressed ? HIGH : LOW);
  Serial.println(pressed ? "LED ON" : "LED OFF");
  delay(200);
}

The output is the same as Lab 2: only the wiring and the == HIGH test changed.

دەرچوونەکە وەک تاقیگەی 2یە: تەنیا بەستنەکە و تاقیکردنەوەی == HIGH گۆڕان.

ExerciseڕاهێنانLab 2 without the floodتاقیگەی 2 بەبێ پڕبوون

Keep the LED following the button, but print LED ON or LED OFF only when the state changes.

با هەر بەدوای دوگمەکەدا بڕوات، بەڵام LED ON یان LED OFF تەنیا کاتێک دۆخەکە دەگۆڕێت بنووسە.

Hintئاماژە

Remember the last state in a variable and compare, as in edge detection.

کۆتا دۆخ لە لەبیر بهێڵە و بەراورد بکە، وەک .

Show answerوەڵامەکە پیشان بدە
Answer: print on change onlyوەڵام: تەنیا لە کاتی گۆڕاندا بنووسەex_noflood.inoARDUINORuns on a phone (or an emulator)لەسەر مۆبایل کار دەکات
// Lab 2 without the flood: print only when the button changes
const int btnPin = 2;
const int ledPin = 3;

bool lastPressed = false;

void setup() {
  pinMode(btnPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  bool pressed = digitalRead(btnPin) == LOW;
  digitalWrite(ledPin, pressed ? HIGH : LOW);
  if (pressed != lastPressed) {           // only on a change (an edge)
    Serial.println(pressed ? "LED ON" : "LED OFF");
    lastPressed = pressed;
  }
  delay(20);                              // a short pause also hides most bounce
}
ExerciseڕاهێنانLonger codesکۆدی درێژتر

How many codes does the lock have with 3 buttons and 4 presses? With 4 buttons and 4 presses? Why is the second lock still weak against a brute-force attack?

قفڵەکە بە 3 دوگمە و 4 پەستان چەند کۆدی هەیە؟ بە 4 دوگمە و 4 پەستان؟ بۆچی قفڵی دووەم هێشتا بەرامبەر لاوازە؟

Show answerوەڵامەکە پیشان بدە
34=8144=2563^4 = 81 \qquad 4^4 = 256

Even 256 codes take only about 15 minutes at 3.5 seconds a try. A real lock adds more keys, longer codes and a growing wait after wrong tries.

تەنانەت ⁦256⁩ کۆد بە ⁦3.5⁩ چرکە بۆ هەر هەوڵێک تەنیا نزیکەی ⁦15⁩ خولەک دەخایەنێت. قفڵی ڕاستەقینە دوگمەی زیاتر، کۆدی درێژتر و چاوەڕێیەکی زیادبوو دوای هەوڵی هەڵە زیاد دەکات.

Summaryپوختە

  • An input pin with nothing connected floats and reads HIGH and LOW at random.
  • A pull-up resistor holds it HIGH (button to GND, pressed = LOW); a pull-down holds it LOW (button to 5 V, pressed = HIGH).
  • pinMode(pin, INPUT_PULLUP) switches on the weak pull-up inside the chip (20–50 kΩ on the UNO R3).
  • React to the falling edge (HIGH → LOW) for one action per press: compare with the last reading and save it every pass.
  • Contacts bounce for a few ms; accept a change only after 50 ms without a new change, timed with millis().
  • Keep buttons off pins 0 and 1 of an UNO R3; a 27-code lock falls to a brute-force attack in minutes.
  • پینێکی کە هیچی پێوە نەبەسترابێت هەڵدەواسرێت و بە هەڕەمەکی HIGH و LOW دەخوێنێتەوە.
  • لە HIGHـدا ڕایدەگرێت (دوگمە بۆ GND، پەستێنراو = LOW)؛ لە LOWـدا ڕایدەگرێت (دوگمە بۆ ⁦5 V⁩، پەستێنراو = HIGH).
  • pinMode(pin, INPUT_PULLUP) لاوازی ناو چیپەکە هەڵدەکات (⁦20–50 kΩ⁩ لەسەر ⁦UNO R3⁩).
  • بۆ یەک کردەوە بۆ هەر پەستانێک کاردانەوەت بۆ (HIGH بۆ LOW) هەبێت: لەگەڵ کۆتا خوێندنەوەدا بەراوردی بکە و لە هەموو جارێکدا هەڵیبگرە.
  • خاڵەکان چەند ms هەڵدەبەزنەوە؛ گۆڕان تەنیا دوای ⁦50 ms⁩ بەبێ گۆڕانی نوێ وەربگرە، بە ⁦millis()⁩ کاتەکەی بپێوە.
  • دوگمە لەسەر پینەکانی 0 و 1ـی ⁦UNO R3⁩ دامەنێ؛ قفڵێکی ⁦27⁩ کۆدی بە لە چەند خولەکێکدا دەشکێت.
Review questionsپرسیارەکانی پێداچوونەوە
Why does a button with INPUT_PULLUP read LOW when pressed?بۆچی دوگمەیەک بە ⁦INPUT_PULLUP⁩ کاتی پەستان LOW دەخوێنێتەوە؟

The pull-up holds the pin at the supply; pressing joins it straight to GND, so the pin reads 0 V.

پینەکە لە سەرچاوەی ڕادەگرێت؛ پەستان ڕاستەوخۆ بە GNDـەوەی دەبەستێت، بۆیە پینەکە ⁦0 V⁩ دەخوێنێتەوە.

What is the difference between a level and an edge?جیاوازیی نێوان ئاست و لێوار چییە؟

A level is the reading now (HIGH or LOW); an edge is the moment it changes. A held button is a level; pressing it is an edge.

ئاست خوێندنەوەی ئێستایە (HIGH یان LOW)؛ لێوار ساتی گۆڕانەکەیە. دوگمەیەکی گیراو ئاستە؛ پەستانی لێوارە.

Why 50 ms for debouncing?بۆچی ⁦50 ms⁩ بۆ لابردنی هەڵبەزینەوە؟

It is longer than nearly all contact bounce and shorter than a delay people notice.

لە نزیکەی هەموو درێژترە و لە دواکەوتنێک کە خەڵک هەستی پێ بکەن کورتترە.

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

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

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

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

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

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