Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Control a Servo Motor with Arduino

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.

What is a Servo Motor?

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.

Components Needed

  • Arduino board (e.g., Arduino Uno)
  • Servo motor (e.g., SG90)
  • Jumper wires
  • Breadboard (optional)
  • External power supply (if needed)

Wiring the Servo Motor to Arduino

  1. Connect the Ground (GND) Pin:

    • Connect the GND pin of the servo motor to the GND pin on the Arduino.
  2. Connect the Power (VCC) Pin:

    • Connect the VCC pin of the servo motor to the 5V pin on the Arduino. If your servo motor requires more current than the Arduino can supply, use an external power source.
  3. Connect the Signal (Control) Pin:

    • Connect the signal pin of the servo motor to a PWM-capable digital pin on the Arduino (e.g., pin 9).

Arduino Code to Control the Servo Motor

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
}

Explanation of the Code

  1. Include the Servo Library:

    #include <Servo.h>

    This line includes the Servo library, which provides functions to control servo motors.

  2. Create a Servo Object:

    Servo myServo;

    This line creates a Servo object named myServo.

  3. Attach the Servo to a Pin:

    myServo.attach(9);

    This line attaches the servo motor to pin 9 on the Arduino.

  4. 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.

Conclusion

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.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.