#include <Servo.h>
Servo myservo;
int ang;
void setup() {
myservo.attach(9);
}
void loop() {
for (ang = 0; ang <= 180; ang += 1) {
myservo.write(ang);
delay(15);
}
for (ang = 180; ang >= 0; ang -= 1) {
myservo.write(ang);
delay(15);
}
}
Instruction Details
Wiring: Connect servo signal wire (orange/yellow) to Digital Pin 9, power (red) to 5V, ground (brown/black) to GND. For high-torque servos, use external 5–6V supply.
Library: Not required (built-in Servo library).
Upload Code: Tools → Board → Arduino Uno. Tools → Port → select your COM port. Click Upload button.
View Output: Servo motor will sweep from 0° to 180° and back continuously.
How to Use
Connect the required components as per the Pin Configuration above
Open Arduino IDE and create a new sketch
Copy and paste the code above
Select your Arduino board and COM port from Tools menu
Click the Upload button to upload the code to your Arduino
Open Serial Monitor (if applicable) to see the output
Components Required
Arduino Uno
Servo Motor (SG90)
Breadboard
Jumper Wires
External 5–6V Power Supply (optional, for high-torque)