Introduction
DC motors are the workhorses of robotics and automation—from simple wheeled robots to conveyor belts and smart gates. But writing custom firmware every time you want to change behavior is tedious. With ASI Biont, an AI agent that connects to any device through chat, you can skip the manual coding and let the AI handle integration on the fly. In this guide, I’ll walk through connecting a BTS7960 motor driver (or L298N) with an ESP32 to an AI agent using MQTT, so you can command your motors via natural language—no Python or Arduino IDE required (the AI writes everything).
Why MQTT + ESP32 for DC Motors?
DC motor drivers like the BTS7960 and L298N are controlled via PWM and digital pins. A common approach is to use a microcontroller (ESP32, Arduino) that listens to commands over a network protocol. MQTT is lightweight, reliable, and perfect for IoT. ASI Biont supports MQTT out-of-the-box: the AI can write a Python script using paho-mqtt that publishes motor commands to a topic, while the ESP32 subscribes and drives the motors accordingly. This method works over Wi-Fi, so no USB tethering is needed.
Hardware Setup
| Component | Quantity | Notes |
|---|---|---|
| ESP32 Dev Board | 1 | Any model with Wi-Fi |
| BTS7960 or L298N | 1 | High-current motor driver |
| DC Motors (12V) | 2 | With wheels or conveyor belt |
| 12V power supply | 1 | For motors (optional separate for ESP32) |
| Jumper wires | As needed |
Wiring (BTS7960 example)
- BTS7960
RPWM→ ESP32 GPIO 12 - BTS7960
LPWM→ ESP32 GPIO 13 - BTS7960
R_EN→ 3.3V (always enabled) - BTS7960
L_EN→ 3.3V - BTS7960
VCC→ 5V (from ESP32 or external) - BTS7960
GND→ GND common - Motors connected to
M+/M-for each channel
For L298N, wire ENA, IN1, IN2, ENB, IN3, IN4 to ESP32 PWM-capable pins.
ESP32 Firmware (Arduino)
The ESP32 will connect to Wi-Fi and subscribe to a MQTT topic. When it receives a command, it adjusts motor speed and direction.
#include <WiFi.h>
#include <PubSubClient.h>
// Wi-Fi & MQTT settings
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";
const char* mqtt_server = "broker.hivemq.com"; // or your broker
WiFiClient espClient;
PubSubClient client(espClient);
// Motor pins (BTS7960)
const int RPWM = 12;
const int LPWM = 13;
void setup() {
Serial.begin(115200);
pinMode(RPWM, OUTPUT);
pinMode(LPWM, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
reconnect();
}
void reconnect() {
while (!client.connected()) {
if (client.connect("ESP32_Motor_Controller")) {
client.subscribe("motor/cmd");
} else delay(2000);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String msg;
for (int i = 0; i < length; i++) msg += (char)payload[i];
if (msg == "F") { // Forward
analogWrite(RPWM, 200);
analogWrite(LPWM, 0);
} else if (msg == "B") { // Backward
analogWrite(RPWM, 0);
analogWrite(LPWM, 200);
} else if (msg == "L") { // Left
analogWrite(RPWM, 100);
analogWrite(LPWM, 0);
} else if (msg == "R") { // Right
analogWrite(RPWM, 0);
analogWrite(LPWM, 100);
} else if (msg == "S") { // Stop
analogWrite(RPWM, 0);
analogWrite(LPWM, 0);
}
}
void loop() {
if (!client.connected()) reconnect();
client.loop();
}
Upload this code to your ESP32 using the Arduino IDE (install ESP32 board support).
Connecting ASI Biont via Chat
Now for the magic: Instead of writing a Python script yourself, you simply open a chat with ASI Biont and describe what you need.
Example conversation:
You: I have an ESP32 with a BTS7960 motor driver connected to MQTT broker
broker.hivemq.com. It subscribes to topicmotor/cmd. Write a Python script that I can run to send commands. Commands: F (forward), B (backward), L (left), R (right), S (stop).
ASI Biont then generates and executes a Python script using execute_python. The sandbox has paho-mqtt pre-installed. The script might look like:
import paho.mqtt.client as mqtt
import time
broker = "broker.hivemq.com"
topic = "motor/cmd"
client = mqtt.Client()
client.connect(broker, 1883, 60)
# Send forward command
client.publish(topic, "F")
print("Forward command sent")
time.sleep(2)
# Send stop
client.publish(topic, "S")
print("Stop command sent")
client.disconnect()
But you don’t have to write that—the AI does it. You can then say “move forward for 3 seconds” and the AI will craft a script that sends F, waits 3s, sends S. The AI can also integrate sensors: if you have an ultrasonic sensor on the ESP32 publishing distance to another topic, the AI script can read that and decide when to stop.
Real-World Scenario: Smart Gate Control
Suppose you have a sliding gate with a DC motor and limit switches. You want to control it via Telegram using ASI Biont. The ESP32 is already subscribed to MQTT. In ASI Biont chat, you tell the AI:
“When I send the telegram message ‘open gate’, publish ‘F’ for 10 seconds, then check if the limit switch topic reports ‘open’. If not, retry.”
The AI writes a Python script that uses both paho-mqtt for commands and reads the sensor topic. The entire integration takes seconds, no firmware changes.
Why This Approach Beats Traditional Coding
- Zero manual code for the AI side: The AI writes, debugs, and runs the integration script instantly.
- No dashboard configuration: Everything happens in natural language.
- Flexibility: Change motor commands, add sensors, or even switch to a different motor driver (L298N) by simply changing the pin mappings in the ESP32 code (which you upload once) and describing new commands to AI.
- Scalable: Use the same method for multiple motors, conveyor belts, or robot arms.
Potential Pitfalls
- Sandbox timeout: The
execute_pythonenvironment has a 30-second timeout. Avoid infinite loops—use time.sleep() for delays and exit cleanly. - MQTT broker reliability: Use a local broker (Mosquitto) for production to avoid latency.
- Power: BTS7960 can handle up to 43A, but use appropriate gauge wires and heat sinks.
Getting Started Today
You don’t need a team of developers to automate your DC motors. With ASI Biont, you can connect any device via MQTT, COM port, Modbus, or a dozen other protocols—all through chat. Upload the ESP32 code above, tell the AI your broker and topic, and watch the AI write the integration on the spot.
Ready to give your motors a brain? Try the integration at asibiont.com.
Comments