Arduino - Knop - LED

We gaan leren hoe u:

We leren hoe u de LED telkens laat wisselen van toestand bij het indrukken van de knop in de tutorial Arduino - Knop schakelt LED.

Hardware Benodigd

1×Arduino Uno R3
1×USB 2.0-kabel type A/B
1×Breadboard-montageknop met kap
1×Breadboard-montageknoppenkit
1×Paneel-montage drukknop
1×Drukknopmodule
1×LED Kit
1×LED (red)
1×220 ohm weerstand
1×Breadboard (experimenteerprint)
1×Jumperdraden
1×(Aanbevolen) Schroefklem Block Shield voor Arduino Uno
1×(Aanbevolen) Breadboard-Shield voor Arduino Uno
1×(Aanbevolen) Behuizing voor Arduino Uno
1×(Aanbevolen) Prototyping Basisplaat & Breadboard Kit voor Arduino Uno

Of u kunt de volgende kits kopen:

1×DIYables Sensorkit (30 sensoren/displays)
1×DIYables Sensorkit (18 sensoren/displays)
Openbaarmaking: Sommige van de links in deze sectie zijn Amazon-affiliate links. We kunnen een commissie ontvangen voor aankopen die via deze links worden gedaan, zonder extra kosten voor u. We waarderen uw steun.

Over LED en Knop

Als u nog niet bekend bent met LED en knop (pinout, werking, hoe te programmeren, enz.), leer er meer over in de volgende tutorials:

Bedradingsschema

Arduino Knop LED Bedradingsschema

This image is created using Fritzing. Click to enlarge image

Arduino Code

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-button-led */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin const int LED_PIN = 3; // the number of the LED pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(LED_PIN, OUTPUT); // initialize the pushbutton pin as an pull-up input: // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(BUTTON_PIN); // control LED according to the state of button if(buttonState == LOW) // If button is pressing digitalWrite(LED_PIN, HIGH); // turn on LED else // otherwise, button is not pressing digitalWrite(LED_PIN, LOW); // turn off LED }

Snelle Stappen

  • Verbind de Arduino met de pc via de USB-kabel
  • Open de Arduino IDE, selecteer het juiste board en de juiste poort
  • Kopieer bovenstaande code en open deze met de Arduino IDE
  • Klik op de Upload-knop in de Arduino IDE om de code naar de Arduino te uploaden
Arduino IDE Upload Code
  • Druk de knop in en houd deze enkele seconden ingedrukt
  • Bekijk hoe de status van de LED verandert

Code-uitleg

Lees de regel-voor-regel uitleg in de commentaarregels van de broncode!

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!