Today we will have a look at how to connect a servo motor to an Arduino and how to control its movement.
A servo motor has 3 connector wires:
– A red wire for (+) power.
– A black wire for (-) power (GND).
– A orange, yellow or white cable for signal.
The signal wire of the servo must be connected to one of the analog pins on the Arduino, for the purpose of this example we will use A2. The red wire must be connected to the 5V pin on the Arduino and the black wire to the GND pin on the Arduino. As can be seen in the diagram above – a 100uF capacitor is connected between the 2 power terminals, the reason for this is that it prevents a voltage drop occurring in the circuit when the servo starts moving. A voltage drop can occur due to the fact that a servo consumes more power when starting to move then when it is already moving.
Below is the code used to rotate the servo to different positions:
#include "Servo.h" #define SERVO_PIN A2 Servo servoMotor; void setup() { servoMotor.attach(SERVO_PIN); } void loop() { servoMotor.write(0); // Rotate Servo to 0 Degrees delay(500); // Delay to allow Servo time to Move servoMotor.write(90); // Rotate Servo to 90 Degrees delay(500); // Delay to allow Servo time to Move servoMotor.write(180); // Rotate Servo to 180 Degrees delay(500); // Delay to allow Servo time to Move }