We will show you how to use an ESP32 and an Ultrasonic Sensor to detect people’s height as they enter into any space. When the sensor detects height changes, it will send data to the MQTT broker. Mimicking a real life scenario of an IoT use case.
What you will need
- ESP32 Dev Kit C – https://www.digikey.com/product-detail/en/espressif-systems/ESP32-DEVKITC-32D-F/1965-1003-ND/9693290
- Ultrasonic Sensor HC-SR04 – https://www.sparkfun.com/products/15569
Follow the following steps:
Step 1 – Download or Clone the IoT Simplified Repo
Step 2 – Launch your ESP-IDF Command Prompt
Enter the following command into your ESP-IDF Command. If you do not have it already installed, please review our previous tutorial on how to setup your ESP-IDF environment.
Step 3 – Configure the menuconfig
After the Espressif IoT Development Framework Configuration is loaded, follow the following steps:
3.1 – Select “Example Configuration”
3.2 – Enter your credentials
- WiFi SSID – The name of your WiFi AP
- WiFi Password – The password of your AP
- Leave at 5 retries, unless you want to retry more than 5 times for successful WiFi connection
- Enter your MQTT broker address. This depends on which broker server you are using. We were using the Mosquitto broker. We ran it locally.
3.3 – Save and Exit
Step 4 – Connect the Ultrasonic Sensor to your ESP32-DevKit-C
Connect as shown in the above diagram:
- VCC to 5V
- Trig to GPIO 22
- Echo to GPIO 23
- GND to GND
Note: You can choose any available GPIO pins but just make sure to change the pin numbers in hc-sr04_driver.c.
Step 5 – Build the Firmware
Run the following command on the ESP-IDF Command Prompt:
Step 6 – Flash and Monitor the program
After the build is successfully completed, run the following command in the ESP-IDF Command Prompt to flash and monitor the firmware image:
idf.py -p COM# flash monitor
Note: Remember to update the COM# to your own specific port number. You can find it under your Device Manager and then Ports.
Step 7 – Visualize the data
First make sure there is a file named sensor_data.csv in the root folder.
Run the main_process.py python script from any Python engine. Should be compatible with > = Python37.
Start to move your hand or a person from under the sensor and you will notice their heights start to show up on your graph, such as below:
You are all done!
Please watch the YouTube video we posted to learn about ways to use and expand on this program. You can do a lot with it. It would be very interesting if you could ping us and let us know what you have built with this idea and we can feature you on our blog and social media!