-- 7 Segment Display for MCP23008 by John Longworth December 2017 -- Using pins 3 & 4 to be compatible with ESP001 -- 7 segment digit 1 = b + c = 64 + 32 = 96 (0x60) -- 7 segment digit 2 = abdeg = 128+64+16+8+2 = 218 (0xDA)... -- DP = 1 added to display to show decimal point nums = {0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6,0xFC} -- 1, 2 ...0 i = 1 id = 0 sda = 3 -- 1 ~ 12 IO index scl = 4 -- 1 ~ 12 IO index i2c.setup(id, sda, scl, i2c.SLOW) -- Initialize I2C device = 0x20 -- I2C address of MCP23017 IODIR = 0x00 -- Pin direction register Port A of MCP23017 GPIO = 0x09 -- I/O register for Port A of MCP23017 function write_reg(dev_addr, reg_addr, reg_val) i2c.start(id) i2c.address(id, dev_addr, i2c.TRANSMITTER) i2c.write(id, reg_addr) i2c.write(id, reg_val) i2c.stop(id) end write_reg(device, IODIR, 0x00) -- Set port A to output tmr.alarm(0,500,1,function() local segs = nums[i] if (i % 2 == 1) then -- Toggle decimal point segs = segs + 1 end write_reg(device, GPIO, segs) print(segs) i = i + 1 if (i > 10) then i = 1 end end)