EBC Reading a Rotary Encoder via eQEP

A common way to read a rotational input is with a quadrature encoder such as these rotary encoders from SparkFun and Adafruit. BeatgleBone Black has an Enhanced Quadrature Encoder Pulse (eQEP) Module (See section 15.4 of the TRM) that makes reading encoders easy.

Wiring the Encoder
The encoders we are using have a common lead and two inputs, A and B. Wire the common to ground. We'll start using the Bone's eQEP2 since it doesn't conflict with the HDMI. Derek Molloy's P8/P9 Header chart shows eQEP2B_in is on pin P8_11 and eQEP2A_in is on P8_12.



Configuring the Encoder
If you are running a current version of Debian on your Bone (3-Mar-2015 or newer), the file for configuring the eQEP are already on your Bone.

bone$ # ls /lib/firmware/ | grep -i qep PyBBIO-eqep0-00A0.dtbo PyBBIO-eqep1-00A0.dtbo PyBBIO-eqep2-00A0.dtbo PyBBIO-eqep2b-00A0.dtbo

The different dtbo files configure for different pins. Get Derek Molloy's P8 and P9 header tables to see what pins are available. wget https://github.com/derekmolloy/boneDeviceTree/raw/master/docs/BeagleboneBlackP8HeaderTable.pdf wget https://github.com/derekmolloy/boneDeviceTree/raw/master/docs/BeagleboneBlackP9HeaderTable.pdf

Open the pdf files and search for eqp. eQEP2 looks like a good one, but it appears in two place. Try the first one bone$ export SLOTS=/sys/devices/bone_capemgr.*/slots bone$ echo PyBBIO-eqep2 > $SLOTS -bash: echo: write error: File exists There's a problem. Use dmesg to see what went wrong. bone$ dmesg | tail # [321550.694044] bone-capemgr bone_capemgr.9: slot #26: Failed verification # [325272.156839] bone-capemgr bone_capemgr.9: part_number 'PyBBIO-eqep2', version 'N/A' # [325272.157175] bone-capemgr bone_capemgr.9: slot #27: generic override # [325272.157484] bone-capemgr bone_capemgr.9: bone: Using override eeprom data at slot 27 # [325272.157539] bone-capemgr bone_capemgr.9: slot #27: 'Override Board Name,00A0,Override Manuf,PyBBIO-eqep2' # [325272.162296] bone-capemgr bone_capemgr.9: slot #27: Requesting part number/version based 'PyBBIO-eqep2-00A0.dtbo # [325272.162358] bone-capemgr bone_capemgr.9: slot #27: Requesting firmware 'PyBBIO-eqep2-00A0.dtbo' for board-name 'Override Board Name', version '00A0' # [325272.185291] bone-capemgr bone_capemgr.9: slot #27: dtbo 'PyBBIO-eqep2-00A0.dtbo' loaded; converting to live tree # [325272.185847] bone-capemgr bone_capemgr.9: slot #27: PyBBIO-eqep2 conflict P8.41 (#5:BB-BONELT-HDMI) # [325272.196171] bone-capemgr bone_capemgr.9: slot #27: Failed verification There's a conflict with the HDMI pins. Try the other one bone$ echo PyBBIO-eqep2b > $SLOTS That works! Wire encoder to P8_11 and P8_12 and then: bone cd /sys/devices/ocp.3/48304000.epwmss/48304180.eqep/ bone$ ls driver enabled  modalias  mode  period  position  power  subsystem  uevent bone$ cat position 0 Turn the encoder and cat again. bone$ cat position 12 Try turning the other way.

Reading the Encoder
In exercises/eQEP you'll find file called rotaryEncoder.js.

// This uses the eQEP hardware to read a rotary encoder // export SLOTS=/sys/devices/bone_capemgr.*/slots // echo PyBBIO-eqep2b > > $SLOTS // Wire encoder to P8_11 and P8_12 when using eQEP2. var b = require('bonescript'), fs = require('fs'); var eQEP0 = "/sys/devices/ocp.3/48300000.epwmss/48300180.eqep/", eQEP1 = "/sys/devices/ocp.3/48302000.epwmss/48302180.eqep/", eQEP2 = "/sys/devices/ocp.3/48304000.epwmss/48304180.eqep/", eQEP = eQEP2; var oldData,           // pervious data read period = 100;      // in ms // Set the eEQP period, convert to ns. fs.writeFile(eQEP+'period', period*1000000, function(err) {        if (err) throw err;         console.log('Period updated to ' + period*1000000);  }) // Enable fs.writeFile(eQEP+'enabled', 1, function(err) {     if (err) throw err;         console.log('Enabled');  }) setInterval(readEncoder, period);   // Check state every 250 ms function readEncoder(x) { fs.readFile(eQEP + 'position', {encoding: 'utf8'}, printValue); } function printValue(err, data) { if (err) throw err; if (oldData !== data) { console.log('position: '+data+' speed: '+(oldData-data)); oldData = data; } }
 * 1) !/usr/bin/env node

Run it with:

bone$ ./rotaryEncoder.js