This scenario demonstrates how to control the position of a servo motor using Arduino. Servo motors are used for precise angular positioning in robotics and automation.
Component | Arduino Pin |
————– | ————- |
Servo Signal | 10 (PWM) |
Servo specifications:
Write a program that rotates the servo sequentially to 0°, 90°, and 180°, pausing for one second at each position.
#include <Servo.h>
Declare a servo object and assign it a pin:
Servo myServo; const int servoPin = 10;
Attach the servo object to the servo pin:
void setup() { myServo.attach(servoPin); }
Program the servo rotation sequence:
void loop() { myServo.write(0); // Move to 0 degrees delay(1000); // Wait 1 second myServo.write(90); // Move to 90 degrees delay(1000); // Wait 1 second myServo.write(180); // Move to 180 degrees delay(1000); // Wait 1 second }
Observe the servo motor rotate precisely to the angles 0°, 90°, and 180°, pausing clearly at each position.
If the servo does not move or jittering occurs: