Title:
I wired an IR sensor to an ESP8266 and watched it on a live dashboard
published: false
tags: esp8266, iot, arduino, beginners
I had an IR obstacle sensor sitting in a drawer and an ESP8266 NodeMCU on my desk. I wanted to see whether I could check the sensor from a browser instead of standing next to the board. This post covers what I built, the three wires it needed, and the two errors that slowed me down.
What it does
Every two seconds the board reads the sensor on pin D7, prints the result to the Serial Monitor, and sends DETECTED or NOT DETECTED to a KiwisIoT dashboard. That's the whole project. It's small on purpose.
Wiring
It takes three wires:
- Sensor VCC to 3V3
- Sensor GND to GND
- Sensor OUT to D7
circuit diagram
The sensor output is active LOW. When something reflects the IR light back, OUT drops to 0. When nothing is there, it reads 1. So the code checks for LOW:
int irState = digitalRead(IR_PIN);
if (irState == LOW) {
objectStatus = "DETECTED";
} else {
objectStatus = "NOT DETECTED";
}
I got this backwards at first and the dashboard said DETECTED when my hand was nowhere near the sensor. Checking the raw value in the Serial Monitor fixed it in about a minute.
connection diagram
The sketch
The sketch connects to Wi-Fi, connects to KiwisIoT with a topic ID, and runs a timer in the main loop so nothing blocks:
void loop() {
kiwisiot.run();
static unsigned long lastSend = 0;
if (millis() - lastSend >= 2000) {
lastSend = millis();
sendObjectStatus();
}
delay(100);
}
I used millis() instead of delay(2000) so that kiwisiot.run() keeps getting called. If you block for two seconds, the connection can drop.
Errors I ran into
Access is denied on the COM port. Windows said the port was in use. The Serial Monitor was still open in another window. Closing it, unplugging the board, and plugging it back in solved it.
invalid head of packet (0x00) during upload. I had the wrong board selected and my USB cable only charges. Switching to a data cable, selecting NodeMCU 1.0 (ESP-12E Module), and holding FLASH while pressing RST got the upload through.
code
#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 IR_PIN D7
KiwisIoT kiwisiot(ssid, pass, topic);
void sendObjectStatus() {
int irState = digitalRead(IR_PIN);
String objectStatus;
if (irState == LOW) {
objectStatus = "DETECTED";
}
else {
objectStatus = "NOT DETECTED";
}
Serial.println();
Serial.println("---------- IR MONITORING ----------");
Serial.print("IR Sensor Value: ");
Serial.println(irState);
Serial.print("Object Status: ");
Serial.println(objectStatus);
kiwisiot.send("0", objectStatus);
Serial.print("Sent to KiwisIoT Channel 0: ");
Serial.println(objectStatus);
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println(" IR OBJECT MONITORING ");
pinMode(IR_PIN, INPUT);
Serial.println("IR sensor initialized");
Serial.println("Connecting to KiwisIoT...");
kiwisiot.begin();
Serial.println("KiwisIoT initialized");
Serial.println("Starting object monitoring...");
}
void loop() {
kiwisiot.run();
static unsigned long lastSend = 0;
if (millis() - lastSend >= 2000) {
lastSend = millis();
sendObjectStatus();
}
delay(100);
}
Tuning the sensor
The small potentiometer on the module sets the detection distance. I turned it until the LED lit only when something was close, then checked that the dashboard matched.
dashboard output
What I'd add next
- A running count of detections, shown as a chart
- A buzzer that fires on detection
- Deep sleep for battery use
arduino output
The code, wiring diagram, and README are on GitHub. Wi-Fi details and the dashboard topic ID are placeholders, so keep your own values in a local secrets.h file.
If you've built something similar, I'd like to hear how you handled sensor noise.




Top comments (0)