- Prolog
- Temperature/Humidity sensor (DHT11/DHT22)
- PIR sensor (HC-SR501)
- Ultrasonic sensor (HC-SR04)
- Light/Shadow detection with LDR (Photo resistor)
- IR Flame detection
After the many previous examples, it is time to measure and collect data from the environment. Sensors are required for this. Depending on what is to be measured, special sensors are used. But there are also sensors that record several environmental variables!
With the DHT11/DHT22 you can record the temperature and humidity. The respective modules for both are already integrated in the standard firmware of MicroPython.
| Device | Delay | Temperature | Humidity |
|---|---|---|---|
| DHT11 | 1 sec. | 0 to 50 °C | 20 to 90% |
| DHT22 | 2 sec. | -40 to 80 °C | 0 to 100% |
- mandatory 1x DHT11/DHT22 Sensor
- mandatory 1x Resistor (min. 10 kilo ohms)
- few cables
- optional breadboard
Example
You can use the same source code and circuit diagram for
DHT11andDHT22! Only the import and object must adapt for specific device.
# create new subdirectory
$ mkdir -p ~/Projects/ESP/examples/sensors
# create script
$ touch ~/Projects/ESP/examples/sensors/dht11.pySource Code for
dht11.py
Check your circuit and copy the script to the microcontroller as main.py.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/sensors/dht11.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] replStart with keys Control + d. Stop the loop with keys Control + c. To leave the REPL, press keys Control + x.
If you want to detect movements in the area, the PIR sensor is already suitable. The sensors are available in various variants.
- mandatory PIR Sensor (HC-SR501)
- few cables
- optional breadboard
Example
# create script
$ touch ~/Projects/ESP/examples/sensors/pir.pySource Code for
pir.py
Check your circuit and copy the script to the microcontroller as main.py.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/sensors/pir.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] replStart with keys Control + d. Stop the loop with keys Control + c. To leave the REPL, press keys Control + x.
There are various ways of measuring the distance to objects. Depending on the area of application and the environment, they have strengths and weaknesses. Here is a sensor that uses ultrasound.
- mandatory Ultrasonic Sensor (HC-SR04)
- few cables
- optional breadboard
Example
# create script
$ touch ~/Projects/ESP/examples/sensors/hcsr04.pySource Code for
hcsr04.py
Check your circuit and copy the script to the microcontroller as main.py.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/sensors/hcsr04.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] replStart with keys Control + d. Stop the loop with keys Control + c. To leave the REPL, press keys Control + x.
With an LDR sensor you can capture/measure the light in the environment. LDR is also referred to as a photoresistor, photocell, or photoconductor. In the current example, the ADC is used on the microcontroller. There are also digital LDRs.
- mandatory 1x LED (any color)
- mandatory 1x Resistor (min. 220 ohms)
- mandatory 1x LDR Photo resistor
- mandatory 1x Resistor (min. 10 kilo ohms)
- few cables
- optional breadboard
Example
Unfortunately Wokwi has another LDR module, Tinkercad doesn't have the ESP32 NodeMCU and I don't have Fritzing installed. Therefore, this time only the GPIO hints.
# create script
$ touch ~/Projects/ESP/examples/sensors/shadow_detection.pySource Code for
shadow_detection.py
Check your circuit and copy the script to the microcontroller as main.py.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/sensors/shadow_detection.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] replStart with keys Control + d. Stop the loop with keys Control + c. To leave the REPL, press keys Control + x.
You can use the protective cap of pens to darken the sensor.
Fire... fire... help it burns! With this sensor you can prevent worse.
- mandatory 1x LED (any color)
- mandatory 1x Resistor (min. 220 ohms)
- mandatory 1x IR Flame Sensor
- few cables
- optional breadboard
Example
| IR device | ESP32 |
|---|---|
| VCC | 3V3 |
| GND | GND |
| DO | 23 |
The LED is simply connected to pin 21 with a resistor (min. 220 ohms).
# create script
$ touch ~/Projects/ESP/examples/sensors/shadow_detection.pySource Code for
ir_flame_detection.py
Check your circuit and copy the script to the microcontroller as main.py.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/sensors/ir_flame_detection.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] replStart with keys Control + d. Stop the loop with keys Control + c. To leave the REPL, press keys Control + x.
You can test the sensor very well with a lighter. But don't burn down your house!








