Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
slug: /gpio expander pcal6416a breakout/arduino/geting-started
title: PCAL6416AHF - Getting started
sidebar_label: Getting started
id: gpio expander pcal6416a breakout-arduino-1
hide_title: false
---

## Arduino library

To install the Arduino library, you can use the **Arduino library manager** or download it from the GitHub repository:
<QuickLink
title="PCAL6416A GPIO Expander Breakout Arduino library"
description="PCAL6416A Arduino library by Soldered"
url="https://github.com/SolderedElectronics/Soldered-PCAL6416A-IO-Expander-Arduino-Library"
/>

<InfoBox>

**Are you a first-time Arduino user?** For a detailed tutorial on getting started with Arduino, refer to this section of our docs:

<QuickLink
title="Getting started with Arduino"
description="A comprehensive tutorial on how to set up and upload code to an Arduino board for the first time, from scratch!"
url="/arduino/quick-start-guide"
/>

</InfoBox>

---

## Connections

| **NULA DeepSleep** | **PCAL6416A Board** |
| ------------------ | ------------------- |
| Qwiic | Qwiic |





Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
slug: /gpio expander pcal6416a breakout/arduino/initialization
title: PCAL6416AHF - Initialization
sidebar_label: Initialization
id: gpio expander pcal6416a breakout-arduino-2
hide_title: false
---


This page contains a simple example of initializing the PCAL6416A GPIO expander.

---

## Initialization

To use the PCAL6416A GPIO expander, first include the required libraries, create an expander object, and initialize communication in the `setup()` function. You can use the `begin()` function to start I2C communication with the device.

```cpp
// Include required libraries
#include <Wire.h>
#include "PCAL6416A-SOLDERED.h"

// Create PCAL6416A object
PCAL6416A expander;

// Setup function, runs once
void setup()
{
Serial.begin(115200); // Begin serial communication with PC

// Required for NULA DeepSleep Qwiic board
Wire.begin(8, 9);

// Initialize communication with PCAL6416A
expander.begin(0x20);

Serial.println("PCAL6416A initialized.");
}

void loop()
{
}
```

<FunctionDocumentation
functionName="expander.begin(0x20)"
description="Initializes communication with the PCAL6416A GPIO expander over the I2C interface using address 0x20."
returnDescription="Starts communication with the PCAL6416A device."
parameters={[
{
"name": "address",
"type": "uint8_t",
"description": "I2C address of the PCAL6416A device."
}
]}
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
slug: /gpio expander pcal6416a breakout/arduino/readwrite
title: PCAL6416AHF - ReadWrite
sidebar_label: ReadWrite
id: gpio expander pcal6416a breakout-arduino-3
hide_title: false
---

This page contains a simple example demonstrating digital input and output control using the PCAL6416A GPIO expander.

---

## Read and Write

In this code snippet, the `loop()` function continuously reads the state of a pushbutton connected to pin A0. Since A0 is configured with an internal pull-up resistor, the pin reads `LOW` when the button is pressed and `HIGH` when the button is released. Based on the button state, pin A1 is used to turn an LED on or off.

```cpp
void loop()
{
// Read state of button connected to A0
bool buttonState = expander.digitalReadPCAL(PCAL6416A_A0);

Serial.print("State of pin A0 is: ");

if (buttonState == LOW)
{
// Button pressed
Serial.println("LOW - Button Pressed");

// Turn LED ON
expander.digitalWritePCAL(PCAL6416A_A1, HIGH);
}
else
{
// Button released
Serial.println("HIGH - Button Released");

// Turn LED OFF
expander.digitalWritePCAL(PCAL6416A_A1, LOW);
}

delay(100);
}
```

<FunctionDocumentation
functionName="expander.digitalReadPCAL(pin)"
description="Reads the current logic state of a selected GPIO pin on the PCAL6416A expander."
returnDescription="Returns HIGH or LOW depending on the state of the selected input pin."
parameters={[
{
name: "pin",
type: "uint8_t",
description: "The GPIO pin to read. Use a constant such as PCAL6416A_A0-PCAL6416A_A7 or PCAL6416A_B0-PCAL6416A_B7."
}
]}
/>

<FunctionDocumentation
functionName="expander.digitalWritePCAL(pin, value)"
description="Sets the output state of a selected GPIO pin on the PCAL6416A expander."
returnDescription="None."
parameters={[
{
name: "pin",
type: "uint8_t",
description: "The GPIO pin to write. Use a constant such as PCAL6416A_A0-PCAL6416A_A7 or PCAL6416A_B0-PCAL6416A_B7."
},
{
name: "value",
type: "uint8_t",
description: "Output level: HIGH or LOW."
}
]}
/>

<FunctionDocumentation
functionName="expander.pinModePCAL(pin, mode)"
description="Configures a GPIO pin on the PCAL6416A expander as an input or output."
returnDescription="None."
parameters={[
{
name: "pin",
type: "uint8_t",
description: "The GPIO pin to configure. Use a constant such as PCAL6416A_A0-PCAL6416A_A7 or PCAL6416A_B0-PCAL6416A_B7."
},
{
name: "mode",
type: "uint8_t",
description: "Pin mode: INPUT, OUTPUT, or INPUT_PULLUP."
}
]}
/>

| Input pin A0 state | Button state | Output pin A1 state | LED state |
| :----------------: | :----------: | :-----------------: | :-------: |
| LOW | Pressed | HIGH | ON |
| HIGH | Released | LOW | OFF |

---

## Full example

Connect a pushbutton between A0 and GND, and connect an LED with a resistor to A1 and GND. Open the Serial Monitor at 115200 baud to observe the input state.

```cpp
// Include required libraries
#include <Wire.h>
#include "PCAL6416A-SOLDERED.h"

// Create PCAL6416A object
PCAL6416A expander;

void setup()
{
Serial.begin(115200); // Start serial communication with PC

// Required for ESP32-C6 Qwiic board
Wire.begin(6, 7);

// Initialize PCAL6416A GPIO Expander
expander.begin(0x20);

// Set A0 as input with internal pull-up resistor
expander.pinModePCAL(PCAL6416A_A0, INPUT_PULLUP);

// Set A1 as output
expander.pinModePCAL(PCAL6416A_A1, OUTPUT);
}

void loop()
{
// Read state of button connected to A0
bool buttonState = expander.digitalReadPCAL(PCAL6416A_A0);

Serial.print("State of pin A0 is: ");

if (buttonState == LOW)
{
// Button pressed
Serial.println("LOW - Button Pressed");

// Turn LED ON
expander.digitalWritePCAL(PCAL6416A_A1, HIGH);
}
else
{
// Button released
Serial.println("HIGH - Button Released");

// Turn LED OFF
expander.digitalWritePCAL(PCAL6416A_A1, LOW);
}

delay(100);
}
```

<CenteredImage src="/img/pcal6416a/ReadWrite.JPG" caption="Read and Write Connection"/>

<CenteredImage src="/img/pcal6416a/ReadWrite_test.png" alt="Serial Monitor" caption="Read and Write Serial Monitor output"/>


<QuickLink
title="PCAL6416A examples"
description="Arduino examples for the PCAL6416A GPIO expander"
url="https://github.com/SolderedElectronics/Soldered-PCAL6416A-IO-Expander-Arduino-Library/tree/main/examples"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
slug: /gpio expander pcal6416a breakout/arduino/troubleshooting
title: PCAL6416AHF - Troubleshooting
sidebar_label: Troubleshooting
id: gpio expander pcal6416a breakout-arduino-4
hide_title: false
pagination_next: null
---
This page contains some tips if you are experiencing problems with the PCAL6416A GPIO expander.

<ExpandableSection title="My expander won't initialize!">

#### Check wiring
Ensure that your Qwiic cable is properly connected and in good condition. Try using the same cable with another Qwiic-compatible device to verify that it works.

#### Check I2C pins
If you are connecting the board using standard I2C pins, double-check that SDA and SCL are connected to the correct pins on your microcontroller.

#### Scan for I2C devices
Run an [**I2C scanner sketch**](https://github.com/SolderedElectronics/Soldered-Hacky-Codes/tree/main/I2C_Scanner) to check if the PCAL6416A is detected on the I2C bus.

#### Check I2C address
Make sure the address used in code matches the detected address. For example:

```cpp
expander.begin(0x20);
```

</ExpandableSection>

<ExpandableSection title="My input or output pin is not working!">

#### Check pin mode
Make sure the pin is configured correctly before reading or writing:

```cpp
expander.pinModePCAL(PCAL6416A_A0, INPUT_PULLUP);
expander.pinModePCAL(PCAL6416A_A1, OUTPUT);
```

#### Check your wiring
For button examples, connect the pushbutton between the input pin and GND.
For LED examples, connect the LED with a resistor to the output pin and GND.

#### Check pull-up settings
If using a button, enable `INPUT_PULLUP` so the input has a stable state when the button is not pressed.

</ExpandableSection>

<ExpandableSection title="Other common issues">

#### The button always reads HIGH or LOW
Check that the button is connected to the correct GPIO pin and GND. If using `INPUT_PULLUP`, the pin should read `HIGH` when released and `LOW` when pressed.

#### The LED does not turn on
Check LED polarity, the resistor connection, and whether the selected pin is configured as `OUTPUT`.

#### I2C communication is unstable
Check cable length, loose connections, and whether the I2C pull-up jumpers are configured correctly.

#### The wrong device is responding
If multiple I2C devices are connected, make sure no other device uses the same I2C address.

</ExpandableSection>

<InfoBox>In case you haven't found the answer to your question, please **contact us** via [**this**](https://soldered.com/contact/) link.</InfoBox>

Loading