DC Motors (L298N, BTS7960) + AI Agent: How to Control Robotics and Automation via Chat with ASI Biont

Introduction

If you’ve ever built a robot or an automated gate, you know the pain: you write C++ on an Arduino, test PWM signals, debug brownouts, and then realize you can’t change the speed remotely without rewriting firmware. Now imagine telling an AI agent “set motor speed to 60% when temperature exceeds 30°C” and it does the integration for you. That’s exactly what ASI Biont offers. In this guide, I’ll show you how to connect L298N and BTS7960 motor drivers to the ASI Biont AI agent, using real code, real schematics, and real-world use cases like a robot vacuum or an intelligent barrier.

Why Connect DC Motor Drivers to an AI Agent?

DC motors are everywhere: conveyor belts, robotic arms, electric curtains, smart gates. The L298N is cheap but bulky; the BTS7960 is more efficient and handles higher currents (up to 43 A). But both require low-level PWM and direction control. With ASI Biont, you can:

  • Control motors via Telegram, voice, or chat commands.
  • Automate actions based on sensor data (stop the conveyor if a sensor is blocked).
  • Change parameters on the fly without reflashing the microcontroller.
  • Get real-time logs and alerts.

Which Connection Method Does ASI Biont Use?

For L298N and BTS7960, the most practical method is Hardware Bridge via COM port. You run a small Python script (bridge.py) on your PC or Raspberry Pi, which talks to the microcontroller (Arduino, ESP32, STM32) over USB-serial. The bridge connects to ASI Biont via WebSocket. All commands are sent through the chat using industrial_command() with protocol serial://. The AI writes the microcontroller firmware (Arduino sketch) and the bridge configuration for you.

Alternatively, you can use SSH if your motor controller is connected to a Raspberry Pi’s GPIO pins, or MQTT if you use an ESP32 with WiFi. But for simplicity and reliability, the COM port approach is the best starting point.

Real Use Case: ESP32 + L298N + ASI Biont – Remote Robot Control via Telegram

Hardware Setup

Component Connection
ESP32 (DevKit) USB to PC (COM3)
L298N IN1 → GPIO 26, IN2 → GPIO 27, ENA → GPIO 14 (PWM)
Power supply 12 V for motors, 5 V for ESP32 (via L298N 5V out)

Step 1: Arduino Sketch (upload once)

// L298N control via serial commands
// Commands: FORWARD, BACKWARD, STOP, SPEED 0-255

int in1 = 26;
int in2 = 27;
int enA = 14;

void setup() {
  Serial.begin(115200);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enA, OUTPUT);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite(enA, 0);
}

void loop() {
  if (Serial.available()) {
    String cmd = Serial.readStringUntil('\n');
    cmd.trim();
    if (cmd == "FORWARD") {
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
    } else if (cmd == "BACKWARD") {
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
    } else if (cmd == "STOP") {
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
    } else if (cmd.startsWith("SPEED ")) {
      int val = cmd.substring(6).toInt();
      analogWrite(enA, constrain(val, 0, 255));
    }
    Serial.println("OK");
  }
}

Step 2: Bridge Configuration

Download bridge.py from the ASI Biont dashboard (Devices → Create API Key → Download bridge). Run it with:

pip install pyserial requests websockets
python bridge.py --token=YOUR_TOKEN --ports=COM3 --baud 115200

Step 3: Chat with AI

In the ASI Biont chat, tell the AI:

“Connect to COM3 at 115200 baud via bridge. I have an L298N motor driver. Send command FORWARD, then SPEED 200, wait 3 seconds, then STOP.”

The AI will execute:

# AI-generated code (executed on the server via industrial_command)
commands = [
    {"protocol": "serial://", "command": "serial_write_and_read", "data": "464f52574152440a"},  # FORWARD\n
    {"protocol": "serial://", "command": "serial_write_and_read", "data": "5350454544203230300a"},  # SPEED 200\n
    {"protocol": "serial://", "command": "serial_write_and_read", "data": "53544f500a"}  # STOP\n
]
for cmd in commands:
    result = industrial_command(**cmd)
    print(result)

