Simple Rotary Example

I’ve tried several different things, but I’m starting to think my rotary knob is broken.


I modified the example dimmer code to only have one knob (the one on pin 2), and I’m only getting 65520 as my read value from pin 2. Rotating the knob does nothing. Any ideas? Even tried something as simple as this:

---------------------------------------------------------------------------------------------------------------
hardware.pin2.configure(ANALOG_IN);

function doIt()
{
    imp.wakeup(0.2,doIt);
    server.show(hardware.pin2.read());
}

imp.configure(“Rotary Tester”, [], []);

doIt();

You need to enable the rotary control first.


In order to reduce power when you’re not using the pot, the bottom of the pot floats (ie will always read high) until you drive the pin on the IO expander.

You need this code at the start:

//I2C Addresses
const i2c_ioexp = 0x7C;

//----------------------------------------
//-- Configure I2C
//----------------------------------------
hardware.configure(I2C_89);
local i2c = hardware.i2c89;

//----------------------------------------
//-- IO Expander Functions
//----------------------------------------
local function ioexp_read(addr) {
    local result = i2c.read(i2c_ioexp, format("%c", addr), 1);
    if (result == null) {
        server.log(“i2c read fail”);
        return -1;
    } else return result[0];
}

local function ioexp_write(addr, data) {
    i2c.write(i2c_ioexp, format("%c%c",addr, data));
}

local function ioexp_writebit(addr, bitn, level) {
    // read modify write
    local reg = ioexp_read(addr);
    reg = (level==0)?(reg&~(1<<bitn)) : (reg | (1<<bitn));
    ioexp_write(addr, reg)
}

local function ioexp_setpin(gpio, level) {
    ioexp_writebit(gpio>=8?0x10:0x11, gpio&7, level?1:0);
}

local function ioexp_setdir(gpio, output) {
    ioexp_writebit(gpio>=8?0x0e:0x0f, gpio&7, output?0:1);
}


// Enable Potentiometer
ioexp_setpin(8, 0);
ioexp_setdir(8, 1);
hardware.pin2.configure(ANALOG_IN);

…then the rest of  your code…

Thanks. I was setting the pin high rather than low in other tests / code of trying to get it working. Per the documentation for Hannah, pin 8 reads “Potentiometer Enable”, which I thought 1 (i.e. true / on) would enable it.

The schematic shows active low signals with labels ending _L.


Rob

This code example works great.