Control a Servo With ESP32-S3 Sense
by Markus Opitz in Circuits > Microcontrollers
2182 Views, 3 Favorites, 0 Comments
Control a Servo With ESP32-S3 Sense
Control a servo with a tiny ESP32-S3 sense XIAO.
Because I wanted to operate several servos on an ESP32-S3 sense (with camera) for a project, I had to find a suitable Arduino IDE library. It cost me a lot of time and error messages because I was missing an important detail. I would like to share this tip with you to save your time.
Supplies
ESP32-S3 sense XIAO
servo SG90
USB-C cable
Arduino IDE
LiPo battery 3.7V (optional)
Hardware: Connect the Servo
Connect the servo as seen in the wiring diagram: 5V (red), GND (brown) and Data to GPIO 44 (D7). You can also connect servo's 5V and GND to another 5V power supply, but don‘t forget to connect the GND to the ESP32.
Software: Setup on Arduino IDE
Actually, there's a great description here at github.com/Seeed-Studio:
Everything works well so far, but two things are not mentioned:
1. Do not add only this to Arduino's board manager (File --> Preferences --> Additional board manager URL's)
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
but also this line:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
2. Board manager: ESP32, Version 2.x. Version 3 is producing error messages with the servo libraries.
Resist when your Arduino IDE prompts you to update the board information!
Software: Install the Library
Download the servo library from github.
Open the Arduino IDE, Sketch --> Include Library --> Add .ZIP Library --> choose ZIP file
Software: the Sweep Sketch
It is important that you specify the GPIO number for input/output in the ESP32-S3 program.
The for-loop is for sweeping the servo from 30° to 150°. Increasing the delay would slow down the movement.
servo1.write(position i) tells the servo which angle to set.
Happy making!
#include <s3servo.h>
s3servo servo1;
int servoPin = 44; //use the GPIO numbers!
void setup() {
servo1.attach(servoPin);
}
void loop() {
for(int i = 30; i <= 150; i++) {
servo1.write(i);
delay(22);
}
for(int i = 150; i >= 30; i--) {
servo1.write(i);
delay(22);
}
}