Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 33: Servo Motor (SG90)

In this lesson, you’ll learn how to control a servo motor with an ESP32 Development Board. We’ll cover the process of making the servo motor scan from 0 to 180 degrees and back, giving you hands-on experience in managing servo movements. This project is ideal for those seeking to grasp motor control and the use of pulse width modulation (PWM) in robotics, utilizing the versatile ESP32 board.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

ESP32 & Development Board (ESP32 Board)

BUY

Servo Motor (SG90)

BUY

Breadboard

BUY

Wiring

../_images/Lesson_33_Servo_esp32_bb.png

Code

Code Analysis

  1. Including the Library

    The ESP32Servo library is included to manage servo motor operations.

    #include <ESP32Servo.h>
    
  2. Defining Servo and Pin

    A Servo object is created, and a pin is defined for servo control.


    Servo myServo;
    const int servoPin = 25;
    
  3. Setting Pulse Width Limits

    Minimum and maximum pulse widths are defined for servo motion limits.


    const int minPulseWidth = 500; // 0.5 ms
    const int maxPulseWidth = 2500; // 2.5 ms
    
  4. Setup Function

    • The servo is attached to the defined pin and its pulse width range is set.

    • The PWM frequency is set to 50Hz, standard for servos.


    void setup() {
      myServo.attach(servoPin, minPulseWidth, maxPulseWidth);
      myServo.setPeriodHertz(50);
    }
    
  5. Loop Function

    • Servo rotation is controlled in a loop, moving from 0 to 180 degrees, then back to 0 degrees.

    • writeMicroseconds() is used to set the servo position based on pulse width.


    void loop() {
      // Rotate the servo from 0 to 180 degrees
      for (int angle = 0; angle <= 180; angle++) {
        int pulseWidth = map(angle, 0, 180, minPulseWidth, maxPulseWidth);
        myServo.writeMicroseconds(pulseWidth);
        delay(15);
      }
    
      // Rotate the servo from 180 to 0 degrees
      for (int angle = 180; angle >= 0; angle--) {
        int pulseWidth = map(angle, 0, 180, minPulseWidth, maxPulseWidth);
        myServo.writeMicroseconds(pulseWidth);
        delay(15);
      }
    }