Project tutorial
Using Serial Monitor to Control Servo Motor

Using Serial Monitor to Control Servo Motor

In this project, you can learn how to control servo motor using serial monitor.

  • 48,058 views
  • 14 comments
  • 47 respects

Components and supplies

Apps and online services

About this project

NOTE: The code included AND mentioned in the comments will still work in 2021 - however not all parts are necessary. For example the very "weird" for loop that seems to do nothing was fixing a bug back when this tutorial was first posted. Keep that in mind :)

One of things people want to do with Arduino is controlling things with serial monitor. Here are some command project uses:

myservo.write(); - Sends a position information to the servo

if(Serial.availiable()) - If serial monitor is available, then continues the loop between { and }

intstate=Serial.parseInt(); - sets "state" to number, that has been sent to serial monitor

More about parseInt - reads every number, sent to the serial monitor. It can read only numbers, not letters etc.

Important - Servo coding in this project is a bit different:

10 degrees = 0 degrees

90 degrees = 90 degrees

169 degrees = 180 degrees

You may be saying. "What? That doesn't make any sense" Well, yes and no at the same time. Some servos cannot read properly and will take 170 as 180°- depends on the speed and quality.

Code

CodeC/C++
#include <Servo.h>
Servo myservo;
int pos = 0;



void setup()

{

Serial.begin(9600);
while (!Serial);
Serial.println("-------------------------");
Serial.println("ARos is loading....");
delay(1000);
Serial.println("ARos loaded succesfully");
Serial.println("-------------------------");
myservo.attach(9);
Serial.println("calibrating servo...");
for(pos = 0; pos <= 180; pos += 1)
myservo.write(0);
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(90);
delay(1000);
Serial.println("servo calibrated");
Serial.println("-------------------------");
Serial.println("Comand input online, write command to perform action");
Serial.println("-------------------------");

}

void loop() {
  
for(pos = 0; pos <= 180; pos += 1)
if (Serial.available())


{
  int state = Serial.parseInt();
    
if (state < 10)

{
Serial.print(">");
Serial.println(state);
Serial.println("cannost execute command, too low number");

}

if (state >= 10 && state < 170)
{
  Serial.print(">");
  Serial.println(state);
  Serial.print("turning servo to ");
  Serial.print(state);
  Serial.println(" degrees");
  myservo.write(state);
  
}

}

}



  

Schematics

Servo schematics
Servo lbl3uwth2d

Comments

Similar projects you might like

Servo Motor Control with Remote

Project showcase by Alfa

  • 26,075 views
  • 3 comments
  • 40 respects

All About Servo Motors and Servo Motor Tester

Project tutorial by RoboCircuits

  • 7,766 views
  • 1 comment
  • 14 respects

Arduino/Android - BLUETOOTH Multi Servo Motor Control

Project tutorial by Danny van den Brande

  • 31,888 views
  • 13 comments
  • 48 respects

Servo Control with TV Remote Control

Project showcase by eldo85

  • 25,621 views
  • 8 comments
  • 48 respects

Servo Motor Interface with Arduino Uno

by Akshay Joseph

  • 18,532 views
  • 7 comments
  • 26 respects

Temperature Sensor To control Servo Motor

by Jasleen

  • 35,106 views
  • 5 comments
  • 28 respects
Add projectSign up / Login