echo '' ;

Archives

ESP8266 NodeMCU Interface – Button

Whan captures button presses and counts the number of pulses generated by these presses. Below is an explanation of the code “ESP8266 266 NodeMCU Interface Button”.

The button interface is a fundamental component in electronics and user interfaces, offering users a simple yet effective way to interact with devices.It consists of a push mechanism and contacts, completing an electrical circuit when pressed to trigger predefined tasks. Additionally, buttons are essential components in electronic devices, facilitating user input and control. Various types of buttons exist, including momentary buttons, toggle switches, and membrane buttons, each tailored to specific applications.

Consumer electronics, industrial control panels, automotive interfaces, and medical devices widely utilize buttons. Design considerations such as size, shape, color, and placement are crucial for usability. Incorporating debounce circuitry is also essential to eliminate bouncing effects. While buttons provide intuitive interaction and are cost-effective, they have limitations such as limited input options and potential wear over time. Additionally, designing interfaces with multiple buttons can pose challenges regarding space constraints and user ergonomics.

Uses – Button

Uses of Button InterfaceExample
1. Input SensingDetecting button presses for menu navigation
2. User InteractionInitiating actions such as turning on/off devices
3. Control MechanismControlling the speed of a motor based on button input
4. Event TriggeringTriggering alarms or notifications
5. User FeedbackProviding feedback through LED indicators
6. Configuration SettingsAdjusting settings in a user interface

Advantage and Disadvantage

AdvantagesDisadvantages
Simple and intuitive user interactionLimited input options compared to touchscreens
Cost-effective and easy to implementPhysical wear and tear over time
Tactile feedback provides confirmationLimited functionality for complex interactions
Suitable for applications requiring precise inputLimited space for multiple buttons on small devices
Works reliably in various environmentsMay require debouncing to prevent false triggers

Importance of Buttons

ESP8266 NodeMCU Interface Button .Buttons are essential components in various electronic systems and interfaces for several reasons:

Importance of ButtonsDescription
User InteractionButtons facilitate direct user interaction with electronic devices, enabling input commands.
ControlThey provide users with control over device functions and operations, enhancing usability.
FeedbackButtons offer tactile feedback upon pressing, confirming the user’s action.
VersatilityButtons can be integrated into various devices and interfaces, offering versatile functionality.
ReliabilityThey are known for their durability and consistent performance, ensuring reliable operation.
Cost-EffectivenessButtons are cost-effective components, making them widely used in different applications.

Overall, buttons play a crucial role in enabling user interaction and control in electronic systems, thereby making them indispensable in many applications across industries.

Types of buttons

Button TypesDescription
Push ButtonTypically a simple momentary switch that closes a circuit when pressed and opens it when released.
Tactile ButtonFeatures a small, tactile protrusion on the surface, providing physical feedback upon pressing.
Toggle SwitchA mechanical switch with a lever or toggle that can be flipped or moved to open or close a circuit.
Rocker SwitchSimilar to a toggle switch but with a flat, paddle-like actuator that rocks back and forth to toggle.
Slide SwitchUtilizes a sliding mechanism to open or close the circuit, often used for simple on/off operations.
Capacitive Touch ButtonOperates through touch-sensitive materials, activated by the presence of a conductive object or a finger.
Membrane ButtonConsists of a flexible membrane with conductive traces, pressed to make contact with underlying circuits.

These button types offer various tactile and operational characteristics suitable for different applications.

Button Interrupt Counter

The Button Interrupt Counter script counts the number of button presses (pulses) detected on a specific GPIO pin (in this case, GPIO2 or GPIO9).

Required

  • NodeMCU Modules (Firmware): GPIO Module, Timer Module,
  • hardware: ESP8266 with Programmer (or)  NodeMCU Dev Kit, Button,
  • software tools: ESPlorer IDE Tool.

Code

The script tallies button presses (pulses) detected on GPIO2, which corresponds to GPIO9. It incorporates debouncing to prevent counting multiple pulses for a single press.

