Closed urish closed 2 years ago
I have more than 8 hours trying to run a Servomotor in esp32 and I could not. Configure everything but nothing happens
You mean in the Simulator? Or on a physical ESP32 chip?
You mean in the Simulator? Or on a physical ESP32 chip?
I mean the simulator
I am setting up the example library and it doesn't work
/*
* ESP32 Servo Example Using Arduino ESP32 Servo Library
* John K. Bennett
* March, 2017
*
* This sketch uses the Arduino ESP32 Servo Library to sweep 4 servos in sequence.
*
* Different servos require different pulse widths to vary servo angle, but the range is
* an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
* sweep 180 degrees, so the lowest number in the published range for a particular servo
* represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
* of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
* 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
* degrees.
*
* Circuit:
* Servo motors have three wires: power, ground, and signal. The power wire is typically red,
* the ground wire is typically black or brown, and the signal wire is typically yellow,
* orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
* considerable power, we will connect servo power to the VBat pin of the ESP32 (located
* near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
*
* We could also connect servo power to a separate external
* power source (as long as we connect all of the grounds (ESP32, servo, and external power).
* In this example, we just connect ESP32 ground to servo ground. The servo signal pins
* connect to any available GPIO pins on the ESP32 (in this example, we use pins
* 22, 19, 23, & 18).
*
* In this example, we assume four Tower Pro SG90 small servos.
* The published min and max for this servo are 500 and 2400, respectively.
* These values actually drive the servos a little past 0 and 180, so
* if you are particular, adjust the min and max values to match your needs.
* Experimentally, 550 and 2350 are pretty close to 0 and 180.
*/
#include <ESP32Servo.h>
// create four servo objects
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
// Published values for SG90 servos; adjust if needed
int minUs = 1000;
int maxUs = 2000;
// These are all GPIO pins on the ESP32
// Recommended pins include 2,4,12-19,21-23,25-27,32-33
int servo1Pin = 15;
int servo2Pin = 16;
int servo3Pin = 14;
int servo4Pin = 32;
int servo5Pin = 4;
int pos = 0; // position in degrees
ESP32PWM pwm;
void setup() {
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
Serial.begin(115200);
servo1.setPeriodHertz(50); // Standard 50hz servo
servo2.setPeriodHertz(50); // Standard 50hz servo
servo3.setPeriodHertz(330); // Standard 50hz servo
servo4.setPeriodHertz(200); // Standard 50hz servo
//servo5.setPeriodHertz(50); // Standard 50hz servo
}
void loop() {
servo1.attach(servo1Pin, minUs, maxUs);
servo2.attach(servo2Pin, minUs, maxUs);
pwm.attachPin(27, 10000);//10khz
servo3.attach(servo3Pin, minUs, maxUs);
servo4.attach(servo4Pin, minUs, maxUs);
//servo5.attach(servo5Pin, minUs, maxUs);
for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
// in steps of 1 degree
servo1.write(pos);
delay(1); // waits 20ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
servo1.write(pos);
delay(1);
}
for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
// in steps of 1 degree
servo2.write(pos);
delay(1); // waits 20ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
servo2.write(pos);
delay(1);
}
for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
// in steps of 1 degree
servo3.write(pos);
delay(1); // waits 20ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
servo3.write(pos);
delay(1);
}
for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
// in steps of 1 degree
servo4.write(pos);
delay(1); // waits 20ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
servo4.write(pos);
delay(1);
}
for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
// in steps of 1 degree
servo5.write(pos);
delay(1); // waits 20ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
servo5.write(pos);
delay(1);
}
servo1.detach();
servo2.detach();;
servo3.detach();
servo4.detach();
pwm.detachPin(27);
delay(5000);
}
I have placed the reference as advertised by the guide in a txt called libreries.txt
# Sample libraries.txt file:
ESP32Servo
# Install a specific version of a library:
MySensors@2.3.0
the diagram.json
{
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-esp32-devkit-v1", "id": "esp", "top": 230.66, "left": -127.11, "attrs": {} },
{ "type": "wokwi-servo", "id": "servo1", "top": 108.09, "left": 99.09, "attrs": {} },
{ "type": "wokwi-servo", "id": "servo2", "top": 3.55, "left": 75.59, "attrs": {} },
{ "type": "wokwi-servo", "id": "servo3", "top": 228.02, "left": 99.78, "attrs": {} },
{ "type": "wokwi-servo", "id": "servo4", "top": -120.69, "left": 83.96, "attrs": {} },
{ "type": "wokwi-servo", "id": "servo5", "top": 345.51, "left": 115.15, "attrs": {} }
],
"connections": [
[ "esp:TX0", "$serialMonitor:RX", "", [] ],
[ "esp:RX0", "$serialMonitor:TX", "", [] ],
[ "servo1:GND", "esp:GND.1", "black", [ "h-41.18", "v229.19", "h-31.86", "v-4.25" ] ],
[ "servo2:PWM", "esp:D13", "green", [ "h-215.21", "v13.61" ] ],
[ "esp:D15", "servo1:PWM", "green", [ "h24.67", "v-193.77", "h52.48" ] ],
[ "esp:D14", "servo3:PWM", "green", [ "h-96.32", "v178.01", "h289.69", "v-164.37" ] ],
[
"esp:D32",
"servo4:PWM",
"green",
[ "h-75.73", "v-311.11", "h216.09", "v-43.43", "h23.38" ]
],
[ "esp:D4", "servo5:PWM", "green", [ "h18.89", "v-10.28", "h96.57", "v73.63" ] ],
[ "servo3:GND", "esp:GND.1", "black", [ "h-79.87", "v100.9", "h-6.34" ] ],
[ "servo5:GND", "esp:GND.1", "black", [ "h1.04", "v-19.67" ] ],
[ "servo4:GND", "esp:GND.2", "black", [ "h-198.63", "v191.18", "h-68.87", "v259.26" ] ],
[ "servo2:GND", "esp:GND.2", "black", [ "h-327.1", "v22.46" ] ],
[ "servo4:V+", "esp:VIN", "red", [ "h-373.05", "v458.26" ] ],
[ "servo2:V+", "esp:VIN", "red", [ "h-347.68", "v347.93" ] ],
[ "servo1:V+", "esp:VIN", "red", [ "h-423.26", "v217.02" ] ],
[ "servo3:V+", "esp:VIN", "red", [ "h-5.57", "v-78.19", "h-400.28", "v175.53" ] ],
[ "servo5:V+", "esp:VIN", "red", [ "h-105.15", "v55.02", "h-194.84", "v-75.08" ] ]
],
"serialMonitor": { "display": "always", "newline": "lf" }
}
Thanks for the details!
It won't work until this feature (PWM on ESP32) is implemented. It's open for voting but doesn't have any votes at the moment. When it get some votes we can start looking into implementing it.
Also required for sound. Please fix https://docs.wokwi.com/parts/wokwi-buzzer to mention the fact that the Buzzer won't work with ESP32
@jflamy the documentation for the ESP32 shows that PWM is not yet implemented. I think it makes more sense to document the limitations of each MCU's simulation on the page for that MCU, rather than spray it across every other component's docs. Would you agree?
Note: I started looking into this, and there are two kinds of PWM peripherals for the ESP32:
As far as I can tell, the Servo library uses the LED_PWM. It's also likely that tone generation uses LED_PWM.
Update: Servo now works!
https://wokwi.com/arduino/projects/323706614646309460
I haven't tested the sound yet, so I'm keeping this issue open until we can confirm that sound also works
Project https://wokwi.com/arduino/projects/323708751863349844 is a hacked down version with a Tone library sound that works in real life. The bottom buzzer should sound 3 times with a one second interval.
The first iteration shows notes, but no sound. In real life, this code works (i.e. the first iteration does work right away -- the first 30 seconds of this video show this feature in real life.
The second and third iteration do provide sound.
Note that there are notes shown next to the buzzer during setup, as if the pins were turned on for a tiny bit.
Thanks for testing, @jflamy!
Interestingly, when looking at the logic analyzer trace, I can see the PWM signal going out three times:
So there's probably some issue with the buzzer simulation itself. My bet is that Web Audio isn't ready yet when the first note starts playing, so Wokwi silently skips that note.
Might be the same issue as #241
Adding a fake "turn sound on/turn sound off" for a very short period in the setup stage kludges around the issue for the simulator. So it's an issue of "not ready for the first sound", and not a delay issue.
Closing this issue as PWM seems to be working properly from the PWM side.
I also did a bunch of fixes to the buzzer algorithm, but if the problem persists, feel free to open a separate issue.
PWM is required for the ESP32Servo library