Arduino code for a push button to turn an LED on and off

Arduino code for a push button to turn an LED on and off

This tutorial will guide you through the process of using a push button to control an LED, turning it on when the button is pressed and off when released. This simple project is perfect for absolute beginners looking to get hands-on experience with Arduino programming and basic electronic components.

Throughout this guide, we will cover the necessary hardware components, wiring instructions, and the Arduino code you'll need to bring your project to life. If you want to see this in action, be sure to check the video for visual clarification (in video at 00:00).

Hardware Explained

The main components needed for this project include an LED, a push button, a resistor, and an Arduino board. The LED (Light Emitting Diode) has two leads: the longer one is the anode (positive), and the shorter one is the cathode (negative). The push button can either have two or four pins, allowing it to connect two wires with a single press or just one wire.

Arduino wiring for Push button wihthout resistor

The resistor, typically around 680 ohms, is used to limit the current flowing through the LED to prevent it from burning out. When the push button is pressed, it connects the input pin on the Arduino to ground, allowing us to read its state and control the LED accordingly.

Datasheet Details

ManufacturerGeneric
Part numberLED, Push Button, Resistor
LED Forward Voltage2.0-3.3 V (typ.)
LED Forward Current20 mA (max)
Resistor Value680 Ω
Push Button Voltage Rating12 V (max)

  • Use a current-limiting resistor for the LED (typically 680 ohms).
  • Ensure correct polarity when connecting the LED.
  • Connect the push button to ground to read its state correctly.
  • Utilize input pull-up resistors to eliminate the need for external resistors.
  • Test your connections before powering the Arduino.

Wiring Instructions

Arduino wriring for push button and LED
Arduino wriring for push button and LED

To wire the circuit, connect the longer leg of the LED (anode) to a breadboard and then link it to pin 10 on the Arduino using a yellow wire. The shorter leg (cathode) should connect to one end of a 680-ohm resistor, with the other end of the resistor connected to the ground rail on the breadboard.

For the push button, insert it into the breadboard with one pin connected to ground. Connect another pin of the push button to pin 2 on the Arduino using an orange wire. Finally, ensure that the Arduino's ground and 5V pins are connected to the respective ground and positive rails on the breadboard to power the components.

Code Examples & Walkthrough

The Arduino code consists of two main functions: setup() and loop(). In the setup() function, we define pin modes for the button and the LED. Here’s the relevant excerpt:

void setup() {
  pinMode(2, INPUT_PULLUP);// define pin two as input for push button
  pinMode(10, OUTPUT);// define pin 10 as output
}

In the loop() function, we continuously read the state of the button and control the LED accordingly. The following excerpt shows how we read the button state:

void loop() {
  int pushed = digitalRead(2);// read pin 2 and put the result in the "pushed" variable
  if(pushed == LOW){
    digitalWrite(10, HIGH);// turn the pin 10 HIGH (give it 5v)
  }else{
    digitalWrite(10, LOW);// set pin 10 to low or zero volt
  }
}

This code checks if the button is pressed (LOW state) and turns the LED on by sending a HIGH signal to pin 10. When the button is released, it sets pin 10 to LOW, turning the LED off. The full code loads below the article for your reference.

Demonstration / What to Expect

When you push the button, the LED should light up, and when you release it, the LED should turn off. This simple interaction demonstrates how digital input and output work in Arduino programming. Common pitfalls include incorrect wiring or not using a current-limiting resistor with the LED, which could damage it (in video at 04:00).

Video Timestamps

  • 00:00 - Introduction and Project Overview
  • 01:30 - Hardware Components Explanation
  • 03:00 - Wiring Instructions
  • 05:00 - Code Walkthrough
  • 06:30 - Demonstration

Bilder

Arduino wriring for push button and LED
Arduino wriring for push button and LED
LED in series with a resistor
LED in series with a resistor
Arduino wiring for Push button with resistor-2
Arduino wiring for Push button with resistor-2
Arduino wiring for Push button with resistor-1
Arduino wiring for Push button with resistor-1
Arduino wiring for Push button wihthout resistor
Arduino wiring for Push button wihthout resistor
33-Arduino code to use push button key
Sprache: C++
Kopiert!

Ressourcen & Referenzen

Noch keine Ressourcen vorhanden.

Dateien📁

Keine Dateien verfügbar.