单片机控制无刷电机转速——arduino篇(详细代码)

时间:2022-07-11 19:18:13 阅读: 最新文章 文档下载
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。
最近对arduino很感兴趣,因为它的开源,编写简单,用它来控制电调。通过调节电位器来控制无刷电机的转速。程序是根据arduino中自带的Servo库中示例程序knob改编而成!当然也是根据电调的通信协议PPM来修改的!如图:



程序代码: #include

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

int potpin = A0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin

void setup() {

myservo.attach(9,1000,2000); // attaches the servo on pin 9 to


the servo object

delay(2500);

myservo.writeMicroseconds(1000); delay(2000); }

void loop() {

val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)

val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)

myservo.write(val); // sets the servo position according to the scaled value

delay(15); // waits for the servo to get there

}



说明:A0引脚接电位器来控制电机速度,9引脚接电调的信号线,电机启动后调节电位器由小逐渐调到最大,电机就开始由慢逐渐变快旋转起来。


本文来源:https://www.wddqw.com/doc/6c2537353968011ca3009119.html