No manual coding. The AI interprets your request and sends the right hex-encoded commands. The bridge decodes them and sends to the ESP32.

Real Use Case: BTS7960 + Raspberry Pi + ASI Biont – Smart Barrier via SSH

Hardware Setup

Component Connection
Raspberry Pi 4 GPIO 18 (RPWM), GPIO 19 (LPWM), GPIO 23 (R_EN), GPIO 24 (L_EN)
BTS7960 Motor power: 12 V, logic: 5 V from Pi
Limit switch GPIO 25 (open limit), GPIO 26 (close limit)

Step 1: Install pigpio on Pi

sudo apt update && sudo apt install pigpio python3-pigpio
sudo systemctl enable pigpiod && sudo systemctl start pigpiod

Step 2: AI Connects via SSH

In the chat, say:

“SSH into my Pi at 192.168.1.100 with user pi. Use the key in my profile. Control a BTS7960 motor driver on GPIO 18 and 19. Make the barrier open for 5 seconds, then close. Use pigpio library.”

The AI writes and executes a Python script on the Pi via paramiko:

import pigpio
import time

pi = pigpio.pi()
RPWM = 18
LPWM = 19
R_EN = 23
L_EN = 24

pi.set_mode(RPWM, pigpio.OUTPUT)
pi.set_mode(LPWM, pigpio.OUTPUT)
pi.set_mode(R_EN, pigpio.OUTPUT)
pi.set_mode(L_EN, pigpio.OUTPUT)

pi.write(R_EN, 1)
pi.write(L_EN, 1)

def motor_speed(speed):
    # speed: -255 to 255; positive = open, negative = close
    if speed > 0:
        pi.hardware_PWM(RPWM, 100, int(speed * 1000))
        pi.hardware_PWM(LPWM, 100, 0)
    elif speed < 0:
        pi.hardware_PWM(RPWM, 100, 0)
        pi.hardware_PWM(LPWM, 100, int(-speed * 1000))
    else:
        pi.hardware_PWM(RPWM, 100, 0)
        pi.hardware_PWM(LPWM, 100, 0)

# Open barrier
motor_speed(200)
time.sleep(5)
# Close barrier
motor_speed(-200)
time.sleep(5)
motor_speed(0)
pi.stop()

The AI runs the script via paramiko, monitors output, and reports back: “Barrier opened and closed successfully.”

Real Use Case: ESP32 + BTS7960 + MQTT + ASI Biont – Conveyor Belt Automation

For a factory conveyor, MQTT is more robust. The ESP32 publishes status and subscribes to commands via a broker (e.g., Mosquitto). ASI Biont connects to the same broker using paho-mqtt in execute_python.

ESP32 MicroPython Code (upload once)

import network
import time
from machine import Pin, PWM
from umqtt.simple import MQTTClient

# WiFi credentials
SSID = "FactoryWiFi"
PASSWORD = "secret"

# MQTT broker
BROKER = "192.168.1.50"
TOPIC_CMD = "conveyor/cmd"
TOPIC_STATUS = "conveyor/status"

# Motor pins (BTS7960)
RPWM = PWM(Pin(18), freq=1000)
LPWM = PWM(Pin(19), freq=1000)
R_EN = Pin(23, Pin.OUT)
L_EN = Pin(24, Pin.OUT)

R_EN.on()
L_EN.on()

def set_motor(speed):
    # speed: -1023 to 1023
    if speed > 0:
        RPWM.duty(speed)
        LPWM.duty(0)
    elif speed < 0:
        RPWM.duty(0)
        LPWM.duty(-speed)
    else:
        RPWM.duty(0)
        LPWM.duty(0)

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
    time.sleep(0.5)

