Using servo with ESP32

Hello,
I'm trying to use a servo with an esp32.
I'm using a uPesy ESP32 Wroom Devkit v2
The servo is TS90D digital 180°

I actually made it work. Two days later is wasn't working anymore. I tried with different ESP32, USB cables, servo and libraries... I can't figure out what's wrong, but I'm pretty sure it's dumb.
The potentiometer seems to works, seem I've got reading.

The current library I'm using is ESP32Servo: GitHub - madhephaestus/ESP32Servo: Arduino-compatible servo library for the ESP32
Here is my lastest wiring and my current code.



// Include the ESP32 Arduino Servo Library instead of the original Arduino Servo Library
#include <ESP32Servo.h> 

Servo myservo;  // create servo object to control a servo

int servoPin = 26;      // GPIO pin used to connect the servo control (digital out)

int potPin = 27;

int ADC_Max = 4096;     // This is the default ADC max value on the ESP32 (12 bit ADC width);
                        // this width can be set (in low-level oode) from 9-12 bits, for a
                        // a range of max values of 512-4096
  
int val;    // variable to read the value from the analog pin

void setup()
{
  Serial.begin(9600);
  myservo.attach(servoPin);
}

void loop() {
  val = analogRead(potPin);            // read the value of the potentiometer (value between 0 and 1023)
  Serial.print("Potentiometre :");
  Serial.println(val);
  val = map(val, 0, ADC_Max, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  Serial.println("Angle :");
  Serial.println(val);
  myservo.write(val);                  // set the servo position according to the scaled value
  delay(200);                          // wait for the servo to get there
}

I hope you'll be able to help me!

  • Servo motors need their own power supply.

  • Does your servo work with a 3V3 servo signal ?

According to previous experiments, a colleague, and the documentation: yes it should work from 3V3 to 5V

I tried with 3 AA Batteries (4.5V). They power a LED but it won't work on the servo. I'll try with something a bit more reliable (I don't have an adequate battery holder just I just hold the AA next to each other), but I'm not sure that's the problem. I never had to use an independant power supply before just to power one or two servo of this size :confused: Plus it worked perfectly fine earlier in the week...

I usually do this project with an wemos ESP8266 but wanted to upgrade to an ESP32

I think you have been lucky so far. That servo can draw 550mA when stalled, quite enough to damage any ESP or arduino
If you couldn't make it work with 3 AA batteries then you had something connected wrong.

That's my second year in the job, second time I do it. All I can tell you is it's used several times a year in the company for at least 6 years, and no one really questioned it... But well, we are not all trained, and we do it for educational purposes. It's usually enough to have a battery pluged to the esp to run 2 servo, it's wifi, and a few leds. But it might explain some difficulties I had last year, and a few buried esp8266 ^^'

I made a more sturdy connection for my batteries (I repurposed a 8 AA holder to just hold 3 using crocodile clip). Even had an LED indicating me it's indeed supplied.

I keep checking my connection but I really can't figure it out.
I have my reading from the potentiometer. Servo is plugged to a 4.5V independant supply.
I just tried once again to plug the servo on a different pin on the esp32, changing it in the code too of course.

I have executed your sketch of post #1 in my 30-pin ESP32 Dev Board using SG-90 Servo. The Servo does not respond to 3.3V supply of the ESP32 Board. I connected the Servo to 5V supply of my Aruino NANO and now the Servo responses to the pot rotation.

image

Do you have the servo ground and the battery negative commected to the ESP GND?

Thanks for trying it Golam!

It was not the case jim, so I tried it and it doesn't change anything :confused:

If anyone has a strike of genius I take it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.