mcp23008_leds.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. ---
  2. -- @description Shows control of 8 GPIO pins/LEDs via I2C with the MCP23008 I/O expander.
  3. -- Tested on odeMCU 0.9.5 build 20150213.
  4. -- @date March 02, 2015
  5. -- @circuit Connect 8 LEDs withs resistors to the GPIO pins of the MCP23008.
  6. -- Connect GPIO0 of the ESP8266-01 module to the SCL pin of the MCP23008.
  7. -- Connect GPIO2 of the ESP8266-01 module to the SDA pin of the MCP23008.
  8. -- Connect two 4.7k pull-up resistors on SDA and SCL
  9. -- Use 3.3V for VCC.
  10. -- @author Miguel (AllAboutEE)
  11. -- GitHub: https://github.com/AllAboutEE
  12. -- Working Example Video: https://www.youtube.com/watch?v=KphAJMZZed0
  13. -- Website: http://AllAboutEE.com
  14. ---------------------------------------------------------------------------------------------
  15. local mcp23008 = require ("mcp23008")
  16. -- ESP-01 GPIO Mapping as per GPIO Table in https://github.com/nodemcu/nodemcu-firmware
  17. local gpio0, gpio2 = 3, 4
  18. ---
  19. -- @name count()
  20. -- @description Reads the value from the GPIO register, increases the read value by 1
  21. -- and writes it back so the LEDs will display a binary count up to 255 or 0xFF in hex.
  22. local function count()
  23. local gpio = mcp23008.readGPIO()
  24. if(gpio<0xff) then
  25. mcp23008.writeGPIO(gpio+1)
  26. else
  27. mcp23008.writeGPIO(0x00)
  28. end
  29. end
  30. do
  31. -- Setup MCP23008
  32. mcp23008.begin(0x0,gpio2,gpio0,i2c.SLOW)
  33. mcp23008.writeIODIR(0x00) -- make all GPIO pins as outputs
  34. mcp23008.writeGPIO(0x00) -- make all GIPO pins off/low
  35. -- Run count() every 100ms
  36. tmr.create():alarm(100, tmr.ALARM_AUTO, count)
  37. end