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
- 026 - ESP32 MicroPython: MFRC522 RFID Module
- 025 - ESP32 MicroPython: ESP32 Bluetooth Low Energy
- 024 - ESP32 MicroPython: How to Use SD Card in MicroPython
- 023 - ESP32 MicroPython: Binary Clock
- 022 - ESP32 MicroPython: MQTT Part 2: Subscribe
- 021 - ESP32 MicroPython: MQTT Part 1: Publish
- 020 - ESP32 MicroPython: RESTful APIs | Demo READ and WRITE
- 019 - ESP32 MicroPython: OpenWeather | RESTful APIs
- 018 - ESP32 MicroPython: Thingspeak | RESTful APIs
- 017 - ESP32 MicroPython: DHT Values Auto Updates using AJAX
- 016 - ESP32 MicroPython: Web Server | ESP32 Access Point
- 015 - ESP32 MicroPython: Web Server | ESP32 Station Mode in MicroPython
- 014 - ESP32 MicroPython: SIM800L GSM Module in MicroPython
- 013 - ESP32 MicroPython: UART Serial in MicroPython
- 012 - ESP32 MicroPython: HC-SR04 Ultrasonic Sensor in MicroPython
- 011 - ESP32 MicroPython: DHT11, DHT22 in MicroPython
- 010 - ESP32 MicroPython: 0.96 OLED in MicroPython
- 009 - ESP32 MicroPython: Non-blocking Delays and Multithreading | Multitasking
- 007 - ESP32 MicroPython: How to make some sound with MicroPython
- 006 - ESP32 MicroPython: How to control servo motor with MicroPython
- 005 - ESP32 MicroPython: Pulse Width Modulation
- 004 - ESP32 MicroPython: External Interrupts
- 003 - ESP32 MicroPython: General Purpose Input Output | GPIO Pins
- 001 - ESP32 MicroPython: What is MicroPython
- 000 - ESP32 MicroPython: How to Get Started with MicroPython
No comments yet!