-- Source URLs and Testing Information
-- http://esp8266iot.blogspot.in/
-- http://aruneworld.blogspot.com/
-- Tested By: Arun(20170219)
-- Example Name: AEW_ButtonInterruptCounter.lua
------------------------------------------------------------------------------------------

-- Count pulses on GPIO2
count = 0
delay = 0

-- Configure GPIO9 (equivalent to GPIO2) for interrupt mode with pull-up enabled
gpio.mode(9, gpio.INT, gpio.PULLUP)

-- Interrupt handler function
function counter(level)
    local x = tmr.now() -- Get current time in microseconds
    if x > delay then
        delay = tmr.now() + 250000 -- Update delay to allow for the next pulse after 250 milliseconds
        count = count + 1 -- Increment pulse count
        print(count) -- Print the current count value
    end
end

-- Set up interrupt trigger on GPIO9 for falling edge detection
gpio.trig(9, "down", counter)

Code Explanation

Source URLs and Testing Information:

  • Two URLs are provided as the source of the code: http://esp8266iot.blogspot.in/ and http://aruneworld.blogspot.com/.
  • The code was tested by Arun on a specific date mentioned as 20170219.
  • The example is named AEW_ButtonInterruptCounter.lua.

Initial Variables Setup:

  • count: Initialized to 0, it will be used to count the pulses detected on GPIO2.
  • delay: Initialized to 0, it will be used to debounce the input and avoid counting multiple pulses for a single press.
  • gpio.mode(9, gpio.INT, gpio.PULLUP): Configures GPIO9 for interrupt mode with pull-up enabled.

Interrupt Handler Function (counter):

  • tmr.now(): Retrieves the current time in microseconds.
  • If the current time is greater than the delay value, it means that enough time has passed since the last pulse.
  • In such a case, the delay is updated to allow for the next pulse after a debounce period of 250 milliseconds.
  • The count variable is incremented to keep track of the number of pulses detected.

Setting up Interrupt Trigger:

  • gpio.trig(9, "down", counter): Sets up an interrupt trigger on GPIO9 for falling edge detection. When a falling edge (button press) is detected on GPIO9, the counter function is called.


Interrupt Example of ESP8266 NodeMCU Interface – Button

Code

do
    -- use pin 1 as the input pulse width counter
    local pin, pulse1, du, now, trig = 1, 0, 0, tmr.now, gpio.trig
    gpio.mode(pin, gpio.INT)

    local function pin1cb(level, pulse2)
        print(level, pulse2 - pulse1)
        pulse1 = pulse2
        trig(pin, level == gpio.HIGH and "down" or "up")
    end

    trig(pin, "down", pin1cb)
end

Code Explanation

This code segment configures GPIO pin 1 of the NodeMCU board as an input for pulse width counting. Here’s a breakdown of what it does:

Overall, this code enables GPIO pin 1 to monitor pulse width changes and execute the callback function accordingly, providing a way to measure the duration of pulses on the input pin.

Variable Initialization:

  • pin: Specifies GPIO pin 1 for pulse width counting.
  • pulse1: Tracks the start time of the pulse.
  • du, now: References to tmr.delay and tmr.now functions for timing operations.
  • trig: Reference to the gpio.trig function for setting up interrupts.

GPIO Configuration:

  • Sets GPIO pin 1 as an interrupt-enabled pin (gpio.mode(pin, gpio.INT)).

Callback Function:

  • Defines a callback function pin1cb(level, pulse2) to be executed when an interrupt occurs on pin 1.
  • The level parameter indicates whether the interrupt is triggered by a rising edge (gpio.HIGH) or falling edge (gpio.LOW).
  • pulse2 represents the time when the interrupt occurred.

Callback Logic:

  • Calculates the pulse width by subtracting the previous pulse time (pulse1) from the current pulse time (pulse2).
  • Prints the level (indicating rising or falling edge) and the calculated pulse width.
  • Updates pulse1 to store the current pulse time for the next interrupt.

