ESP32 Starter Kit
(continued from previous page)
}
//Servo rotation program
void Set_Angle(int angle_val) { //Impulse function
int pulsewidth = map(angle_val, 0, 180, 500, 2500); //Map Angle to pulse width
for (int i = 0; i < 10; i++) { //Output a few more pulses
digitalWrite(servo_pin, HIGH);//Set the servo interface level to high
delayMicroseconds(pulsewidth);//The number of microseconds of delayed pulse width␣
˓→value
digitalWrite(servo_pin, LOW);//Lower the level of servo interface
delay(20 - pulsewidth / 1000); //Add the bracket
}
}
void setup() {
// put your setup code here, to run once:
pinMode(servo_pin,OUTPUT);
pinMode(TrigPin, OUTPUT);//Set Trig pin to output
pinMode(EchoPin, INPUT); //Set Echo pin to input
}
void loop() {
// put your main code here, to run repeatedly:
distance = checkdistance();
Serial.println();
if(distance < 30){
Set_Angle(180);
delay(5000);//Wait for 5s
}
if(distance > 30){
Set_Angle(0);
}
}
5. Test Result
After connecting the wiring and uploading code, the servo will rotate to 180° for 5s if the detected distance is less than
30cm. On the contrary, the servo will rotate to 0°.
8.5.30 Project 29: IR Remote Control
1. Description
The IR remote control uses IR signal to control LED, which greatly simplifies the process of controlling LED.
8.5. Arduino Project 151