GPIO expander
What this example shows
Blink an LED from a pin of the PCAL6416A GPIO expander. The expander's 16 pins come out on the GPIO EXPANDER header as P0-0 to P0-7 and P1-0 to P1-7. In code, P0-0 to P0-7 are pins 0 to 7 (IO_PIN_A0 to IO_PIN_A7) and P1-0 to P1-7 are pins 8 to 15 (IO_PIN_B0 to IO_PIN_B7).
Wire it up before you upload: connect one side of a 330 Ω resistor to P1-7, the other side to the LED's anode, and the LED's cathode to GND. This value comes from this sketch; the MicroPython GPIO expander example asks for a 300 Ω resistor on P1-5 instead.
Example code
#include "Inkplate.h" //Include Inkplate library to the sketch
// We are going to use pin P1-7 (or GPB7 on older Inkplates).
// Remember! GPA0 = 0, GPA1 = 1, ..., GPA7 = 7, GPB0 = 8, GBP1 = 9, ..., GPB7 = 15.
// And also P0-0 = GPA0, P0-1 = GPA1, ..., P0-7 = GPA7, P1-0 = GPB0, P1-1 = GPB1, ..., P1-7 = GPB7.
#define LED_PIN IO_PIN_B7
Inkplate display; // Create an object on Inkplate library and also set library into 1-bit mode (BW)
void setup()
{
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
display.expander1.pinMode(LED_PIN,
OUTPUT); // Set P1-7 to output. On that pin, we sholud connect LED with current limiting resistor
// If we do not specify which IO expander we want to use, by the default external IO
// expander will be used of the one with header named IO Expander 2.
}
void loop()
{
display.expander1.digitalWrite(LED_PIN, LOW); // Set output to low (LED does not light up)
delay(1000); // Wait for one second
display.expander1.digitalWrite(LED_PIN, HIGH); // Set output to high (LED lights up)
delay(1000); // Wait for one second
}
Some comments in the file were written for other Inkplates: Inkplate 7SPECTRA has no 1-bit mode, and its expander header is labelled GPIO EXPANDER.
Functions used
Inkplate()returns NoneCreates the Inkplate object. Inkplate 7SPECTRA has a single six-color mode, so the constructor takes no mode argument.
ReturnsConstructor; no return value.
display.begin()returns voidInitializes the library and the peripherals, the GPIO expander at I2C address 0x20 included. Call it once in setup().
ReturnsNothing.
display.expander1.pinMode(uint8_t _pin, uint8_t _mode, bool _bypassCheck = false)returns voidSets an expander pin's mode, like the Arduino pinMode().
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | _pin | Expander pin, 0 to 15, here IO_PIN_B7 (P1-7). |
uint8_t | _mode | The Arduino pin mode, here OUTPUT. |
bool | _bypassCheck | Defaults to false. |
display.expander1.digitalWrite(uint8_t _pin, uint8_t _state, bool _bypassCheck = false)returns voidSets an expander output pin HIGH or LOW.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | _pin | Expander pin, 0 to 15. |
uint8_t | _state | HIGH or LOW. |
bool | _bypassCheck | Defaults to false. |
What to expect
The LED on P1-7 blinks: one second off, one second on. The screen does not change, and the sketch prints nothing to the Serial Monitor.
The library uses P1-1 for the battery measurement and P1-2 for the microSD card's supply. Leave those two pins free; see Jumpers.
🔗Open the complete example
examples/Inkplate7SPECTRA/Advanced/IO/Inkplate7SPECTRA_External_IO_Expander