Skip to content

ESP32 PWM Support (for Servo, Sound) #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
urish opened this issue Oct 22, 2021 · 13 comments
Closed

ESP32 PWM Support (for Servo, Sound) #214

urish opened this issue Oct 22, 2021 · 13 comments
Labels
enhancement New feature or request open for vote Vote at https://wokwi.com/features

Comments

@urish
Copy link
Contributor

urish commented Oct 22, 2021

PWM is required for the ESP32Servo library

@urish urish added enhancement New feature or request open for vote Vote at https://wokwi.com/features labels Oct 22, 2021
@discapacidad5
Copy link

I have more than 8 hours trying to run a Servomotor in esp32 and I could not. Configure everything but nothing happens

@urish
Copy link
Contributor Author

urish commented Nov 10, 2021

You mean in the Simulator? Or on a physical ESP32 chip?

@discapacidad5
Copy link

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:
[email protected]

Captura

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" }
}

@urish
Copy link
Contributor Author

urish commented Nov 14, 2021

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.

@jflamy
Copy link

jflamy commented Jan 30, 2022

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

@sutaburosu
Copy link

sutaburosu commented Jan 30, 2022

@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?

@urish urish changed the title ESP32 PWM Support (for Servo) ESP32 PWM Support (for Servo, Sound) Jan 30, 2022
@urish
Copy link
Contributor Author

urish commented Jan 30, 2022

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.

@urish
Copy link
Contributor Author

urish commented Feb 16, 2022

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

@jflamy
Copy link

jflamy commented Feb 16, 2022

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.

@urish
Copy link
Contributor Author

urish commented Feb 16, 2022

Thanks for testing, @jflamy!

Interestingly, when looking at the logic analyzer trace, I can see the PWM signal going out three times:

image

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.

@urish
Copy link
Contributor Author

urish commented Feb 16, 2022

Might be the same issue as #241

@jflamy
Copy link

jflamy commented Feb 16, 2022

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.

@urish
Copy link
Contributor Author

urish commented Mar 3, 2022

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.

@urish urish closed this as completed Mar 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request open for vote Vote at https://wokwi.com/features
Projects
None yet
Development

No branches or pull requests

4 participants