ESP32 MicroPython Servo Motor

This tutorial instructs you how to control a servo motor using a ESP32 and MicroPython. In detail, We will learn:

ESP32 MicroPython Servo Motor

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Servo Motor
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Servo Motor

A servo motor is a part that can rotate its handle, typically from 0 to 180 degrees. It is used to adjust the position of an object.

Pinout

This example shows how to use a servo motor that has three pins:

  • VCC pin: Attach the red wire to VCC (5 volts).
  • GND pin: Connect the black or brown wire to GND (0 volts).
  • Signal pin: Connect the yellow or orange wire to get the PWM control signal from a ESP32 pin.
Servo Motor Pinout

Wiring Diagram

Online diagrams might show a connection between the VCC pin of a servo motor and the VBUS pin of the ESP32 board. Avoid doing this as it can damage the ESP32 board.

The wiring diagram between ESP32 MicroPython Servo

This image is created using Fritzing. Click to enlarge image

To keep your ESP32 board safe, use a separate power supply for the servo motor. The diagram below explains how to connect the servo motor to this power source.

The wiring diagram between ESP32 MicroPython Servo Motor

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and servo motor

Ensure that you connect the GND (ground) from the external power supply to the GND of the ESP32 board. This is crucial for it to function properly.

ESP32 MicroPython Code

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-servo-motor """ from DIYables_MicroPython_Servo import Servo import utime # Create a Servo object servo = Servo(26) # The ESP32 pin GPIO26 connected to the servo motor while True: # Move servo to different angles servo.move_to_angle(0) utime.sleep(2) servo.move_to_angle(90) utime.sleep(2) servo.move_to_angle(180) utime.sleep(2)

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the servo motor according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-Servo”, then find the Servo Motor library created by DIYables.
  • Click on DIYables-MicroPython-Servo, then click Install button to install Servo Motor library.
ESP32 MicroPython Servo Motor library
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Watch the servo motor: The servo motor slowly rotates from 0 to 180 degrees and then returns gradually from 180 back to 0 degrees.

Code Explanation

You can find the explanation in the comments section of the above ESP32 MicroPython code.

How to Control Speed of Servo Motor

The following MicroPython script controls a servo on the ESP32 to transition smoothly between angles without blocking the program. It uses utime for timing, periodically updating the servo's position while keeping the main loop responsive to other tasks.

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-servo-motor """ from DIYables_MicroPython_Servo import Servo import utime # Create a Servo object servo = Servo(26) # The ESP32 pin GPIO26 connected to the servo motor # Define the start and stop angles and the duration for the movement start_angle = 30 stop_angle = 90 moving_time = 3000 # Duration in milliseconds # Function to map the progress to an angle def map_value(x, in_min, in_max, out_min, out_max): return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min # Initial setup with timer move_start_time = utime.ticks_ms() # Main loop while True: progress = utime.ticks_diff(utime.ticks_ms(), move_start_time) # Calculate elapsed time since start if progress <= moving_time: # Calculate intermediate servo angle angle = map_value(progress, 0, moving_time, start_angle, stop_angle) servo.move_to_angle(angle) # Set servo position to calculated angle else: # Optionally, restart the motion or break the loop move_start_time = utime.ticks_ms() # Reset the timer to start the motion again

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!