009 - ESP32 MicroPython: Non-blocking Delays and Multithreading | Multitasking

Introduction

In previous tutorials we learned to use the hardware timer interrupt which is very useful when we need to execute a task at specific interval period. We also use hardware timer interrupts for executing timed threaded task which is similar to multitasking. Using the hardware timer in that manner, sooner or later, we will runout of resources because ESP32 have 4 hardware timer only namely Timer0 to Timer3.

In this video, we will try to explore alternative ways of doing multiple task at almost the same time using the time module.

Circuit Diagram

Hardware Instruction

  1. Connect the ESP32 development board to the breadboard.
  2. Connect the cathode lead of the red LED to the ground while the anode pin through limiting resistor to GPIO D27 of ESP32.
  3. Connect the cathode lead of the green LED to the ground while the anode pin through limiting resistor to GPIO D26 of ESP32.
  4. Connect the cathode lead of the blue LED to the ground while the anode pin through limiting resistor to GPIO D25 of ESP32.
  5. Connect the ‘mode’ tactile switch with one pin to the ground while the other to GPIO D33 of ESP32.
  6. Connect the ‘left’ tactile switch with one pin to the ground while the other to GPIO D32 of ESP32.
  7. Connect the ‘rght’ tactile switch with one pin to the ground through a 10K resistor while the other to GPIO D35 of ESP32.
  8. Connect the ‘entr’ tactile switch with one pin to the ground through a 10K resistor while the other to GPIO D34 of ESP32.

Pins D32 and D33 of ESP32 has internal pull-up resistor so take advantage of it while for pins D34 and D35 the internal pull-ups is NOT PRESENT so we use external pull-up resistor of 10 kilo Ohms which works exactly the same. The button switches are configured as ACTIVE-LOW, meaning the value when press is LOW or 0.

Video Demonstration

Call To Action

If you have any question regarding this tutorial, please do not hesitate to write it in the comment box provided.

You may also like to Subscribe to my Youtube Channel. Please click this to Subscribe to TechToTinker Youtube Channel.
Thank you and have a good days ahead.

Source Code

Example # 1 Blinking an LED, Blocking Code:

 1
 2import machine
 3import time
 4
 5led = machine.Pin(2, machine.Pin.OUT)
 6
 7while True:
 8<span>    </span>led.on()
 9    time.sleep(0.5)
10    led.off()
11    time.sleep(0.5)

Example # 2 Blinking an LED, Non-Blocking Code:

 1
 2import machine
 3import time
 4
 5led = machine.Pin(2, machine.Pin.OUT)
 6
 7start_time = time.ticks_ms()
 8interval = 500      # 500 ms interval
 9led_state = 0
10
11while True:
12  if ( time.ticks_ms() - start_time > interval ):
13    if led_state == 1:
14      led_state = 0
15    else:
16      led_state = 1
17    led.value(led_state)
18    start_time = time.ticks_ms()

Example # 3 Multi-threading or doing multiple task at almost the same time:

  1
  2import machine
  3import time
  4
  5red = machine.Pin(27, machine.Pin.OUT)
  6grn = machine.Pin(26, machine.Pin.OUT)
  7blu = machine.Pin(25, machine.Pin.OUT)
  8
  9mode = machine.Pin(33, machine.Pin.IN, machine.Pin.PULL_UP)
 10left = machine.Pin(32, machine.Pin.IN, machine.Pin.PULL_UP)
 11rght = machine.Pin(35, machine.Pin.IN)
 12entr = machine.Pin(34, machine.Pin.IN)
 13
 14r_start = time.ticks_ms()
 15g_start = time.ticks_ms()
 16b_start = time.ticks_ms()
 17k_start = time.ticks_ms()
 18
 19r_interval = 300
 20g_interval = 500
 21b_interval = 700
 22k_interval = 200
 23
 24state = 0
 25EDIT_RESOLUTION = 10
 26reset = 0
 27
 28print('**************************')
 29print('  DEFAULT Interval Values ')
 30print('--------------------------')
 31print('Red interval:', r_interval, 'ms')
 32print('Grn interval:', g_interval, 'ms')
 33print('Blu interval:', b_interval, 'ms')
 34print('**************************')
 35            
 36while True:
 37    if time.ticks_ms() - r_start >= r_interval:
 38        red.value( not red.value() )
 39        r_start = time.ticks_ms()
 40    if time.ticks_ms() - g_start >= g_interval:
 41        grn.value( not grn.value() )
 42        g_start = time.ticks_ms()
 43    if time.ticks_ms() - b_start >= b_interval:
 44        blu.value( not blu.value() )
 45        b_start = time.ticks_ms()
 46    if time.ticks_ms() - k_start >= k_interval:
 47        k_start = time.ticks_ms()
 48        
 49        if mode.value()==0:
 50            if state==0: # idle mode
 51                state = 1
 52                print()
 53                print('*************')
 54                print('Red edit mode')
 55                print('-------------')
 56            elif state==1: # red edit mode
 57                state = 2
 58                print()
 59                print('*************')
 60                print('Grn edit mode')
 61                print('-------------')
 62            elif state==2: # grn edit mode
 63                state = 3
 64                print()
 65                print('*************')
 66                print('Blu edit mode')
 67                print('-------------')
 68            elif state==3: # blu edit mode
 69                state = 0
 70                print()
 71                print('*************')
 72                print('Idle mode')
 73                print('-------------')
 74                
 75        if left.value()==0:
 76            if   state==1: # red edit mode
 77                if r_interval - EDIT_RESOLUTION > 0:
 78                    r_interval -= EDIT_RESOLUTION
 79                print('Red interval:', r_interval, 'ms')
 80            elif state==2: # grn edit mode
 81                if g_interval - EDIT_RESOLUTION > 0:
 82                    g_interval -= EDIT_RESOLUTION
 83                print('Grn interval:', g_interval, 'ms')
 84            elif state==3: # blu edit mode
 85                if b_interval - EDIT_RESOLUTION > 0:
 86                    b_interval -= EDIT_RESOLUTION
 87                print('Blu interval:', b_interval, 'ms')
 88                    
 89        if rght.value()==0:
 90            if   state==1: # red edit mode
 91                r_interval += EDIT_RESOLUTION
 92                print('Red interval:', r_interval, 'ms')
 93            elif state==2: # grn edit mode
 94                g_interval += EDIT_RESOLUTION
 95                print('Grn interval:', g_interval, 'ms')
 96            elif state==3: # blu edit mode
 97                b_interval += EDIT_RESOLUTION
 98                print('Blu interval:', b_interval, 'ms')
 99        
100        if entr.value()==0:
101            r_interval = 300
102            g_interval = 500
103            b_interval = 700
104            print()
105            print('**************************')
106            print('Values RESETTED to DEFAULT')
107            print('--------------------------')
108            print('Red interval:', r_interval, 'ms')
109            print('Grn interval:', g_interval, 'ms')
110            print('Blu interval:', b_interval, 'ms')
111            print('**************************')


Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.