增量编码器与Arduino连接(读取编码器脉冲数)

https://mp.weixin.qq.com/s/e_25NUo5C_IhVck6dl3Tfg

如下将编码器与Arduino连接:
A相:PIN 2( arduino的中断器引脚)
B相:PIN 4( arduino的中断器引脚)
电源:5V
地:GND
在这里,我们必须注意,编码器的A、B相输出必须仅连接到Aorduino的中断引脚。否则,arduino无法记录来自编码器的每个脉冲。
在这里插入图片描述

volatile long temp, encoderCounter =0; //This variable will increase or decreas depending on the rotation of encoder
int encoderPinA = 2; //interrupt pin 2 
int encoderPinB = 3; //interrrupt pin 3

void setup() {
Serial.begin (115200);
pinMode (encoderPinA, INPUT); 
pinMode (encoderPinB, INPUT); 

//Setting up interrupt
//attach an interrupt to pin encoderPinA & encoderPinA of the Arduino, and when the pulse is in the CHANGE edge called the function doEncoderA()/doEncoderB()
attachInterrupt (digitalPinToInterrupt(encoderPinA), doEncoderA, CHANGE);//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 isDigitalPin nr 3 on moust Arduino.
attachInterrupt (digitalPinToInterrupt(encoderPinB), doEncoderB, CHANGE);
}

void loop() {
  // Send the value of counter
if ( encoderCounter!= temp){
  Serial.println (encoderCounter);
  temp = encoderCounter;
}
}

void doEncoderA(){

  // look for a low-to-high on channel A
  if (digitalRead(encoderPinA) == HIGH) { 
    // check channel B to see which way encoder is turning
    if (digitalRead(encoderPinB) == LOW) {  
      encoderCounter = encoderCounter + 1;         // CW
    } 
    else {
      encoderCounter = encoderCounter - 1;         // CCW
    }
  }
  else   // must be a high-to-low edge on channel A                                       
  { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(encoderPinB) == HIGH) {   
      encoderCounter = encoderCounter + 1;          // CW
    } 
    else {
      encoderCounter = encoderCounter - 1;          // CCW
    }
  }
  //Serial.println (encoder0Pos, DEC);          
  // use for debugging - remember to comment out
}

void doEncoderB(){

  // look for a low-to-high on channel B
  if (digitalRead(encoderPinB) == HIGH) {   
   // check channel A to see which way encoder is turning
    if (digitalRead(encoderPinA) == HIGH) {  
      encoderCounter = encoderCounter + 1;         // CW
    } 
    else {
      encoderCounter = encoderCounter - 1;         // CCW
    }
  }
  // Look for a high-to-low on channel B
  else { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(encoderPinA) == LOW) {   
      encoderCounter = encoderCounter + 1;          // CW
    } 
    else {
      encoderCounter = encoderCounter - 1;          // CCW
    }
  }
}

将代码上传到arduino后,打开串行监视器

并旋转编码器轴,如果沿顺时针方向旋转编码器,则值会增加;如果沿逆时针方向旋转,则值会减小。

进一步通过:

double angle = encoderCounter*360/172032.0;//ppr=172032

可以获取电机转动角度

  • 8
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
增量编码器是一种常用的传感器,用于测量旋转运动或线性运动的位置和方向。如果你想在Arduino上使用增量编码器,你需要连接编码器的输出信号线到Arduino的数字引脚上,并使用相应的代码来读取编码器数据。以下是一个简单的示例代码: ```cpp // 定义编码器引脚 const int encoderPinA = 2; const int encoderPinB = 3; // 定义变量 volatile int encoderPos = 0; int encoderPinALast = LOW; // 中断服务程序 void encoderInterrupt() { int encoderPinAState = digitalRead(encoderPinA); if (encoderPinAState != encoderPinALast) { if (digitalRead(encoderPinB) != encoderPinAState) { encoderPos++; } else { encoderPos--; } } encoderPinALast = encoderPinAState; } void setup() { // 设置编码器引脚为输入 pinMode(encoderPinA, INPUT); pinMode(encoderPinB, INPUT); // 启用编码器引脚的中断 attachInterrupt(digitalPinToInterrupt(encoderPinA), encoderInterrupt, CHANGE); // 初始化串口通信 Serial.begin(9600); } void loop() { // 读取编码器的位置 int currentPosition = encoderPos; // 输出位置到串口 Serial.println(currentPosition); // 延迟一段时间 delay(100); } ``` 这段代码使用了Arduino的中断功能来读取编码器数据编码器的引脚A连接Arduino的引脚2,引脚B连接到引脚3。中断服务程序根据编码器引脚A和B的状态变化来更新编码器的位置。在主循环中,我们读取编码器的位置并通过串口输出。 请注意,这只是一个简单的示例代码,你可能需要根据你的具体编码器和需求进行适当的调整。希望对你有帮助!如果你有更多问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值