Module pca9539::expander

source ·
Expand description

Abstraction of PCA9539

Central part of this crate is the struct PCA9539, which either allows central I/O control or or alternatively offers a breakdown into individual pins.

The following examples demonstrates central I/O control. For getting separate pin instances, see the pins module.


PCA9539 instance is created using a I2CBus implementing the I2C traits of embedded-hal.

 use pca9539::example::DummyI2CBus;
 use pca9539::expander::PCA9539;

 let i2c_bus = DummyI2CBus::default();
 // Assuming I2C device address 0x74
 let expander = PCA9539::new(i2c_bus, 0x74);

Changing mode

// Switch Pin02 to input mode
expander.set_mode(Bank0, Pin2, Input).unwrap();

// Switch Pin14 to output mode
expander.set_mode(Bank1, Pin4, Output).unwrap();

Reading input state

let is_high = expander.is_pin_input_high(Bank0, Pin1);


Setting output state

expander.set_mode(Bank0, Pin1, Output);

expander.set_state(Bank0, Pin1, true);

let is_high = expander.is_pin_output_high(Bank0, Pin1);

Invert input polarity

PCA9539 has built-in hardware support for inverting input state. See datasheet for more details.

expander.reverse_polarity(Bank0, Pin3, true).unwrap();



  • GPIO bank. PCA9539 has two with 7 pins each
  • GPIO mode
  • GPIO pin ID. Builds together with bank an unique pin identification.
  • Wrapped I2C error when refreshing input state Reading input state consists of one write, followed by a read operation