Skip to content

Latest commit

 

History

History
241 lines (152 loc) · 6.73 KB

File metadata and controls

241 lines (152 loc) · 6.73 KB

Sensors

Table of Contents

Prolog

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!

Temperature/Humidity sensor (DHT11/DHT22)

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%

Requirements

  • mandatory 1x DHT11/DHT22 Sensor
  • mandatory 1x Resistor (min. 10 kilo ohms)
  • few cables
  • optional breadboard

Example

dht11.jpg

Circuit

014_circuit_diagram_dht22.png

Code

You can use the same source code and circuit diagram for DHT11 and DHT22! 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.py

Source 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] repl

Start with keys Control + d. Stop the loop with keys Control + c. To leave the REPL, press keys Control + x.

PIR sensor (HC-SR501)

If you want to detect movements in the area, the PIR sensor is already suitable. The sensors are available in various variants.

Requirements

  • mandatory PIR Sensor (HC-SR501)
  • few cables
  • optional breadboard

Example

pir.jpg

Circuit

014_circuit_diagram_pir.png

Code

# create script
$ touch ~/Projects/ESP/examples/sensors/pir.py

Source 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] repl

Start with keys Control + d. Stop the loop with keys Control + c. To leave the REPL, press keys Control + x.

Ultrasonic sensor (HC-SR04)

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.

Requirements

  • mandatory Ultrasonic Sensor (HC-SR04)
  • few cables
  • optional breadboard

Example

hcsr04.jpg

Circuit

014_circuit_diagram_hcsr04.png

Code

# create script
$ touch ~/Projects/ESP/examples/sensors/hcsr04.py

Source 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] repl

Start with keys Control + d. Stop the loop with keys Control + c. To leave the REPL, press keys Control + x.

Light/Shadow detection with LDR (Photo resistor)

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.

Requirements

  • 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

ldr.jpg

Circuit

014_circuit_diagram_ldr.png

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.

Code

# create script
$ touch ~/Projects/ESP/examples/sensors/shadow_detection.py

Source 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] repl

Start 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.

IR Flame detection

Fire... fire... help it burns! With this sensor you can prevent worse.

Requirements

  • mandatory 1x LED (any color)
  • mandatory 1x Resistor (min. 220 ohms)
  • mandatory 1x IR Flame Sensor
  • few cables
  • optional breadboard

Example

ir_flame_sensor.jpg

Pinout table

IR device ESP32
VCC 3V3
GND GND
DO 23

The LED is simply connected to pin 21 with a resistor (min. 220 ohms).

Code

# create script
$ touch ~/Projects/ESP/examples/sensors/shadow_detection.py

Source 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] repl

Start 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!

Home | Previous | Next