Interfacing Servo Motor With ESP32

by TechMartian in Circuits > Arduino

56626 Views, 52 Favorites, 0 Comments

Interfacing Servo Motor With ESP32

t.jpg

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!!!

Tools and Materials

2016-08-01 18.32.24.jpg
2016-08-01 18.33.18.jpg
2016-08-01 18.33.58.jpg
2016-08-01 18.33.47.jpg
2016-08-01 18.32.35.jpg
  • ESP32 Development Board
  • Servo Motor
  • 3 pieces of male-to-male jumper wires
  • Breadboard

Circuitry

Screen Shot 2017-08-02 at 9.30.27 PM.png
Screen Shot 2017-08-02 at 9.32.46 PM.png
2016-08-01 18.42.21.jpg
2016-08-01 18.41.36.jpg
  • 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 black wire, to the GND pin on the ESP32 board.

Coding

Screen Shot 2017-08-07 at 12.40.09 PM.png
#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);
   }
}

Downloads

Demonstration Video

Interfacing Servo with ESP32

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