Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Voltaat Arduino Ultimate Kit
50 min
Share

Sweep the servo motor shaft with the Arduino

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.

Project Video

Overview

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.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
SG90 Servo -Positional Rotation
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the servo motor and the Arduino, as shown in the image below:

 

 Connections from the servo motor:


 • Servo GND pin→ Arduino GND pin


 • Servo VCC pin→ Arduino VCC pin


 • Servo signal pin → Arduino pin 9

Coding

 
/*
 Voltaat learn (https://www.voltaat.com)
 Link for full tutorial:
 Link for libraries:

 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 access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.

know when your plant needs watering

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.

know when your plant needs watering

Resources

No items found.