Ultrasonic Sentinel KiwisIoT: ESP8266-Based Real-Time Distance Monitoring
Hello, DEV Community! ๐
I'm excited to share my IoT project, Ultrasonic Sentinel KiwisIoT, built using an ESP8266 NodeMCU, an HC-SR04 ultrasonic sensor, Arduino IDE, and the KiwisIoT platform.
This project measures the distance between the sensor and a nearby object and sends the readings to a KiwisIoT dashboard over Wi-Fi for remote monitoring.
๐ ๏ธ Components Used
- ESP8266 NodeMCU
- HC-SR04 ultrasonic distance sensor
- Jumper wires
- USB cable
- Arduino IDE
- KiwisIoT dashboard
- Wi-Fi connection
๐ Circuit Connection Diagram
The HC-SR04 ultrasonic sensor has four pins: VCC, GND, TRIG, and ECHO.
| HC-SR04 pin | ESP8266 NodeMCU connection |
|---|---|
| VCC | Suitable 5 V supply |
| GND | GND |
| TRIG | D5 (GPIO14) |
| ECHO | D6 (GPIO12), through a voltage divider |
The TRIG pin initiates an ultrasonic pulse. When the pulse reflects from an object, the ECHO pin provides a signal whose duration is used to calculate the distance.
Safety note: The HC-SR04 ECHO output can be 5 V. Use a resistor voltage divider to reduce it to a safe level before connecting it to an ESP8266 GPIO.
โ๏ธ How It Works
- The ESP8266 sends a trigger pulse to the HC-SR04.
- The sensor emits ultrasonic sound waves.
- The waves reflect off a nearby object.
- The sensor returns an ECHO pulse.
- The ESP8266 calculates the distance in centimetres.
- The measured value is sent to KiwisIoT over Wi-Fi.
- The dashboard displays the latest distance reading.
The program is designed to update the dashboard approximately every two seconds.
โ๏ธ KiwisIoT Dashboard
I configured a KiwisIoT dashboard with a numeric widget labelled DISTANCE. The ESP8266 sends distance readings through Channel 0, allowing the dashboard to display the latest received value.
In my captured dashboard screenshot, the displayed reading is 7.41. The unit should be set to centimetres if the program is sending centimetre measurements.
This provides a simple way to observe sensor readings without relying only on the Arduino Serial Monitor.
๐งช Live Test Result

The hardware consists of an ESP8266 NodeMCU connected to the ultrasonic sensor using jumper wires and powered through USB.
The dashboard screenshot captured during testing shows a numerical reading of 7.41. To verify measurement accuracy, compare the displayed distance with the actual distance measured using a ruler or measuring tape.
Expected observations:
- A nearby object produces a distance reading.
- Moving the object changes the reading.
- If no valid echo is received, the program reports an invalid measurement in the Serial Monitor.
- If Wi-Fi is disconnected, new remote dashboard updates may stop.
๐ป Arduino Code
The following Arduino sketch reads the HC-SR04 sensor and sends valid distance measurements to KiwisIoT.
Replace the Wi-Fi credentials and dashboard topic with your own values before uploading.
#include <ESP8266WiFi.h>
#include <KiwisIoT.h>
const char* ssid = "YOUR_WIFI_NAME";
const char* pass = "YOUR_WIFI_PASSWORD";
const char* topic = "YOUR_DASHBOARD_TOPIC_ID";
#define TRIG_PIN D5
#define ECHO_PIN D6
KiwisIoT kiwisiot(ssid, pass, topic);
float measureDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long duration =
pulseIn(ECHO_PIN, HIGH, 30000UL);
if (duration == 0) {
return -1;
}
return duration * 0.0343 / 2.0;
}
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
kiwisiot.begin();
Serial.println("Ultrasonic Sentinel KiwisIoT");
}
void loop() {
kiwisiot.run();
static unsigned long lastSend = 0;
if (millis() - lastSend >= 2000) {
lastSend = millis();
float distance = measureDistance();
if (distance >= 0) {
Serial.print("Distance: ");
Serial.print(distance, 2);
Serial.println(" cm");
kiwisiot.send("0", String(distance, 2));
} else {
Serial.println("No valid echo received");
}
}
delay(50);
}
The code assumes the KiwisIoT library supports the begin(), run(), and send() methods used in this sketch. Test it with your installed library and confirm that Channel 0 is configured correctly.
Security reminder: Never publish your real Wi-Fi password in a public code snippet.
๐ System Architecture
HC-SR04 Ultrasonic Sensor โ ESP8266 NodeMCU โ Wi-Fi โ KiwisIoT Dashboard
The sensor measures the distance, the ESP8266 processes the reading, and the dashboard provides a remote view of the data.
๐ Applications
- Object distance monitoring
- Proximity detection experiments
- Smart parking demonstrations
- Educational IoT projects
- Distance measurement prototypes
- Embedded systems learning
๐ Future Improvements
- Add a buzzer for near-object alerts.
- Send notifications when an object comes too close.
- Store distance measurements for historical analysis.
- Add multiple sensors for different monitoring zones.
- Design a compact enclosure for the electronics.
๐ References
- KiwisIoT Projects Repository: https://github.com/Kiwistron/KiwisIoT-Projects
- ESP8266 Arduino Core: https://github.com/esp8266/Arduino
- ESP8266 Documentation: https://arduino-esp8266.readthedocs.io/
- Arduino IDE: https://www.arduino.cc/en/software
Conclusion
Ultrasonic Sentinel KiwisIoT demonstrates how an HC-SR04 ultrasonic sensor and ESP8266 NodeMCU can be combined with a Wi-Fi-connected IoT dashboard for remote distance monitoring.
This project helped me explore ultrasonic sensing, embedded programming, wireless communication, and IoT data visualization.
Thanks for reading! Feel free to share your feedback and ideas for future improvements. ๐


Top comments (1)
3rd project