One Push-Button Controlling 2 leds - Arduino Projects + Tinkercad

3 min read 4 months ago
Published on Aug 14, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you will learn how to control two LEDs with a single push-button using Arduino and Tinkercad. This project is a great introduction to basic electronics and programming with Arduino. By the end, you will understand how to set up your circuit and write the code necessary to operate the LEDs.

Step 1: Set Up Your Tinkercad Workspace

  • Go to the Tinkercad website and create a free account if you don’t have one.
  • Start a new circuit project by selecting “Circuits” from the dashboard and clicking on “Create New Circuit.”
  • Familiarize yourself with the workspace, where you will place your components.

Step 2: Gather Components

For this project, you will need the following components:

  • 1 Arduino UNO board
  • 1 push-button switch
  • 2 LEDs (one red and one green)
  • 2 220-ohm resistors
  • 1 10k-ohm resistor
  • Breadboard
  • Jumper wires

Step 3: Build the Circuit

  1. Place Components on the Breadboard:

    • Insert the push-button switch onto the breadboard.
    • Place the two LEDs, making sure to note their anodes (long leg) and cathodes (short leg).
  2. Connect the LEDs:

    • Connect the anode of the red LED to a digital pin on the Arduino (e.g., pin 8).
    • Connect the cathode of the red LED to a 220-ohm resistor, and then to the ground (GND) on the Arduino.
    • Repeat this process for the green LED, connecting it to another digital pin (e.g., pin 9).
  3. Connect the Push-Button:

    • Connect one terminal of the push-button to a digital pin on the Arduino (e.g., pin 7).
    • Connect the other terminal to the ground (GND).
    • Use a 10k-ohm resistor to pull the pin to ground when the button is not pressed.
  4. Power the Arduino:

    • Connect the Arduino to your computer using a USB cable to power it.

Step 4: Write the Arduino Code

  1. Open the Arduino IDE or Tinkercad's code editor.
  2. Write the following code to control the LEDs:
const int buttonPin = 7;
const int redLEDPin = 8;
const int greenLEDPin = 9;

int buttonState = 0;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(redLEDPin, HIGH);
    digitalWrite(greenLEDPin, LOW);
  } else {
    digitalWrite(redLEDPin, LOW);
    digitalWrite(greenLEDPin, HIGH);
  }
}
  1. This code defines pin numbers for the button and LEDs. In the loop(), it checks if the button is pressed and lights up the red LED. When released, it turns on the green LED.

Step 5: Upload and Test the Code

  • Upload the code to your Arduino board.
  • Press the push-button and observe the behavior of the LEDs:
    • The red LED should light up while the button is pressed.
    • The green LED should light up when the button is released.

Conclusion

In this tutorial, you successfully built a simple circuit to control two LEDs with a single button using Arduino and Tinkercad. This project not only demonstrates basic circuit design but also introduces you to programming the Arduino.

Next steps could include experimenting with different LED colors, adding more buttons, or integrating other components like buzzers or motors for more complex projects. Happy tinkering!