Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ESP32 Servo control
- *
- * learnelectronics
- * 2 JAN 2018
- *
- * www.youtube.com/c/learnelectronics
- *
- *
- * Sketch adapted from original code by MJRobot
- *
- */
- #define ANALOG_PIN_0 32 //we need to tell the ESP32 what pin we want for the analog read
- int analog_value = 0; //set our initial value as 0
- #define SERVO_PIN 5 //we need to tell the ESP32 what pin the servo is on
- int freq = 50; //servos operate @ 50Hz so we ned to set the freq to that value
- int channel = 0; //which channel do we want to use
- int resolution = 8; //set out resolution for a resonable value
- int dutyCycle = 21; //this is about the halfway point (90deg)
- void setup()
- {
- Serial.begin(115200); //serial comms @ 115200
- delay(1000); // wait one
- Serial.println("ESP32 Servo Control"); //intro
- ledcSetup(channel, freq, resolution); //we need to setup the ledc function with channel, freq, and resultion
- ledcAttachPin(SERVO_PIN,channel); //then we attach our servo
- ledcWrite(channel,dutyCycle); //reset the servo
- }
- void loop()
- {
- analog_value = analogRead(ANALOG_PIN_0); //read the value from the pot
- Serial.print(analog_value); //print the value to serial
- Serial.print(" Duty Cycle -> "); //print words
- Serial.println(dutyCycle); //print out the duty cycle
- dutyCycle = map(analog_value, 0, 4095, 10, 33); //calculate duty cycle
- ledcWrite(channel, dutyCycle); //write the current duty cycle to the servo
- delay(50); //wait 50mS
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement