DEV Community

Cover image for Obstacle Detection with KiwisIoT
S. Nishanth
S. Nishanth

Posted on Edited on

Obstacle Detection with KiwisIoT

The Idea Behind My Project

I wanted to explore something interesting in IoT, so I decided to build a distance monitoring system using an ESP8266 NodeMCU and an HC-SR04 ultrasonic sensor.

My idea was to measure the distance of nearby objects and display the readings on a live dashboard instead of checking everything manually through the Serial Monitor.

After exploring the KiwisIoT platform, I decided to connect my ESP8266 to it and make the project more interactive.

Starting with the Hardware

I started by collecting the required components and connecting the ultrasonic sensor to the ESP8266.

The connections were simple:

  • VCC → VIN (5V)
  • GND → GND
  • TRIG → D1
  • ECHO → D2

Circuit diagram

One thing I had to be careful about was the ECHO pin. Since the HC-SR04 sends a 5V signal and the ESP8266 supports only 3.3V GPIO input, I used a voltage divider to protect the board.

Once the connections were completed, I moved on to programming.

Making the Sensor Work

I used Arduino IDE to write the program. The ultrasonic sensor sends a sound pulse and waits for it to bounce back from an object.

The ESP8266 calculates the distance based on the time taken by the reflected signal.

I programmed it to read the distance every two seconds and display the result in centimeters.

Connecting It to KiwisIoT

This was the most exciting part of the project!

I connected the ESP8266 to Wi-Fi and configured my KiwisIoT dashboard using its Topic ID.

The board started sending distance values to the dashboard every two seconds.

When I placed an object in front of the sensor and moved it closer or farther away, I could see the distance readings changing on the dashboard.

dashboard

Seeing my hardware communicate with a live dashboard was a really satisfying moment!

Challenges Along the Way

The development process wasn't completely smooth. I faced a few issues, such as upload errors, missing echo signals, and unstable readings.

I checked the wiring, corrected the board configuration, used a proper USB data cable, and verified the power supply.

After a few attempts, everything started working correctly.

These small problems taught me that patience and troubleshooting are important parts of building hardware projects.

coding

#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 D6
#define ECHO_PIN D7

KiwisIoT kiwisiot(ssid, pass, topic);

void sendDistance() {

  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000);

  if (duration == 0) {

    Serial.println();
    Serial.println("Ultrasonic: No echo received");

    return;
  }

  float distance = duration * 0.0343 / 2;

  Serial.println();
  Serial.println("---------- ULTRASONIC MONITORING ----------");

  Serial.print("Distance: ");
  Serial.print(distance, 2);
  Serial.println(" cm");

  String distanceData = String(distance, 2);

  kiwisiot.send("0", distanceData);

  Serial.print("Sent to KiwisIoT Channel 0: ");
  Serial.print(distanceData);
  Serial.println(" cm");
}

void setup() {

  Serial.begin(115200);
  Serial.println("   ULTRASONIC DISTANCE MONITOR   ");

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  digitalWrite(TRIG_PIN, LOW);

  Serial.println("Ultrasonic sensor initialized");

  Serial.println("Connecting to KiwisIoT...");

  kiwisiot.begin();

  Serial.println("KiwisIoT initialized");
  Serial.println("Starting distance monitoring...");
}

void loop() {

  kiwisiot.run();

  static unsigned long lastSend = 0;

  if (millis() - lastSend >= 2000) {

    lastSend = millis();

    sendDistance();
  }

  delay(100);
}
Enter fullscreen mode Exit fullscreen mode

The Final Outcome

Finally, I successfully built an IoT-based distance monitoring system.

The ESP8266 measures the distance using the HC-SR04 sensor and sends the readings to the KiwisIoT dashboard through Wi-Fi. I can monitor the distance remotely without being near the hardware.

Through this project, I gained practical experience with sensor interfacing, ESP8266 programming, Wi-Fi communication, and IoT dashboards.

arduino output

What's Next?

I have a few ideas to improve this project:

  • Add a buzzer when an object comes too close.
  • Display distance history using graphs.
  • Add more sensors for multiple measurements.

My Takeaway

This project started with a simple idea, but it helped me understand how hardware and IoT platforms work together.

Watching real-time sensor readings appear on a dashboard made the learning experience even more interesting.

I'm excited to explore more IoT projects and continue learning by building things!

official kiwisiot GitHub Repository link: KiwisIoT – Ultrasonic Distance Monitoring

Have you built any projects using ESP8266 or ultrasonic sensors? Share your experience in the comments!

Top comments (0)