[Robot Arm Control Project 1] Sub-Motor Control Using Serial Communication
· 2019-11-11 · 4
Through this hands-on exercise, you can practice the fundamentals of servo motors and eventually connect to a robot arm control project.
Materials: Arduino, 2 servo motors
Content: Control 2 servo motors using Arduino Uno.
When power is connected, the 2 servo motors align to 90 degrees and enter a waiting state for input.
The operations are as follows:
1. W input => Servo motor 1 rotates 30 degrees forward
2. S input => Servo motor 1 rotates 30 degrees backward
3. A input => Servo motor 2 rotates 30 degrees to the left
4. D input => Servo motor 2 rotates 30 degrees to the right
☞Since servo motors can rotate from 0 to 180 degrees, rotation must be limited to 0~180 degrees in the code.
The circuit connection is as follows:
The 5V and Ground of the Arduino are connected to the breadboard to form a common power supply, and the VCC and GND of the 2 servo motors are connected. Input/output pins 3 and 5 are used for the servo motors to construct the circuit. (Since servo motors are controlled by PWM, pins 3, 5, 6, 9, 10, 11 can be used)
The coding is as follows: (including comments)
```
#include //Include header file
Servo servo1; //Declare servo1 variable
Servo servo2; //Declare servo2 variable
int motor1 = 3; //Connect motor1 to input/output pin 3
int motor2 = 5; //Connect motor2 to input/output pin 5
int angle1 = 90; //Set initial angle value
int angle2 = 90; //Set initial angle value
void setup() {
servo1.attach(motor1); //Assign input/output pin 3 to servo1
servo2.attach(motor2); //Assign input/output pin 5 to servo2
Serial.begin(9600); //Use serial monitor
Serial.println("Enter the w,a,s,d ");
}
void loop() {
if(Serial.available()) //If serial communication is available
{
char input = Serial.read(); //Read serial monitor input value
if(input =='w') //If input value is w
{
Serial.print("+30");
for(int i = 0; i <30; i++) //Repeat 30 times
{
angle1 = angle1 + 1; //Add 1 to angle1 value 30 times
if(angle1 >=180) //If angle1 becomes greater than or equal to 180
angle1 = 180; //Fix angle1 to 180
servo1.write(angle1); //Operate servo1 according to angle1 value
delay(10);
}
Serial.print("\t\t");
Serial.println(angle1);
}
else if(input =='a') //If input value is a
{
Serial.print("+30");
for(int j = 0; j <30; j++)
{
angle2 = angle2 + 1;
if(angle2 >=180)
angle2 = 180;
servo2.write(angle2);
delay(10);
}
Serial.print("\t\t");
Serial.println(angle2);
}
else if(input == 's') //If input value is s
{
Serial.print("\t-30\t");
for(int i = 0; i <30; i++) //Repeat 30 times
{
angle1 = angle1 -1; //Subtract 1 from angle1 value 30 times
if(angle1 <= 0) //If angle1 becomes less than or equal to 0
angle1 = 0; //Fix angle1 to 0
servo1.write(angle1); //Operate servo1 according to angle value
delay(10);
}
Serial.println(angle1);
}
else if(input == 'd') //If input value is d
{
Serial.print("\t-30\t");
for(int j = 0; j <30; j++)
{
angle2 = angle2 -1;
if(angle2 <= 0)
angle2 = 0;
servo2.write(angle2);
delay(10);
}
Serial.println(angle2);
}
}
}
```
You can check the angle of the servo motor numerically through the serial monitor. In all future hands-on activities, angles, input values, and data will be checked through the serial monitor, so please make good use of it.
Click the magnifying glass button in the red box above to open the serial monitor window. Alternatively, you can click the Tool button at the top and select the serial monitor to open it.
The serial monitor results are as follows:
When entering w, a, s, d in the serial monitor input window, the current servo motor angle is calculated and output by calculating the angle according to the input value.
Operation video
4
댓글 0개
등록된 댓글이 없습니다.