Sweep the servo motor shaft with the Arduino 

You may often need to control the movement of an object at a specific angle and speed.


The servo motor allows you to achieve this easily. You can use it in many applications, such as remotely opening and closing a door.


In this tutorial, we will use the Arduino to control the servo motor shaft to sweep back and forth across 180 degrees. Let's have a look at it.


Overview


A servo motor is just a simple DC motor with certain modifications. When you look at it, you'll notice that it has several gears and an electronic circuit.


These improvements provide the motor with new capabilities, such as precise control of the shaft's speed and direction. It also gives the motor the ability to know the current position of the shaft.


Servo motors are used in a variety of applications, including:


-Robots

-Security cameras

-Solar trackers



Getting the items


For this project you will need the following components: 

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat Positional Rotation Servo - Generic (Micro Size)
20 QAR
Sale Off
Voltaat Jumper Wires - Male to Male (40 Pack)
10 QAR

Wiring it up


To set up the servo motor in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the servo motor and the Arduino. Once the servo motor and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.


Connections from the servo motor:

• Servo GND pin→ Arduino GND pin

• Servo VCC pin→ Arduino VCC pin

• Servo signal pin → Arduino pin 9


Coding


The function of this sketch is to control the servo motor, making it sweep back and forth across 180 degrees, and print the current position through the serial monitor.


In order for the code to work correctly, you need to download the servo motor library. Libraries are files that you can download and copy to the Arduino IDE software files so the Arduino can recognize different sensors and components. You can download the library files from the resources section and then install it by following this tutorial.


The code is basic and straightforward; simply follow the comments and instructions and you will be able to grasp it quickly.


/*
Voltaat learn (https://www.voltaat.com)
Link for full tutorial: https://bit.ly/3VysCRP
Link for libraries: https://bit.ly/3VClW5w

Tutorial: Sweep the servo motor shaft with Arduino

The function of this sketch is to control the servo motor,
making it sweep back and forth across 180 degrees,
and print the current position through the serial monitor.

Connections from the servo motor:
• Servo GND pin→ Arduino GND pin
• Servo VCC pin→ Arduino VCC pin
• Servo signal pin → Arduino pin 9
*/


//Servo motor library
#include< Servo.h >

//Create servo object to control a servo
Servo myservo;

//Variable to store the servo position
int pos =0;

//Commands inside void setup run once
void setup(){
//Start the serial monitor at 9600 baud rate (9600 bits per second)
Serial.begin(9600);
//Attaches the servo on pin 9 to the servo object
myservo.attach(9);
}

//Commands inside void loop run forever
void loop(){
//Goes from 0 degrees to 180 degrees
for(pos =0; pos <=180; pos +=1)
{
//Tell the servo to go to position in variable 'pos'
myservo.write(pos);
//Print to serial monitor
Serial.print("Servo Motor Current Position: ");
Serial.println(pos);
//Waits 15ms for the servo to reach the position
delay(15);
}

//Goes from 180 degrees to 0 degrees
for(pos =180; pos >=0; pos -=1)
{
//Tell the servo to go to position in variable 'pos'
myservo.write(pos);
//Print to serial monitor
Serial.print("Servo Motor Current Position: ");
Serial.println(pos);
//Waits 15ms for the servo to reach the position
delay(15);
}
}

Testing it out




Now you must have correctly wired the servo motor to the Arduino as we explained in the wiring section, as well as uploaded the code to your Arduino board.


You may now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.



The serial monitor is a great tool that can facilitate communication between the computer and the Arduino. It can allow us to send and receive different commands as well as view information directly from the Arduino.


Now, as seen in the image below, the serial monitor displays the current position of the servo motor in degrees. You will also notice the sweeping movement of your servo motor.


You should also make sure you have chosen the right baud rate (9600) as specified in the code.

 

Resources 


Arduino Code

Servo motor  Library

Fritzing Wiring file

Related Tutorials


The RFID reader is a module that reads a unique number from a tag or a card when it comes near it. You can then use the scanned number to identify the card holder.

In this tutorial we will use the IR remote control and the receiver module with the Arduino to receive orders when you press different buttons and display the result on your computer.


In this tutorial, we will use the PIR sensor to send a message to your computer when someone enters your house.