Skip to content

Commit cb25fcd

Browse files
committed
Update DHT11 + timer code to always deinit the timer so program can be run multiple times in a row without a hard reset
1 parent d6de448 commit cb25fcd

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

05_interrupts_and_timers/05_04_dht11_timer.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ def getTemperature(t):
5252
DHT.measure()
5353
print('temperature:',DHT.temperature(),'humidity:',DHT.humidity())
5454

55-
timer = Timer(0)
56-
timer.init(mode=Timer.PERIODIC, period=2000, callback=getTemperature)
55+
try:
56+
timer = Timer(0)
57+
timer.init(mode=Timer.PERIODIC, period=10, callback=getTemperature)
58+
59+
while True:
60+
pass
61+
finally:
62+
timer.deinit()
5763
```
5864

5965
## Code Explanation
@@ -75,7 +81,21 @@ timer = Timer(0)
7581
timer.init(mode=Timer.PERIODIC, period=2000, callback=getTemperature)
7682
```
7783

78-
> Notice there is no `while True:` loop, it is not necessary because the timer runs until `deinit` is called on the timer or the microcontroller is reset.
84+
The timer is wrapped in a try/finally block to ensure the timer is always deinitialized when the program is stopped.
85+
86+
```python
87+
try:
88+
timer = Timer(0)
89+
timer.init(mode=Timer.PERIODIC, period=2000, callback=getTemperature)
90+
91+
while True:
92+
pass
93+
finally:
94+
timer.deninit()
95+
96+
```
97+
98+
> Notice the `while True:` loop is not necessary to read the sensor but it is necessary because the timer runs until `deinit` is called on the timer or the microcontroller is hard reset. The `while True:` loop keeps the program from calling `timer.deinit()` before you sent a reset command by hitting the stop icon. In a real use case your main program loop would take the place of this dummy `while True:` loop.
7999
80100
## Key Concepts
81101

@@ -84,4 +104,5 @@ timer.init(mode=Timer.PERIODIC, period=2000, callback=getTemperature)
84104

85105
## Further Exploration
86106

87-
- Adjust the period to make temperature measurements more frequent. Can you make it so frequent that it fails?
107+
- Adjust the period to make temperature measurements more frequent. Can you make it so frequent that it fails or no longer runs faster?
108+
- What happens if you remove the `timer.deinit()` then try to run the program more than once without a hard reset?

05_interrupts_and_timers/code/Hygrothermograph_timer.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ def getTemperature(t):
77
DHT.measure()
88
print('temperature:',DHT.temperature(),'humidity:',DHT.humidity())
99

10-
timer = Timer(0)
11-
timer.init(mode=Timer.PERIODIC, period=2000, callback=getTemperature)
12-
10+
try:
11+
timer = Timer(0)
12+
timer.init(mode=Timer.PERIODIC, period=2000, callback=getTemperature)
13+
14+
while True:
15+
pass
16+
finally:
17+
timer.deinit()
1318

1419

1520

0 commit comments

Comments
 (0)