client = MQTTClient("conveyor_esp", BROKER)
client.set_callback(lambda topic, msg: set_motor(int(msg)))
client.connect()
client.subscribe(TOPIC_CMD)

while True:
    client.check_msg()
    time.sleep(0.1)

AI Integration via MQTT

In the ASI Biont chat, say:

“Connect to MQTT broker at 192.168.1.50. Subscribe to conveyor/status. If the status says ‘jammed’ for more than 10 seconds, publish 0 to conveyor/cmd to stop the motor and alert me via Telegram.”

The AI writes:

import paho.mqtt.client as mqtt
import time

broker = "192.168.1.50"
topic_cmd = "conveyor/cmd"
topic_status = "conveyor/status"

jammed_start = None

def on_message(client, userdata, msg):
    global jammed_start
    if msg.payload.decode() == "jammed":
        if jammed_start is None:
            jammed_start = time.time()
        elif time.time() - jammed_start > 10:
            client.publish(topic_cmd, "0")
            print("Motor stopped. Alert sent.")
            # send_telegram_alert("Conveyor jammed!")
    else:
        jammed_start = None

mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.connect(broker)
mqttc.subscribe(topic_status)
mqttc.loop_start()
time.sleep(30)  # monitor for 30 seconds (sandbox limit)
mqttc.loop_stop()

Comparison: L298N vs BTS7960 for AI Integration

Feature L298N BTS7960
Max continuous current 2 A per channel 43 A per channel
Voltage range 4.5–46 V 5.5–27 V
PWM frequency limit ~40 kHz (with proper heatsink) Up to 25 kHz
Heat dissipation Poor (linear driver) Good (MOSFET H-bridge)
Ideal for Small robots, education, low-cost projects High-power motors, industrial conveyors, e-bikes
AI control via ASI Biont Same methods (COM, SSH, MQTT) Same methods

Why Use ASI Biont Instead of Writing All Code Yourself?

  1. No manual coding of integration logic – you describe the behavior in plain English (or Russian), and the AI generates the Python code, the bridge commands, or the MQTT publish/subscribe logic.
  2. Multi-protocol support – ASI Biont can talk to your motor driver via COM port, SSH, MQTT, Modbus, or even a custom HTTP API. You don’t need to learn all protocols.
  3. Real-time adaptability – change motor speed, direction, or automation rules on the fly. No firmware reflash required.
  4. Telegram, voice, and chat control – give commands from anywhere. For example, “set conveyor speed to 70%” while you’re at lunch.

Pitfalls to Avoid

  • Baud rate mismatch – ensure ESP32/Arduino sketch uses the same baud as bridge.py. Check with HELP command: the bridge sends HELP\n (hex 48454c500a) and expects a response. If no response, verify wiring and baud.
  • Power supply – L298N can drop voltage; use a separate 5V for logic if motors draw >1 A. BTS7960 needs a common ground with the microcontroller.
  • PWM frequency – L298N works best at 1–10 kHz; BTS7960 can handle up to 25 kHz. For smooth motion, use 20 kHz for BTS7960.
  • Sandbox timeout – if you use execute_python, the script runs for max 30 seconds. For long-running tasks (like a barrier that stays open for 1 hour), use MQTT or COM with the bridge – the bridge runs continuously on your PC.
  • Hex encoding – when sending commands via serial_write_and_read, the data field must be a hex string. Use online hex converters or let the AI do it.

Conclusion

Connecting L298N or BTS7960 motor drivers to ASI Biont turns a static robot into an intelligent, remotely controllable system. Whether you’re building a robot vacuum, a conveyor belt, or a smart barrier, the AI agent handles the integration in seconds. You don’t need to be a Python expert or a firmware guru – just describe what you want in the chat. The AI writes the code, selects the right protocol (COM, SSH, MQTT), and executes it.

Try it yourself on asibiont.com. Create an API key, download bridge.py, and start controlling your motors via Telegram. The future of robotics is conversational – and it’s already here.

← All posts

Comments