Arduino Servo Motor and LED syncing On | Off

Created At: 2024-10-18 08:29:46 Updated At: 2024-10-18 08:29:46

Here we will see that, how to work with servo and LED light. In this tutorial, we will turn on LED light as the servo rotates. 

As opposite, we will turn off the LED as the servo rotates to the opposite direction.

#include <Servo.h>

Servo myServo; // Create servo object
int ledPin = 7; // LED connected to pin 7

void setup() {
myServo.attach(9); // Attach the servo to pin 9
pinMode(ledPin, OUTPUT); // Set LED pin as output
}

void loop() {
// Turn on the LED when the servo rotates
digitalWrite(ledPin, HIGH);

// Rotate servo from 0 to 180 degrees
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle); // Set servo position
delay(15); // Delay to allow servo to reach position
}

// Turn off the LED after rotation completes
digitalWrite(ledPin, LOW);

// Pause for 2 seconds before the next rotation
delay(2000);
}

The complete set up could be seen from the video above there.

Comment

Add Reviews

Latest Posts

Subscribe our newsletter