From 19aaf5e28d2423aae70927ff424383b95f6f78b5 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Thu, 27 Oct 2022 16:29:05 -0400 Subject: [PATCH] Add 8575 examples. --- .gitignore | 1 + .../pcf8574_blink8leds.ino | 30 +++++++++++ .../pcf8575_blinkled/pcf8574_blinkled.ino | 27 ++++++++++ .../pcf8574_buttonledirq.ino | 52 +++++++++++++++++++ .../pcf8574_read8buttons.ino | 33 ++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 examples/pcf8575_blink16leds/pcf8574_blink8leds.ino create mode 100644 examples/pcf8575_blinkled/pcf8574_blinkled.ino create mode 100644 examples/pcf8575_buttonledirq/pcf8574_buttonledirq.ino create mode 100644 examples/pcf8575_read16buttons/pcf8574_read8buttons.ino diff --git a/.gitignore b/.gitignore index 8a871e3..cc08faa 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ Doxyfile .vscode/ html/ .DS_Store +.idea \ No newline at end of file diff --git a/examples/pcf8575_blink16leds/pcf8574_blink8leds.ino b/examples/pcf8575_blink16leds/pcf8574_blink8leds.ino new file mode 100644 index 0000000..7e5ac75 --- /dev/null +++ b/examples/pcf8575_blink16leds/pcf8574_blink8leds.ino @@ -0,0 +1,30 @@ +#include + +/* Example for 16 output LEDs that are connected from power to the GPIO expander pins + * Note the LEDs must be connected with the CATHODES to the expander, to SINK current! + * The PCF8575 cannot SOURCE current! + */ + +Adafruit_PCF8574 pcf; + +void setup() { + while (!Serial) { delay(10); } + Serial.begin(115200); + Serial.println("Adafruit PCF8575 LED blink test"); + + if (!pcf.begin(0x20, &Wire)) { + Serial.println("Couldn't find PCF8575"); + while (1); + } + for (uint8_t p=0; p<16; p++) { + pcf.pinMode(p, OUTPUT); + } +} + +void loop() { + for (uint8_t p=0; p<16; p++) { + pcf.digitalWrite(p, LOW); // turn LED on by sinking current to ground + delay(100); + pcf.digitalWrite(p, HIGH); // turn LED off by turning off sinking transistor + } +} diff --git a/examples/pcf8575_blinkled/pcf8574_blinkled.ino b/examples/pcf8575_blinkled/pcf8574_blinkled.ino new file mode 100644 index 0000000..f92b664 --- /dev/null +++ b/examples/pcf8575_blinkled/pcf8574_blinkled.ino @@ -0,0 +1,27 @@ +#include + +/* Example for 1 output LED that is connected from power to the GPIO expander pin #7 + * Note the LEDs must be connected with the CATHODES to the expander, to SINK current! + * The PCF8575 cannot SOURCE current! + */ + +Adafruit_PCF8575 pcf; + +void setup() { + while (!Serial) { delay(10); } + Serial.begin(115200); + Serial.println("Adafruit PCF8575 LED blink test"); + + if (!pcf.begin(0x20, &Wire)) { + Serial.println("Couldn't find PCF8575"); + while (1); + } + pcf.pinMode(7, OUTPUT); +} + +void loop() { + pcf.digitalWrite(7, LOW); // turn LED on by sinking current to ground + delay(100); + pcf.digitalWrite(7, HIGH); // turn LED off by turning off sinking transistor + delay(100); +} diff --git a/examples/pcf8575_buttonledirq/pcf8574_buttonledirq.ino b/examples/pcf8575_buttonledirq/pcf8574_buttonledirq.ino new file mode 100644 index 0000000..7f1646a --- /dev/null +++ b/examples/pcf8575_buttonledirq/pcf8574_buttonledirq.ino @@ -0,0 +1,52 @@ +#include + +/* Example for 1 button that is connected from PCF GPIO #0 to ground, + * and one LED connected from power to PCF GPIO #7 + * We also have the IRQ output connected to an Interrupt input pin on the + * Arduino so we are not constantly polling from the PCF8575 expander + */ + +Adafruit_PCF8575 pcf; + +#define PCF_BUTTON 0 // on the GPIO expander! +#define PCF_LED 7 // on the GPIO expander! + +#define ARDUINO_IRQ 2 // make sure this pin is possible to make IRQ input + +void setup() { + while (!Serial) { delay(10); } + Serial.begin(115200); + Serial.println("Adafruit PCF8575 button/led IRQ test"); + + if (!pcf.begin(0x20, &Wire)) { + Serial.println("Couldn't find PCF8575"); + while (1); + } + + pcf.pinMode(PCF_BUTTON, INPUT_PULLUP); + pcf.pinMode(PCF_LED, OUTPUT); + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, LOW); + + // set up the interrupt pin on IRQ signal toggle + pinMode(ARDUINO_IRQ, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(ARDUINO_IRQ), button_detect, CHANGE); +} + +// We use a flag to make sure we don't enter the interrupt more than once +volatile bool in_irq = false; + +// called when the button is pressed! +void button_detect(void) { + if (in_irq) return; // we are already handling an irq so don't collide! + + in_irq = true; + interrupts(); // Arduino UNO seems to require that we turn on interrupts for I2C to work! + bool val = pcf.digitalRead(PCF_BUTTON); + pcf.digitalWrite(PCF_LED, val); + in_irq = false; +} + +void loop() { + delay(100); // we do nothing here! +} diff --git a/examples/pcf8575_read16buttons/pcf8574_read8buttons.ino b/examples/pcf8575_read16buttons/pcf8574_read8buttons.ino new file mode 100644 index 0000000..d6f5a8c --- /dev/null +++ b/examples/pcf8575_read16buttons/pcf8574_read8buttons.ino @@ -0,0 +1,33 @@ +#include + +/* Example for 16 input buttons that are connected from the GPIO expander pins to ground. + * Note the buttons must be connected with the other side of the switch to GROUND. There is + * a built in pull-up 'resistor' on each input, but no pull-down resistor capability. + */ + +Adafruit_PCF8575 pcf; + +void setup() { + while (!Serial) { delay(10); } + Serial.begin(115200); + Serial.println("Adafruit PCF8575 button read test"); + + if (!pcf.begin(0x20, &Wire)) { + Serial.println("Couldn't find PCF8575"); + while (1); + } + for (uint8_t p=0; p<16; p++) { + pcf.pinMode(p, INPUT_PULLUP); + } +} + +void loop() { + for (uint8_t p=0; p<16; p++) { + if (! pcf.digitalRead(p)) { + Serial.print("Button on GPIO #"); + Serial.print(p); + Serial.println(" pressed!"); + } + } + delay(10); // a short debounce delay +}