Controlling a servo with ESP32-C3

kmaker
Posts: 2
Joined: Tue Feb 22, 2022 3:36 am

Controlling a servo with ESP32-C3

Postby kmaker » Tue Feb 22, 2022 3:47 am

Hi, I'm working on a project that uses ESP32-C3 (ESP32-C3-01M from Ai-thinker) to control a 9g servo. So far, I'm having no luck getting it to work. I've tried several servo libraries for ESP32, like esp32servo, and ledc based servoesp32, and none could make it work. I was able to see ledc control an LED on GPIO4 using ledc. While I see several mention of servo projects using ESP32-C3 on the internet, chip selector on Espressif shows ESP32-C3 to be not recommended for servo. I have been powering the servo with 5v power, and try to feed the PWM output directly into control pin. I know the servo is alive as it works with ESP8266 based setup i have.

I just wanted to find out if I'm on a path that's not going to succeed, or if there're other's who's been able to make it work... thanks!

kmaker
Posts: 2
Joined: Tue Feb 22, 2022 3:36 am

Re: Controlling a servo with ESP32-C3

Postby kmaker » Wed Feb 23, 2022 4:02 am

shortly after I posted, I had a small series of breakthrough. what i learned:
1. servo can be controlled by plugging in the control pin directly (I tried GIPO4 and GPIO2)
2. ledc can be initialized with different bid width and frequency, but it stops working if I set anything less than 200 for the frequency. ServoESP32 had 16 as bitwidth, but that didn't work and changing it to 8 worked.
3. since servo is controlled by pulses of width 1-2ms, i had to compute appropriate duty ratio value in 200hz

and after that it worked!

flipflop
Posts: 1
Joined: Sun Feb 23, 2025 9:57 pm

Re: Controlling a servo with ESP32-C3

Postby flipflop » Sun Feb 23, 2025 10:00 pm

This worked for me using GPIO2 but the example from the ESP32Servo repo works using GPIO7 too.
This is using an SG90 servo taking the 5v straight from the board's 5v pin

Code: Select all

#include <Arduino.h>

#include <ESP32Servo.h>

Servo myservo;  // create servo object to control a servo
// 16 servo objects can be created on the ESP32

int pos = 0;    // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33 
// Possible PWM GPIO pins on the ESP32-S2: 0(used by on-board button),1-17,18(used by on-board LED),19-21,26,33-42
// Possible PWM GPIO pins on the ESP32-S3: 0(used by on-board button),1-21,35-45,47,48(used by on-board LED)
// Possible PWM GPIO pins on the ESP32-C3: 0(used by on-board button),1-7,8(used by on-board LED),9-10,18-21
#if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
int servoPin = 17;
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
int servoPin = GPIO_NUM_2;
#else
int servoPin = 18;
#endif

void setup() {
	// Allow allocation of all timers
	ESP32PWM::allocateTimer(0);
	ESP32PWM::allocateTimer(1);
	ESP32PWM::allocateTimer(2);
	ESP32PWM::allocateTimer(3);
	myservo.setPeriodHertz(50);    // standard 50 hz servo
	myservo.attach(servoPin, 500, 2000); // attaches the servo on pin 18 to the servo object
	// using default min/max of 1000us and 2000us
	// different servos may require different min/max settings
	// for an accurate 0 to 180 sweep
}

void loop() {

	for (pos = 0; pos <= 1800; pos += 1) { // goes from 0 degrees to 180 degrees
		// in steps of 1 degree
		myservo.write(pos/10);    // tell servo to go to position in variable 'pos'
		delay(10);             // waits 15ms for the servo to reach the position
	}
	for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
		myservo.write(pos);    // tell servo to go to position in variable 'pos'
		delay(5);             // waits 15ms for the servo to reach the position
	}
}

Who is online

Users browsing this forum: No registered users and 3 guests