Teensy 4.x Meets AI Agent: Real-Time Control via UART with ASI Biont

Why Teensy 4.x Needs an AI Brain

The Teensy 4.x is a beast — 600 MHz ARM Cortex-M7, 2 MB flash, 1 MB RAM, and enough I/O to handle high-speed sensors, audio processing, and multi-axis motor control. But even the fastest microcontroller has a blind spot: it can't reason, plan, or communicate beyond its firmware. That's where ASI Biont steps in. By connecting your Teensy to an AI agent, you offload decision-making, logging, and remote control to a cloud-based brain — without writing a single line of server code.

ASI Biont supports 15+ connection protocols, but for Teensy 4.x the most practical is UART/Serial via the Hardware Bridge. Why? Teensy has native USB-to-serial (a single USB cable gives you power + data), and the bridge runs on your PC (Windows/Linux/macOS) to relay commands between the AI cloud and your microcontroller. No WiFi, no Ethernet dongle — just a USB cable and a Python script.

How the Connection Actually Works

Here's the architecture:

  1. You download bridge.py from the ASI Biont dashboard (Devices → Create API Key → Download bridge).
  2. You run it on your computer: python bridge.py --token=YOUR_TOKEN --ports=COM3 --baud=115200
  3. The bridge opens a WebSocket connection to the ASI Biont cloud (the only communication channel).
  4. In the chat, you tell the AI: "Connect to Teensy on COM3 at 115200 baud. Send the HELP command to verify."
  5. The AI calls industrial_command(protocol='serial', command='serial_write_and_read', data='48454c500a') — the hex for HELP\n.
  6. Bridge sends the bytes to COM3, reads the response, and returns it to the AI.

The AI can now read sensor data, send motor commands, or run calibration routines — all through chat conversation. No REST API, no dashboard buttons, no middleware.

Concrete Use Case: Adaptive Lighting with Audio

Imagine a smart studio light that adjusts brightness based on ambient noise (e.g., louder music → brighter lights). Here's how to build it with Teensy 4.x + ASI Biont.

Hardware Setup

  • Teensy 4.0 (or 4.1)
  • Electret microphone module (MAX9814) on A0
  • High-power LED strip on pin 3 (PWM)
  • USB cable to your PC

Teensy Firmware (Arduino IDE)

// Simple command-response protocol
void setup() {
  Serial.begin(115200);
  pinMode(3, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    String cmd = Serial.readStringUntil('\n');
    cmd.trim();
    if (cmd == "HELP") {
      Serial.println("READ_AUDIO, SET_LED:value");
    } else if (cmd == "READ_AUDIO") {
      int mic = analogRead(A0);
      Serial.println(mic);
    } else if (cmd.startsWith("SET_LED:")) {
      int val = cmd.substring(8).toInt();
      analogWrite(3, constrain(val, 0, 255));
      Serial.println("OK");
    }
  }
}

AI Agent Integration (via Chat)

User says: "Read audio level from Teensy every 5 seconds. Map it to LED brightness (0–255). If audio > 700, set max brightness. Log readings to a file."

The AI writes and executes a Python script using execute_python with pyserial-like logic — but actually the AI uses the industrial_command tool to send serial_write_and_read commands through the bridge, because execute_python has no direct COM access.

What the AI does internally:

# Pseudocode — AI generates this and runs via industrial_command
import time
for _ in range(12):  # 1 minute of monitoring
    response = industrial_command(
        protocol='serial',
        command='serial_write_and_read',
        data=b'READ_AUDIO\n'.hex()
    )
    audio_level = int(response.decode().strip())
    brightness = min(255, audio_level // 3)
    industrial_command(
        protocol='serial',
        command='serial_write_and_read',
        data=f'SET_LED:{brightness}\n'.encode().hex()
    )
    time.sleep(5)

But you don't write this — you just describe what you need, and the AI writes the integration code in seconds.

Result

  • Ambient noise changes → Teensy reads mic → AI calculates brightness → Teensy adjusts LED → all in <100 ms round trip.
  • You can also ask: "Send me a Telegram alert if audio stays above 800 for 10 seconds." — the AI adds Twilio or Telegram bot logic.

Real-World Pitfalls (I Learned the Hard Way)

  1. Baud rate mismatch — Teensy 4.x can run Serial at up to 12 Mbps, but bridge.py defaults to 115200. Always match both sides.
  2. Windows COM port flakiness — On Windows, pyserial sometimes fails with overlapped I/O. The bridge handles this by calling CancelIoEx + PurgeComm + synchronous WriteFile. If you see "written: 0" in logs, it's auto-recovered.
  3. HELP protocol — Always implement HELP returning a list of commands in your Teensy sketch. The AI uses it to verify connectivity and discover capabilities.
  4. No while True in execute_python — Sandbox timeout is 30 seconds. Use finite loops or async tasks.

Why This Beats Manual Coding

  • No server setup — The AI handles WebSocket, serial parsing, and error recovery.
  • No API design — Describe in natural language, get working code.
  • Protocol flexibility — Switch from Serial to MQTT, Modbus, or SSH in the same chat (e.g., "Now also publish audio level to MQTT broker at 192.168.1.50").
  • Instant alerts — Add Telegram, email, or SMS notifications without touching firmware.

Other Teensy 4.x Integration Ideas

Scenario Sensor/Actuator AI Agent Role
Motor calibration Encoder + DC motor Send step sequence, read position, compute PID gains
Audio spectrum analyzer MEMS microphone (SPH0645) Read FFT bins via serial, visualize in chat
Remote data logger Any analog/digital sensor Poll every 10s, store to Google Sheets via API
Smart greenhouse Temperature + humidity + fan Maintain setpoint, send alerts on thresholds

How to Start in 5 Minutes

  1. Flash the Teensy with the simple command-response sketch above.
  2. Download bridge.py from your ASI Biont dashboard.
  3. Run: pip install pyserial requests websockets then python bridge.py --token=YOUR_TOKEN --ports=COM3 --baud=115200
  4. In the ASI Biont chat, type: "Connect to Teensy on COM3, send HELP, then read audio and control LED brightness based on noise."
  5. Watch the AI do the rest.

No waiting for firmware updates, no custom server code. Just you, your Teensy, and an AI agent that speaks serial.

Conclusion

Teensy 4.x is powerful, but raw. Pair it with ASI Biont and you get real-time AI reasoning, remote monitoring, and multi-protocol connectivity — all through a single USB cable and a chat conversation. Whether you're building a robot, a smart studio, or an industrial controller, the integration path is the same: describe what you need, let the AI write the glue.

Ready to give your Teensy a brain? Try it now at asibiont.com — create an API key, run the bridge, and start chatting with your hardware.

← All posts

Comments