当前位置: 首页 > news >正文

Arduino - 旋转编码器 - 伺服电机

Arduino - 旋转编码器 - 伺服电机

Arduino - Rotary Encoder
In this tutorial, We are going to learn how to program Arduino to rotate a servo motor according to the rotary encoder’s output value.
在本教程中,我们将学习如何对Arduino进行编程,使其根据旋转编码器的输出值旋转伺服电机。

About Servo Motor and Rotary Encoder 关于伺服电机和旋转编码器

If you do not know about servo motor and rotary encoder (pinout, how it works, how to program …), learn about them in the following tutorials:
如果您不了解伺服电机和旋转编码器(引脚排列、工作原理、如何编程等),请在以下教程中了解它们:

  • Arduino - Servo Motor tutorial
    Arduino - 伺服电机教程
  • Arduino - Rotary Encoder tutorial
    Arduino - 旋转编码器教程

Wiring Diagram 接线图

Arduino Rotary Encoder Servo Motor Wiring Diagram

This image is created using Fritzing. Click to enlarge image
此图像是使用 Fritzing 创建的。点击放大图片

Arduino Code Arduino代码

/** Created by ArduinoGetStarted.com** This example code is in the public domain** Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rotary-encoder-servo-motor*/#include <Servo.h>#define CLK_PIN 2#define DT_PIN 3#define SW_PIN 4#define SERVO_PIN 9#define DIRECTION_CW 0   // clockwise direction#define DIRECTION_CCW 1  // counter-clockwise directionint counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;Servo servo;  // create servo object to control a servovoid setup() {Serial.begin(9600);// configure encoder pins as inputspinMode(CLK_PIN, INPUT);pinMode(DT_PIN, INPUT);// read the initial state of the rotary encoder's CLK pinprev_CLK_state = digitalRead(CLK_PIN);servo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo objectservo.write(0);
}void loop() {// read the current state of the rotary encoder's CLK pinCLK_state = digitalRead(CLK_PIN);// If the state of CLK is changed, then pulse occurred// React to only the rising edge (from LOW to HIGH) to avoid double countif (CLK_state != prev_CLK_state && CLK_state == HIGH) {// if the DT state is HIGH// the encoder is rotating in counter-clockwise direction => decrease the counterif (digitalRead(DT_PIN) == HIGH) {counter--;direction = DIRECTION_CCW;} else {// the encoder is rotating in clockwise direction => increase the countercounter++;direction = DIRECTION_CW;}​    Serial.print("DIRECTION: ");
​    if (direction == DIRECTION_CW)
​      Serial.print("Clockwise");
​    else
​      Serial.print("Counter-clockwise");​    Serial.print(" | COUNTER: ");
​    Serial.println(counter);​    if (counter < 0)
​      counter = 0;
​    else if (counter > 180)
​      counter = 180;​    // sets the servo angle according to the counter
​    servo.write(counter);}// save last CLK stateprev_CLK_state = CLK_state;
}

Quick Steps 快速步骤

  • Connect Arduino to PC via USB cable
    通过USB线将Arduino连接到PC
  • Open Arduino IDE, select the right board and port
    打开Arduino IDE,选择正确的板卡和端口
  • Copy the above code and open with Arduino IDE
    复制上面的代码并使用Arduino IDE打开
  • Click Upload button on Arduino IDE to upload code to Arduino
    单击Arduino IDE上的“上传”按钮,将代码上传到Arduino

Arduino IDE Upload Code

  • Open Serial Monitor 开放式串行监视器
  • Rotate the rotary encoder
    旋转旋转编码器
  • See the servo motor’s rotation
    查看伺服电机的旋转
  • See the result on Serial Monitor
    在串行监视器上查看结果

Function References

  • analogRead()
  • map()
  • Servo.attach()
  • Servo.write()
  • Servo.writeMicroseconds()
  • Servo.read()
  • Servo.attached()
  • Servo.detach()
http://www.lryc.cn/news/384694.html

相关文章:

  • 儿童电动音乐牙刷OTP芯片方案:NV040C,耐温耐压,抗干扰能力强
  • Sentinel链路流控模式失效的解决方法
  • Web应用安全测试-专项漏洞(一)
  • VMware ESXi 8.0U2c macOS Unlocker OEM BIOS Huawei (华为) FusionServer 定制版
  • python中的高阶函数介绍
  • 华为OD机试 - 石头剪刀布游戏(Java 2024 D卷 200分)
  • [开发|java] LocalDate转化为LocalDateTime
  • 介绍几种 MySQL 官方高可用方案
  • IMU坐标系与自定义坐标系转化
  • 《STM32 HAL库》RCC 相关系列函数详尽解析—— HAL_RCC_OscConfig()
  • 手动将jar包导入本地Maven仓库
  • 煤安防爆手机为什么能在煤矿井下使用
  • 科普小课堂|不同版本USB接口详细解析
  • Spring Boot中的JSON解析优化
  • 全彩屏负氧离子监测站
  • LeetCode 1207.独一无二的数
  • 自然语言处理——英文文本预处理
  • 2024年二级建造师机电工程专业历年考试题库精选答案解析。
  • Oracle 19C19.3 rac安装并RU升级到19.14
  • 1012:Joseph
  • 【高级篇】备份与恢复:守护数据的长城(十一)
  • Docker构建多平台镜像
  • 群体优化算法---石墨烯优化算法介绍以及在期权定价上的应用(Black-Scholes模型来计算欧式期权的理论价格)
  • 创纪录!沃飞长空完成新一轮融资,实力获资方认可
  • 1991java Web体检预约管理系统eclipse定制开发mysql数据库BS模式java编程jdbc
  • 如何优雅终止线程/线程池
  • 泰迪智能科技实验室产品-云计算资源管理平台介绍
  • .Net WebApi启动 Swagger异常报错: Failed to load API definition
  • CSS新手入门笔记【导入方法、选择器介绍、选择器优先级、属性详细介绍、盒子模型】
  • 制作高校专属PPT时,如何将校徽设置成透明底色?无须PS