Dual IR Entry Exit Detector with Telegram Alerts
Dual IR Entry Exit Detector with Telegram Alerts is a runnable microcontroller simulation example on SensBench β open the wired circuit, inspect the code, and compile real firmware in your browser, no install needed. Last updated Aug 14, 2026.
FREE SIMULATOR EXAMPLE · INTERMEDIATE
Dual IR Entry Exit Detector with Telegram Alerts
This example builds a directional doorway counter on the ESP32 β two IR beams detect entry vs exit by trip order, and every crossing posts a Telegram alert. The classic dual-beam people-counter pattern.
| Board | ESP32 DevKit (Xtensa LX6, 240 MHz) |
|---|---|
| Difficulty | intermediate |
| Category | communication |
| Libraries | None β just the Arduino core |
| Runs in | Sensbench browser simulator β free, no account |

The exact circuit you get when you open this example β already wired, ready to run.
The sketch
# main.py
import network
import urequests
import machine
import time
# βββ WiFi βββββββββββββββββββββββββββββββββββββββββββββ
SSID = "kritish"
PASSWORD = "iot"
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(SSID, PASSWORD)
print("Connecting to WiFi...")
while not wifi.isconnected():
time.sleep(1)
print("WiFi Connected!")
print(wifi.ifconfig())
# βββ Telegram βββββββββββββββββββββββββββββββββββββββββ
BOT_TOKEN = ""
CHAT_ID = 54 # int only, no quotes
def send_telegram(msg):
msg_encoded = msg.replace(" ", "%20").replace("\n", "%0A")
url = (
"https://api.telegram.org/bot"
+ BOT_TOKEN
+ "/sendMessage?chat_id="
+ str(CHAT_ID)
+ "&text="
+ msg_encoded
)
try:
r = urequests.get(url)
print("Telegram response:", r.text[:60])
r.close()
except Exception as e:
print("Telegram error:", e)
# βββ Pins βββββββββββββββββββββββββββββββββββββββββββββ
ir_entry = machine.Pin(14, machine.Pin.IN) # Entry IR
ir_exit = machine.Pin(27, machine.Pin.IN) # Exit IR
red_led = machine.Pin(26, machine.Pin.OUT) # Red LED
green_led = machine.Pin(25, machine.Pin.OUT) # Green LED
red_led.value(0)
green_led.value(0)
print("System Ready!")
# βββ Main Loop ββββββββββββββββββββββββββββββββββββββββ
entry_triggered = False
exit_triggered = False
while True:
entry_val = ir_entry.value()
exit_val = ir_exit.value()
# Entry IR blocked
if entry_val == 0 and not entry_triggered:
entry_triggered = True
exit_triggered = False
red_led.value(1) # Red ON - object inside
green_led.value(0)
print("Object Entered!")
send_telegram("Object Entered!")
# Exit IR blocked
if exit_val == 0 and not exit_triggered and entry_triggered:
exit_triggered = True
entry_triggered = False
red_led.value(0) # Red OFF
green_led.value(1) # Green ON - object gone
print("Object Gone!")
send_telegram("Object Gone!")
time.sleep(2) # Green stays for 2 sec
green_led.value(0)
time.sleep(0.1)
How it works
The script watches two IR beam sensors. When their trip order indicates an entry or exit, it increments the corresponding counter and posts an alert to Telegram via the Bot API. The order detection is the core logic β a small state machine tracking which beam broke first.
The Telegram call is a single HTTPS POST with the bot token, chat ID and message text. The WiFi connection must be alive for the alert to leave the device, which makes the network state part of the detector’s reliability story.
Before you build it for real
The direction logic assumes a single person in the beam path: two people crossing simultaneously trip both sensors in an ambiguous order, and the state machine miscounts β the classic limitation of every beam-based people counter. The Telegram alert depends on WiFi: offline, the crossing is counted locally but the alert is silently lost unless the code queues and retries. And the bot token in the source is a credential β anyone with the firmware can impersonate the bot, so treat it like a password.
Get the parts
Related examples
FAQ
Why two IR sensors for one doorway?
Two beams in sequence distinguish direction: beam A then beam B means entry, B then A means exit. One sensor only tells you something passed; two sensors tell you which way β the difference between a counter and a directional counter.
How do Telegram alerts reach the phone?
The ESP32 posts to the Telegram Bot API over HTTPS with a bot token and chat ID. The bot relays the message to the subscribed chat β no app development, no push infrastructure, just an HTTP call from the device.