#include <ESP32Servo.h>
Servo myservo; // create servo object to control a servo
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 5; // the number of the LED pin
const int servoPin = 4; // the number of the servo pin
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
myservo.attach(servoPin); // attaches the servo on pin 10 to the servo object
}
void loop() {
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // turn LED on
myservo.write(90); // set servo to 90 degrees
} else {
digitalWrite(ledPin, LOW); // turn LED off
myservo.write(0); // set servo to 0 degrees
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
servo1:GND
servo1:V+
servo1:PWM
led1:A
led1:C
r1:1
r1:2
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r