Wiring

Connecting the AW9523 breakout to your Feather or Arduino is easy:

  • If you are running a Feather (3.3V), connect Feather 3V to board VIN
  • If you are running a 5V Arduino (Uno, etc.), connect Arduino 5V to board VIN
  • Connect Feather or Arduino GND to board GND
  • Connect Feather or Arduino SCL to board SCL
  • Connect Feather or Arduino SDA to board SDA

The final results should resemble the illustration above, showing an Adafruit Metro development board.

Installation

You can install the Adafruit AS9523 Library for Arduino using the Library Manager in the Arduino IDE:

Click the Manage Libraries ... menu item, search for Adafruit AW9523, and select the Adafruit AW9523 library:

Then follow the same process for the Adafruit BusIO library.

Load Example

There are three different examples for the Adafruit AW9523 in Arduino.

Blink

This example, unsurprisingly, blinks a single GPIO output pin. It's a good demo for setting a pin to be an output and changing the state

In this example, connect an LED + resistor to the expander pin labeled 0

Connect the anode (positive) leg of the LED to the 0 pad, then the cathode (negative) leg of the LED to a ~ 1K ohm resistor and the other end of the resistor to ground.

#include <Adafruit_AW9523.h>

Adafruit_AW9523 aw;

uint8_t LedPin = 0;  // 0 thru 15

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(1);  // wait for serial port to open
  
  Serial.println("Adafruit AW9523 GPIO Expander test!");

  if (! aw.begin(0x58)) {
    Serial.println("AW9523 not found? Check wiring!");
    while (1) delay(10);  // halt forever
  }

  Serial.println("AW9523 found!");
  aw.pinMode(LedPin, OUTPUT);
}


void loop() {
  aw.digitalWrite(LedPin, HIGH);
  delay(100);
  aw.digitalWrite(LedPin, LOW);
  delay(100);
}

The LED connected to pin 0 will blink every 100 milliseconds!

Button Input and LED Output

This example shows how you can create an input on the expander. Note that this expander does not support internal pull resistors

Keep the LED wired up on pin 0, with a resistor to ground, this is the output

Connect a tactile switch with one side connected to pin 1 of the expander. Add a ~10K pull-up resistor on the expander pin 1 to Vin. Connect the other side of the button to ground. This makes it so the button pin is HIGH by default, and when you press the button, the pin goes LOW

Open up File -> Examples -> Adafruit AW9523 -> ledbutton_demo and upload to your Arduino wired up to the breakout.

#include <Adafruit_AW9523.h>

Adafruit_AW9523 aw;

uint8_t LedPin = 0;
uint8_t ButtonPin = 1;

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(1);  // wait for serial port to open
  
  Serial.println("Adafruit AW9523 Button + LED test!");

  if (! aw.begin(0x58)) {
    Serial.println("AW9523 not found? Check wiring!");
    while (1) delay(10);  // halt forever
  }

  Serial.println("AW9523 found!");
  aw.pinMode(LedPin, OUTPUT);
  aw.pinMode(ButtonPin, INPUT);
  aw.enableInterrupt(ButtonPin, true);
}


void loop() {
  aw.digitalWrite(LedPin, aw.digitalRead(ButtonPin));

  delay(10);
}

This line in the main loop does all the work:

aw.digitalWrite(LedPin, aw.digitalRead(ButtonPin));

We read the button pin, and whatever the logic value is, sets that to the LED output. So when the button is not pressed, the LED is on (HIGH). When the button is pressed, the LED is off (LOW)

You can also see we enable the interrupt pin to listen to the button, you can use the INT pin to then monitor for 'changes' without performing an I2C read of the expander

Constant current LED fade demo

This example shows how to set up a pin to use it as a constant-current driver for an LED. This is great for flicker-free LED driving, and consistent brightness no matter what the voltage is

In this example, connect an LED to the expander pin labeled 0

Connect the cathode (negative) leg of the LED to the 0 pad, then the anode (positive) to power

Note that you do not need a resistor with the LED because we are limited the current internally. However, there's no harm if the LED has a resistor.

Constant current mode is open drain sink ONLY. That means you must connect LED negative sides to the pin and the positive sides to power - this is opposite from how we normally drive LEDs from a GPIO pin
#include <Adafruit_AW9523.h>

Adafruit_AW9523 aw;

uint8_t LedPin = 0;  // 0 thru 15

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(1);  // wait for serial port to open
  
  Serial.println("Adafruit AW9523 Constant Current LED test!");

  if (! aw.begin(0x58)) {
    Serial.println("AW9523 not found? Check wiring!");
    while (1) delay(10);  // halt forever
  }

  Serial.println("AW9523 found!");
  aw.pinMode(LedPin, AW9523_LED_MODE); // set to constant current drive!
}


uint8_t x = 0;

void loop() {
  // Loop from 0 to 255 and then wrap around to 0 again
  aw.analogWrite(LedPin, x++);
  delay(10);
}

Open up File -> Examples -> Adafruit AW9523 -> constcurrent_demo and upload to your Arduino wired up to the breakout.

The aw.analogWrite(LedPin, x++); line will cause the LED on pin 0 to fade up from 0 (no current) to 255 (max current) Then once at maximum 255, the x variable will wrap around and start again from 0 so you'll see it 'pulse'

This guide was first published on Feb 10, 2021. It was last updated on Mar 27, 2021.

This page (Arduino) was last updated on Mar 05, 2023.

Text editor powered by tinymce.