How to control a servo motor with Arduino, ESP8266, ESP32, Raspberry Pi and MicroBit
Hello and welcome back. In this tutorial, we will learn how to control a servo motor with the most famous development boards. These are Arduino, ESP8266, ESP32, raspberry Pi Pico, and MicroBit boards. Whether you’re a beginner or a seasoned hobbyist, understanding servo control is essential for various projects. If you want to know the basics of these boards and servo motors, please read our previous articles. We’ll cover everything you need to start and explore practical applications for each board. Ok, let’s go ahead.
OK let’s control a servo motor using one by one development board. For that, I have used the SG90 servo motor. But you can use any servo motor. The required components are given below.
- SG90 servo motor x 1 — Our store / Amazon
- Arduino Nano board x — Our store / Amazon
- ESP8266 board x 1 — Our store / Amazon
- ESP32 board x 1 — Our store / Amazon
- Raspberry Pi Pico board x 1 — Our store / Amazon
- MicroBit board x 1 — Our store / Amazon
- Breadboard x 1 — Our store / Amazon
- Jumper wires — Our store / Amazon
- Microbit shield x1 — Our store / Amazon
Firstly, identify these components.
Servo Motor Control Using Arduino Nano Board
Step 1
Firstly, place the Arduino board on the breadboard. Then, connect the servo motor to it. For that, use the circuit diagram below.
Step 2
Secondly, connect the Arduino board to the computer. Then, copy and paste the following program to the Arduino IDE.
- Code and circuit diagram — Download
#include <Servo.h> //Library path -- Sketch -> Include library -> Servo
Servo srituhobby; //Make an object for the library
void setup() {
srituhobby.attach(3); //set arduino PWM pin D3
}
void loop() {
for (int a = 0 ; a <= 180 ; a++) {
srituhobby.write(a);
delay(10);
}
for (int k = 179; k > 0 ; k--) {
srituhobby.write(k);
delay(10);
}
}
Step 3
Thirdly, select the board and port. After, click the upload button.
Now you can change the values and test this project as you like.
Servo Motor Control Using ESP8266 Board
Step 1
Firstly, place the ESP8266 Nodemcu board on the breadboard. Then, connect the servo motor to it. For that, use the circuit diagram below.
Step 2
Secondly, connect the ESP8266 Nodemcu board to the computer. Then, copy and paste the following program to the Arduino IDE.
- Code and circuit diagram — Download
#include <Servo.h> //Library path -- Sketch -> Include library -> Servo
Servo srituhobby; //Make an object for the library
void setup() {
srituhobby.attach(D2); //set the ESP8266 pin D2
}
void loop() {
for (int a = 0 ; a <= 180 ; a++) {
srituhobby.write(a);
delay(5);
}
for (int k = 179; k > 0 ; k--) {
srituhobby.write(k);
delay(5);
}
}
Step 3
Thirdly, select the board and port. After, click the upload button.
Now you can check this project. Also, change these values and experience this project as you like.
Servo Motor Control Using Raspberry Pi Pico Board
Step 1
Firstly, place the Raspberry Pi board on the breadboard. Then, connect the servo motor to it. For that, use the circuit diagram below.
Step 2
Secondly, connect the Raspberry Pi Pico board to the computer. Then, copy and paste the following script to the Thonny IDE.
- Code and circuit diagram — Download
from machine import Pin,PWM
from time import sleep
servo = PWM(Pin(0))#Include the servo motor pin
servo.freq(50)#Set the frequency
while True:
for a in range(1000,8000):
servo.duty_u16(a)
sleep(0.001)
for a in range(7999,1000,-1):
servo.duty_u16(a)
sleep(0.001)
Step 3
Thirdly save this script as “main.py” on the Raspberry Pi Pico board. Then run this script.
Now you can check this project. Also, change these values and experience this project as you like.
Servo Motor Control Using ESP32 DEVKIT V1 Board
Step 1
Firstly, place the ESP32 board on the breadboard. Then, connect the servo motor to it. For that, use the circuit diagram below.
Step 2
Secondly, connect the ESP32 board to the computer. Then, copy and paste the following script to the Thonny IDE.
- Code and circuit diagram — Download
#include <ESP32Servo.h>
Servo srituhobby; // create servo object to control a servo
void setup() {
srituhobby.setPeriodHertz(50);// Standard 50hz servo
srituhobby.attach(12,500, 2400);
}
void loop() {
for (int a = 0; a <= 180; a++) {
srituhobby.write(a);
delay(20);
}
for (int a = 179; a > 0; a--) {
srituhobby.write(a);
delay(20);
}
}
Step 3
Thirdly, select the board and port. After, click the upload button.
Now you can check this project. Also, change these values and experience this project as you like.
Servo Motor Control Using MicroBit V2 Board
Step 1
Firstly, install the MicroBit V2 or V1 board on the sensor shield. Then, connect the servo motor to it. For that, use the circuit diagram below.
Step 2
Secondly, connect the MicroBit board to the computer. Then, go to the MakeCode website and create a new project. I have named it the servo motor control.
Step 3
Thirdly, install the servo extension. Then, copy and paste the following script on the Python tab.
- Code and circuit diagram — Download
def on_forever():
for c in range(180):
servos.P0.set_angle(c)
basic.pause(20)
a = 179
while a > 0:
servos.P0.set_angle(a)
basic.pause(20)
a += -1
basic.forever(on_forever)
Step 4
Now, you can see the auto-generated code blocks. Then, click the download button.
OK, enjoy these projects. Also, please change the codes and experience these codes. Then you can get good knowledge about servo motor control. The full video guide is below. So we hope to see you in the next project. Have a good day.
How to control a servo motor with Arduino, ESP8266, ESP32, Raspberry Pi and MicroBit