Imagine a tiny ultrasonic sensor that measures distance with sound waves. Now imagine giving it a brain — an AI agent that interprets its readings and makes decisions autonomously. That's exactly what ASI Biont does for the HC-SR04. In this article, we'll explore how to connect this classic sensor to ASI Biont, why it's a game-changer for automation, and how you can do it yourself in minutes, not days.
The HC-SR04 is one of the most popular distance sensors in hobbyist and industrial projects. It emits a 40 kHz ultrasonic pulse and measures the time it takes to bounce back. With a range of 2 cm to 400 cm, it's used in everything from obstacle-avoiding robots to smart parking systems. But standalone, it just gives you raw numbers. Integrate it with ASI Biont, and you unlock a world of intelligent automation: the AI can monitor, alert, and even control other devices based on proximity data.
Why Connect HC-SR04 to ASI Biont?
ASI Biont is an AI agent that connects to physical devices through chat. You describe what you want, and it writes the code, handles the communication, and even suggests logic. For the HC-SR04, the typical connection method is via a microcontroller (like Arduino or ESP32) that reads the sensor and sends data to your computer over a COM port (USB serial). ASI Biont uses its Hardware Bridge (bridge.py) to talk to that COM port. Alternatively, if the microcontroller is networked, you can use MQTT or HTTP API. But for simplicity, we'll focus on the most common setup: HC-SR04 → Arduino → USB → COM port → ASI Biont.
Getting Started: Hardware and Setup
You'll need:
- HC-SR04 sensor
- Arduino board (or ESP32) — we'll use Arduino Uno for this example
- Jumper wires
- USB cable
- ASI Biont account (free tier available)
Connect the sensor to the Arduino:
- VCC → 5V
- GND → GND
- TRIG → digital pin 9
- ECHO → digital pin 10
Upload a simple sketch that reads the distance and prints it to Serial. Here's a minimal example:
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; // cm
Serial.println(distance);
delay(500);
}
Now, on your computer, you need to download the Hardware Bridge from the ASI Biont dashboard. Launch it with your token and specify the COM port (e.g., COM3 on Windows, /dev/ttyUSB0 on Linux) and baud rate:
python bridge.py --token=YOUR_TOKEN --ports=COM3 --baud 115200 --rate=10
The --rate=10 means the bridge reads from the serial port 10 times per second. That's plenty for real-time distance monitoring.
Connecting to ASI Biont: The Chat Interface
Now comes the magic. In the ASI Biont chat, you simply tell the AI: "Connect to HC-SR04 on COM3, baud 115200, and send me distance readings every second." The AI will respond by sending a command through the bridge. The bridge uses the industrial_command() function, which is the standard way to send commands to devices. Here's an example of what the AI might generate:
from asi_biont import industrial_command
# Read distance from the sensor
response = industrial_command(
protocol='serial',
command='read_distance',
port='COM3',
baud=115200
)
print(response)
But wait — the AI doesn't just magically know how to read the sensor. It uses execute_python to write a custom script that reads the serial data. For instance:
import serial
import time
ser = serial.Serial('COM3', 115200, timeout=1)
time.sleep(2) # wait for the Arduino to reset
# Read one line
line = ser.readline().decode().strip()
distance = float(line)
print(f"Distance: {distance} cm")
The AI executes this in a sandboxed Python environment, gets the result, and can then act on it. For example, if the distance is less than 20 cm, it can trigger a Telegram alert (using requests.post to the Telegram Bot API) or even send a command to another device via MQTT.
Real-World Scenario: Smart Parking with HC-SR04 and ASI Biont
Let's build a practical example: a smart parking spot occupancy detector. You have an HC-SR04 installed above a parking spot. It measures the distance to the ground. When a car is present, the distance is short (e.g., 50 cm); when empty, it's long (e.g., 150 cm).
Your goal: notify you via Telegram when a car parks or leaves.
Step 1: Describe the task in chat
"Watch the distance from my HC-SR04 on COM3. If it drops below 60 cm, send me a Telegram message 'Spot occupied'. If it goes above 100 cm, send 'Spot free'. Check every 2 seconds."
Step 2: AI writes the integration
The AI will use the bridge to read data and then use requests.post to send Telegram messages. Here's a pseudo-code snippet of what it might run:
import serial
import requests
import time
ser = serial.Serial('COM3', 115200, timeout=1)
def send_telegram(text):
url = f"https://api.telegram.org/bot{TOKEN}/sendMessage"
data = {"chat_id": CHAT_ID, "text": text}
requests.post(url, data=data)
prev_state = None
while True:
line = ser.readline().decode().strip()
if line:
distance = float(line)
state = "occupied" if distance < 60 else "free"
if state != prev_state:
send_telegram(f"Parking spot {state}")
prev_state = state
time.sleep(2)
But wait — the AI can't use while True in execute_python because of a 30-second timeout. So it would use a different approach: it can run a scheduled check or use the bridge's rate setting. In practice, you'd set up a recurring task in ASI Biont that reads the distance and checks the condition. The bridge handles the continuous reading, and the AI runs a periodic check.
Step 3: Result
Now you get a Telegram message every time a car arrives or leaves. This is just one example. The same principle applies to:
- Home automation: measure water tank level, alert when low.
- Robotics: obstacle avoidance with AI decision-making.
- Security: detect intruders by distance changes.
Comparison: HC-SR04 + ASI Biont vs. Traditional Approach
| Aspect | Traditional (e.g., Arduino + code) | With ASI Biont |
|---|---|---|
| Time to set up | Hours of coding and debugging | Minutes of chat conversation |
| Flexibility | Hard-code logic; changes require re-upload | Modify logic by simply describing new behavior |
| AI capabilities | None; just raw data | AI can analyze trends, predict maintenance, integrate with other services |
| Learning curve | Requires programming knowledge | No programming needed; AI writes code |
The Power of execute_python
One of the standout features of ASI Biont is its universal execute_python capability. You can connect any device, not just HC-SR04. If you have a sensor that communicates over I2C, SPI, or even a proprietary protocol, you can write a Python script to interface with it using libraries like pyserial, spidev, or smbus2. ASI Biont runs your script in a sandbox, so it's safe. You just describe the device and its parameters in chat, and the AI generates the code.
For example, you might say: "Connect to my pH sensor via I2C on address 0x63 and log readings every minute." The AI will write a script using smbus2, execute it, and start logging. No need to wait for official support.
Technical Accuracy: Sources and Standards
For those who want to dive deeper, the HC-SR04 datasheet (available from SparkFun and other sources) specifies the timing: a 10 µs trigger pulse, and the echo pulse width corresponds to the round-trip time. The speed of sound in air is approximately 343 m/s at 20°C, which we used in the formula. For more on serial communication, refer to the RS-232 standard (TIA-232-F) and USB CDC ACM specification. MQTT is an OASIS standard (MQTT 3.1.1, OASIS Standard, 2014). Modbus is maintained by Modbus Organization. These are well-documented, and you can find official specs online.
Conclusion: Try It Yourself
Integrating an HC-SR04 with ASI Biont is a perfect starting point for exploring AI-powered automation. It's simple, practical, and instantly rewarding. You don't need to be a programmer — the AI does the heavy lifting. Whether you're building a smart parking system, a security alert, or just experimenting, ASI Biont turns your sensor into a smart agent.
So why wait? Head over to asibiont.com, create an account, and start chatting with your HC-SR04 today. Describe your idea, and watch the AI bring it to life. The future of device automation is conversational — and it's already here.
Comments