Detecting through python if a pushbutton is pressed

Sir i am given a task to control leds using pushbutton via esp8266mod. But i am stuck at “how to detect through python if a pushbutton is pressed”. Through google searches i have found that there is a module for pushbutton, there it can be created and controlled but there is no module or library for hardware pushbutton. I have tried the command if mybolt.digitalRead(‘0’)==‘HIGH’ , but it doesnt work.
Please help if you have any solution regarding this.

hello @sutaraisha123
you just want to check the condition of the push switch in BOLT through right?

To detect if a pushbutton is pressed using an ESP8266 module and Python, you can use a python library called " machine " which provides access to the low-level hardware of ESP8266.

You can use a function called " machine.Pin() " to create a pin object for the pin connected to the pushbutton

The function called " value() " can be used to read the state of the pin

Here is an example [ on how to use the "
machine " library to detect if a pushbutton connected to pin 0 is pressed]


import machine

button_pin = machine.Pin(0, machine.Pin.IN)

while True:
    if button_pin.value() == 0:
        print("Button pressed")
    else:
        print("Button not pressed")

It’s important to note that, depending on the physical push button, and the ESP8266 board you’re using, you may need to add a pull-up or pull-down resistor to the push button pin to ensure that it reads a stable value when the button is not pressed.

Hope this helps :smile:

You need to first import machine library . The machine module contains specific functions related to the hardware on a particular board. Most functions in this module allow to achieve direct and unrestricted access to and control of hardware blocks on a system (like CPU, timers, buses, etc.).
and then you can use machine.pin() function