008 - ESP32 MicroPython: Hardware Timer Interrupts

Introduction

In this tutorial, we will learn how to use the hardware Timer Interrupts of ESP32 in MicroPython. Timer in essence is basically a counter that either counts up or counts down. Timers can be use to trigger an interrupt event when an overflow happened or a certain count value is reached.

Circuit Diagram

Video Demonstration

Call To Action

If you have any question or suggestion, please do not hesitate to write it in the comment box provided.

If you find this article as helpful, please kindly consider supporting my Youtube channel by Subscribing. Please click this to Subscribe to TechToTinker.

Source Code

Example # 1 Simple Timer:

 1import machine
 2
 3led = machine.Pin(2, machine.Pin.OUT)
 4
 5tim0 = machine.Timer(0)
 6
 7def handle_callback(timer):
 8    led.value( not led.value() )
 9
10# tim0.init(period=2000, mode=machine.Timer.ONE_SHOT, callback=lambda t: led.value(not led.value()))
11tim0.init(period=50, mode=machine.Timer.PERIODIC, callback=handle_callback)

Example # 2 Hardware Timer Interrupts use for multithreading:

 1# Load the machine module
 2import machine
 3
 4# 1. Create the GPIO object as
 5#    normal GPIO pin assignment 
 6p23 = machine.Pin(23, machine.Pin.OUT)
 7p22 = machine.Pin(22, machine.Pin.OUT)
 8
 9# 2. Create the servo object and
10#    attach the pwm driver to the GPIO pin
11b_servo = machine.PWM(p23)
12s_servo = machine.PWM(p22)
13#    Initialize the servo object with
14#    20ms pulse interval and set the
15#    angle to 0 degree
16b_servo.init(freq=50, duty=20)
17s_servo.init(freq=50, duty=20)
18
19# 3. Create a timer object for both servo
20timB = machine.Timer(0)
21timS = machine.Timer(1)
22#    Functions to handle the Timer callbacks
23def handle_timB(timer):
24    global is_timB
25    is_timB = True
26def handle_timS(timer):
27    global is_timS
28    is_timS = True
29#    and initialize the timer objects
30timB.init(period=200, mode=machine.Timer.PERIODIC, callback=handle_timB)
31timS.init(period=100, mode=machine.Timer.PERIODIC, callback=handle_timS)
32
33# Create a global variables
34is_timB = False
35is_timS = False
36b_angle = 0
37s_angle = 0
38
39# Functions:
40# Converts a value using map function of Arduino
41# this basically functions like by ratio and proportion
42def map(x, in_min, in_max, out_min, out_max):
43    return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
44# Function to convert an angle to a duty value
45def servo(pin, angle):
46    pin.duty(map(angle, 0, 180, 20, 120))
47    
48# Main loop
49while True:
50    # Checks if the Timer interrupt sets the global variable
51    if is_timB==True:
52        is_timB = False         # disable the flag
53        servo(b_servo, b_angle) # rotate the servo
54        if b_angle < 180:       # if the angle is less than 180 
55            b_angle += 10        # increment the angle
56    if is_timS==True:
57        is_timS = False
58        servo(s_servo, s_angle)
59        if s_angle < 180:
60            s_angle += 5


Posts in this series



No comments yet!

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