Buttons on the microcontroller

FunduinoLast updated on

Pulldown-Widerstand verstehen und LEDs per Tastendruck schalten

Aufgabe: Eine LED soll fünf Sekunden leuchten, wenn du einen Taster betätigst.

Der Mikrocontroller kann an seinen digitalen Pins nicht nur Spannungen ausgeben, sondern auch auslesen. Genau das probierst du in diesem Programm aus.

You need this

Master-Taster mit farbigem Knopf - 12*12*6mm / 12*12*9mm - F23106071 - Funduino 12 x 12 mm pushbutton - colored button, 24 V / 50 mA, various colors — 12*12*6mm - White 0,25 € *Item no. F23106071About the product — 12 x 12 mm pushbutton - colored button, 24 V / 50 mA, various colors — 12*12*6mm - White
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 6 parts together 19,70 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Choose a microcontroller

Pins, schematic, and sketch in the steps change with your selection. It remains saved.

Why the button needs a pull-down resistor

There’s one special point when setting this up. If you simply connect the button to the microcontroller, voltage is present at the pin as soon as you press it. Imagine lots of electrons buzzing around at the pin. When you release the button, no new ones are added.

Here’s the key point: The electrons that settled on the pin earlier are still there, and only slowly escape through tiny leakage currents. So the microcontroller thinks the button is being held down for a very long time - until no electrons remain at the pin.

You can fix this by grounding the pin through a resistor of about 1000 ohms (1 kΩ). The electrons then flow away quickly, and the microcontroller recognizes that the button was only pressed briefly. Because the resistor always pulls the voltage at the input pin down to 0V, it’s called a pulldown resistor.

Caution: If you use a resistor that is too small, pressing the button can cause a short circuit in the microcontroller.

Activate an LED with the push of a button

Select your board below. The LED lights up for five seconds when you press the button.

Button and LED on the Arduino UNO
Arduino UNO schematic
Arduino UNO
// LED an Pin 6, Taster an Pin 7
int LEDgelb = 6;
int TasterGelb = 7;
// speichert den Status der LED: 0 = aus, 1 = ein
int StatusGelb = 0;
void setup()
{
  // LED wird als OUTPUT (Ausgang) definiert
  pinMode(LEDgelb, OUTPUT);
  // der Taster wird als INPUT (Eingang) deklariert
  pinMode(TasterGelb, INPUT);
}
void loop()
{
  // Taster abfragen
  StatusGelb = digitalRead(TasterGelb);
  // wenn der Taster gedrückt wurde
  if (StatusGelb == HIGH)
  {
    // LED für 5 Sekunden (5000 ms) einschalten
    digitalWrite(LEDgelb, HIGH);
    delay(5000);
    // LED ausschalten
    digitalWrite(LEDgelb, LOW);
  }
}

Download: arduino-uno.ino

Board in the Arduino IDEArduino Uno

You don’t need an additional board package for this. The UNO is included in the Arduino IDE by default.

Control two LEDs with two push buttons

Now you’re expanding the circuit with a second button and a red LED. Press the left button, and the yellow LED lights up for one second. Press the right button to light up the red LED.

Two buttons and two LEDs on the Arduino UNO
Arduino UNO circuit diagram, two buttons and two LEDs
Arduino UNO
// Pins für die LEDs
int LEDgelb = 8;
int LEDrot = 9;
// Pins für die Taster
int TasterRot = 6;
int TasterGelb = 7;
// speichert den Status der LEDs: 0 = aus, 1 = ein
int StatusGelb = 0;
int StatusRot = 0;
void setup()
{
  // LEDs werden als OUTPUT (Ausgang) definiert
  pinMode(LEDgelb, OUTPUT);
  pinMode(LEDrot, OUTPUT);
  // die Taster werden als INPUT (Eingang) deklariert
  pinMode(TasterGelb, INPUT);
  pinMode(TasterRot, INPUT);
}
void loop()
{
  // Taster für die gelbe LED abfragen
  StatusGelb = digitalRead(TasterGelb);
  if (StatusGelb == HIGH)
  {
    digitalWrite(LEDgelb, HIGH);
    delay(1000);
    digitalWrite(LEDgelb, LOW);
  }
  // Taster für die rote LED abfragen
  StatusRot = digitalRead(TasterRot);
  if (StatusRot == HIGH)
  {
    digitalWrite(LEDrot, HIGH);
    delay(1000);
    digitalWrite(LEDrot, LOW);
  }
}

Download: arduino-uno.ino

Board in the Arduino IDEArduino Uno

You don’t need an additional board package for this. The UNO is included in the Arduino IDE by default.

Rate the instructions

Comments

4 posts
Funduino TeamTeam

Hallo @Stefan B,
danke dir für das Lob und den guten Hinweis! Da hast du recht: Bei „erden“ denkt man schnell an den grün-gelben Schutzleiter. Gemeint ist GND, also der Minuspol des Mikrocontrollers. Die Formulierung haben wir angepasst. INPUT_PULLUP nehmen wir gern als Idee mit: Damit nutzt du den eingebauten Pullup-Widerstand und sparst dir den externen Widerstand. Dafür musst du umdenken, denn der Pin meldet beim Drücken dann LOW statt HIGH.
Funduino Team

Stefan B

Super erklärt!
Ich würde es vielleicht nicht “erden” nennen – obwohl GND ja Ground bedeutet.
Im Deutschen denkt man dann eher an so einen Grün/Gelben Draht ;-)

Jetzt noch das Ganze mit INPUT_PULLUP vergleichen – und es ist perfekt!

Funduino TeamTeam

Servus, danke fürs genaue Hinschauen! Du hast recht, die Taster-Pins im Code waren vertauscht. Ist jetzt korrigiert: Taster für Gelb an Pin 7, Taster für Rot an Pin 6.

Funduino Team

jei

Servus, im Code sind die Pins für die Taster in Bezug zum Schaltplan vertauscht: lt. Plan wird der Taster für die gelbe LED an Pin 7 und der für die rote LED an Pin 6 angeschlossen, im Code ist es genau andersherum (6 gelb und 7 rot).

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.