I've spent the last hour trying to get an incremental encoder working with an mbed module.
Keyes KY-040 Rotary Encoder
https://www.amazon.co.uk/gp/product/B079H3C98M/ref=oh_aui_detailpage_o06_s00?ie=UTF8&psc=1
LPC1768 mbed module
os.mbed.com/platforms/mbed-LPC1768
The code I've written is as follows:
#include "QEI.h"
Serial pc(USBTX, USBRX);
//Use X4 encoding.
//QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING);
//Use X2 encoding by default.
QEI wheel (p5, p6, NC, 20);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
int main() {
int x;
led1 = 0;
led2 = 0;
led3 = 0;
led4 = 0;
while(1){
x = wheel.getPulses() % 4;
if (x == 0){
led1 = 1;
led2 = 0;
led3 = 0;
led4 = 0;
} else if (x == 1) {
led1 = 0;
led2 = 1;
led3 = 0;
led4 = 0;
} else if (x == 2) {
led1 = 0;
led2 = 0;
led3 = 1;
led4 = 0;
} else if (x == 3) {
led1 = 0;
led2 = 0;
led3 = 0;
led4 = 1;
}
}
}
I've tried both pull up and pull down resistors on the lines, but the mbed is not showing any sign of processing these. In all likeliness, I've made a wiring mistake.
Does anyone have a schematic I can follow on how to hook it up correctly?