Trigger Setup:

  • Sets up the interrupt trigger using gpio.trig(pin, "down", pin1cb).
  • The trigger is set to detect falling edges ("down") on pin 1 and execute the pin1cb callback function.

Interrupt

The Button Interrupt Counter script showcases the effectiveness of utilizing interrupts in microcontroller programming. By leveraging interrupts, the script efficiently responds to button presses without the need for continuous monitoring. This approach optimizes the utilization of system resources and enables the microcontroller to perform other tasks while waiting for button input.

Furthermore, the script demonstrates an essential concept in embedded systems programming: debouncing. This feature enhances the reliability and accuracy of the button press count, making the script suitable for applications where precise user input tracking is essential.

Overall, the Button Interrupt Counter script exemplifies best practices in microcontroller programming by combining interrupt-driven design with debounce mechanisms to achieve robust and efficient button input handling.

NEXT

NodeMCU Get Start
NodeMCU Build Firmware
NodeMCU Flash Firmware
NodeMCU IDE
ESP8266 NodeMCU Modules
NodeMCU Module–Firmware Info
NodeMCU Module – GPIO
NodeMCU Module – Node
NodeMCU Module – WiFi
NodeMCU Module – Timer
NodeMCU Module – I2C
NodeMCU Module – File
NodeMCU Module – NET
NodeMCU Module – HTTP
NodeMCU Module – MQTT
ESP8266 NodeMCU Interface
NodeMCU Interface LED
NodeMCU Interface Button
NodeMCU Interface 7 Seg
NodeMCU Interface LCD
NodeMCU Interface ADC
NodeMCU Interface DHT11
NodeMCU Interface MCP23008
NodeMCU Interface MCP23017
NodeMCU Interface ADXL345
NodeMCU Interface DS18B20
ESP8266 NodeMCU Tutorials
NodeMCU Tutorials Google Time
NodeMCU Tutorials WebServer
ESP8266 NodeMCU Projects
Imperial March Ringtone Play
WiFi Router Status Indicator
ESP8266 NodeMCU Project – Home Automation
Others
NodeMCU All Post
Sitemap

ESP8266 Arduino-Core Interface – Button

Button – Digital Read Serial

  • Required Hardware ESP8266 with Programmer (or)  NodeMCU Dev Kit
  • Required Software Tools Arduino IDE with ESP8266 Core

Code

/* 
  http://www.ArunEworld.com/Embedded/ESPressif/ESP8266/ESP8266_Arduino-core/ESP8266-Arduino-Core-Interface-Button
  Tested By  : Arun(20170219)
  Example Name : AEW_ADC-Interface.ino
 */
 /*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

 

Output

0
1
1
1
0
0

 


Next :

Previous :


 

Embedded Interface – Button

Examples

  • LED ON/OFF Using Button
  • Button De-bounce
  • Four Button (UP, DOWN, ENTER, BACK)
    • Set Value By Four_Buttons using 7 Segment Display
    • Set Value By Four_Buttons using LCD Display
  • Matrix Keypad
    • Set Value by Matrix Keypad using 7 Segment Display
    • Set Value by Matrix Keypad using LCD Display
    • Display characters by Matrix Keypad using 7 Segment Display
    • Display characters by Matrix Keypad using LCD Display

 

Next Topic

Embedded Interface 7 Segment (Add Soon)
Embedded Interface ADC (Add Soon)
Embedded Interface Button (Add Soon)
Embedded Interface EEPROM (Add Soon)
Embedded Interface LCD (Add Soon)
Embedded Interface LCD HD44780 (Add Soon)
Embedded Interface LED
Embedded Interface MCP23017
Embedded Interface Motor (Add Soon)
Embedded Interface PCF8574 and PCF8574A
Embedded Interface RTC (Add Soon)
Embedded Interface Switch
Embedded Interface Touch Kypad
Embedded Interface RGB LED (Add Soon)