Controlling LED Lights with Buttons Using Arduino Uno

Controlling LED Lights with Buttons Using Arduino Uno

Performance Parameters① Main control chip: ATmega328P② Digital input/output pins: 14③ PWM pins: 6④ Storage (code space) 32KB⑤ RAM (runtime memory) 2KB; EEPROM (power-off storage) 1KB⑥ Crystal oscillator 16MHz Online Simulation: https://wokwi.com/

Arduino Uno Tutorial ①: Install Arduino IDE

Arduino Uno Tutorial ②: Development Board and Light Test

Arduino Uno Tutorial ③: Button Control LED Light, with Exercises

(1) Introduction Buttons are input devices that can control microcontrollers and are one of the important devices for microcontroller interaction.

In this tutorial, we will use the TTP223 Touch Button Module to control the LED light.

Controlling LED Lights with Buttons Using Arduino Uno

(2) Wiring① For convenience, we will use the onboard LED (pin 13).② The button can be connected to any pin that supports input; here we choose pin 2.③ Make sure not to connect the module’s VCC and GND incorrectly.

Controlling LED Lights with Buttons Using Arduino Uno

(3) Button Lighting Program① First, note that the TTP223 module is active high, and the LED light is also active high.

② Then, TTP223 is in input mode, pin 2; LED is in output mode, pin 13.③ Initialization program, set pin 2 as input and pin 13 as output;

void setup() { pinMode(13,OUTPUT); pinMode(2,INPUT); digitalWrite(13, LOW); // Initialize LED to off state}

④ Set a variable; when judging with if, only check the variable state.

int bool1 = digitalRead(2);// Read pin 2 and store in variable bool1

⑤ The if function checks the high and low level (whether the button is pressed). If high level, write high to the LED; if low level, write low to the LED.

if(bool1 == HIGH)// If bool1 is high { digitalWrite(13, HIGH); // Light up LED } else { digitalWrite(13, LOW); // Turn off LED }
⑥ Result demonstration

Controlling LED Lights with Buttons Using Arduino Uno

(4) One Button Controls Light On/Off① With one button, you can control a light to turn on and off; when the light is on, press the button to turn it off, and when the light is off, press the button to turn it on.② Initialization

void setup() { pinMode(13,OUTPUT); pinMode(2,INPUT); digitalWrite(13, LOW); // Initialize LED to off state}

③ Set two variables, bool1 to save button state, led1 to save light state.

int bool1 = digitalRead(2);// Read pin 2 and store in variable bool1 int led1 =  digitalRead(13);// Read LED state

④ First, check if the button is pressed; if the button is high, enter if. If the LED is high at this time, write low; otherwise, write high. The delay function is used to prevent false triggering.

if(bool1 == HIGH)// If bool1 is high { if(led1 == HIGH) { digitalWrite(13, LOW); // Initialize LED to off state delay(200); } else { digitalWrite(13, HIGH); // Initialize LED to off state delay(200); }}

⑤ Experimental phenomenon: when the button is pressed, the light stays on or stays off.

Controlling LED Lights with Buttons Using Arduino Uno

(5) ExercisesQuestion: Use two buttons (connected to digital pins 2 and 3) to write a program that lights up the LED connected to digital pin 9 when either button is pressed; when neither button is pressed, turn off the LED. Answer:

const int button1 = 2; const int button2 = 3; const int ledPin = 9; void setup() { pinMode(button1, INPUT); pinMode(button2, INPUT); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); // Initialize LED to off state } void loop() { int button1State = digitalRead(button1); int button2State = digitalRead(button2); if (button1State == HIGH || button2State == HIGH) { digitalWrite(ledPin, HIGH); // If either button is pressed, light up LED } else { digitalWrite(ledPin, LOW); // If neither button is pressed, turn off LED }}

To be continued…

END

Free Application Development Board

Controlling LED Lights with Buttons Using Arduino Uno

Submission/Promotion/Cooperation/Join Group Please scan the code to add WeChat

(Please indicate your intention, for joining the group please note city-name-industry position information)

Controlling LED Lights with Buttons Using Arduino Uno

Controlling LED Lights with Buttons Using Arduino Uno

Leave a Comment

×