An Arduino library for controlling Basicmicro motor controllers with comprehensive support for all controller features including duty cycle control, speed control, position control, encoder feedback, PID parameter configuration, and more.
This library provides a complete interface for communicating with Basicmicro motor controllers using serial communication. It supports the full command set for controlling motors, reading sensors, and configuring controller parameters.
This library is compatible with:
- All Basicmicro motor controllers with serial interface
- Arduino boards with hardware or software serial capabilities
- ESP8266, ESP32, and other Arduino-compatible boards
- In the Arduino IDE, go to Sketch > Include Library > Manage Libraries
- Search for "Basicmicro"
- Click "Install"
- Download the latest release from the GitHub repository
- In the Arduino IDE, go to Sketch > Include Library > Add .ZIP Library
- Select the downloaded zip file
Connect your Arduino to the Basicmicro controller using the following connections:
- Arduino TX pin → Controller RX pin
- Arduino RX pin → Controller TX pin
- Common ground between Arduino and controller
For hardware serial:
#include <Basicmicro.h>
Basicmicro roboclaw(&Serial1, 10000); // Using Serial1, 10ms timeout
For software serial (AVR-based boards only):
#include <Basicmicro.h>
#include <SoftwareSerial.h>
SoftwareSerial serial(10, 11); // RX, TX
Basicmicro roboclaw(&serial, 10000); // Using SoftwareSerial, 10ms timeout
#include <Basicmicro.h>
#define ADDRESS 0x80 // Default address of the controller
Basicmicro roboclaw(&Serial1, 10000);
void setup() {
Serial.begin(115200);
roboclaw.begin(38400); // Set the baud rate for communication with the controller
}
// Basic duty cycle control (-32768 to +32767)
roboclaw.DutyM1(ADDRESS, 16384); // 50% duty cycle forward
roboclaw.DutyM2(ADDRESS, -16384); // 50% duty cycle backward
roboclaw.DutyM1M2(ADDRESS, 8192, 8192); // Both motors at 25% forward
// Speed control using encoders
roboclaw.SpeedM1(ADDRESS, 1000); // Run at 1000 encoder counts per second
roboclaw.SpeedAccelM1(ADDRESS, 500, 1000); // Accelerate to 1000 counts/sec at 500 counts/sec²
// Position control
roboclaw.SpeedAccelDeccelPositionM1(ADDRESS, 500, 1000, 500, 10000, 0); // Move to position 10000
// Stop motors
roboclaw.DutyM1(ADDRESS, 0);
roboclaw.DutyM2(ADDRESS, 0);
// Read encoder values
uint32_t enc1 = roboclaw.ReadEncM1(ADDRESS);
uint32_t enc2 = roboclaw.ReadEncM2(ADDRESS);
// Read speed values
uint8_t status;
bool valid;
uint32_t speed1 = roboclaw.ReadSpeedM1(ADDRESS, &status, &valid);
if (valid) {
Serial.print("Motor 1 Speed: ");
Serial.println(speed1);
}
// Read current values
int16_t current1, current2;
if (roboclaw.ReadCurrents(ADDRESS, current1, current2)) {
Serial.print("Motor 1 Current: ");
Serial.print(current1);
Serial.print("mA, Motor 2 Current: ");
Serial.print(current2);
Serial.println("mA");
}
// Read battery voltage
uint16_t voltage = roboclaw.ReadMainBatteryVoltage(ADDRESS, &valid);
if (valid) {
float volts = voltage / 10.0; // Convert to volts
Serial.print("Battery Voltage: ");
Serial.print(volts);
Serial.println("V");
}
// Set PID parameters for velocity control
roboclaw.SetM1VelocityPID(ADDRESS, 1.0, 0.5, 0.25, 12000);
// Set current limits
roboclaw.SetM1MaxCurrent(ADDRESS, 5000, 2500); // 5A max, 2.5A min
// Save settings to non-volatile memory
roboclaw.WriteNVM(ADDRESS);
Here's a more complete example showing movement with encoder feedback:
#include <Basicmicro.h>
#define ADDRESS 0x80
Basicmicro roboclaw(&Serial1, 10000);
void setup() {
Serial.begin(115200);
Serial1.begin(38400);
// Set PID parameters for velocity control
roboclaw.SetM1VelocityPID(ADDRESS, 1.0, 0.5, 0.25, 12000);
// Reset encoder counters
roboclaw.ResetEncoders(ADDRESS);
}
void loop() {
// Move forward for 5 seconds
roboclaw.SpeedAccelM1(ADDRESS, 500, 2000);
delay(5000);
// Read encoder position
uint8_t status;
bool valid;
uint32_t position = roboclaw.ReadEncM1(ADDRESS, &status, &valid);
if (valid) {
Serial.print("Encoder position: ");
Serial.println(position);
}
// Stop motor
roboclaw.SpeedAccelM1(ADDRESS, 500, 0);
delay(1000);
// Return to start position
roboclaw.SpeedAccelDeccelPositionM1(ADDRESS, 500, 2000, 500, 0, 0);
delay(5000);
}
The library provides many more functions for advanced control and configuration. See the header file or refer to the examples folder for more details on specific functions.
For complete documentation of all commands and protocols, please refer to the official Basicmicro documentation.
- No communication with controller: Verify wiring, check baudrate settings, and ensure power is connected
- Motors don't move: Check error codes with
ReadError()
, verify voltage withReadMainBatteryVoltage()
- Erratic movement: Adjust PID parameters for smoother control
Contributions to improve the library are welcome. Please submit issues and pull requests on the GitHub repository.
This library is released under the MIT License. See the LICENSE file for details.
For technical support or questions:
- Submit issues on the GitHub repository
- Contact Basicmicro technical support at support@basicmicro.com