In this post, I'll be sharing a mini project of mine, again using ESP 8266 NodeMCU chip and MicroPython.
This project mainly focuses on GPIO pins and methods for this chip. LEDs will be my output and I'll use a button as an Input. My input button will be used as a Start/Stop command for ongoing operation. Ongoing operation will be blinking of LEDs with varying frequency.
I'll implement this project in two different methods:
This project mainly focuses on GPIO pins and methods for this chip. LEDs will be my output and I'll use a button as an Input. My input button will be used as a Start/Stop command for ongoing operation. Ongoing operation will be blinking of LEDs with varying frequency.
I'll implement this project in two different methods:
- Using Asynchronous Polling
- Using Interrupt Request
Setting Up Circuitry
- GPIO 16, GPIO 5, GPIO 4, GPIO 0, GPIO 2, GPIO 14 pins will be configured as Output pins being pin_D0, pin_D1, pin_D2, pin_D3, pin_D4, pin_D5 respectively.
- GPIO 12 will be configured as Input pin and will be connected to a small button circuitry.
- Button simply behaves as a switch and default position is OPEN (no contact between side pins). By default, logic LOW (0) is applied to input pin, when button is pressed (switch is closed) logic HIGH (1) is applied to input pin.
1- Asynchronous Polling
Main body of the program consists of blinking each LEDs one by one. In this approach, value of the Input pin (GPIO 12) will be checked before each LED is blinked.
For complete code, see here => https://github.com/mdemiray/MicroPython-ButtonWithAsyncPolling
2- Interrupt Request
In this approach, an interrupt request is defined for Input Pin (GPIO 12) along with a callback function which will be called whenever and interrupt occurs. In callback function, I simply negate the current value of boolean variable and check this bool variable before blinking each LED.
# INPUT PIN - Button
button = machine.Pin(12, machine.Pin.IN)
# Interrupt Callback function
def mycallback(p):
global IsButtonPressed
IsButtonPressed = not IsButtonPressed
# Interrupt
button.irq(trigger=machine.Pin.IRQ_FALLING, handler=mycallback)
...
....
....
if IsButtonPressed:
ToggleGivenPin(pin_D0, sleepTime)
else:
sleepTime = 0.5
break
Summary
In this post a mini GPIO project is presented with two approaches: (i) Asynchronous Polling, (ii) Interrupt Request. Circuitry will be 100 % the same for both approaches, only coding will change.
Test Environment
Chip Used: ESP8266 NodeMCU (Flash Size 4 MB)
Firmware Used: esp8266-20171101-v1.9.3.bin
Circuit Components: 1K Resistors, LEDs, Button, Jumper Wires
Test Environment
Chip Used: ESP8266 NodeMCU (Flash Size 4 MB)
Firmware Used: esp8266-20171101-v1.9.3.bin
Circuit Components: 1K Resistors, LEDs, Button, Jumper Wires
Comments
Post a Comment