当前位置:首页 > 心得体会 > [Arduino 如何控制舵机] 多个舵机同时控制
 

[Arduino 如何控制舵机] 多个舵机同时控制

发布时间:2019-08-02 09:49:11 影响了:

Arduino 如何控制舵机(伺服电机)

发布日期:2011-08-13

舵机,又称伺服马达,是一种具有闭环控制系统的机电结构。舵机主要是由外 壳、电路板、无核心马达、齿轮与位置检测器所构成。其工作原理是由控制器发出PWM(脉冲宽度调制)信号给舵机,经电路板上的IC处理后计算出转动方向, 再驱动无核心马达转动,透过减速齿轮将动力传至摆臂,同时由位置检测器(电位器)返回位置信号,判断是否已经到达设定位置,一

般舵机只能旋转180度。

舵机有3根线,棕色为地,红色为电源正,橙色为信号线,但不同牌子的舵机,线的颜色可能不同,请大家注意。

舵机的转动位置是靠控制PWM(脉冲宽度调制)信号的占空比来实现的,标准PWM(脉冲宽度调制)信号的周期固定为20ms,占空比0.5~2.5ms 的正脉冲宽度和舵机的转角-90°~90°相对应。注意,由于舵机牌子不同,其控制器解析出的脉冲宽度也不同,所以对于同一信号,不同牌子的舵机旋转的角度也不同。

本实验需要舵机1 只。由于只是做演示用,舵机5v 电源就暂时使用Arduino 上的,但不可长时间使用,因为舵机转动时电流比较大,Arduino 上的电源芯片可能会因过热而保护甚至损坏。正常使用时需要使用外部电源给舵机供电。

硬件连接:

舵机电源接Arduino 5V电源脚

舵机地接Arduino GND脚

信号线接Arduino 9脚

软件代码:

arduino IDE使用的版本为0022版本,此次演示就使用其自带的例子代码,打开File->Examples->Servo->sweep,代码如下:

#include

Servo myservo; // create servo object to control a servo

// a maximum of eight servo objects can be created

intpos = 0; // variable to store the servo position

void setup()

{

myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop()

{

for(pos = 0; pos

{ // 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>=1; 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 }

}

下载程序进入arduino后,可以看到舵机摇臂左右慢慢摆动。

猜你想看
相关文章

Copyright © 2008 - 2022 版权所有 职场范文网

工业和信息化部 备案号:沪ICP备18009755号-3