1
0

Sweep.ino 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Sweep
  2. by BARRAGAN <http://barraganstudio.com>
  3. This example code is in the public domain.
  4. modified 8 Nov 2013
  5. by Scott Fitzgerald
  6. modified for the ESP32 on March 2017
  7. by John Bennett
  8. see http://www.arduino.cc/en/Tutorial/Sweep for a description of the original code
  9. * Different servos require different pulse widths to vary servo angle, but the range is
  10. * an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
  11. * sweep 180 degrees, so the lowest number in the published range for a particular servo
  12. * represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
  13. * of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
  14. * 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
  15. * degrees.
  16. *
  17. * Circuit: (using an ESP32 Thing from Sparkfun)
  18. * Servo motors have three wires: power, ground, and signal. The power wire is typically red,
  19. * the ground wire is typically black or brown, and the signal wire is typically yellow,
  20. * orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
  21. * considerable power, we will connect servo power to the VBat pin of the ESP32 (located
  22. * near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
  23. *
  24. * We could also connect servo power to a separate external
  25. * power source (as long as we connect all of the grounds (ESP32, servo, and external power).
  26. * In this example, we just connect ESP32 ground to servo ground. The servo signal pins
  27. * connect to any available GPIO pins on the ESP32 (in this example, we use pin 18.
  28. *
  29. * In this example, we assume a Tower Pro MG995 large servo connected to an external power source.
  30. * The published min and max for this servo is 1000 and 2000, respectively, so the defaults are fine.
  31. * These values actually drive the servos a little past 0 and 180, so
  32. * if you are particular, adjust the min and max values to match your needs.
  33. */
  34. #include <ESP32Servo.h>
  35. Servo myservo; // create servo object to control a servo
  36. // 16 servo objects can be created on the ESP32
  37. int pos = 0; // variable to store the servo position
  38. // Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
  39. int servoPin = 18;
  40. void setup() {
  41. // Allow allocation of all timers
  42. ESP32PWM::allocateTimer(0);
  43. ESP32PWM::allocateTimer(1);
  44. ESP32PWM::allocateTimer(2);
  45. ESP32PWM::allocateTimer(3);
  46. myservo.setPeriodHertz(50); // standard 50 hz servo
  47. myservo.attach(servoPin, 1000, 2000); // attaches the servo on pin 18 to the servo object
  48. // using default min/max of 1000us and 2000us
  49. // different servos may require different min/max settings
  50. // for an accurate 0 to 180 sweep
  51. }
  52. void loop() {
  53. for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
  54. // in steps of 1 degree
  55. myservo.write(pos); // tell servo to go to position in variable 'pos'
  56. delay(15); // waits 15ms for the servo to reach the position
  57. }
  58. for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
  59. myservo.write(pos); // tell servo to go to position in variable 'pos'
  60. delay(15); // waits 15ms for the servo to reach the position
  61. }
  62. }