Simple Doorbell System Using MicroPython for ESP32 and ESP8266

In this project, you’ll create a Simple Doorbell System Using MicroPython for ESP32 and ESP8266. A button will act as the doorbell trigger, and when pressed, it will turn on an LED to simulate a doorbell ringing. This project is perfect for beginners and introduces the concepts of pin mode and digital input/output.

What is Pin Mode and Digital Input in MicroPython for ESP32 and ESP8266?

  • Pin Mode: This defines whether a GPIO pin will act as an input or output.
  • Digital Input: This reads the state of a pin, typically detecting if a button is pressed or not (HIGH or LOW).

Purpose of the Project:

This project will teach you how to:

  • Set a GPIO pin as input to read the button press.
  • Set another GPIO pin as output to control an LED.
  • Create a simple doorbell system, where pressing the button turns on the LED to indicate the doorbell is activated.

MicroPython Syntax for Simple Doorbell System:

Operation Syntax Example
Set pin mode to output led = Pin(pin_number, Pin.OUT) led = Pin(5, Pin.OUT)
Set pin mode to input button = Pin(pin_number, Pin.IN) button = Pin(14, Pin.IN)
Read pin state (button press) button.value() if button.value() == 1:
Write pin state (LED control) led.value(state) led.value(1) (Turn LED on)

Required Components:

  • ESP32 or ESP8266
  • 1 LED
  • 1 Resistor (220Ω)
  • 1 Push Button
  • Breadboard
  • Jumper Wires

Circuit Diagram:

   Button        LED       ESP32/ESP8266

    ——-      —–      ————–

    One Pin     Anode      GPIO5 (Pin 5)

    Other Pin   Cathode    GND

    One Pin     GPIO14

    Other Pin   GND

 

Circuit Connection Table:

Component Pin ESP32/ESP8266 Pin Explanation
LED Anode Connect to GPIO5 GPIO5 Controls the LED on/off state
LED Cathode Connect to GND GND Ground connection for the LED
Button Pin 1 Connect to GPIO14 GPIO14 Reads the state of the button (pressed or not)
Button Pin 2 Connect to GND GND Ground connection for the button

Warnings:

  • Make sure to use a 220Ω resistor with the LED to prevent it from drawing too much current and burning out.
  • Ensure proper button wiring for correct detection of button presses.

Circuit Analysis:

In this simple doorbell system, an LED connected to GPIO5 will light up when the button connected to GPIO14 is pressed. The button functions as the doorbell trigger, and the LED simulates the doorbell being rung.

Installing MicroPython and Libraries:

Install MicroPython on your ESP32 or ESP8266 using Thonny or esptool:
esptool.py –chip esp32 erase_flash

esptool.py –chip esp32 write_flash -z 0x1000 esp32-20210902-v1.17.bin

  1. No Additional Libraries Required: The built-in machine library in MicroPython is sufficient for this project.

Writing the MicroPython Code for Simple Doorbell System:

import machine

import time

 

# Initialize LED and Button

led = machine.Pin(5, machine.Pin.OUT)

button = machine.Pin(14, machine.Pin.IN)

 

while True:

    # Read the button state

    if button.value() == 1:

        # Turn the LED on to simulate doorbell activation

        led.value(1)

        print(“Doorbell is ringing!”)

        

        # Keep LED on for 2 seconds

        time.sleep(2)

        

        # Turn the LED off

        led.value(0)

    time.sleep(0.1)  # Debounce the button

Running the Code and Checking the Output:

  1. Upload the code to your ESP32/ESP8266 using Thonny or another MicroPython IDE.
  2. Press the button to simulate ringing the doorbell (turning the LED on).
  3. Observe the console: It will print “Doorbell is ringing!” when the button is pressed, and the LED will turn on for 2 seconds.

Explanation of the Code:

  • Pin Initialization: The LED is connected to GPIO5, and the button is connected to GPIO14.
  • Button Read: The button’s state is read using button.value(). If pressed, the LED is turned on for 2 seconds to simulate a doorbell ring.
  • Debouncing: A small delay (time.sleep(0.1)) is added to debounce the button and avoid multiple triggers.

Expanding the Project:

  • Add Sound: Connect a buzzer along with the LED to produce a sound when the doorbell is pressed.
  • Add Wireless Notification: Use Wi-Fi to send a notification to your smartphone when the doorbell is pressed.
  • Add Multiple Doorbells: Extend the project to include multiple doorbells for different entry points.

Common Problems and Solutions:

  1. Problem: The LED doesn’t light up when the button is pressed.
    • Solution: Check the button connections and make sure the LED is connected correctly with a resistor.
  2. Problem: The button press is detected multiple times.
    • Solution: Ensure you are using debouncing with time.sleep(0.1) to prevent multiple triggers.
  3. Problem: The code throws an error when reading the button state.
    • Solution: Ensure that the button pin is correctly set to Pin.IN mode.

FAQ:

Q: Can I use different GPIO pins for the button and LED?
A: Yes, you can use any available GPIO pins on your ESP32 or ESP8266, but remember to update the pin numbers in your code.

Q: How do I modify the code to keep the LED on until the button is pressed again?
A: You can use a toggle mechanism in the code to alternate between turning the LED on and off with each button press.

Q: Can I use a different type of button for this project?
A: Yes, any momentary push button will work as long as it is connected properly to the GPIO pin and ground.

Conclusion:

In this project, you created a simple doorbell system using MicroPython for ESP32 and ESP8266. The button acts as the doorbell trigger, and the LED simulates the doorbell ringing when pressed. This project introduces the concepts of pin mode and digital input/output and can be expanded to include sound, wireless notifications, or additional functionality like multiple doorbells.