0

I'm new in using ESP32 boards. For some reasons, the servo motor is not working when RGB LED code is used. I have looked everywhere and I did not see any solution for this. Any help is appreciated. Here is the code I'm using:

#include <ESP32Servo.h>
#include "BluetoothSerial.h"
#define LEDC_CHANNEL_R 0
#define LEDC_CHANNEL_G 1
#define LEDC_CHANNEL_B 2
#define LEDC_TIMER 0
#define LEDC_BASE_FREQ 5000 // Frequency in Hz
#define LEDC_RESOLUTION 8 // 8-bit resolution (0-255)
const int redPin = 18;
const int greenPin = 19;
const int bluePin = 21;
Servo indexServo;
BluetoothSerial SerialBT; 
void setup() {
 pinMode(redPin, OUTPUT);
 pinMode(greenPin, OUTPUT);
 pinMode(bluePin, OUTPUT);
 indexServo.attach(14,1000,2000);
 Serial.begin(115200);
 SerialBT.begin("ESP32_BT_Test");
 ledcAttachChannel(redPin, LEDC_BASE_FREQ, LEDC_RESOLUTION, LEDC_CHANNEL_R);
 ledcAttachChannel(greenPin, LEDC_BASE_FREQ, LEDC_RESOLUTION, LEDC_CHANNEL_G);
 ledcAttachChannel(bluePin, LEDC_BASE_FREQ, LEDC_RESOLUTION, LEDC_CHANNEL_B);
}
void loop() {
 indexServo.writeMicroseconds(1000);
 delay(1200);
 indexServo.writeMicroseconds(2000);
 delay(1200);
 ledcWrite(redPin, 255); 
 ledcWrite(greenPin, 0); 
 ledcWrite(bluePin, 0); 
 delay(50);
}

If I comment out these lines:

 /*ledcAttachChannel(redPin, LEDC_BASE_FREQ, LEDC_RESOLUTION, LEDC_CHANNEL_R);
 ledcAttachChannel(greenPin, LEDC_BASE_FREQ, LEDC_RESOLUTION, LEDC_CHANNEL_G);
 ledcAttachChannel(bluePin, LEDC_BASE_FREQ, LEDC_RESOLUTION, LEDC_CHANNEL_B);*/
 //ledcWrite(redPin, 255); 
 //ledcWrite(greenPin, 0); 
 //ledcWrite(bluePin, 0); 

The servo motor moves back and forth without any problem. I even tried analogWrite() funtion for the led and the same problem occurred.

Servo is connected to pin 14 and powered via an external power supply. RG&B pins of led is connected to pins 18,19, and 21. The wiring is correct because both led amd servo is working fine individually. Problem occurs only if the code for both led and servo is running.

I'm not able to find what is the issue here. Any help will be much helpfull. Thankyou

asked Oct 28, 2024 at 15:59
1
  • please edit your post ... add information about the electrical connections Commented Oct 28, 2024 at 16:53

1 Answer 1

0

The issue stems from how the ESP32 handles its PWM channels. I see that you are trying to use the same timer/channel for the the servo library and LED PWM, which ends up in a conflict. Also I'd recommend a pin definition where you have the pins for the LED defined. The key is proper timer and channel management for the ESP32's PWM system.

Try this code:

#include <ESP32Servo.h>
#include "BluetoothSerial.h"
// PWM Channels for RGB LED
#define LEDC_CHANNEL_R 1 // Changed from 0 to avoid conflict
#define LEDC_CHANNEL_G 2
#define LEDC_CHANNEL_B 3
#define LEDC_TIMER 1 // Changed timer
#define LEDC_BASE_FREQ 5000
#define LEDC_RESOLUTION 8
// Pin Definitions
const int redPin = 18;
const int greenPin = 19;
const int bluePin = 21;
const int servoPin = 14;
Servo indexServo;
BluetoothSerial SerialBT;
void setup() {
 Serial.begin(115200);
 SerialBT.begin("ESP32_BT_Test");
 
 // Setup LED PWM
 ledcAttachChannel(redPin, LEDC_BASE_FREQ, LEDC_RESOLUTION, LEDC_CHANNEL_R);
 ledcAttachChannel(greenPin, LEDC_BASE_FREQ, LEDC_RESOLUTION, LEDC_CHANNEL_G);
 ledcAttachChannel(bluePin, LEDC_BASE_FREQ, LEDC_RESOLUTION, LEDC_CHANNEL_B);
 
 // Setup Servo
 ESP32PWM::allocateTimer(0); // Use timer 0 for servo
 indexServo.setPeriodHertz(50); // Standard 50Hz servo
 indexServo.attach(servoPin, 1000, 2000);
}
void loop() {
 // Servo movement
 indexServo.writeMicroseconds(1000);
 delay(1200);
 indexServo.writeMicroseconds(2000);
 delay(1200);
 
 // LED control
 ledcWrite(redPin, 255); // Red
 ledcWrite(greenPin, 0); // Green
 ledcWrite(bluePin, 0); // Blue
 
 delay(50);
}

I changed the LED channels to start from 1 instead of 0 (servo typically uses channel 0), used different timer for LED PWM and allocated timer for servo using ESP32PWM::allocateTimer(0) (you can read more about it here: https://madhephaestus.github.io/ESP32Servo/classESP32PWM.html)

Furthermore: I added proper servo frequency setup with setPeriodHertz(50) (don't overdo it). If you still have an issue, than it might be the ESP-IDF version.

answered Oct 30, 2024 at 9:57
1
  • 1
    Thank you, this was the solution. Now everything works properly. Commented Oct 31, 2024 at 11:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.