180 dgree servo + 2 Buttons

This tutorial instructs you

  • How to use a servo motor with esp32.
  • How to control a servo motor using buttons.

Video Tutorial

you can watch this video tutorial

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Servo Motor
2×Buttons
1×Breadboard
n×Jumper Wires

About this project

Introduction to Servo Motor

The standard servo motor is a motor that can rotate between 0° and 180°.

Servo Motor Pinout

The servo motor has three pins:

  • GND pin:(brown or black) connects this pin to GND (0V)
  • VCC pin: (red) connects this pin to VCC (5V)
  • Signal pin: (yellow or orange) receives the PWM control signal from an ESP32's pin.
ESP32 Button LED Wiring Diagram

How Servo Motor Works

This section is the in-depth knowledge. DON'T worry if you don't understand. Ignore this section if it overloads you, and come back in another day. Keep reading the next sections.

After connecting VCC pin and GND pin to 5V and 0V, respectively, we can control the servo motor by generating proper PWM signal to signal pin.

The angle is determined by the width of PWM signal.

Datasheet of the servo motor provides us the following parameters:

  • Period of PWM (PERIOD)
  • Minimum width of PWM (WIDTH_MAX)
  • Maximum width of PWM (WIDTH_MIN)

These parameters are fixed in Arduino Servo library. We do NOT need to know the value of parameters.

The angle is determined as follows:

  • If PWM's width = WIDTH_MIN, the servo motor rotates to 0°.
  • If PWM's width = WIDTH_MAX, the servo motor rotates to 180°.
  • If PWM's width is between WIDTH_MIN and WIDTH_MAX, the servo motor rotates to angle between 0° and 180° in proportion.
ESP32 Button LED Wiring Diagram

Introduction to Button

please review our tutorial about Button

ESP32 - Servo Motor

We can control the servo motor by connecting the servo motor's signal pin to an ESP32's pin, and programming to generate PWM on the ESP32's pin.

Thanks to Arduino Servo library, controlling servo motor is a piece of cake. We even do NOT need to know how servo motor works. We also do NOT need to know how to generate PWM signal. We JUST need to learn how to use the library.

  • Click here to download the ESP32_Arduino_Servo_Library. You should have a .zip folder in your Downloads folder
  • Unzip the .zip folder and you should get ESP32-Arduino-Servo-Library-Master folder
  • Rename your folder from ESP32-Arduino-Servo-Library-Master to ESP32_Arduino_Servo_Library
  • Move the ESP32_Arduino_Servo_Library folder to your Arduino IDE installation libraries folder
  • Finally, re-open your Arduino IDE

How To Program For Servo Motor

  • Include the library:
  • #include <Servo.h> 
  • Declare a Servo object:
  • Servo myServo;
  • If you control more than one servo motors, you just need to declare more Servo objects:
  • Servo myServo1;
    Servo myServo2;
  • Set the control pin of Arduino, which connects to the signal pin of the servo motor. For example, pin 9:
  • myServo.attach(9);
  • Lastly, rotate the angle of the servo motor to the desired angle. For example, 90°:
  • myServo.write(90);

Wiring Diagram

Schematic Diagram

ESP32 Button LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

Breadboard Connections

ESP32 Button LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

click to see ESP32 pin out

ESP32 Code

Copy

#include <Servo.h>

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

int pos = 0;    // variable to store the servo position


void setup() {
  myservo.attach(21);  // attaches the servo on pin 4 to the servo object
  pinMode(5,INPUT_PULLUP);
  pinMode(18,INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
  if(digitalRead(5)==LOW)
  {
    pos++;
    Serial.println(pos);
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
    if(pos>=180)
      pos=180;
  }

  if(digitalRead(18)==LOW)
  {
    pos--;
    Serial.println(pos);
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
    if(pos<=0)
      pos=0;    
  }

}          

Quick Steps

  • power up your board
  • Open Arduino IDE
  • Select the right board
  • Select the right port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Arduino IDE Upload Code
  • Press and keep pressing the button several seconds
  • See the changes you made

Book Tutorial

We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book

References

※ NOTE THAT:

Some components works on 3.3v and others works on 5v!