Skip to content

Commit d6de448

Browse files
committed
Add dht11 w/ timer as an introduction to timers
1 parent 92d0ace commit d6de448

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# DHT11 Temperature and Humidity sensor using a timer
2+
3+
In [Project 3.6](../03_sensors/03_06_hygrothermograph_dht11.md) a DHT11 is used to measure temperature and humidity every 2 seconds. To do so the program contains an infinite loop that checks the temperature then sleeps for two seconds. This is inconvenient when your program is doing something else, an alternative solution is to use a timer to check the temperature at a regular time interval without sleeping.
4+
5+
## New Concepts
6+
7+
- Timer
8+
9+
### Timer
10+
11+
A MicroPython class that controls the hardware timers of the microcontroller. They can execute a callback once after a given amount of time has passed or repeatedly once every period of time.
12+
13+
Use `mode=Timer.PERIODIC` to run callback multiple times and `mode=Timer.ONE_SHOT` to run the callback once.
14+
15+
For more info see [MicroPython Timer documentation](https://docs.micropython.org/en/latest/library/machine.Timer.html)
16+
17+
> NOTE: The ESP32-S3 has 4 timers, so only 4 can be active at a time.
18+
19+
*Circuit and components are identical to Project 3.6*
20+
21+
## Component List
22+
23+
![Components](../images/03_06_components.png)
24+
25+
## Circuit
26+
27+
> Disconnect all power before building the circuit. Reconnect once verified.
28+
29+
### Wiring Diagram
30+
31+
![Wiring Diagram](../images/03_06_wiring_diagram.png)
32+
33+
**Connections:**
34+
- DHT11 VCC → 3.3V (through a 10kΩ pull-up to the SDA line)
35+
- DHT11 SDA → GPIO21
36+
- DHT11 NC → not connected
37+
- DHT11 GND → GND
38+
39+
### Schematic Diagram
40+
41+
![Schematic Diagram](../images/03_06_schematic_diagram.png)
42+
43+
## Code
44+
45+
```python
46+
from machine import Pin, Timer
47+
import dht
48+
49+
DHT = dht.DHT11(Pin(21))
50+
51+
def getTemperature(t):
52+
DHT.measure()
53+
print('temperature:',DHT.temperature(),'humidity:',DHT.humidity())
54+
55+
timer = Timer(0)
56+
timer.init(mode=Timer.PERIODIC, period=2000, callback=getTemperature)
57+
```
58+
59+
## Code Explanation
60+
61+
This code is very similar to the code from [Project 3.6](../03_sensors/03_06_hygrothermograph_dht11.md#code-explanation).
62+
63+
The code to take a measurement from the DHT11 sensor is moved from the main loop in Project 3.6 to a function that can be used as a callback.
64+
65+
```python
66+
def getTemperature(t):
67+
DHT.measure()
68+
print('temperature:',DHT.temperature(),'humidity:',DHT.humidity())
69+
```
70+
71+
Create a timer and initialize it as a periodic timer with a period of 2000ms and `getTemperature` as the callback function. This configures the timer to run for ever and call the `getTemperature` callback every 2000ms.
72+
73+
```python
74+
timer = Timer(0)
75+
timer.init(mode=Timer.PERIODIC, period=2000, callback=getTemperature)
76+
```
77+
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.
79+
80+
## Key Concepts
81+
82+
- **Hardware Timers**: Timers run in hardware on the microcontroller providing a small number of high precision timers that have low or no runtime overhead to other code running on the microcontroller
83+
- **Periodic**: Something that happens over and over again at a fixed time interval.
84+
85+
## Further Exploration
86+
87+
- Adjust the period to make temperature measurements more frequent. Can you make it so frequent that it fails?
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from machine import Pin, Timer
2+
import dht
3+
4+
DHT = dht.DHT11(Pin(21))
5+
6+
def getTemperature(t):
7+
DHT.measure()
8+
print('temperature:',DHT.temperature(),'humidity:',DHT.humidity())
9+
10+
timer = Timer(0)
11+
timer.init(mode=Timer.PERIODIC, period=2000, callback=getTemperature)
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+

0 commit comments

Comments
 (0)