Knob.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Controlling a servo position using a potentiometer (variable resistor)
  3. by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
  4. modified on 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/Knob 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 SG90 small servo connected to VBat.
  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. */
  34. // Include the ESP32 Arduino Servo Library instead of the original Arduino Servo Library
  35. #include <ESP32Servo.h>
  36. Servo myservo; // create servo object to control a servo
  37. // Possible PWM GPIO pins on the ESP32: 0(used by on-board button),2,4,5(used by on-board LED),12-19,21-23,25-27,32-33
  38. int servoPin = 18; // GPIO pin used to connect the servo control (digital out)
  39. // Possible ADC pins on the ESP32: 0,2,4,12-15,32-39; 34-39 are recommended for analog input
  40. int potPin = 34; // GPIO pin used to connect the potentiometer (analog in)
  41. int ADC_Max = 4096; // This is the default ADC max value on the ESP32 (12 bit ADC width);
  42. // this width can be set (in low-level oode) from 9-12 bits, for a
  43. // a range of max values of 512-4096
  44. int val; // variable to read the value from the analog pin
  45. void setup()
  46. {
  47. // Allow allocation of all timers
  48. ESP32PWM::allocateTimer(0);
  49. ESP32PWM::allocateTimer(1);
  50. ESP32PWM::allocateTimer(2);
  51. ESP32PWM::allocateTimer(3);
  52. myservo.setPeriodHertz(50);// Standard 50hz servo
  53. myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 18 to the servo object
  54. // using SG90 servo min/max of 500us and 2400us
  55. // for MG995 large servo, use 1000us and 2000us,
  56. // which are the defaults, so this line could be
  57. // "myservo.attach(servoPin);"
  58. }
  59. void loop() {
  60. val = analogRead(potPin); // read the value of the potentiometer (value between 0 and 1023)
  61. val = map(val, 0, ADC_Max, 0, 180); // scale it to use it with the servo (value between 0 and 180)
  62. myservo.write(val); // set the servo position according to the scaled value
  63. delay(200); // wait for the servo to get there
  64. }