ESP32 IOT
DIO
Timer
Interrupt
ESP32 DO Pin π
ESP32 DO Source Current π
ESP32 DO Source Current π
ESP32 DO Sink Current π
ESP32 DO Sink Current π
ESP32: LED Blinking π
ESP32: DI Pin
ESP32: Digital Input
ESP32: Digital Input Pull UP
ESP32: Digital Input Pull UP
ESP32: Digital Input Pull Down
ESP32: Digital Input Pull Down
Controlling LED with Button using Pull-Up Resistor
Controlling LED with Button using Pull-Down Resistor
Introduction to Blocking
Introducing the concept of blocking in programming and its implications.
LED + Button with Blocking
This code controls the state of two LEDs. The red LED toggles every 2 seconds, # and the blue LED's state is controlled by the button's state.
Β
ζ€η¨εΌη’Όζ§εΆε ©ε LED ηηζ γη΄ θ² LED ζ― 2 η§εζδΈζ¬‘οΌθθθ² LED ηηζ η±ζιηηζ ζ§εΆγ
LED + Button with Blocking
Observations
- When the button is pressed during
time.sleep()
, Blue LED does not turn on immediately. - The system is blocked and unresponsive during sleep intervals.
LED + Button with Non-Blocking
Code Sample using Timer
LED + Button with Non-Blocking
Code Sample using Timer & IRQ
Summary
- Comparison of blocking vs non-blocking approaches.
- Discussing the advantages of using timers and IRQs to handle multitasking.
Timer Overview
- Definition of Timer:
- A Timer is a hardware feature that can measure time intervals.
- Importance in MicroPython:
- Useful for running functions at specific intervals and creating delays.
Timer Initialization
from machine import Timer
timer0 = Timer(0)
timer0.init(period=2000, callback=lambda t:print(0))
Timer.init() method:
Used to configure the timer with specific parameters.
Timer.init(*, mode=Timer.PERIODIC, freq=-1, period=-1, callback=None)
Timer Modes
from machine import Timer
timer0 = Timer(0)
timer0.init(mode=Timer.ONE_SHOT, period=2000, callback=lambda t:print(0))
timer1 = Timer(1)
timer1.init(mode=Timer.PERIODIC, period=1000, callback=lambda t:print(1))
Timer Callbacks
Definition of Callbacks: Functions that are called when a timer expires.
from machine import Pin, Timer
timer = Timer(0)
led = Pin(21, Pin.OUT)
def toggle_led(timer):
led.value(not led.value())
timer.init( period=1000, mode=Timer.PERIODIC,
callback = toggle_led )
Timer Callbacks
Deinitialize a Timer
Stops the timer, and removes the configuration.
from machine import Timer
import time
# Initialize the counter and timer
counter = 0
timer = Timer(0)
def on_timer(timer):
global counter
print(f"Timer Triggered! {counter}")
counter += 1 # Increment the counter each time the timer is triggered
# Initialize the timer
timer.init(period=2000, mode=Timer.PERIODIC, callback=on_timer)
while True:
# Check the counter in the main loop
if counter == 5:
print(f"Deinitializing Timer... {counter}")
timer.deinit()
counter += 1
if counter > 5:
print(f"Main loop in while is ruuning... {counter}")
counter += 1
time.sleep(1) # Sleep for a while before checking the counter again
Deinitialize a Timer
Example: Variable Timer Period
from machine import Timer
import time
timer = Timer(0)
period = 1000 # 1 second
last_time = time.ticks_ms()
def on_timer(timer):
global period, last_time
current_time = time.ticks_ms()
time_diff = time.ticks_diff(current_time, last_time)
print("Timer Triggered! Time Difference: {} ms".format(time_diff))
period += 1000 # Increase period by 1 second
timer.init(period=period, mode=Timer.ONE_SHOT, callback=on_timer)
last_time = current_time # Update the last_time to the current_time
timer.init(period=period, mode=Timer.ONE_SHOT, callback=on_timer)
Β Example: Mutiple Timer
from machine import Timer
import time
# Initialize multiple timers
timer1 = Timer(0)
timer2 = Timer(1)
# Initialize last time for each timer
last_time1 = time.ticks_ms()
last_time2 = time.ticks_ms()
def on_timer1(timer):
global last_time1
current_time = time.ticks_ms()
time_diff = time.ticks_diff(current_time, last_time1)
print("Timer 1 Triggered! Time Difference: {} ms".format(time_diff))
last_time1 = current_time # Update the last_time1 to the current_time
def on_timer2(timer):
global last_time2
current_time = time.ticks_ms()
time_diff = time.ticks_diff(current_time, last_time2)
print("Timer 2 Triggered! Time Difference: {} ms".format(time_diff))
last_time2 = current_time # Update the last_time2 to the current_time
# Initialize timers with different periods and modes
timer1.init(period=1000, mode=Timer.PERIODIC, callback=on_timer1)
timer2.init(period=2000, mode=Timer.PERIODIC, callback=on_timer2)
Traffic Light Simulation
from machine import Pin, Timer
# Define a list to hold the color and pin number tuples
color_and_pin_list = [('red', 23), ('yellow', 21), ('green', 4)]
# Use a dictionary comprehension to create the 'leds' dictionary
leds = {color: Pin(pin, Pin.OUT) for color, pin in color_and_pin_list}
# Initialize timer
timer = Timer(0)
def change_light(t):
if leds['red'].value(): # If red is ON
leds['red'].off()
leds['green'].on()
timer.init(period=700, mode=Timer.ONE_SHOT, callback=change_light)
elif leds['green'].value(): # If green is ON
leds['green'].off()
leds['yellow'].on()
timer.init(period=200, mode=Timer.ONE_SHOT, callback=change_light)
else: # If yellow is ON
leds['yellow'].off()
leds['red'].on()
timer.init(period=500, mode=Timer.ONE_SHOT, callback=change_light)
# Initialize the starting state and start the timer
leds['green'].on()
timer.init(period=700, mode=Timer.ONE_SHOT, callback=change_light)
In this example, we are using one timer to control all three lights, changing the callback period to simulate the different light durations.
What is an IRQ?
- IRQ stands for Interrupt Request.
- It's a mechanism by which a processor can be interrupted from its current task to run a specific set of instructions.
- It's useful for handling external events or inputs, like button presses or sensor readings.
Handling IRQΒ
- Initialize the pin as an input pin.
- Configure the pin to trigger an interrupt.
- Define a callback function that will be executed when the interrupt is triggered.
- Attach the callback function to the interrupt.
Simple Example - Button Press
Objective
- Create a simple example where a button press triggers an IRQ.
- When the button is pressed, an LED will toggle its state (ON/OFF).
Components
- Push Button
- LED
- Resistors
Button Press Example CodeΒ
from machine import Pin
import time
# Initialize LED and Button
led = Pin(21, Pin.OUT) # LED connected to GPIO12
button = Pin(32, Pin.IN, Pin.PULL_UP) # Button connected to GPIO13
led.on()
# Callback function to handle button press
def button_handler(pin):
led.value(not led.value()) # Toggle LED state
# Attach the callback function to button press interrupt
button.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
while True:
time.sleep(0.1) # Sleep to reduce CPU usage
Button Press Example Connection Diagram
Button Press Example
Timer Interrupt Example
- Timer interrupts can be used to execute a function at regular intervals.
from machine import Timer
tim = Timer(0)
def tick(timer):
print('Tick!', timer)
# Initialize timer interrupt
tim.init(mode=Timer.PERIODIC, period=1000, callback=tick)
# The function 'tick' will be called every 1000ms (1 second)
Conclusion
Recap
- We learned about IRQ and how it allows the processor to handle specific events like button presses.
- We explored how to use MicroPython to handle IRQs on an ESP32 board.
- We implemented simple examples where a button press toggles an LED and a timer interrupt prints a message every second.
Further Learning
- Explore more advanced IRQ features like debouncing.
- Try handling different types of events like sensor readings.
ESp32 Introduction
By wschen
ESp32 Introduction
This presentation discusses observations related to the behavior of a button and LED during sleep intervals. It introduces a code sample using Timer and IRQ to address the unresponsiveness issue. Curious to know more? Join the presentation!
- 196