In this project, you will learn how to control the Blinking LED with If-Else in MicroPython for ESP32 and ESP8266. The LED will blink when a button is pressed, and it will remain off when the button is not pressed. This project demonstrates how to use if-else conditional logic, which is an essential part of programming. By working through this simple project, you’ll gain a solid understanding of how conditional statements work in MicroPython.
Data Types and Variable Table for Blinking LED with If-Else:
Data Type | Variable Name | Description | Example Value |
Boolean | button_pressed | Stores the state of the button (True/False) | True |
Boolean | led_state | Controls whether the LED is on or off | True or False |
The boolean variable tracks whether the button is pressed, and the LED state determines whether the LED is blinking or turned off.
Syntax Table for Blinking LED with If-Else in MicroPython:
Function | Syntax | Example |
Read button input | button.value() | if button.value() == 1: |
If-else statement | if condition: and else: | if button_pressed: |
Toggle LED | led.value() | led.value(1) (Turn LED on) |
Add a delay | time.sleep(seconds) | time.sleep(0.5) |
This project uses the if-else structure to control the LED’s behavior based on the state of the button.
Required Components:
- ESP32 or ESP8266
- 1 LED
- 1 Button
- 1 Resistor (220Ω for LED, 10kΩ for button)
- Breadboard
- Jumper wires
Circuit Diagram:
LED ESP32/ESP8266
—– ————
Anode ——> GPIO5 (Pin 5)
Cathode ——> GND
Button ESP32/ESP8266
—– ————
One Leg ——> GPIO4 (Pin 4)
Other Leg —-> GND
Circuit Connection Table:
Component | Connection | ESP32/ESP8266 Pin | Explanation |
LED Anode | Connect to GPIO5 | GPIO5 on ESP32/ESP8266 | Controls the LED ON/OFF state |
LED Cathode | Connect to GND | GND on ESP32/ESP8266 | Completes the LED circuit |
Button (Leg 1) | Connect to GPIO4 | GPIO4 on ESP32/ESP8266 | Detects when the button is pressed |
Button (Leg 2) | Connect to GND | GND on ESP32/ESP8266 | Completes the button circuit |
Warnings:
- Ensure that the LED has a 220Ω resistor to prevent it from being damaged by excessive current.
- Use a 10kΩ pull-down resistor for the button to stabilize the button input and avoid floating signals.
Circuit Analysis:
- The button is connected to GPIO4 and serves as an input to detect when it is pressed.
- The LED is connected to GPIO5, and it will blink when the button is pressed. When the button is not pressed, the LED will stay off.
- The program continuously checks the button state and uses an if-else statement to determine whether the LED should blink or remain off.
Installing MicroPython and Required Libraries:
Install MicroPython on ESP32/ESP8266: Ensure that MicroPython is installed on your ESP32 or ESP8266. Use esptool or Thonny to flash the MicroPython firmware:
esptool.py –chip esp32 erase_flash
esptool.py –chip esp32 write_flash -z 0x1000 esp32-20210902-v1.17.bin
- No Additional Libraries Needed: The built-in machine module is enough to handle GPIO control for the LED and button.
Writing the MicroPython Code for Blinking LED with If-Else:
import machine
import time
# Initialize the LED (GPIO5) and the button (GPIO4)
led = machine.Pin(5, machine.Pin.OUT)
button = machine.Pin(4, machine.Pin.IN)
while True:
# Check if the button is pressed
button_pressed = button.value() == 1
# If the button is pressed, blink the LED
if button_pressed:
led.value(1) # Turn LED ON
time.sleep(0.5) # Wait for half a second
led.value(0) # Turn LED OFF
time.sleep(0.5) # Wait for half a second
print(“LED Blinking”)
else:
led.value(0) # Keep LED OFF when the button is not pressed
print(“LED OFF”)
# Short delay to avoid rapid state changes
time.sleep(0.1)
Explanation of the Code:
- LED and button are initialized using their respective GPIO pins.
- The program continuously checks if the button is pressed by reading the button’s state.
- If the button is pressed, the LED blinks by turning it on, waiting for 0.5 seconds, then turning it off.
- If the button is not pressed, the LED remains off.
- The program adds a short delay to avoid rapid state changes in the button reading.
Running the Code and Checking the Output:
- Upload the code to your ESP32/ESP8266 using Thonny or another MicroPython IDE.
- Press the button to observe the LED blinking, and release the button to keep the LED off.
The output on the serial monitor will show:
LED Blinking
LED OFF
Expanding the Project:
- Add Multiple LEDs: Add more LEDs and control each one with a separate button using the if-else logic.
- Adjust Blinking Speed: Allow the button to control the speed of the blinking LED by adjusting the delay values.
- Add a Display: Use an OLED display to show the status of the button and LED in real-time.
Common Problems and Solutions:
- Problem: The LED does not blink when the button is pressed.
- Solution: Check the wiring of the button and the LED, and ensure the correct GPIO pins are being used in the code.
- Problem: The LED flickers or behaves inconsistently.
- Solution: Ensure the pull-down resistor is properly connected to prevent floating input signals from the button.
- Problem: The program does not respond to the button press.
- Solution: Make sure the button is correctly wired and that the GPIO pin reading the button’s state is properly initialized.
FAQ:
Q: Can I use a different GPIO pin for the button or LED?
A: Yes, you can use any available GPIO pin on the ESP32/ESP8266. Update the pin numbers in the code, such as button = machine.Pin(pin_number, machine.Pin.IN).
Q: How can I change the speed of the LED blinking?
A: You can adjust the time.sleep() values in the code to increase or decrease the time between the LED turning on and off.
Q: Can I control more than one LED with this project?
A: Yes, you can add more LEDs and buttons and use additional if-else statements to control multiple LEDs.
Conclusion:
In this project, you successfully built a blinking LED controlled by a button using if-else statements in MicroPython for ESP32 and ESP8266. The project demonstrates how to use conditional logic to control hardware components based on user input. This simple example can be expanded into more complex projects involving multiple LEDs, sensors, or even displays for real-time feedback.