[vc_row][vc_column width=”1/1″][vc_column_text]In this post, we are going to look at how to use pcDuino to control RC (hobby) servo motors. Servos have integrated gears and a shaft that can be precisely controlled. Standard servos allow the shaft to be positioned at various angles, usually between 0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to various speeds.
In this tutorial, we are going to use a SG90 RC servo.
[/vc_column_text][vc_tour][vc_tab title=”BOM” tab_id=”1384279516-1-87″][vc_column_text]1. pcDuino experiment package
2. SG90 RC servo
3. Jumper wires[/vc_column_text][/vc_tab][vc_tab title=”Wire Diagram” tab_id=”1384279516-2-98″][vc_column_text]SG90 RC servo has three pins: VCC, GND, and control signal. We can wire VCC to 5V pin of pcDuino, and GND to ground of ground of pcDuino. We use pin 7 of pcDuino to act as the control signal.
[/vc_column_text][/vc_tab][vc_tab title=”Sample Code” tab_id=”1384280086316-2-4″][vc_column_text]We are going to use Arduino IDE comes with the pcDuino as the programming environment.
The code is as following:
int servopin=7;//Define servo PWM control as digital 7
int myangle;//define variable angle
int pulsewidth;//define variable pulse width
int val;
void servopulse(int servopin,int myangle)//define a pulse function
{
pulsewidth=(myangle*11)+500;//translate angle to a pulse width value between 500-2480
digitalWrite(servopin,HIGH);//pull the interface signal level to high
delayMicroseconds(pulsewidth);//delay in microseconds
digitalWrite(servopin,LOW);//pull the interface signal level to low
delay(20-pulsewidth/1000);
}
void setup()
{
pinMode(servopin,OUTPUT);//set the interface pin as output
}
void loop()//translate the number 0-9 to angle between 0 degree to 180 degree, and let LED blink the same times
{
val=9;
val=val*(180/9);//translate number to angle
for(int i=0;i<=50;i++)//wait enough time so that the servo can rotate to the specified angle
{
servopulse(servopin,val);//call the pulse function
}
}
[/vc_column_text][/vc_tab][vc_tab title=”Servo in Action” tab_id=”1384281511642-3-1″][vc_column_text]val=0:
val=4:
val=9:
[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.