The objective of this tutorial is to learn how to control two LEDs using the ESP8266 NodeMCU board and two push buttons. Each button will be used to control one LED — when a button is pressed, the corresponding LED will turn on, and when it is released, the LED will turn off.
Through this project, you will:
- understand how to connect LEDs and push buttons to the ESP8266 NodeMCU.
- learn to configure the GPIO pins as inputs (for buttons) and outputs (for LEDs).
- write and upload a MicroPython program to control LEDs based on button inputs.
- gain practical experience with basic digital input/output operations on the ESP8266.
1. ESP8266 NodeMCU Board
The main microcontroller used to control the LEDs and read the state of the push buttons.
Two LEDs (Light Emitting Diodes)
The output devices that will blink according to the program.
Two Resistors (220Ω – 330Ω)
They are used in series with the LEDs to limit current and prevent them from burning out.
Two Push Buttons
The Push Buttons are used as digital input devices to control the LEDs.
When pressed, they send a signal to the ESP8266 to turn on the corresponding LED.
Breadboard
A prototyping board to easily connect all the components without soldering.
Jumper Wires
They make the electrical connections between the NodeMCU, resistors, push buttons and LEDs.
1- Place the NodeMCU on the Breadboard
2- Place the red LED on the breadboard :
a) The anode (long leg, +) will connect to one leg of a 220Ω resistor
b) The other leg of the resistor should go to D0 (DPIO16)
c) The cathode (short leg, –) will connect to GPIO pin of ESP8266
3- Place the blue LED on the breadboard.
a) The anode (long leg, +) will connect to one leg of a 220Ω resistor
b) The other leg of the resistor should go to D1 (DPIO5)
c) The cathode (short leg, –) will connect to GPIO pin of ESP8266
4- Place the first push button on the breadboard.
a) connect one side of the button to GPIO D2 (DPIO4).
b) connect the other side of the button to the GND pin of ESP8266.
5- Place the second push button similarly:
a) Connect one side to GPIO D3 (DPIO0).
b) Connect the other side to the GND pin of ESP8266.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import machine from machine import Pin import time # Define red LED pin red_led=Pin(16, Pin.OUT) # Define button 1 pin red_button = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP) red_state=0; # Define blue LED pin blue_led=Pin(5, Pin.OUT) # Define button pin blue_button = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP) blue_state=0; while True: # Read button 2 state red_first = red_button.value() time.sleep(0.1) red_second = red_button.value() if red_first and not red_second: print('Button pressed!') if (red_state==0): red_led.value(1) # Turn on red LED red_state=1 else: red_led.value(0) # Turn off red LED red_state=0; time.sleep(0.5) elif not red_first and red_second: print('Button released!') # Read button 1 state blue_first = blue_button.value() time.sleep(0.1) blue_second = blue_button.value() if blue_first and not blue_second: print('Button pressed!') if (blue_state==0): blue_led.value(1) # Turn on blue LED blue_state=1 else: blue_led.value(0) # Turn off blue LED blue_state=0; time.sleep(0.5) elif not blue_first and blue_second: print('Button released!') |
Explanation of the Code
1- Import libraries
machine is used to access GPIO pins.
time provides delay functions.
2- Pin Configuration
red_led and blue_led are set as output pins to control the LEDs.
button1 and button2 are set as input pins.
3- Infinite Loop (while True)
Continuously reads the state of each button.
If a button is pressed (value() == 1), the corresponding LED turns ON.
If the button is released (value() == 0), the LED turns OFF.
4- Sleep Delay
A short delay (sleep(0.1)) helps to stabilize button readings and reduce CPU usage.
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.
Zaouiet Kontech-Jemmel-Monastir-Tunisia
+216 92 886 231
medaliprof@gmail.com
Robotic site created by MedAli-Teacher info