mcp23008_leds.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. require ("mcp23008")
  16. -- ESP-01 GPIO Mapping as per GPIO Table in https://github.com/nodemcu/nodemcu-firmware
  17. gpio0, gpio2 = 3, 4
  18. -- Setup MCP23008
  19. mcp23008.begin(0x0,gpio2,gpio0,i2c.SLOW)
  20. mcp23008.writeIODIR(0x00) -- make all GPIO pins as outputs
  21. mcp23008.writeGPIO(0x00) -- make all GIPO pins off/low
  22. ---
  23. -- @name count()
  24. -- @description Reads the value from the GPIO register, increases the read value by 1
  25. -- and writes it back so the LEDs will display a binary count up to 255 or 0xFF in hex.
  26. local function count()
  27. local gpio = 0x00
  28. gpio = mcp23008.readGPIO()
  29. if(gpio<0xff) then
  30. mcp23008.writeGPIO(gpio+1)
  31. else
  32. mcp23008.writeGPIO(0x00)
  33. end
  34. end
  35. -- Run count() every 100ms
  36. tmr.alarm(0,100,1,count)