_SKU_SEN0235_360度编码器开关 - jimaobian/DFRobotWikiCn GitHub Wiki
概述
DFRobot新品360度编码器开关,基于旋转编码器设计,专为音量旋钮、灯光调节等场景设计。模块拥有三路输出,其中A、B端口为编码器脉冲输出,可以通过AB相位差来判断旋转方向与旋转圈数。C端为按钮开关,当按下开关时,输出低电平,可以用来配置相关参数。本产品使用简单,用途广泛,可以应用在汽车电子,多媒体音箱,仪器仪表,智能家居等各种领域。
性能描述
- 工作电压:3.3V~5V
- 全回转角度:360度(无止档点)
- 单圈脉冲数:15
- 回转寿命:30000±200 Cycles
- 接触电阻:≤100mΩ
- 绝缘阻抗:100MΩ以上
- 振荡:<10ms按压
- 工作温度:-30℃~+80℃
- 模块尺寸:33.8 * 22.4(mm)/1.3 * 1.1(inches)
- 安装孔尺寸:内径3.1mm/外径6mm
- 重量:14g
接口说明
| | | | -------------------------------------------- | | | : | |
标号 | 名称 | 功能描述 |
1 | VCC | 3.3~5V |
2 | GND | GND |
3 | A | 编码器A相输出 |
4 | B | 编码器B相输出 |
5 | C | 按键输出口,当按下时输出低电平 |
引脚说明
当顺时针旋转时,A、B输出相位差如下图所示:
教程
这里采用两个终端,分别接到A、B输出端,连线如下图所示:
int encoderPinA = 2;
int encoderPinB = 3;
int buttonPin = 4;
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
long readEncoderValue(void){
return encoderValue/4;
}
boolean isButtonPushDown(void){
if(!digitalRead(buttonPin)){
delay(5);
if(!digitalRead(buttonPin))
return true;
}
return false;
}
void setup() {
Serial.begin (9600);
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
pinMode(buttonPin, INPUT);
digitalWrite(encoderPinA, HIGH); //turn pullup resistor on
digitalWrite(encoderPinB, HIGH); //turn pullup resistor on
//call updateEncoder() when any high/low changed seen
//on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
void loop(){
//Do stuff here
if(isButtonPushDown()){
Serial.println("you push button down!!!");
}
Serial.println(readEncoderValue());
delay(50); //just here to slow down the output, and show it will work even during a delay
}
void updateEncoder(){
int MSB = digitalRead(encoderPinA); //MSB = most significant bit
int LSB = digitalRead(encoderPinB); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
lastEncoded = encoded; //store this value for next time
}
当顺时针旋转时,脉冲计数会增加,反之脉冲计数会减小;按下按钮时,会打印“you push button down”信息,打印效果如下图所示:
常见问题
还没有客户对此产品有任何问题,欢迎通过qq或者论坛联系我们!
| 更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。 |