Control a lamp with ESP8266 NodeMCU and push button

ESP8266 08-10-25
9 0

Tutorial plan

1- Objective of this tutorial

2- The necessary components

3- Mounting ESP8266 NodeMCU card with lamp and push buttons

4- Program ESP8266 NodeMCU with MicroPython

 

Objective of this tutorial

The objective of this tutorial is to control a lamp using the ESP8266 NodeMCU board and a push button. By pressing the button, the user can turn the lamp ON or OFF through a relay module connected to the NodeMCU. This project demonstrates how to use digital input (push button) and digital output (relay controlling a 220V lamp) with the ESP8266.

Through this tutorial, you will learn how to:

- connect a relay module to the ESP8266 NodeMCU to control a high-voltage lamp safely.

- use a push button as a digital input to trigger actions.

- write a MicroPython program to detect button presses and toggle the lamp’s state.

- understand the basic principles of home automation using microcontrollers.

 

The necessary components

1. ESP8266 NodeMCU board:

The NodeMCU is a microcontroller board based on the ESP8266 Wi-Fi chip. It can be programmed using MicroPython or Arduino IDE. In this project, it acts as the main controller that reads the state of the push button and controls the relay to turn the lamp ON or OFF.

2. Lamp (220V AC):

The lamp is the load we want to control. It operates on 220V AC and will be switched ON or OFF by the relay module under the control of the NodeMCU.

3. Relay module:

The relay module acts as an electrical switch that allows the low-voltage NodeMCU to safely control a high-voltage device such as a lamp. When the NodeMCU sends a HIGH signal to the relay, the relay activates and closes the circuit to power the lamp.

4. Push button:

bouton-poussoir

The push button serves as a manual control input. When pressed, it sends a signal to one of the digital input pins of the NodeMCU, which then changes the state of the lamp (ON or OFF).

4. Power Supply (220V AC)

It provides the electrical power needed to operate the lamp.

Wiring:

They safely connect a 220V AC lamp to a relay module that is controlled by the ESP8266 NodeMCU board.

5. Breadboard:

plaque d'essai

The breadboard allows easy assembly of the components without soldering. It helps in making temporary connections between the NodeMCU, button, and relay.

6. Jumper wires:

Fils de connexion

These wires are used to connect different components together on the breadboard and to the NodeMCU. They ensure proper electrical communication between the microcontroller, relay, and button.

 

Mounting ESP8266 NodeMCU card with lamp and push buttons

1- Connect the Relay to the ESP8266 board:

- Connect the relay's control pin (S) to pin D0 (GPIO16) of ESP8266 board.

- Connect the pin (+) of relay to pin 3.3V of ESP8266 board.

- Connect the relay's ground pin (GND) to the ESP8266's GND.

2- Connect the push button to the ESP8266 board:

- Connect one leg of the push button to pin D1 (GPIO5) of the ESP8266 board.

- Connect another leg of the push button to pin GND of the ESP8266 board.

3- Connect the Lamp and the power supply to the Relay:

- Connect one of the power supply's wires to the relay's common (COM) terminal.

- Connect the phase wire of the the lamp to the normally open (NO) terminal of the relay.

- Connect the neutral wire of the power supply directly to the neutral wire of the lamp.

 

Program ESP8266 NodeMCU with MicroPython

You can connect to your ESP8622 board using a serial terminal (e.g., PuTTY, minicom) or use a tool like Thonny to write and execute MicroPython code.

Here's a simple example code to turn on the lamp when the push button is pressed:

1- Import the necessary modules:

2- Configure the pins (GPIO) for the push button and the relay who will light the lamp:

Make sure to connect the push button to GPIO pin D1 (GPIO5) and the relay to GPIO pin D0 (GPIO16).

3- Create a variable to keep the state of the relay (0 if the lamp is off and 1 if the lamp is on)

Create a loop to monitor the pushbutton status and turn the lamp on/off accordingly:

Here is the complete program in Micropython:

When the button is pressed (the state is low), the lamp lights up. If the button is pressed another time, the lamp turns off. The 0.5 second delay is used to prevent pushbutton bounce, which can cause multiple button activations with a single press.

This simple example demonstrates how to use MicroPython to control a lamp using a relay and a push button with an ESP8266 board.

0 comment

Leave a comment

Veuillez noter s'il vous plaît*

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Purpose of this website

Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.

Contact details

Zaouiet Kontech-Jemmel-Monastir-Tunisia

+216 92 886 231

medaliprof@gmail.com

Pictures of the articles

Robotic site created by MedAli-Teacher info