Control an RGB LED with Push Buttons and ESP8266 NodeMCU

ESP8266 10-10-25
21 0

Tutorial plan

1- Objective of the tutorial

2- The components needed to control RGB LED module with push buttons

3- Mounting ESP8266 NodeMCU card with RGB LED module and push buttons

4- Program ESP8266 NodeMCU with MicroPython

 

Objective of the tutorial

The main objective of this tutorial is to explain step by step how to control an RGB LED using multiple push buttons connected to the ESP8266 NodeMCU board.

This project helps learners understand how to use digital inputs and outputs to interact with external components such as LEDs and buttons.

By following this tutorial, you will learn how to:

- understand the working principle of an RGB LED, including the difference between common anode and common cathode types.

- connect the RGB LED and push buttons properly to the ESP8266 NodeMCU using a breadboard and jumper wires.

- write and upload a MicroPython program to the ESP8266 NodeMCU to control the RGB LED according to the button pressed.

- use the digital input pins of the ESP8266 to detect the state of each push button.

- use the PWM (Pulse Width Modulation) outputs to adjust or mix colors of the RGB LED.

- combine different buttons to create new colors by mixing red, green, and blue light.

- develop a better understanding of basic electronics, MicroPython programming, and interactive control systems using the ESP8266 NodeMCU.

 

The components needed to control RGB LED module with push buttons

1- ESP8266 NodeMCU Board

The microcontroller to read button inputs and control the RGB LED.

2- RGB LED Module

A module containing a common anode or cathode RGB LED with built-in resistors (simplifies wiring).

3- Push Buttons

bouton-poussoir

They are used for selecting or activating each color of the RGB LED (Red, Green, Blue).

4- Breadboard

plaque d'essai

They are used to  easy connections and prototyping.

5- Jumper Wires

Fils de connexion

They are used to connect the push buttons and RGB LED module to the ESP8266 NodeMCU.

 

Mounting ESP8266 NodeMCU card with RGB LED module and push buttons

Once you have the necessary components, you can proceed with the following steps to control the RGB LED module:

1- Connect pin R (red) of RGB LED module to D0 (GPIO16) of the ESP8266 board

2- Connect pin G (green) of RGB LED module to D1 (GPIO5) of the ESP8266 board

3- Connect pin B (blue) of RGB LED module to pin D2 (GPIO4) of the ESP8266 board

4- Connect pin GND of RGB LED module to pin GND of the ESP32 board

For push buttons 

1- Push button 1:

- Connect one leg of the push button to D3 (GPIO0) of the ESP8266 board.

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

2- Push button 2:

- Connect one leg of the push button to D4 (GPIO2) of the ESP8266 board.

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

3- Push button 3:

- Connect one leg of the push button to D5 (GPIO 14) of the ESP8266 board.

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

 

Program ESP8266 NodeMCU with MicroPython

To program the ESP8266 board with MicroPython to control RGB LED module using push buttons, write the MicroPython code

Here's a simple example of code to get you started :

1- Import the necessary modules:

2- Configure the pins (GPIO) for the push buttons and the three pins of RGB LED:

3- Create a variable to keep the state of the RGB LED (0 if the color of RGB LED is off and 1 if the color of RGB LED is on)

Create a loop to monitor the push buttons status and turn the three LEDs on/off accordingly:

Here is the complete program in Micropython:

When the button is pressed (the state is low), the LED lights up. If the button is pressed another time, the LED 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 RGB LED module using  push buttons with the ESP32 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