# Building a Simple Light Switch
In this STEM project, we’ll guide you through building a simple light switch using a Lonely Binary Uno R3 (an Arduino-compatible microcontroller), a push button, and an LED. This hands-on activity will introduce you to basic circuitry, coding, and the fundamentals of input/output control—key concepts in science, technology, engineering, and math (STEM). By the end, you’ll have a working light switch that turns an LED on and off with the press of a button!
## What You’ll Need
To get started, gather these materials:
- **Lonely Binary Uno R3**: This is the brain of your project, a microcontroller similar to the Arduino Uno R3.
- **Push Button**: Acts as your switch, connected to GPIO 4.
- **LED**: The light you’ll control, connected to GPIO 3.
- **Resistors**:
- A 220-ohm resistor for the LED (to limit current).
- A 10k-ohm resistor for the button (as a pull-down resistor).
- **Breadboard**: A platform to build your circuit without soldering.
- **Jumper Wires**: To connect everything together.
- **USB Cable**: To connect your Uno R3 to a computer.
- **Computer with Arduino IDE**: For writing and uploading code.
## Understanding the Components
Before we dive in, let’s break down the key parts:
- **Lonely Binary Uno R3**: This microcontroller has multiple General Purpose Input/Output (GPIO) pins. We’ll use GPIO 4 as an input (for the button) and GPIO 3 as an output (for the LED).
- **Push Button**: When pressed, it sends a signal to the Uno R3. We’ll use a pull-down resistor to ensure the pin reads LOW (0V) when the button isn’t pressed.
- **LED**: Lights up when GPIO 3 sends a HIGH signal (5V). The resistor prevents too much current from damaging it.
## Step 1: Wiring the Circuit
Let’s build the circuit on your breadboard. Follow these steps carefully:
1. **Connect the Button:**
- Place the push button across the center gap of the breadboard so its legs are on opposite sides.
- Connect one leg to the 5V pin on the Uno R3 using a jumper wire.
- Connect the opposite leg to GPIO 4. From this same leg, connect a 10k-ohm resistor to GND (ground). This is your pull-down resistor, keeping GPIO 4 LOW when the button isn’t pressed.
2. **Connect the LED:**
- Insert the LED into the breadboard. The longer leg (anode) is the positive side, and the shorter leg (cathode) is the negative side.
- Connect the anode to GPIO 3 through a 220-ohm resistor.
- Connect the cathode directly to GND.
3. **Power the Uno R3:**
- Plug the USB cable into the Uno R3 and your computer. The onboard power LED should light up, indicating it’s ready.
Double-check your connections to avoid short circuits or incorrect wiring!
## Step 2: Writing the Code
Now, let’s program the Uno R3 to control the LED with the button. Open the Arduino IDE on your computer and follow these steps:
1. **Set Up Variables and Pin Modes:**
- In the `setup()` function, define GPIO 4 as an input and GPIO 3 as an output. This tells the Uno R3 how to treat each pin.
2. **Read the Button and Control the LED:**
- In the `loop()` function, continuously check the button’s state. If pressed (HIGH), turn the LED on (HIGH). If not pressed (LOW), turn it off (LOW).
Here’s the complete code:
```cpp
// Define pin numbers
const int buttonPin = 4; // Button connected to GPIO 4
const int ledPin = 3; // LED connected to GPIO 3
// Variable to store button state
int buttonState = 0;
void setup() {
// Set button pin as input
pinMode(buttonPin, INPUT);
// Set LED pin as output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// If button is pressed (HIGH), turn LED on
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
// If button is not pressed (LOW), turn LED off
else {
digitalWrite(ledPin, LOW);
}
}
```
## Step 3: Upload and Test
1. In the Arduino IDE, select your board (Tools > Board > Arduino Uno) and port (Tools > Port).
2. Click the “Upload” button (right arrow). The IDE will compile the code and send it to your Uno R3.
3. Press the button. The LED should light up when pressed and turn off when released. If it doesn’t work, check your wiring and code for errors.
## How It Works
- When you press the button, it connects GPIO 4 to 5V, making it HIGH. The code detects this and sets GPIO 3 HIGH, turning on the LED.
- When you release the button, the pull-down resistor brings GPIO 4 back to 0V (LOW), and the code turns GPIO 3 LOW, switching off the LED.
## Explore Further
Congratulations—you’ve built a light switch! Now, try these extensions:
- **Toggle Switch**: Modify the code to toggle the LED on/off with each press (hint: use a variable to track the LED’s state).
- **Multiple LEDs**: Add more LEDs to other GPIO pins and control them with the same button.
- **Blink Effect**: Make the LED blink when the button is pressed by adding `delay()` and alternating HIGH/LOW.
## Why This Matters
This project teaches you how microcontrollers process inputs and control outputs, a foundation for robotics, home automation, and more. Plus, it’s a fun way to apply STEM skills and spark creativity!
So, grab your Lonely Binary Uno R3, wire it up, and start experimenting. What will you build next? Happy tinkering!
Project - Simple Light Switch
RELATED ARTICLES
Photoresistor Sensor
Project - Temperature Measurement
NTC Temperature Sensor
Project - IR Sender
Project - IR Receiver
Remote infrared Sensor
Project - Motion Detection Alarm
- Choosing a selection results in a full page refresh.