YourESP32 Sketch Servo Pot Position
Jump to navigation
Jump to search
/* YourDuinoStarter Example: ESP32_ServoPotPosition - Moves a Servomotor through a range of positions in response to the position of a Potentiometer attached to an Analog input. - SEE the comments after "//" on each line below - CONNECTIONS:ESP32 Analog input 0 - Servo connector plugged on YourDuinoRobo1 port 9 - If separate wires: - Servo Black to Gnd. - Servo Red or Orange (Center wire) to +5V - Servo White or Yellow to Signal (Pin 9) - V1.00 09/17/12 Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ #include <ESP32Servo.h> /*-----( Declare Constants and Pin Numbers )-----*/ int ADC_Max = 4096; #define SERVOPIN 18 // Can be changed 12-19,21-23,25-27,32-33 #define POTPIN 34 // Analog input Can be 12-15,32-39 (recommended) #define ServoMIN 20 // Don't go to very end of servo travel #define ServoMAX 160 // which may not be all the way from 0 to 180. /*-----( Declare objects )-----*/ Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created /*-----( Declare Variables )-----*/ int potValue; // User moves the pot. int servoPosition; // variable to store the servo position void setup() /****** SETUP: RUNS ONCE ******/ { // Allow allocation of all timers ESP32PWM::allocateTimer(0); ESP32PWM::allocateTimer(1); ESP32PWM::allocateTimer(2); ESP32PWM::allocateTimer(3); myservo.setPeriodHertz(50);// Standard 50hz servo myservo.attach(SERVOPIN, 500, 2400); }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { potValue = analogRead(POTPIN); // read the value of the potentiometer (value between 0 and 1023) servoPosition = map(potValue, 0, ADC_Max, 0, 180); // scale it to use it with the servo (value between 0 and 180) myservo.write(servoPosition); // set the servo position according to the scaled value delay(200); // wait for the servo to reach the position }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ //none //*********( THE END )***********