Hi first of all i unable to move the motor and I'm not sure if my coding correct or not or there is another problem . Both my esc signal wire connected to esp32 gpio26 and 27. Below is my code.
#include <driver/ledc.h>
// Pin configuration
const int motorPin1 = 26; // Pin connected to motor 1 ESC
const int motorPin2 = 27; // Pin connected to motor 2 ESC
// LEDC settings for motor 1
const int ledcChannel1 = 0; // LEDC channel for motor 1
const int ledcResolution1 = 12; // LEDC resolution (bits) for motor 1
const int ledcFrequency1 = 50; // LEDC frequency (Hz) for motor 1
// LEDC settings for motor 2
const int ledcChannel2 = 1; // LEDC channel for motor 2
const int ledcResolution2 = 12; // LEDC resolution (bits) for motor 2
const int ledcFrequency2 = 50; // LEDC frequency (Hz) for motor 2
const int minPulseWidth = 1000; // Minimum pulse width in microseconds
const int maxPulseWidth = 2000; // Maximum pulse width in microseconds
void setup() {
// Configure motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// Configure LEDC timers for motor 1 and motor 2
ledcSetup(ledcChannel1, ledcFrequency1, ledcResolution1);
ledcSetup(ledcChannel2, ledcFrequency2, ledcResolution2);
// Attach motor pins to the respective LEDC channels
ledcAttachPin(motorPin1, ledcChannel1);
ledcAttachPin(motorPin2, ledcChannel2);
}
void loop() {
// Set motor 1 speed to maximum throttle
setMotorSpeed(motorPin1, maxPulseWidth);
// Set motor 2 speed to maximum throttle
setMotorSpeed(motorPin2, maxPulseWidth);
}
// Function to set the motor speed
void setMotorSpeed(int motorPin, int pulseWidth) {
int ledcChannel;
// Determine the LEDC channel based on the motor pin
if (motorPin == motorPin1) {
ledcChannel = ledcChannel1;
} else if (motorPin == motorPin2) {
ledcChannel = ledcChannel2;
} else {
return; // Invalid motor pin
}
ledcWrite(ledcChannel, pulseWidth);
}