Controlling Servo Motor Using Keyboard Input
by halim930112 in Circuits > Arduino
27658 Views, 26 Favorites, 0 Comments
Controlling Servo Motor Using Keyboard Input
I'M BACK!!!!
Well hello everybody!! It has been quite a long time since i posted here. A bit busy with studies lately. But nevertheless, today I would like to share to you guys about a simple experiment concerning arduino and servo motor. In this experiment, I will be controlling my servo motor movement, which is connected to arduino, through keyboard input. Let's go to step 1 shall we?
Step 1: List of Items
As usual I will list all of the items(software included) for this simple experiment:
Hardware:
1. Arduino board (I used arduino UNO)
2. Servo motor ( I used a HD-3001HB servo motor)
3. Male to Male wires
Software:
1. Arduino IDE
2 Tera Term
Step 2: Assembling the Servo Motor to the Arduino
In a servo motor, there will be three connections provided. In my case, my servo motor consists of connection coloured dark brown,red and orange. After several rounds of internet search, I found that:
1. Dark brown connects to the GROUND
2.Red connects to 5V
3.Orange connects to arduino UNO pin 9
I have attached a JPEG image of the sketch for further clarification(I made the sketch using fritzing by the way). However in the sketch the connections are colored Black, Red, and Yellow:
1. Black connects to the GROUND
2. Red to 5V
3. Yellow to arduino UNO pin 9
Step 3: the CODE
Now comes the coding part. I'll try my best to explain as deep as possible of the code that I had made for this experiment.
THE CODE:
#include<Servo.h> // include server library
Servo ser; // create servo object to control a servo
int poser = 0; // initial position of server
int val; // initial value of input
void setup() {
Serial.begin(9600); // Serial comm begin at 9600bps
ser.attach(9);// server is connected at pin 9
}
void loop() {
if (Serial.available()) // if serial value is available
{
val = Serial.read();// then read the serial value
if (val == 'd') //if value input is equals to d
{
poser += 1; //than position of servo motor increases by 1 ( anti clockwise)
ser.write(poser);// the servo will move according to position
delay(15);//delay for the servo to get to the position
}
if (val == 'a') //if value input is equals to a
{
poser -= 1; //than position of servo motor decreases by 1 (clockwise)
ser.write(poser);// the servo will move according to position
delay(15);//delay for the servo to get to the position
}
}
}
Pssst: I have also attached the arduino code just in case!!
Downloads
STEP 4: the Output
Now, upload your code to the arduino. After uploading, open Tera Term and make a serial connection to the arduino board. Now press the designated button( a or d) and watch how the servo responds to the keyboard input. Thank You very much for your time in reading this instructable. I am sorry if I had written anything wrong (please inform me about it) or my English is bad as English is my second language. Other than that, I would like bid good luck and happy Innovating. Lastly, I would like to say, I'LL BE BACK!!