Introduction: PCF8574 (i2c Digital I/O Expander) Fast Easy Usage
You can find updated version on my site https://www.mischianti.org/2019/01/02/pcf8574-i2c... .
Libreria per utilizzare i2c pcf8574 IC con arduino e esp8266.
Questo IC può controllare (fino a 8) dispositivi digitali come pulsante o led con 2 soli pin.
Può leggere e scrivere valori digitali con solo 2 fili (perfetto per ESP-01).
Cerco di semplificare l'uso di questo IC, con un minimo di operazioni.
Step 1: How I2c Works
I2C works with it's two wires, the SDA(data line) and SCL(clock line).
Both these lines are open-drain, but are pulled-up with resistors.
Usually there is one master and one or multiple slaves on the line, although there can be multiple masters, but we'll talk about that later.
Both masters and slaves can transmit or receive data, therefore, a device can be in one of these four states: master transmit, master receive, slave transmit, slave receive.
Step 2: Library
You can find my library here.
To download.
Click the DOWNLOADS button in the top right corner, rename the uncompressed folder PCF8574.
Check that the PCF8574 folder contains PCF8574.cpp and PCF8574.h.
Place the PCF8574 library folder your /libraries/ folder.
You may need to create the libraries subfolder if its your first library.
Restart the IDE.
Step 3: Usage
Constructor: you must pas the address of i2c (to check the adress use this guide I2cScanner)
PCF8574(uint8_t address);
for esp8266 if you want specify SDA e SCL pin use this:
PCF8574(uint8_t address, uint8_t sda, uint8_t scl);
You must set input/output mode:
pcf8574.pinMode(P0, OUTPUT); pcf8574.pinMode(P1, INPUT); pcf8574.pinMode(P2, INPUT);
Step 4: Read Value
then IC as you can see in the image have 8 digital input/output:
So to read all analog input in one trasmission you can do (even if I use a 10millis debounce time to prevent too much read from i2c):
PCF8574::DigitalInput di = PCF8574.digitalReadAll(); Serial.print(di.p0); Serial.print(" - "); Serial.print(di.p1); Serial.print(" - "); Serial.print(di.p2); Serial.print(" - "); Serial.println(di.p3);
if you want read a single input:
int p1Digital = PCF8574.digitalRead(P1); // read P1
Step 5: Write Value
If you want write a digital value you must do:
PCF8574.digitalWrite(P1, HIGH);
or:
PCF8574.digitalWrite(P1, LOW);
Step 6: Interrupt Pin
You can also use interrupt pin: You must initialize the pin and the function to call when interrupt raised from PCF8574.
// Function interrupt void keyPressedOnPCF8574();
// Set i2c address PCF8574 pcf8574(0x39, ARDUINO_UNO_INTERRUPT_PIN, keyPressedOnPCF8574);
Step 7: Examples Wiring Diagram
Step 8: Thanks
i2c project series (Collection):
8 Comments
5 years ago
why im still wrong when PCF8574(uint8_t 0x38);
IDE said "expected primary-expression before '(' token" why?
thank u
Reply 2 years ago
PCF8574(uint8_t 0x38); must be 1. PCF8574((uint8_t) 0x38); or 2. PCF8574(0x38); uint8_t is a type so that is why the compiler expect a typecast at this position, see my 1. example - (uint8_t). Actually you don't need this so example 2. without the typecast is enough. A typecast is to force a type of value to be of a specific type. Google C++ typecast. Have fun.
Reply 5 years ago
Hi, Send me the sketch file, seems an error on code.
Thanks
Reply 3 years ago
Did you figure it out?
Reply 3 years ago
Sorry no response from muhammadA1065
5 years ago
Nice work. I have wrote a lib myself, you can check it at:
https://github.com/StoneAgeSkillz/PCF8574
Added some rottary encoder example.
Reply 5 years ago
Thanks.
I think the i2c components are very usefully, my first 3 instructable are on i2c components, this, PCF8591 for analog expander and DHT12 temp humidity sensor.
And I try to create a simple library for all this components as you.
5 years ago
Thanks for sharing :)