MicroPython simple GPIO control

The Arduino IDE contains several examples that help us get started with c/c++, in the digital section there is a sample called BlinkWithoutDelay. Although BlinkWithoutDelay is a simple begginers project it contains multiple elements that we use daily in our microcontroller code and it should not be underestimated. In this tutorial we will port BlinkWithoutDelay to MicroPython, in particular for the NanoESP32, and study its methods along the way.

The recommended materials for this tutorial are
Nano ESP32 with MicroPython firmware
Arduino Lab For Micropython @ Arduino Labs
I also recommend downloading the Nano ESP32 pinout diagram @ https://docs.arduino.cc/hardware/nano-esp32.

Our first lines of code will be modules, simply put modules contain code and function definitions for use in our programs. To start with you may wonder which modules you need, but early on, with repeated use, you will get familiar with the modules that come packaged with MicroPython. We need just two objects for this example, they are Pin so that we can control our output and the second is ticks so that we can control the frequency of the blinking output. To access these two objects we need to import machine which contains Pin and we need to import time which contains ticks.

import machine
import time

In the next line we will initialize our output pin and give it a recognizable name. Initialization requires a minimum two parameters the GPIO number and the pin mode. Take a look at the pin out diagram we referenced earlier and you will see that LED_BUILTIN is attached to GPIO48, you can use whichever description you like I prefer to use the GPIO number. The second parameter we need is mode and we need Pin.OUT to specify an output. Finally we will give it the name 'led'. There is more detailed info regarding pins @ class Pin – control I/O pins — MicroPython latest documentation.

led=machine.Pin(48,machine.Pin.OUT)

We need to initialize a ticks variable, ticks are continuously counted from when the microcontoller is powered on and they are translated to microseconds, milliseconds and seconds that we can access in our programs, our next line takes a snapshot of ticks as milliseconds and stores the value in a variable named start.

start=time.ticks_ms()

Now to create some code blocks that do the work. The starting line of a code block is usually terminated with a colon and the code within the block is indented. Our fist block is a while True statement that is basically a coninuous run forever loop.

while True:

The next line is a variable that is asssigned the result of the ticks_diff() method, this method takes two ticks values as parameters and returns the difference between the two, so we are actually measuring the time from start to now (time.ticks_ms()) and we are translating to milliseconds. Remember this line is in the while True: block and must be indented.

`delta=time.ticks_diff(time.ticks_ms(), start)`

All we have to do now is monitor the delta value while the program loops and if it reaches 100 mS we will change the state of our GPIO pin not forgetting to reset our start value each time. Notice each block has its own level of indentation.

    	if delta>=100:              #if the difference is greater or equal to 100 milliseconds
        	if led.value()==0:
            		led(1)
            		start=time.ticks_ms()  
        	else:
            		led(0)
            		start=time.ticks_ms()

The full listing

import machine
import time                     

led=machine.Pin(48, machine.Pin.OUT)      
start=time.ticks_ms()           

while True:
    
    delta = time.ticks_diff(time.ticks_ms(), start) #compute the time difference between start and current time
    
    if delta>=100:              #if the difference is greater or equal to 100 milliseconds
        if led.value()==0:
            led(1)
            start=time.ticks_ms()  
        else:
            led(0)
            start=time.ticks_ms()
3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.