This project demonstrates how to control an LED using a momentary pushbutton switch. When the button is pressed, the LED toggles between ON and OFF states. The project has two versions:
- Arduino Version: Located in
arduino/Pushbutton
, this version uses the Arduino framework. - Embedded C Version: Located in
src/
, this version is written in pure Embedded C, using direct register manipulation for greater flexibility and compatibility with non-Arduino environments.
- Arduino Board
- Breadboard
- Jumper Wires
- LED
- Momentary Tactile Pushbutton (4-pin)
- 10kΩ Resistor (for pull-up or pull-down)
- 220Ω Resistor (for current limiting)
The pushbutton has four pins, but only two are used in this project. When the button is pressed, it completes the circuit, allowing current to flow, which toggles the LED. The button acts as a momentary switch, meaning it only stays on while pressed.
The LED is connected to an output pin, and a 220Ω resistor is placed in series to limit the current.
- Place the pushbutton on the breadboard.
- Connect pin A of the pushbutton to one leg of the 10kΩ resistor and Arduino pin 2. Connect the other leg of the resistor to the GND rail on the breadboard.
- Connect pin B of the pushbutton to the +5V rail.
- Add the LED to the breadboard: connect the longer (positive) leg to Arduino pin 12 via a 220Ω resistor and the shorter (negative) leg to the GND rail.
The original Arduino version uses the Arduino-specific functions (pinMode()
, digitalWrite()
, etc.) for simple and quick development. The code for this version is located in the arduino/Pushbutton/
directory.
The Embedded C version, located in the src/
directory, is designed for more advanced users who want to work with direct register manipulation and avoid Arduino dependencies. This version ensures compatibility with a broader range of embedded systems.
In this version:
- GPIO and Timer Functions: Instead of using Arduino's
digitalWrite()
orpinMode()
, we control the hardware directly via registers (DDRB
,PORTB
, etc.). - Debouncing: A software debounce mechanism is implemented using custom timing and delay functionality to prevent multiple button presses from being registered due to mechanical bounce.
- When the button is pressed, the microcontroller detects the state change and toggles the LED between ON and OFF states.
- A debouncing mechanism ensures that only one button press is registered even if there is noise from the mechanical button press.
- Build the circuit as described in the circuit build section.
- Open the
arduino/Pushbutton/Pushbutton.ino
file in the Arduino IDE. - Connect your Arduino to the computer and upload the code.
- Press the button to toggle the LED between ON and OFF states.
- GNU AVR Toolchain: Ensure
avr-gcc
,avr-libc
, andavrdude
are installed. - Make: For building the project using the
Makefile
.
- Build the circuit as described in the circuit build section.
- Clone or download the repository and navigate to the root directory.
- Modify the Makefile: Open the
Makefile
and update thePORT
line to match your system's serial port. For example:PORT = /dev/ttyUSB0 # Update this to match your system
- Run the following commands to compile and upload the Embedded C version to your board:
make clean # Clean previous build files make # Compile the project and generate the .hex file make upload # Upload the compiled .hex file to your board
- Press the button to toggle the LED between ON and OFF states.