You can control the servo motor connected to the Arduino using the serial monitor. enter the required amount of angle in the serial monitor window. Wait for the action. This example is possible to demonstrate without real hardware! Thanks to Wokwi Arduino Simulator. Wokwi Arduino Simulator is free, easy to use and state of the art! Try it out for yourself!
AttributesArduino Simulator - https://wokwi.com
Code - https://create.arduino.cc/projecthub/Kub_Luk/using-serial-monitor-to-control-servo-motor-cc1daf
Arduino simulation output:
You can also refer to the below image for the connection diagram. The connection diagram is fairly simple as well!
Arduino Servo motor Simulator - Here is the code
#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("Cannot execute command, too low number");
Serial.read();
Serial.read();
}
if (state >= 10 && state < 170)
{
Serial.print(">");
Serial.println(state);
Serial.print("turning servo to ");
Serial.print(state);
Serial.println(" degrees");
myservo.write(state);
Serial.read();
Serial.read();
}
}
}
Arduino Simulator live link for the example above
The below link can be used to edit the code and see the effects instantly. You can also save the changes and share the project with others easily. Just click on the share button on the Wokwi Arduino simulator page!
https://wokwi.com/arduino/projects/306535600598549058
Please feel free to leave your suggestions in the comments. We will be glad to address/reply to all of them. Hop on to the Discord channel for more help/project discussions!
Share your interesting projects and browse through several curious projects from fellow developers and makers on Facebook Wokwi Group!
Stay Safe!
Don't stop learning!
#wokwiMakes
Comments