Skip to main content

ESP8266 Mini Project: GPIO with Button and LEDs

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:

  1. Using Asynchronous Polling
  2. 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.






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 UsedESP8266 NodeMCU (Flash Size 4 MB)
Firmware Used: esp8266-20171101-v1.9.3.bin
Circuit Components: 1K Resistors, LEDs, Button, Jumper Wires


Comments

Popular posts from this blog

Mobile App Review: Amnesty SOS

A. INTRODUCTION In this post, I will review Amnesty International's mobile app for you on both Android and iOS platforms. Amnesty International has already been doing its own part for years successfully to: Stop violation of human rights  Protect people and communities who are in immediate danger Now it is your turn to do your part and  Join this great community by taking actions for human rights Speak out for people facing with unlawful, inhumane treatment Encourage friends and family to raise their voices for human rights Collect more signatures and amplify your impact  Share on social media Figure : Amnesty SOS App on Google Play Store Figure : Amnesty SOS App on iOS App Store Android App details are as follows: Version : 1.1 Released on : Feb 20, 2018 Developer e-mail : digitalcomms@amnesty.ca iOS App details are as follows: Version : 1.1.1 Released on : April 2017 Developer e-mail : digitalcomms@amnesty.ca B. ...

How to Record Screen of Your iPhone or Mac freely and easily?

There are a lot of fake apps on Appstore that you might waste your money just to record your screen. You give money buy you get nothing but a garbage. Although there are bunch of methods to record your screen, I will mention in this post the method which uses QuickTime Player. Here is how to record your screen: 1- Install QuickTime player (if you have not already) Download QuickTime player on your Mac. Use this  link  : If you have any trouble with installing QuickTime, have a look at  here for support : 2- Open QuickTime player. After you have installed QuickTime, you most probably find it in " Launchpad -> Other ". Or simply you can search it inside your Mac. 3- Connect your iPhone to your Mac with a lightning cable. To record screen of iPhone:  4- Click on "File -> New Movie Recording" 5- Click on the down arrow and select your connected iPhone. 6- Now your phone's screen is being displayed. You can st...

How to change Country/Region of App Store in iOS 10.0.2 on iPhone?

When you moved to any country other than your original country, you need to change the region/country settings of your iPhone/iPad so that you are able to download the apps specifically published for that country/region. 1- Open Settings App. 2- Tap on “iTunes & App Store”. 3- Tap on Apple ID: 4- Tap on “ View Apple ID ”. 5- Then sign in into your iTunes store either with your AppleID/Password or Touch ID . 6- Then, your iTunes account is opened as follows… Tap on " Country/Region" 7- Tap on “ Change Country or Region ”. 8- Select your current country or region from the list. 9- Then tap on “ Agree ” to agree with the Apple Media Services Terms and Conditions. 10- Tap again on “ Agree ” on the pop-up. 11- Now, Billing information on your account is displayed. Insert 3-digit security code at back of your credit card and tap on “ Next ” again. ...