A servo motor is a rotary actuator that precisely controls angular position. It is commonly used in robotics, model planes, and other projects requiring controlled movement. A typical servo has three connections: power, ground, and a signal line.
Components:
Arduino (any model, like Uno)
Servo motor (e.g., SG90 or MG996R)
Jumper wires
External power supply (optional for larger servo motors)
Servo Motor Pin Description:
Power (VCC) → Connects to a 5V or 6V power source.
Ground (GND) → Connects to the ground of the power source and Arduino.
Signal (Control) → Connects to a digital pin on the Arduino.
Wiring the Servo Motor to Arduino:
VCC → 5V pin on Arduino (use an external power supply if required for higher current draw).
GND → GND pin on Arduino.
Signal → Digital Pin (e.g., D9) on Arduino.
Code Example:
#include <Servo.h> // Include the Servo library
Servo myServo; // Create a Servo object
void setup() {
myServo.attach(9); // Attach the servo to pin 9
}
void loop() {
// Move the servo to 0 degrees
myServo.write(0);
delay(1000); // Wait for 1 second
// Move the servo to 90 degrees
myServo.write(90);
delay(1000); // Wait for 1 second
// Move the servo to 180 degrees
myServo.write(180);
delay(1000); // Wait for 1 second
}
How the Code Works:
#include <Servo.h>: Includes the Servo library, which simplifies controlling servo motors.
Servo myServo: Creates a servo object.
myServo.attach(9): Attaches the servo object to pin 9 on the Arduino.
myServo.write(angle): Sends a command to move the servo to the specified angle (0 to 180 degrees).
delay(1000): Pauses for 1 second to allow the servo to move to the position.
Testing:
Upload the code to your Arduino using the Arduino IDE.
Observe the servo motor moving to 0 degrees, then 90 degrees, and finally 180 degrees, each time pausing for 1 second.
Tips:
Power Supply: Small servo motors (e.g., SG90) can be powered directly from the Arduino. Larger servos may need an external power source to avoid overloading the Arduino.
Connections: Always connect the ground of the external power supply to the Arduino ground when using an external power source.
This setup allows you to control the angle of a servo motor, enabling projects like robotic arms, automated doors, and pan/tilt camera systems.
Comentarios