舵机使用
目录概述舵机不像普通电机那样只会转圈圈,它可以根据你的指令转至0-180°之间的任意角停下来。 也叫伺服电机, 主要是由外壳、电路板、无核心马达、齿轮与位置检测器所构成。 工作原理由控制器发出讯号给舵机,经由电路板上的 芯片判断转动方向,再驱动无核心马达开始转动,透过减速齿轮将动力传至摆臂,同时由位置检测器送回讯号,判断是否已经到达定位。位置检测器其实就是可变电阻,当舵机转动时电阻值也会随之改变,藉由检测电阻值便可知转动的角度。 使用的注意事项舵机转角在0~180°,当高电平脉冲大于2.5ms,,一般没有自我保护的舵机,都会使转角超出正常的范围,使内部直流电机处于堵转状态,一两分钟就会使舵机发烫,甚至烧坏舵机。使用时,尽量让舵机在45°到135°之间转动,这范围内舵机转角也更精准。 舵机转接板
规格舵机
舵机转接板
开发设备
准备
实验一:驱动舵机
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
#define servo_pin SDA
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(servo_pin); // attaches the servo on pin SDA to the servo object
}
void loop()
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
程序调试
实验一:驱动舵机
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
#define servo_pin SDA
String inString = "";
int pos = 0; // variable to store the servo position
void setup()
{
Serial.begin(9600);
myservo.attach(servo_pin); // attaches the servo on pin 9 to the servo object
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop()
{
while (Serial.available() > 0)
{
inString += char(Serial.read());
delay(2);
}
if (inString.length() > 0)
{
myservo.write(inString.toInt());
Serial.println(inString.toInt());
}
inString = "";
}
程序调试
while (Serial.available() > 0)
{
inString += char(Serial.read());
delay(2);
}
if (inString.length() > 0)
{
myservo.write(inString.toInt());
Serial.println(inString.toInt());
}
应用视频 |