Multiple-Servo-Example-Arduino.ino 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * ESP32 Servo Example Using Arduino ESP32 Servo Library
  3. * John K. Bennett
  4. * March, 2017
  5. *
  6. * This sketch uses the Arduino ESP32 Servo Library to sweep 4 servos in sequence.
  7. *
  8. * Different servos require different pulse widths to vary servo angle, but the range is
  9. * an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
  10. * sweep 180 degrees, so the lowest number in the published range for a particular servo
  11. * represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
  12. * of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
  13. * 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
  14. * degrees.
  15. *
  16. * Circuit:
  17. * Servo motors have three wires: power, ground, and signal. The power wire is typically red,
  18. * the ground wire is typically black or brown, and the signal wire is typically yellow,
  19. * orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
  20. * considerable power, we will connect servo power to the VBat pin of the ESP32 (located
  21. * near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
  22. *
  23. * We could also connect servo power to a separate external
  24. * power source (as long as we connect all of the grounds (ESP32, servo, and external power).
  25. * In this example, we just connect ESP32 ground to servo ground. The servo signal pins
  26. * connect to any available GPIO pins on the ESP32 (in this example, we use pins
  27. * 22, 19, 23, & 18).
  28. *
  29. * In this example, we assume four Tower Pro SG90 small servos.
  30. * The published min and max for this servo are 500 and 2400, respectively.
  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. * Experimentally, 550 and 2350 are pretty close to 0 and 180.
  34. */
  35. #include <ESP32Servo.h>
  36. // create four servo objects
  37. Servo servo1;
  38. Servo servo2;
  39. Servo servo3;
  40. Servo servo4;
  41. Servo servo5;
  42. // Published values for SG90 servos; adjust if needed
  43. int minUs = 1000;
  44. int maxUs = 2000;
  45. // These are all GPIO pins on the ESP32
  46. // Recommended pins include 2,4,12-19,21-23,25-27,32-33
  47. int servo1Pin = 15;
  48. int servo2Pin = 16;
  49. int servo3Pin = 14;
  50. int servo4Pin = 32;
  51. int servo5Pin = 4;
  52. int pos = 0; // position in degrees
  53. ESP32PWM pwm;
  54. void setup() {
  55. // Allow allocation of all timers
  56. ESP32PWM::allocateTimer(0);
  57. ESP32PWM::allocateTimer(1);
  58. ESP32PWM::allocateTimer(2);
  59. ESP32PWM::allocateTimer(3);
  60. Serial.begin(115200);
  61. servo1.setPeriodHertz(50); // Standard 50hz servo
  62. servo2.setPeriodHertz(50); // Standard 50hz servo
  63. servo3.setPeriodHertz(330); // Standard 50hz servo
  64. servo4.setPeriodHertz(200); // Standard 50hz servo
  65. //servo5.setPeriodHertz(50); // Standard 50hz servo
  66. }
  67. void loop() {
  68. servo1.attach(servo1Pin, minUs, maxUs);
  69. servo2.attach(servo2Pin, minUs, maxUs);
  70. pwm.attachPin(27, 10000);//10khz
  71. servo3.attach(servo3Pin, minUs, maxUs);
  72. servo4.attach(servo4Pin, minUs, maxUs);
  73. //servo5.attach(servo5Pin, minUs, maxUs);
  74. for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
  75. // in steps of 1 degree
  76. servo1.write(pos);
  77. delay(1); // waits 20ms for the servo to reach the position
  78. }
  79. for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
  80. servo1.write(pos);
  81. delay(1);
  82. }
  83. for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
  84. // in steps of 1 degree
  85. servo2.write(pos);
  86. delay(1); // waits 20ms for the servo to reach the position
  87. }
  88. for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
  89. servo2.write(pos);
  90. delay(1);
  91. }
  92. for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
  93. // in steps of 1 degree
  94. servo3.write(pos);
  95. delay(1); // waits 20ms for the servo to reach the position
  96. }
  97. for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
  98. servo3.write(pos);
  99. delay(1);
  100. }
  101. for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
  102. // in steps of 1 degree
  103. servo4.write(pos);
  104. delay(1); // waits 20ms for the servo to reach the position
  105. }
  106. for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
  107. servo4.write(pos);
  108. delay(1);
  109. }
  110. for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
  111. // in steps of 1 degree
  112. servo5.write(pos);
  113. delay(1); // waits 20ms for the servo to reach the position
  114. }
  115. for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
  116. servo5.write(pos);
  117. delay(1);
  118. }
  119. servo1.detach();
  120. servo2.detach();;
  121. servo3.detach();
  122. servo4.detach();
  123. pwm.detachPin(27);
  124. delay(5000);
  125. }