BITE SIZE ARDUINO – SERVO

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.

servo_bb

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
}
BITE SIZE ARDUINO – SERVO

4 thoughts on “BITE SIZE ARDUINO – SERVO

    1. JR says:

      When using the Servo.h library, either the Digital or Analog pins can be used and they will deliver exactly the same result. However when using a Servo without the Servo.h library you will need to use a pin that is capable of the AnalogWrite command, so in that case you will need to use one of the Analog or Digital PWM pins.

      Like

  1. Yes, of course with Servo.h library you can also use Analog pins because all Analog pins can act as Digital pins. So technically your code will work as expected. This is just my personal ‘bug’, that is telling me that using Analog pin for digital signal is not logical if you have a lot of free Digital pins available 😉

    Liked by 1 person

  2. Regarding using servos without Servo.h library I totally disagree with you. First of all, despite its name AnalogWrite doesn’t output analog signals on boards that don’t have analog output pins (Uno, Mega and most of other Arduino boards can only read analog signals, but can’t output analog signal). PWM is, by the way, digital sinal. And secondly and most importantly, without Servo.h library you can’t use servos because of the differences in frequency (servo needs 50 Hz and analogWrite() is working at least 10 times quicker) and the pattern of the signal. This article https://lynx2015.wordpress.com/2015/08/13/arduino-mega-2560-r3/ gives more details about servos, PWM and AnalogWrite, if you are interested in this topic.

    Liked by 1 person

Leave a comment