Hi all, I am Emmanuel Katto. I’m new to the world of microcontrollers and I’m trying to use a SparkFun ESP32 Thing to control a servo motor. I’ve managed to get the basics of the ESP32 working, such as connecting to Wi-Fi and sending/receiving data, but I’m having trouble getting the servo motor to work.
I’ve connected the servo motor to the ESP32’s PWM pin (GPIO 0) and set up the code to send a pulse width modulation signal to the motor. However, the motor isn’t moving at all. I’ve checked the voltage and current going to the motor and they seem to be correct, so I’m not sure what’s going wrong.
Here’s my code:
#include <WiFi.h>
#include <Servo.h>
WiFiServer server(80);
Servo myServo; // create a servo object
void setup() {
Serial.begin(115200);
WiFi.begin(“my_wifi_ssid”, “my_wifi_password”);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}
Serial.println(“Connected to WiFi”);
server.begin();
myServo.attach(0); // attach servo to PWM pin 0
}
void loop() {
// create a client object
WiFiClient client = server.available();
if (client) {
// read the request from the client
String request = client.readStringUntil(‘\r’);
// parse the request
if (request.startsWith(“SET_Servo”)) {
int angle = request.substring(9).toInt();
myServo.write(angle);
}
client.flush();
client.stop();
}
}
- Is there something obvious that I’m missing in my code?
- Is there a specific way I should be setting up the servo motor or its connections?
Please let me know.
Thanks!
Emmanuel Katto