In this project, you will control Toggle LED Using OR Operator in MicroPython for ESP32 and ESP8266. The LED will turn on if either Button A or Button B is pressed. This project demonstrates how to use the or operator, which is a fundamental logical operator that checks if at least one of the conditions is true. This is an excellent beginner-friendly project for learning how to use multiple input buttons to control a single output in MicroPython.
Data Types and Variable Table for Toggle LED Using OR Operator:
Data Type | Variable Name | Description | Example Value |
Boolean | button_a_pressed | Stores the state of Button A (True/False) | True |
Boolean | button_b_pressed | Stores the state of Button B (True/False) | False |
Boolean | led_state | Controls whether the LED is on or off | True or False |
In this project, boolean variables are used to store whether Button A or Button B is pressed, while another boolean variable controls the LED’s state.
Syntax Table for Toggle LED Using OR Operator in MicroPython:
Function | Syntax | Example |
Read button input | button.value() | if button_a.value() == 1: |
Apply OR condition | if condition1 or condition2: | if button_a_pressed or button_b_pressed: |
Set LED state | led.value() | led.value(1) (Turn LED on) |
This project uses the OR operator (or) to check if either Button A or Button B is pressed to toggle the LED.
Required Components:
- ESP32 or ESP8266
- 1 LED
- 2 Buttons (Button A and Button B)
- 2 Resistors (10kΩ for buttons, 220Ω for LED)
- Breadboard
- Jumper wires
Circuit Diagram:
LED ESP32/ESP8266
—– ————
Anode ——> GPIO5 (Pin 5)
Cathode ——> GND
Button A ESP32/ESP8266
—– ————
One Leg ——> GPIO4 (Pin 4)
Other Leg —-> GND
Button B ESP32/ESP8266
—– ————
One Leg ——> GPIO15 (Pin 15)
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 A (Leg 1) | Connect to GPIO4 | GPIO4 on ESP32/ESP8266 | Detects when Button A is pressed |
Button A (Leg 2) | Connect to GND | GND on ESP32/ESP8266 | Completes Button A circuit |
Button B (Leg 1) | Connect to GPIO15 | GPIO15 on ESP32/ESP8266 | Detects when Button B is pressed |
Button B (Leg 2) | Connect to GND | GND on ESP32/ESP8266 | Completes Button B circuit |
Warnings:
- Ensure the LED has a 220Ω resistor to prevent it from burning out due to excessive current.
- Use 10kΩ pull-down resistors with the buttons to ensure stable and accurate readings.
Circuit Analysis:
- Button A is connected to GPIO4, and Button B is connected to GPIO15 on the ESP32/ESP8266. These buttons act as inputs that detect user presses.
- The LED is connected to GPIO5 and will turn on when either Button A or Button B is pressed, thanks to the use of the OR operator in the code.
Installing MicroPython and Required Libraries:
Install MicroPython on ESP32/ESP8266: Ensure MicroPython is installed on your ESP32 or ESP8266. You can flash the MicroPython firmware using esptool or Thonny:
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 sufficient for handling GPIO pins in this project.
Writing the MicroPython Code for Toggle LED Using OR Operator:
import machine
import time
# Initialize the LED (GPIO5), Button A (GPIO4), and Button B (GPIO15)
led = machine.Pin(5, machine.Pin.OUT)
button_a = machine.Pin(4, machine.Pin.IN)
button_b = machine.Pin(15, machine.Pin.IN)
while True:
# Check if Button A or Button B is pressed
button_a_pressed = button_a.value() == 1
button_b_pressed = button_b.value() == 1
# If either button is pressed, turn on the LED
if button_a_pressed or button_b_pressed:
led.value(1) # Turn the LED ON
print(“LED ON – Either Button A or Button B is pressed”)
else:
led.value(0) # Turn the LED OFF
print(“LED OFF”)
# Add a short delay to avoid rapid state changes
time.sleep(0.1)
Explanation of the Code:
- LED, Button A, and Button B are initialized using their respective GPIO pins.
- The program continuously checks if either Button A or Button B is pressed using the OR operator (or).
- If either button is pressed, the LED is turned on. Otherwise, the LED remains off.
- The LED state is printed to the console, indicating whether the LED is ON or OFF.
Running the Code and Checking the Output:
- Upload the code to your ESP32/ESP8266 using Thonny or another MicroPython IDE.
- Press Button A or Button B, and observe the LED turning on.
The output on the serial monitor will show:
LED ON – Either Button A or Button B is pressed
LED OFF
Expanding the Project:
- Add a Buzzer: Include a buzzer that sounds when either button is pressed, along with the LED lighting up.
- Add More Buttons: Expand the project by adding additional buttons and controlling multiple LEDs based on different conditions.
- Wireless Control: Integrate Wi-Fi to control the buttons and LED remotely via a mobile app.
Common Problems and Solutions:
- Problem: The LED does not turn on when either button is pressed.
- Solution: Double-check the button wiring and ensure the correct GPIO pins are used in the code for Button A and Button B.
- Problem: The LED flickers or behaves erratically.
- Solution: Ensure that pull-down resistors are correctly connected to stabilize the button inputs.
- Problem: The serial monitor does not display the expected output.
- Solution: Ensure that the MicroPython code is correctly uploaded and that the serial monitor is open and working in your IDE.
FAQ:
Q: Can I use different GPIO pins for the buttons and LED?
A: Yes, you can change the GPIO pin assignments in the code by updating the pin numbers during initialization, such as button_a = machine.Pin(pin_number, machine.Pin.IN).
Q: Can I add more buttons to this project?
A: Yes, you can easily expand the project by adding more buttons and using additional logical operators like and or not to control the LED in different ways.
Q: Why do I need pull-down resistors for the buttons?
A: Pull-down resistors ensure that the GPIO pins read a LOW state when the buttons are not pressed, preventing erratic behavior caused by floating input values.
Conclusion:
In this project, you learned how to toggle an LED using the OR operator in MicroPython for ESP32 and ESP8266. By using two buttons, you controlled the LED to turn on when either Button A or Button B is pressed.