Created
March 17, 2021 11:05
-
-
Save elktros/5a17c649c5e8eb6270412638af21eed6 to your computer and use it in GitHub Desktop.
Control Servo Motor using ESP32 and Serial Input.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int servoPin = 16; /* GPIO16 */ | |
int dutyCycle = 0; | |
/* Setting PWM properties */ | |
const int PWMFreq = 50; | |
const int PWMChannel = 0; | |
const int PWMResolution = 8; | |
//const int MAX_DUTY_CYCLE = (int)(pow(2, PWMResolution) - 1); | |
void setup() | |
{ | |
Serial.begin(115200); | |
ledcSetup(PWMChannel, PWMFreq, PWMResolution); | |
/* Attach the LED PWM Channel to the GPIO Pin */ | |
ledcAttachPin(servoPin, PWMChannel); | |
ledcWrite(PWMChannel, dutyCycle); | |
} | |
void loop() | |
{ | |
while(Serial.available()) | |
{ | |
String in_char = Serial.readStringUntil('\n'); | |
dutyCycle = in_char.toInt(); | |
Serial.println(dutyCycle); | |
ledcWrite(PWMChannel, dutyCycle); | |
delay(10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment