Hello, I am trying to drive a Robomaster C620 ESC with ESP32, by controlling it with a DS4 controller. The controller and the board initializes just fine by connecting, but the C620 keeps giving a error code after start. On the manual, it says PWM output is not at the minimum, and I don't have the slightest idea how to make it at the minimum.
I have basic knowledge but not too deep so mostly it's chatgpt generated.
Also, when I jiggle the joystick randomly, the motor just initializes and works as intended. I need it to start ASAP so can anyone help me with this? Also I am trying to use 3 different motors so I need it to start working synchronized and fast as possible.
Documentation here
Any help would be greatly appreciated.
#include <Bluepad32.h>
#include <ESP32Servo.h>
// Global pointer for the connected gamepad
GamepadPtr myGamepad = nullptr;
// Set up the motor control pin and the Servo object
const int motorPin = 13;
Servo motorController;
// Deadzone to avoid drift when the joystick is nearly centered.
const int JOYSTICK_DEADZONE = 10;
// Updated PWM constants for two-way ESC calibration:
// 1000 µs = full reverse; 1500 µs = neutral; 2000 µs = full forward.
const int PWM_MIN = 1000; // Full reverse pulse width
const int PWM_STOP = 1500; // Neutral (stop)
const int PWM_MAX = 2000; // Full forward pulse width
// Called when a gamepad connects.
void onConnectedGamepad(GamepadPtr gp) {
Serial.println("Gamepad connected");
myGamepad = gp;
Serial.print("Model: ");
Serial.println(gp->getModelName());
}
// Called when a gamepad disconnects.
void onDisconnectedGamepad(GamepadPtr gp) {
Serial.println("Gamepad disconnected");
if (gp == myGamepad) {
myGamepad = nullptr;
// Set motor to stop when no controller is connected.
motorController.writeMicroseconds(PWM_MIN);
}
}
void setup() {
Serial.begin(115200);
Serial.println("Initializing Bluepad32 and setting up motor...");
// Initialize Bluepad32 (for gamepad handling)
BP32.setup(&onConnectedGamepad, &onDisconnectedGamepad);
// Allocate one timer for the ESP32 PWM control and set the PWM frequency to 50 Hz.
ESP32PWM::allocateTimer(0);
motorController.setPeriodHertz(50); // Standard frequency for ESC/servo control
// Attach the motor controller to the specified pin with the given min and max pulse widths.
motorController.attach(motorPin, PWM_MIN, PWM_MAX);
// Initialize the motor to neutral (stop)
motorController.writeMicroseconds(PWM_STOP);
Serial.println("Motor initialized to stop (1500 µs). Waiting for controller input...");
}
void loop() {
// Update Bluepad32 events
BP32.update();
// If a gamepad is connected, use its left joystick Y-axis value
if (myGamepad && myGamepad->isConnected()) {
int joystickY = myGamepad->axisY(); // Expected range: -127 (full forward) to +127 (full reverse)
controlMotor(joystickY);
printDebugInfo(joystickY);
} else {
// If no gamepad is connected, ensure the ESC/motor is stopped.
motorController.writeMicroseconds(PWM_STOP);
}
delay(50); // Small delay to prevent flooding the serial output.
}
// Map the joystick input to the PWM output required by the ESC.
void controlMotor(int joystickY) {
// Apply deadzone: if the absolute value is below deadzone, set motor to neutral.
if (abs(joystickY) < JOYSTICK_DEADZONE) {
motorController.writeMicroseconds(PWM_STOP);
return;
}
int pwmValue = PWM_STOP;
// For negative values (joystick pushed forward) — map from neutral (1500 µs) to full forward (2000 µs)
if (joystickY < 0) {
pwmValue = map(joystickY, -127, 0, PWM_MAX, PWM_STOP);
}
// For positive values (joystick pulled backward) — map from neutral (1500 µs) to full reverse (1000 µs)
else {
pwmValue = map(joystickY, 0, 127, PWM_STOP, PWM_MIN);
}
// Send the PWM signal to the ESC.
motorController.writeMicroseconds(pwmValue);
}
// Output debug information on the serial monitor.
void printDebugInfo(int joystickY) {
Serial.print("Joystick Y: ");
Serial.print(joystickY);
Serial.print(" | PWM output: ");
Serial.println(motorController.readMicroseconds());
}