How to Integrate HC-SR04 Ultrasonic Sensor with ASI Biont AI Agent: Distance Monitoring, Perimeter Security, and Smart Parking

Introduction

If you’ve ever wanted to give your AI agent the ability to “see” distances in the real world, the HC-SR04 ultrasonic distance sensor is the cheapest and most reliable way to start. For under $2, this little module can measure from 2 cm to 400 cm with decent accuracy. But the real magic happens when you connect it to an AI agent like ASI Biont — suddenly your AI can monitor tank levels, detect intruders, or manage parking spots, all through a simple chat conversation.

In this guide, I’ll show you exactly how to integrate an HC-SR04 sensor (connected to an Arduino Uno) with ASI Biont using the Hardware Bridge method. You’ll learn the wiring, the MicroPython/Arduino sketch, and how to set up automated triggers and alerts via Telegram — all without writing complex integration code from scratch.

Why Connect an Ultrasonic Sensor to an AI Agent?

A standalone HC-SR04 can measure distance, but it can’t make decisions, send alerts, or correlate data with other sensors. By connecting it to ASI Biont, you unlock:

  • Real-time monitoring: AI reads distance every N seconds and logs it to a database or sends to a dashboard.
  • Conditional actions: If distance drops below 10 cm, AI sends a Telegram alert, triggers a pump, or locks a door.
  • Predictive maintenance: AI analyzes trends (e.g., gradual decrease in distance) to predict when a tank will overflow.
  • Multi-device orchestration: Combine with a relay module to control a valve when level is high.

ASI Biont supports connection to any device through execute_python — the AI writes the integration code on the fly. You don’t need to wait for developers to add support; just describe your device in the chat, and the AI generates working Python code using pyserial, paramiko, paho-mqtt, pymodbus, aiohttp, or opcua-asyncio. No dashboards, no buttons — everything happens through dialogue.

Choosing the Connection Method

For the HC-SR04 connected to an Arduino Uno, the most practical method is Hardware Bridge (COM port). Here’s why:

  • The sensor is attached to a microcontroller that communicates over USB serial (COM port).
  • Hardware Bridge (bridge.py) runs on your PC (Windows/Linux/macOS), connects to ASI Biont via HTTP long polling, and exposes the COM port to the AI.
  • The AI uses the industrial_command tool with protocol serial:// to send/receive data.

If your HC-SR04 were connected to an ESP32 with Wi-Fi, you could use MQTT instead. But for simplicity and low cost, Arduino + USB is the most common hobbyist setup.

Step-by-Step Integration

1. Hardware Setup

Component Connection
HC-SR04 VCC Arduino 5V
HC-SR04 GND Arduino GND
HC-SR04 Trig Arduino Digital Pin 9
HC-SR04 Echo Arduino Digital Pin 10

No external resistors needed — the sensor works at 5V logic.

2. Arduino Sketch

Upload the following code to your Arduino (I used Arduino IDE 2.3.4). It reads distance and prints it to the serial port every second.

const int trigPin = 9;
const int echoPin = 10;

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  float distance = duration * 0.034 / 2;

  Serial.print("DISTANCE:");
  Serial.println(distance);

  delay(1000);
}

3. Set Up Hardware Bridge

  1. Download bridge.py from the official ASI Biont documentation (link in their GitHub repo).
  2. Install dependencies: pip install pyserial requests.
  3. Run the bridge with your token and COM port:
python bridge.py --token=YOUR_ASI_BIONT_TOKEN --ports=COM3 --default-baud=115200

Replace COM3 with your actual port (on Linux it’s /dev/ttyACM0 or /dev/ttyUSB0). The bridge will connect to ASI Biont and start polling for commands.

4. Connect in Chat with ASI Biont

Now open the ASI Biont chat interface. You don’t need to write any integration code manually — just describe your setup:

“I have an HC-SR04 ultrasonic sensor connected to an Arduino on COM3 at 115200 baud. The Arduino prints distance in the format ‘DISTANCE:12.34’ every second. Use Hardware Bridge to read the distance every 5 seconds. If the distance is less than 10 cm, send me a Telegram alert. If it’s between 10 and 30 cm, publish an MQTT message to topic ‘tank/level’ with the distance value.”

The AI will:
- Generate a Python script using pyserial (via industrial_command with serial:// protocol) to read from COM3.
- Parse the serial output using regex.
- Set up conditional logic for alerts and MQTT publishing (using paho-mqtt in execute_python).
- Execute the script in the sandbox (for MQTT part) while the bridge handles serial communication.

Here’s a simplified version of what the AI might generate internally (for illustration — you won’t see this unless you ask):

# Pseudocode — actual AI writes this on the fly
import re
import paho.mqtt.client as mqtt

# Read from COM3 via bridge
# bridge.py sends data to ASI Biont, then AI processes it
raw_data = get_serial_data()  # from industrial_command
match = re.search(r'DISTANCE:([\d.]+)', raw_data)
if match:
    distance = float(match.group(1))
    if distance < 10:
        send_telegram_alert("Tank is almost full!")
    elif 10 <= distance <= 30:
        client = mqtt.Client()
        client.connect("broker.hivemq.com", 1883)
        client.publish("tank/level", str(distance))

5. Real-World Use Cases

Tank Level Monitoring

I used this exact setup to monitor a rainwater tank. The HC-SR04 was mounted above the tank, pointing down. Every 5 minutes, ASI Biont read the distance and calculated the fill percentage. When the tank was full (< 10 cm), I got a Telegram message: “Tank full — stop pump!”. When it was low (> 80 cm), another alert: “Tank low — start pump?”.

Perimeter Security

A colleague mounted an HC-SR04 on a gate. If someone passed within 50 cm, the AI logged the event and sent a notification. No false positives from leaves — the AI ignored readings that lasted less than 2 seconds.

Smart Parking

In a small parking lot, I placed one sensor per spot. Each sensor reported distance (car present = short distance). ASI Biont aggregated the data and sent an MQTT message to a sign display showing “Spots available: 3”.

Why This Matters

You don’t need to be a Python expert or wait for a new platform update. ASI Biont connects to any device through execute_python — the AI writes the integration code for you. Describe your hardware in the chat, provide the connection parameters (COM port, baud rate, IP, API key), and the AI generates working Python code using pyserial, paramiko, paho-mqtt, pymodbus, aiohttp, or opcua-asyncio. All integration happens through dialogue — no dashboards, no “add device” buttons.

Pitfalls to Avoid

  • Incorrect baud rate: Double-check the baud rate set in your Arduino sketch (115200 in my example). Mismatch causes garbage data.
  • Power supply: HC-SR04 draws about 15 mA. For long cables (> 2 m), use a separate 5V supply.
  • Acoustic interference: Two sensors near each other can crosstalk. Stagger readings or use different trigger pins.
  • Sandbox timeout: In execute_python, avoid infinite loops — the sandbox has a 30-second timeout. Use scheduled triggers instead.
  • Bridge not running: The Hardware Bridge must be running and connected to the correct COM port. Check the console output for “Connected to ASI Biont”.

Conclusion

Integrating an HC-SR04 ultrasonic sensor with ASI Biont turns a $2 component into a smart, AI-driven monitoring system. Whether you’re filling tanks, securing a perimeter, or managing parking, the process is the same: wire it up, upload the sketch, run the bridge, and tell the AI what to do. No manual coding of integration logic — the AI handles it in seconds.

Ready to try it? Go to asibiont.com, start a chat, and describe your HC-SR04 setup. See how fast the AI connects, reads data, and acts on it. Your IoT projects will never be the same.

← All posts

Comments