PCF8574_library/README.md

142 lines
4.8 KiB
Markdown
Raw Normal View History

2020-05-29 12:12:19 +00:00
<div>
2020-06-05 20:02:22 +00:00
<a href="https://www.mischianti.org/forums/forum/mischiantis-libraries/pcf8574-i2c-digital-i-o-expander/"><img
2020-05-30 20:13:22 +00:00
src="https://github.com/xreef/LoRa_E32_Series_Library/raw/master/resources/buttonSupportForumEnglish.png" alt="Support forum pcf8574 English"
2020-05-29 12:12:19 +00:00
align="right"></a>
</div>
<div>
2020-06-05 20:02:22 +00:00
<a href="https://www.mischianti.org/it/forums/forum/le-librerie-di-mischianti/pcf8574-expander-digitale-i-o-i2c/"><img
2020-05-30 20:13:22 +00:00
src="https://github.com/xreef/LoRa_E32_Series_Library/raw/master/resources/buttonSupportForumItaliano.png" alt="Forum supporto pcf8574 italiano"
2020-05-29 12:12:19 +00:00
align="right"></a>
</div>
2020-12-11 21:14:31 +00:00
#
#
#
#
#
#
2020-11-18 17:46:42 +00:00
### Additional information and documentation on my site: [pcf8574 Article](https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/).
2020-03-06 20:56:24 +00:00
2020-11-18 17:46:42 +00:00
### If you need more pins [here](https://www.mischianti.org/2019/07/22/pcf8575-i2c-16-bit-digital-i-o-expander/) you can find the pcf8575 16bit version of the IC.
2020-03-06 20:56:24 +00:00
2020-04-11 14:40:51 +00:00
### Version 2.2
2020-03-06 20:56:24 +00:00
2020-11-18 17:46:42 +00:00
Library to use I2C analog IC with arduino and esp8266. Can read and write digital value with only 2 wires (perfect for ESP-01).
2020-03-06 20:56:24 +00:00
Tutorial:
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 DHT library folder your `<arduinosketchfolder>/libraries/` folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
## Changelog
2022-04-27 06:26:53 +00:00
26/04/2022: v2.3.1 Fix example for esp32 and double begin issue #56.
2022-04-07 05:52:03 +00:00
06/04/2022: v2.3.0 Fix package size
30/12/2021: v2.2.4 Minor fix and remove deprecated declaration
23/11/2020: v2.2.2 Add multiple implementation for encoder management (you can enable by uncomment relative define)
2020-03-06 20:56:24 +00:00
# Reef complete PCF8574 PCF8574AP digital input and output expander with i2c bus.
2020-11-18 17:46:42 +00:00
I try to simplify the use of this IC, with a minimal set of operations.
2020-03-06 20:56:24 +00:00
PCF8574P address map 0x20-0x27
PCF8574AP address map 0x38-0x3f
2020-11-18 17:46:42 +00:00
**Constructor:**
Pass the address of I2C (to check the address use this guide [I2cScanner](https://playground.arduino.cc/Main/I2cScanner))
2020-03-06 20:56:24 +00:00
```cpp
PCF8574(uint8_t address);
```
2020-11-18 17:46:42 +00:00
For ESP8266 if you want to specify SDA and SCL pins use this:
2020-03-06 20:56:24 +00:00
```cpp
PCF8574(uint8_t address, uint8_t sda, uint8_t scl);
```
You must set input/output mode:
```cpp
pcf8574.pinMode(P0, OUTPUT);
pcf8574.pinMode(P1, INPUT);
pcf8574.pinMode(P2, INPUT);
```
2020-11-18 17:46:42 +00:00
then IC as you can see in the image has 8 digital input/output ports:
2020-03-06 20:56:24 +00:00
![PCF8574 schema](https://github.com/xreef/PCF8574_library/blob/master/resources/PCF8574-pins.gif)
2020-11-18 17:46:42 +00:00
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):
2020-03-06 20:56:24 +00:00
```cpp
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);
```
2020-11-18 17:46:42 +00:00
To follow a request (you can see It on [issue #5](https://github.com/xreef/PCF8574_library/issues/5)) I create a define variable to work with low memory devices, if you uncomment this line in the .h file of the library:
2020-03-06 20:56:24 +00:00
```cpp
// #define PCF8574_LOW_MEMORY
```
2020-11-18 17:46:42 +00:00
Enable low memory props and gain about 7 bytes of memory, and you must use the method to read all like so:
2020-03-06 20:56:24 +00:00
```cpp
byte di = pcf8574.digitalReadAll();
Serial.print("READ VALUE FROM PCF: ");
Serial.println(di, BIN);
```
2020-11-18 17:46:42 +00:00
where `di` is a byte like 1110001, so you must do a bitwise operation to get the data, operation that I already do in the "normal" mode. For example:
2020-03-06 20:56:24 +00:00
```cpp
p0 = ((di & bit(0))>0)?HIGH:LOW;
p1 = ((di & bit(1))>0)?HIGH:LOW;
p2 = ((di & bit(2))>0)?HIGH:LOW;
p3 = ((di & bit(3))>0)?HIGH:LOW;
p4 = ((di & bit(4))>0)?HIGH:LOW;
p5 = ((di & bit(5))>0)?HIGH:LOW;
p6 = ((di & bit(6))>0)?HIGH:LOW;
p7 = ((di & bit(7))>0)?HIGH:LOW;
```
2020-11-18 17:46:42 +00:00
if you want to read a single input:
2020-03-06 20:56:24 +00:00
```cpp
int p1Digital = PCF8574.digitalRead(P1); // read P1
```
2020-11-18 17:46:42 +00:00
If you want to write a digital value:
2020-03-06 20:56:24 +00:00
```cpp
PCF8574.digitalWrite(P1, HIGH);
```
or:
```cpp
PCF8574.digitalWrite(P1, LOW);
```
2020-11-18 17:46:42 +00:00
You can also use an interrupt pin:
2020-03-06 20:56:24 +00:00
You must initialize the pin and the function to call when interrupt raised from PCF8574
```cpp
2020-11-18 17:46:42 +00:00
// Function interrupt
2020-03-06 20:56:24 +00:00
void keyPressedOnPCF8574();
// Set i2c address
PCF8574 pcf8574(0x39, ARDUINO_UNO_INTERRUPT_PIN, keyPressedOnPCF8574);
```
2020-11-18 17:46:42 +00:00
Remember you can't use Serial or Wire on an interrupt function.
2020-03-06 20:56:24 +00:00
2020-11-18 17:46:42 +00:00
It's better to only set a variable to read on loop:
2020-03-06 20:56:24 +00:00
```cpp
void keyPressedOnPCF8574(){
// Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
keyPressed = true;
}
```
For the examples I use this wire schema on breadboard:
2022-04-07 05:52:03 +00:00
![Breadboard](https://github.com/xreef/PCF8574_library/raw/master/resources/testReadWriteLedButton_bb.png)
2020-03-06 20:56:24 +00:00
2022-04-08 06:40:19 +00:00
https://downloads.arduino.cc/libraries/logs/github.com/xreef/PCF8574_library/