Interfacing Servo Motor With ESP32


18,142

39

4

Servo Motors are one of the most important actuators in the realm of robotics being applied in use cases from RC planes to automated door locks. Therefore, this second module of the ESP32 lesson, we will cover how to control servo motors.

Interfacing a servo motor to the ESP32 is very difficult compared to an Arduino, since it does not support analogWrite(). However, it uses various frequencies and timers, allowing the all digital pins to be used as PWM pins as well send signals significantly faster than any Arduino!!!

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Tools and Materials

  • ESP32 Development Board
  • Servo Motor
  • 3 pieces of male-to-male jumper wires
  • Breadboard

Step 2: Circuitry

  • Connect the signal pin of the servo, shown as the white wire, to pin D2 on the ESP32 board.
  • Connect the VCC pin of the servo, shown as the red wire, to the 3.3V pin on the ESP32 board.
  • Finally, connect the ground pin of the servo, shown as the blackwire, to the GND pin on the ESP32 board.

Step 3: Coding

#define COUNT_LOW 0
 #define COUNT_HIGH 8888
 #define TIMER_WIDTH 16
#include "esp32-hal-ledc.h"
void setup() {
   ledcSetup(1, 50, TIMER_WIDTH); // channel 1, 50 Hz, 16-bit width
   ledcAttachPin(2, 1);   // GPIO 22 assigned to channel 1
}
void loop() {
   for (int i=COUNT_LOW ; i < COUNT_HIGH ; i=i+100)
   {
      ledcWrite(1, i);       // sweep servo 1
      delay(50);
   }
}

Step 4: Demonstration Video

The servo motions follows as programmed going from the minimum to maximum at much faster speeds relative to an Arduino or ESP8266.

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017

Be the First to Share

    Recommendations

    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    4 Discussions

    0
    None
    lantanka

    6 weeks ago

    I use the esp32 to control a servo motor using the uPyCraft IDE, I find it easier to use and for beginners it would be ideal. Here is my code using uPyCraft IDE:
    from machine import Pin, PWM
    import time
    servo = PWM(Pin(4), freq=50, duty=512)
    while True:
    for d in range(51,115):
    servo.duty(51)
    time.sleep(2)
    servo.duty(112)
    time.sleep(2)
    servo.duty(51)
    time.sleep(2)
    servo.duty(21)
    time.sleep(2)
    def pwm():
    pwm.duty(d)
    time.sleep(0.01)
    continue
    Enjoy!!

    0
    None
    felixhc

    5 months ago

    Very interesting.
    As a newbie it took me some time to find out that the right pin to use is IO2 (not the IO22 in the comment of the sketch nor the IO0 in one of the diagrams). Now it works really well.

    Thanks!.

    0
    None
    Maker Saga

    2 years ago

    Nice work. This will be handy for my future projects.

    Couldn't you just use the servo library to call a specific degree of rotation? I've used one of the servo libraries with a NodeMCU Lolin V3 board based on the ESP8266 chip with pin D2 for signal and 3.3v for servo power without any issues. Does the Arduino IDE have Bluetooth capability with the ESP32? I thought the ESP32 has a bluetooth transceiver built in. How much current can does the ESP32 board output for VCC and GPIO?

    Sorry to ask so many questions!

    1 reply
    0
    None
    TechMartianMaker Saga

    Reply 2 years ago

    That's one of the most important reasons why I posted this on Instructables. The servo library cannot be used and is not supported in the ESP32. But since we're using timers, it makes it super fast! I was hoping it'd be featured to more people would know about this board it is so awesome but also have some big differences from the ESP8266 as shown.

    It does have bluetooth. I'm going to write a bluetooth module soon actually, so stay tuned!

    I haven't had problems with the current and voltage even with a 3.3V power which was very surprising to me. The max current output it 12mA.

    I hope I've answered your questions.