如何使用Arduino UNO板和电位器控制伺服电机

在本arduino伺服电机教程中,您将学习如何使用arduino uno板和电位器控制伺服电机。
一旦你启动并运行了这个示例项目,你就掌握了从arduino控制伺服电机的技能。这可以成为更先进的机器人项目的基础。
您需要的组件:
arduino的板
5v伺服电机(如sg90)
电位器(1k及以上的任意值)
面包板和跳线
电路设置:
将舵机的电源线(通常为红色)连接到 arduino 上的 5v 输出。
将舵机的接地电缆(通常为棕色或黑色)连接到 arduino 上的一个 gnd 引脚。
将舵机的信号线(通常为橙色或黄色)连接到 arduino 上的数字引脚 9。
将电位器插入试验板,并将其一个外部引脚连接到 5v,另一个外部引脚连接到 arduino 上的 gnd。
将电位计的中间引脚连接到arduino上的模拟引脚a0。
示例代码 1:自动伺服运动
首先,要测试伺服电机是否工作,请上传以下测试代码。上传后,伺服电机应开始在 0 到 180 度之间来回移动。确保您的舵机连接到引脚
9,如电路图所示。
#include // include the servo libraryservo myservo; // create a servo object to control the servo motorvoid setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object}void loop() { // goes from 0 degrees to 180 degrees in steps of 1 degree for (int pos = 0; pos = 0; pos -= 1) { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits for the servo to reach the position }}上传代码:
通过 usb 数据线将 arduino uno 连接到计算机。
在计算机上打开arduino ide。
将提供的代码复制到新草图中。
在“工具”菜单下选择正确的主板和端口。
单击“上传”按钮将代码传输到arduino。
示例代码 2:使用电位计控制伺服
此示例草图使用电位计来控制伺服电机的位置。当您转动电位器时,伺服电机应相应地移动到电位器电阻所指示的位置。
此代码基于基本的arduino电位器示例。
#include servo myservo; // servo object to control the motorint potpin = a0; // where the potentiometer is connectedint val; // variable to read the potentiometer valuevoid setup() { // tell the servo object which pin to use myservo.attach(9);}void loop() { // read the value of the potentiometer (value between 0 and 1023) val = analogread(potpin); // scale it to use it with the servo (value between 0 and 180) val = map(val, 0, 1023, 0, 180); // set the servo position according to the scaled value myservo.write(val); // wait for the servo to reach the position delay(15);}上传代码后,转动电位器将改变电阻和arduino读取的模拟值,从而控制伺服电机的位置。
现在,您可以对位置进行试验,并了解伺服电机如何响应电位计的调整。
arduino伺服电机故障排除
如果伺服电机没有响应:
根据原理图检查所有连接。
确保在ide中正确选择了arduino板。
确保试验板上没有短路。
确保您的舵机在 5v 下工作


为多挖显卡挖币矿工自废主板M.2
小米6亮白版今日上午十点开售2499元:黑边控制得到解决
电容选用与安装注意事项
openEuler社区邓一诺:实践是探索和提升的最佳捷径
高速模数转换器所需的极低抖动编码(采样)时钟
如何使用Arduino UNO板和电位器控制伺服电机
PRISMO蔡司三坐标CMM加速模式包
功放出现故障时的原因及排除方法
工业物联网网关BL110网口采集Modbus TCP从机操作步骤
星环科技基于知识图谱平台Sophon KG推出3.1版本
ST双通道DTV单芯片集成MPEG2高清,支持模拟电视
测评表现AI硬盘:安防发展之必然
库房动力环境综合监控系统
LIN总线的应用 LIN网络的应用
新型DNA纳米结构有什么作用?
未来共享交通的新蓝图?未来出行趋势滴滴能否预示?
发布不到一个月,小米note3这款对标OPPO R11的手机已经开始降价
如何使用MOS晶体管研究NMOS零增益放大器原理
iPhone如何一键更换主题皮肤
微软为Android的开发发展做出了很大的贡献