Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Servo motors are widely used in robotics, automation, and control systems due to their precision and ease of control. In this article, we will discover how to control a servo motor using an Arduino board. This guide will provide practical examples with sample codes to help you get started.
A servo motor is a rotary actuator that allows for precise control of angular position, velocity, and acceleration. It consists of a motor coupled to a sensor for position feedback. Servo motors are commonly used in applications such as RC cars, robotics, and industrial automation.
Connect the Ground (GND) Pin:
Connect the Power (VCC) Pin:
Connect the Signal (Control) Pin:
Below is a simple example code to control the position of a servo motor using the Arduino Servo library.
#include <Servo.h>
// Create a Servo object
Servo myServo;
void setup() {
// Attach the servo on pin 9 to the Servo object
myServo.attach(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
}
Include the Servo Library:
#include <Servo.h>
This line includes the Servo library, which provides functions to control servo motors.
Create a Servo Object:
Servo myServo;
This line creates a Servo object named myServo
.
Attach the Servo to a Pin:
myServo.attach(9);
This line attaches the servo motor to pin 9 on the Arduino.
Control the Servo Position:
myServo.write(0); // Move to 0 degrees
delay(1000); // Wait for 1 second
myServo.write(90); // Move to 90 degrees
delay(1000); // Wait for 1 second
myServo.write(180); // Move to 180 degrees
delay(1000); // Wait for 1 second
These lines move the servo to different positions (0, 90, and 180 degrees) with a 1-second delay between each movement.
Controlling a servo motor with an Arduino is straightforward using the Servo library. By following the wiring instructions and using the provided example code, you can easily control the position of a servo motor for your projects.