Thursday, August 4, 2016

ESP Basic -1 - PCF8574 I2C I/O Expander Driver example



Mutually-exclusive switch driver Example :







     Now that we have the I2C scanner program  that can help us to detect our I2C devices is time to move to some driver examples

    I think everybody know already the famous PCF8574. I have also cover it extendely in the previous Articles about, here for I/O input and here for output I/O.

    Now is time to put it at work also in ESP8266 Basic and see how is working.

   The example for today is a implementation of a mutually-exclusive switch that can be handy for many, many things as for example for doing the selection of a source in a Audio mixer or a any other analog path. Or driving Power relays.

   Mutually exclusive switch means  that when the corresponding switch button is pressed ON must also automatically turn the other sources OFF.



What we will need: 


      This is how is looking the setup on a breadboard, for a better visibility:





Software implementation:

Something to remember: 

  • PCF8574 can SINK but NOT SOURCE much current - 100uA only (it cannot output high, if you want). Look at the above example how is connected the LED for SINKING current.
  • Each of the 8 GPIOs have a minimum guaranteed sinking current of 10 mA per bit at 5 V.
  • Each pin needs its own limiting resistor to prevent damage to the device!! keep under 25mA/pin.
  • Maximum device limit sink current in about 80mA. If you need more, look after PCA8574 (200mA max sink current!)

For more details please take a look at the PCF8574 Datasheet


ESP8266 Basic code: 
let address=32     'PCF8574 I2C Address

button "1", [1]     'Define push buttons for each I/O pin
button "2", [2]
button "3", [3]
button "4", [4]
button "5", [5]
button "6", [6]
button "7", [7]
button "8", [8]
button "OFF", [9]  'Button for turning OFF all
wait

[9]
i2c.begin(address)    'Start I2C communication with device at 'address'
i2c.write(255)           'write the corresponding I/O pin value to register
i2c.end()                   'end I2C communication
wait

[1]
i2c.begin(address)
i2c.write(254)
i2c.end()
wait

[2]
i2c.begin(address)
i2c.write(253)
i2c.end()
wait

[3]
i2c.begin(address)
i2c.write(251)
i2c.end()
wait

[4]
i2c.begin(address)
i2c.write(247)
i2c.end()
wait

[5]
i2c.begin(address)
i2c.write(239)
i2c.end()
wait

[6]
i2c.begin(address)
i2c.write(223)
i2c.end()
wait

[7]
i2c.begin(address)
i2c.write(191)
i2c.end()
wait

[8]
i2c.begin(address)
i2c.write(127)
i2c.end()
wait

This is how is looking the interface in the Web Browser after running the above program:



No comments:

Post a Comment