Connecting a servo to an arduino
Today it’s the turn of the servo motor or servo drive. Connecting a servo to an arduino
What is the interest of the servo motor for us? Using the circuit from the previous lesson we can control it with the potentiometer knob. So if you have already forgotten the previous post or missed it, please check it out here.Connecting a Potentiometer. Sensors. Arduino
Servo motors are remarkable in that with the help of control commands it is possible to set the motor shaft to a certain position. And this position will be maintained until new commands are received.
Standard servos have a fixed range of rotation angles, from 0 to 180 degrees. They are based on a potentiometer that is connected to the drive shaft. It determines the rotation angle.
Control is performed by sending a pulse to the control contact. The pulse duration determines the rotation angle.
If you remove the potentiometer from the circuit, you get a continuous rotation servo that can rotate continuously, with the pulse duration determining the rotation speed.
Servo drive
The servmotor needs a separate power source. Like the Arduino board, it operates from 5 volts. But when executing commands, it consumes a current of several hundred milliamps. When powering the Arduino from USB, the maximum possible current is 500 mA.
However, when stationary, the servo motor consumes a small current. Therefore, for the test, we will connect the drive to the arduino directly.
But remember that for real projects or for connecting multiple servos, a separate power supply is necessary. Also, if the power supply is insufficient, the drive shaft will move erratically, and the movement of the shaft causes voltage spikes through the circuit and can damage the board.
In order to constantly maintain the exact position of the drive shaft, signals must be sent every 20 milliseconds. To simplify programming of the drives, the Arduino Servo library has already been written, which we will use so as not to rewrite the entire code. In addition, the standard example library already has a program for controlling the motor from a potentiometer, so let’s look at it.
Program
Let’s open the Knob program from the File — Examples — Servo menu.
We will connect the Servo.h library at the very beginning of the program to be able to use the functions from this library. And we will create an instance of the Servo object through which we will access the methods.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Let’s add the ability to output information to the serial port to the program and upload it to the Arduino.
Full text of the program
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val,scale; // variable to read the value from the analog pin
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
scale = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(scale); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
Serial.print("potentiometer = ");
Serial.print(val);
Serial.print("\t servo = ");
Serial.println(scale);
}
Conclusion
We connected a servo drive to the Arduino and set up control of the servo drive position from a potentiometer. This scheme is used in many projects and ready-made devices. In the following lessons, we will look at an ultrasonic distance sensor and try to create our first project based on already known sensors.