SPRING SALE up to 20% OFF! (excludes trade customers)   •   FREE Standard Delivery for orders over £35 (ex VAT)
Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

Knowledge Base

I2C Switch Library for C++

I2C Switch C++ Library Installation and Usage

Created 29/07/2024 | Last Updated: 19/06/2025

YOU WILL NEED THIS BOARD

215 available
Order a I2C Switch Photo of I2C Switch

I2C Switch

4 Channel PCA9546 I2C Multiplexer for the Raspberry Pi and Single-Board Computers

£5.59 ex VAT

C++ Library to use with I2C Switch Raspberry Pi development board.

The example C++ files can be found in /ABElectronics_CPP_Libraries/I2CSwitch/demos

Downloading and Installing the library

To download to your Raspberry Pi type in the terminal:

git clone https://github.com/abelectronicsuk/ABElectronics_CPP_Libraries.git

Class:

I2CSwitch(uint8_t address)

The I2CSwitch class provides control over the I2C Switch outputs on the PCA9546A controller. Methods include setting and getting the I2C channel and resetting the switch.

Parameters: address - Initialise with the I2C address for the I2C Switch. Supported I2C addresses are 0x70 to 0x77. The default address is 0x70.
Returns: null

Methods:

switch_channel(uint8_t channel) 

Switch on the selected channel and switch off all other channels.
Parameters: channel - 1 to 4.
Returns: null


set_channel_state(uint8_t channel, uint8_t state) 

Set the state for the selected channel. All other channels remain in their previous state.
Parameter: channel - 1 to 4
Parameter: state - 1 or 0. 1 = channel on, 0 = channel off.
Returns: null


get_channel_state(uint8_t channel) 

Get the state for the selected channel.
Parameters: channel - 1 to 4
Returns: (uint8_t) 1 or 0. 1 = channel on, 0 = channel off.


reset() 

Reset the PCA9546A I2C switch. Resetting allows the PCA9546A to recover from a situation in which one of the downstream I2C buses is stuck in a low state. All channels will be set to an off-state.
Returns: null

Also useful for your Raspberry Pi project

Temperature & Sensing

1 Wire Pi Plus

Connect dozens of 1-Wire sensors - temperature, iButtons, EEPROMs - via a single GPIO pin. Stacks directly on the 40-pin header.

£9.59 ex VAT

Analogue I/O

ADC Pi

Read up to 8 analogue inputs - perfect for pairing with your temperature sensors or other analogue-output devices.

£14.39 ex VAT

All-in-one

Expander Pi

Combines ADC, DAC, 32 GPIO ports and a real-time clock on one board. The most versatile board for complex Raspberry Pi projects.

£15.99 ex VAT

Order these Boards

All boards are designed & manufactured in the UK with RoHS-compliant components.