Arduino Nano ESP32 - Servo Motor

This tutorial provides instructions on how to use Arduino Nano ESP32 to control a servo motor.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Servo Motor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of 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.
Servo Motor Pinout

How Servo Motor Works

See How servo motor works

Wiring Diagram between Servo Motor and Arduino Nano ESP32

  • When powering the Arduino Nano ESP32 board via USB port.
The wiring diagram between Arduino Nano ESP32 and Servo Motor

This image is created using Fritzing. Click to enlarge image

  • When powering the Arduino Nano ESP32 board via Vin pin.
The wiring diagram between Arduino Nano ESP32 and servo motor external power supply

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

When powering the Arduino Nano ESP32 board via USB port, it should NOT power the servo motor via Vin pin VBUS pin. If you power the servo motor via this pin, your board may be get burned.

Arduino Nano ESP32 Code

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-servo-motor */ #include <Servo.h> #define SERVO_PIN D2 // The Arduino Nano ESP32 pin connected to servo motor Servo servoMotor; void setup() { servoMotor.attach(SERVO_PIN); // attaches the servo on ESP32 pin } void loop() { // rotates from 0 degrees to 180 degrees for (int pos = 0; pos <= 180; pos += 1) { // in steps of 1 degree servoMotor.write(pos); delay(15); // waits 15ms to reach the position } // rotates from 180 degrees to 0 degrees for (int pos = 180; pos >= 0; pos -= 1) { servoMotor.write(pos); delay(15); // waits 15ms to reach the position } }

Detailed Instructions

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32) board and its corresponding COM port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Type ServoESP32 on the search box, then look for the servo library by Jaroslav Paral. Please be aware that both version 1.1.1 and 1.1.0 are affected by bugs. Kindly choose a different version.
  • Click Install button to install servo motor library for Arduino Nano ESP32.
Arduino Nano ESP32 servo motor library
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
How to upload Arduino Nano ESP32 code on Arduino IDE
  • See the result: Servo motor rotates slowly about 180° in clockwise and counter-clockwise direction

Line-by-line Code Explanation

The above Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!

How to Control Speed of Servo Motor

See How to Control Speed of Servo Motor

Video Tutorial

The instruction and source code for the above video available at how to control servo motor via web tutotiral