MicroPython Beginner’s Input Tutorial

Here’s how to set up and test the MicroPython unit for push button input.

Components:

  • 2 Male to Male Jumper cables
  • 1 Push Button tactile switch.
  • 1 Breadboard

Step 1. Hardware Setup

micropython push button input

Plug one of your jumpers in to the 3.3V Supply Pin.

Plug the other jumper in to the Y1 Pin

Step 2. Coding your program

Plug your MicroPython board in to a USB port on your computer and open up the PYBFLASH drive.

pyb flash icon

Open up the main.py file and replace the contents of the file with the following code:


import time,pyb
g = pyb.Pin("Y1",pyb.Pin.IN) #set the pin to input
g.init(pyb.Pin.IN, pull = pyb.Pin.PULL_DOWN) # Set up a pull down resistor.
led = pyb.LED(1)
while True:
    if g.value():
        led.on()
    else:
        led.off()
    time.sleep(0.05)

Save the file and then wait for the red LED to turn off. Once the red LED has turned off it is now safe to press the reset button.

If you have wired up and coded correctly then the red LED on your MicroPython unit should light up whenever you press down the push button.

Troubleshooting

Check that you have installed the push switch the correct way round ( if you used a 4 legged push switch).

If your program still isn’t working then you can use the REPL interface instead of saving to the main.py file.