首页 > 编程知识 正文

arduino 编码器计数(arduino编码器计数程序)

时间:2023-05-04 12:04:49 阅读:76596 作者:1141

在本文中,您将学习旋转编码器的工作原理,以及如何将其与Arduino开发板一起使用。 旋转编码器是用于确定旋转轴角度位置的位置传感器。 根据旋转运动生成模拟或数字电信号。

rotary-encoder-module-300 x286.jpg (14.75 kb,下载次数: 149 )。

2018-11-20 20:11上传

有各种类型的旋转编码器,可以通过输出信号和传感技术进行分类。 本文使用增量旋转编码器,它是测量旋转的最简单的位置传感器。

rotary-encoders-class ification.png (16.26 kb,下载次数: 141 ) )。

2018-11-20 20:11上传

该旋转编码器也称为正交编码器或者相对旋转编码器,其输出是一系列方波脉冲。

旋转编码器的工作原理

让我们详细看看编码器的工作原理。 方波脉冲的产生方法如下所示。 编码器有一个磁盘,具有均匀间隔的接触区域,连接到公共引脚c和其他两个独立的接触引脚a和b,如下图所示。

rotary-encoder-how-it-works-working-principle.png (17.99 kb,下载次数: 139 ) )。

2018-11-20 20:11上传

当磁盘逐渐开始旋转时,针脚a和针脚b开始与公共针脚接触,并相应地产生两个方波输出信号。

如果只计算信号的脉冲,则可以使用两个输出之一来确定旋转位置。 但是,如果想决定旋转方向,就必须同时考虑两个信号。

可见两个输出信号相互错开90度。 编码器顺时针旋转时,输出a在输出b之前。

rotary-encoder-output-signal-working-principle-photo-. png (12.56 kb,下载次数: 138 ) )。

2018-11-20 20:11上传

因此,如果每次信号变化时计算步数,则可以看到从高到低或从低到高,这两个输出信号具有相反的值。 相反,如果编码器逆时针旋转,则输出信号为相同的值。 因此,考虑到这一点,可以简单地对控制器进行编程,以读取编码器的位置和旋转方向。

旋转编码器Arduino示例

用Arduino开发板来看看实际的例子吧。 本示例中使用的特定模块位于具有五个针脚的拆分板上。 第一个端子是输出a,第二个端子是输出b,第三个端子是Button端子,当然其他两个端子是VCC和GND端子。

rotary-encoder-arduino-tutorial-example.jpg (38.44 kb,下载次数: 145 )。

2018-11-20 20:11上传

输出端子可以连接到Arduino开发板的任意数字端子上。

本文所需的组件如下:

旋转编码器模块

Arduino开发板

摇板和跳线

源代码

本例中的Arduino代码如下所示。

#define outputA 6

#define outputB 7

int counter=0;

int aState;

int aLastState;

void设置() }

pinmode(outputa,INPUT );

pinmode(outputb,INPUT );

Serial.Begin(9600;

//readstheinitialstateoftheoutputa

alaststate=数字读取(输出);

}

void loop () }

a state=数字读取(outputa; //reads the ' current ' stateoftheoutputa

//ifthepreviousandthecurrentstateoftheoutputaaredifferent,that means a Pulse has occured

if(astate!=aLastState ) {

//iftheoutputbstateisdifferenttotheoutputastate,thatmeanstheencoderisrotatingclockwise

if (数字读取(输出)!=aState

柜台;

p>} else {

counter --;

}

Serial.print("Position: ");

Serial.println(counter);

}

aLastState = aState; // Updates the previous state of the outputA with the current state

}复制代码

代码描述:首先,我们需要定义编码器所连接的引脚,并定义程序所需的一些变量。在setup()函数部分,我们需要将两个引脚定义为输入,启动串行通信以在串行监视器上打印结果,以及读取输出A的初始值并将值放入变量aLastState中。

然后在loop()函数部分我们再次读取输出A,但现在我们将值放入aState变量。因此,如果我们旋转编码器并生成脉冲,则这两个值将不同,并且第一个“if”语句将变为true。在那之后使用第二个“if”语句,我们确定旋转方向。如果输出B状态与输出A状态不同,则计数器将增加1,否则它将减小。最后,在串行监视器上打印结果后,我们需要使用aState变量更新aLastState变量。

这就是我们在这个例子中所需要的一切。如果上传代码,启动串行监视器并开始旋转编码器,我们将开始获取串行监视器中的值。我拥有的特定模块每个完整周期计数30个。

示例2 - 使用旋转编码器控制步进电机

除了这个基本示例之外,我再举一个使用旋转编码器控制步进电机位置的例子。

Controlling-a-stepper-motor-using-a-Rotary-Encoder.jpg (46.11 KB, 下载次数: 158)

2018-11-20 20:11 上传

以下是该示例的Arduino代码:

#include // includes the LiquidCrystal Library

LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

// defines pins numbers

#define stepPin 8

#define dirPin  9

#define outputA 10

#define outputB 11

int counter = 0;

int angle = 0;

int aState;

int aLastState;

void setup() {

// Sets the two pins as Outputs

pinMode(stepPin,OUTPUT);

pinMode(dirPin,OUTPUT);

pinMode (outputA,INPUT);

pinMode (outputB,INPUT);

aLastState = digitalRead(outputA);

lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }

}

void loop() {

aState = digitalRead(outputA);

if (aState != aLastState){

if (digitalRead(outputB) != aState) {

counter ++;

angle ++;

rotateCW();

}

else {

counter--;

angle --;

rotateCCW();

}

if (counter >=30 ) {

counter =0;

}

lcd.clear();

lcd.print("Position: ");

lcd.print(int(angle*(-1.8)));

lcd.print("deg");

lcd.setCursor(0,0);

}

aLastState = aState;

}

void rotateCW() {

digitalWrite(dirPin,LOW);

digitalWrite(stepPin,HIGH);

delayMicroseconds(2000);

digitalWrite(stepPin,LOW);

delayMicroseconds(2000);

}

void rotateCCW() {

digitalWrite(dirPin,HIGH);

digitalWrite(stepPin,HIGH);

delayMicroseconds(2000);

digitalWrite(stepPin,LOW);

delayMicroseconds(2000);

}复制代码

以上就是本篇文章的全部内容,如果遇到任何问题,请随时在本帖下面进行回复。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。