Interfacing SG90 Servo Motor with ESP32

In this tutorial, we will learn how to interface an SG90 servo motor with an ESP32 microcontroller. The SG90 is a popular micro servo motor used in various DIY electronics projects due to its compact size and ease of use. The ESP32 is a powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities, making it suitable for IoT applications.

Components Required:

    • ESP32 Development Board
    • SG90 Servo Motor
    • Breadboard
    • Jumper Wires
    • Micro USB Cable

Step-by-Step Guide

1. Understanding the SG90 Servo Motor

The SG90 servo motor has three wires:

    • Brown: Ground (GND)
    • Red: Power (VCC)
    • Orange: Signal (PWM control)

The servo motor operates on 5V power and uses Pulse Width Modulation (PWM) to control the angle of the motor shaft.

2. Wiring the Components

Connect the servo motor to the ESP32 as follows:

    • GND: Connect the brown wire of the servo to a GND pin on the ESP32.
    • VCC: Connect the red wire of the servo to the 5V (VIN) pin of the ESP32.
    • Signal: Connect the orange wire of the servo to a PWM-capable GPIO pin on the ESP32 (e.g., GPIO 4).

3. Setting Up the Software

We will use the Arduino IDE to program the ESP32. Ensure you have the ESP32 board package installed in your Arduino IDE. If not, you can install it by following these steps:

    1. Open Arduino IDE 2.
    2. Open the Library Manager, search for ServoESP32Fix by alunit3, and install it.
    3. Open File > Examples > ServoESP32Fix > 01-SimpleServo.
preview

 

This code sweeps the servo motor back and forth between 0 and 180 degrees. The Servo library simplifies the control of the servo motor by abstracting the PWM signals.

5. Uploading the Code

    1. Connect the ESP32 to your computer via USB.
    2. Select the correct board and port in the Arduino IDE: Tools > Board > ESP32 Dev Module and Tools > Port > (your ESP32 port).
    3. Click the Upload button to compile and upload the code to the ESP32.

6. Testing

Once the code is uploaded, the servo motor should start sweeping back and forth.

Conclusion

Interfacing an SG90 servo motor with an ESP32 is straightforward using the Servo library and PWM pins. This setup can be extended to more complex projects involving remote control and automation. Happy experimenting!

Leave a Reply

Your email address will not be published. Required fields are marked *