ultrasonic sensor and an esp32

Ultrasonic Sensor to Detect People Heights as they enter a Space using ESP32

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

Video Instructions

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.

idf.py menuconfig

Step 3 – Configure the menuconfig

After the Espressif IoT Development Framework Configuration is loaded, follow the following steps:

3.1 – Select “Example Configuration”

ESP-IDF image

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.
ESP-IDF program image

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

HC-SR04 Datasheet for Reference.

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:

idf.py build

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!

How To's , , , , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: