### Initialize Library with begin() Source: https://context7.com/wifwaf/tca9548a/llms.txt Initialize the library with a TwoWire instance. For ESP32 boards, ensure custom SDA/SCL pins are set before calling this method. ```cpp #include #include "TCA9548A.h" TCA9548A I2CMux; void setup() { Serial.begin(9600); // For ESP32 with custom pins (call before begin) // Wire.setPins(21, 22); // SDA, SCL // Initialize with default Wire instance I2CMux.begin(Wire); // Set known initial state I2CMux.closeAll(); Serial.println("TCA9548A initialized"); } void loop() { // I2C multiplexer ready for use } ``` -------------------------------- ### begin(TwoWire &inWire) Source: https://context7.com/wifwaf/tca9548a/llms.txt Initializes the library with a specific TwoWire instance. ```APIDOC ## void begin(TwoWire &inWire) ### Description Initializes the TCA9548A library. Must be called before other functions. ### Parameters #### Request Body - **inWire** (TwoWire&) - Required - The I2C bus instance to use. ``` -------------------------------- ### Open all channels with openAll() Source: https://context7.com/wifwaf/tca9548a/llms.txt Opens all 8 channels simultaneously by writing 0xFF to the control register. Ensure all connected devices have unique I2C addresses to prevent bus conflicts. ```cpp #include #include "TCA9548A.h" TCA9548A I2CMux; void setup() { Serial.begin(9600); I2CMux.begin(Wire); I2CMux.closeAll(); } void loop() { Serial.println("Opening all channels..."); I2CMux.openAll(); Serial.print("Register value: "); Serial.println(I2CMux.readRegister()); // Output: 255 (binary: 11111111) // Warning: Only use openAll() when devices on different channels // have unique I2C addresses to avoid bus conflicts delay(1000); I2CMux.closeAll(); Serial.print("After closeAll: "); Serial.println(I2CMux.readRegister()); // Output: 0 delay(2000); } ``` -------------------------------- ### Initialize TCA9548A Instance Source: https://context7.com/wifwaf/tca9548a/llms.txt Create a multiplexer instance using the default address 0x70 or a custom address based on hardware pin configuration. ```cpp #include #include "TCA9548A.h" // Create instance with default address 0x70 TCA9548A I2CMux; // Create instance with custom address (0x70-0x77 based on A0-A2 pins) TCA9548A I2CMux2(0x71); void setup() { Serial.begin(9600); I2CMux.begin(Wire); I2CMux2.begin(Wire); } ``` -------------------------------- ### Manage Channels with openChannel() Source: https://context7.com/wifwaf/tca9548a/llms.txt Open one or multiple channels simultaneously for parallel device communication. Channels remain open until explicitly closed. ```cpp #include #include "TCA9548A.h" TCA9548A I2CMux; void setup() { Serial.begin(9600); I2CMux.begin(Wire); I2CMux.closeAll(); } void loop() { // Open single channel for device communication I2CMux.openChannel(0); // Communicate with device on channel 0 // ... your I2C device code here ... I2CMux.closeChannel(0); // Open multiple channels simultaneously I2CMux.openChannel(1); I2CMux.openChannel(2); I2CMux.openChannel(3); // All three channels now active // ... communicate with devices on channels 1, 2, and 3 ... I2CMux.closeAll(); delay(1000); } ``` -------------------------------- ### Open All Channels Source: https://context7.com/wifwaf/tca9548a/llms.txt Opens all 8 channels simultaneously. Use with caution as this exposes all connected devices to the I2C bus at once. Ensure devices on different channels have unique I2C addresses to avoid bus conflicts. ```APIDOC ## openAll() ### Description Opens all 8 channels simultaneously by writing 0xFF to the control register. ### Method `openAll()` ### Parameters None ### Request Example ```cpp #include "TCA9548A.h" TCA9548A I2CMux; void setup() { I2CMux.begin(Wire); I2CMux.openAll(); } ``` ### Response None (modifies hardware state) ### Response Example None ``` -------------------------------- ### Reset Channels with closeAll() Source: https://context7.com/wifwaf/tca9548a/llms.txt Close all channels simultaneously to reset the multiplexer to a known state, preventing cross-talk between devices. ```cpp #include #include "TCA9548A.h" TCA9548A I2CMux; void setup() { Serial.begin(9600); I2CMux.begin(Wire); // Always start with a known state I2CMux.closeAll(); Serial.println("All channels closed"); } void loop() { // Scan through each channel individually for (uint8_t channel = 0; channel < 8; channel++) { I2CMux.openChannel(channel); Serial.print("Channel "); Serial.print(channel); Serial.println(" open - scanning for devices..."); // ... perform I2C scan or device communication ... delay(500); } // Reset all channels at end of scan I2CMux.closeAll(); Serial.println("Scan complete, all channels closed\n"); delay(2000); } ``` -------------------------------- ### Channel Constants Source: https://context7.com/wifwaf/tca9548a/llms.txt Predefined hex constants for each channel to use with writeRegister() for direct bitmask operations. ```APIDOC ## Channel Constants ### Description Predefined constants representing each I2C channel on the TCA9548A multiplexer. These are used to construct bitmasks for the `writeRegister()` function. ### Constants - **TCA_CHANNEL_0**: `0x01` (bit 0) - **TCA_CHANNEL_1**: `0x02` (bit 1) - **TCA_CHANNEL_2**: `0x04` (bit 2) - **TCA_CHANNEL_3**: `0x08` (bit 3) - **TCA_CHANNEL_4**: `0x10` (bit 4) - **TCA_CHANNEL_5**: `0x20` (bit 5) - **TCA_CHANNEL_6**: `0x40` (bit 6) - **TCA_CHANNEL_7**: `0x80` (bit 7) ### Usage Example ```cpp #include "TCA9548A.h" TCA9548A I2CMux; // Open channels 1, 3, 5, and 7 uint8_t oddChannels = TCA_CHANNEL_1 | TCA_CHANNEL_3 | TCA_CHANNEL_5 | TCA_CHANNEL_7; I2CMux.writeRegister(oddChannels); ``` ``` -------------------------------- ### openChannel(uint8_t channel) Source: https://context7.com/wifwaf/tca9548a/llms.txt Opens a specific channel on the multiplexer. ```APIDOC ## void openChannel(uint8_t channel) ### Description Opens a specific channel (0-7). Channels remain open until explicitly closed. ### Parameters #### Request Body - **channel** (uint8_t) - Required - The channel index to open (0-7). ``` -------------------------------- ### TCA9548A Constructor Source: https://context7.com/wifwaf/tca9548a/llms.txt Creates a new instance of the TCA9548A multiplexer with an optional custom I2C address. ```APIDOC ## Constructor TCA9548A(uint8_t address) ### Description Creates a TCA9548A multiplexer instance. The default address is 0x70. ### Parameters #### Path Parameters - **address** (uint8_t) - Optional - I2C address of the multiplexer (0x70-0x77). ``` -------------------------------- ### closeAll() Source: https://context7.com/wifwaf/tca9548a/llms.txt Closes all channels simultaneously. ```APIDOC ## void closeAll() ### Description Closes all channels by writing 0x00 to the control register. ``` -------------------------------- ### Selectively Close Channels with closeChannel() Source: https://context7.com/wifwaf/tca9548a/llms.txt Close a specific channel while leaving other active channels unaffected. Use readRegister() to verify the current state of the multiplexer. ```cpp #include #include "TCA9548A.h" TCA9548A I2CMux; void setup() { Serial.begin(9600); I2CMux.begin(Wire); I2CMux.closeAll(); } void loop() { // Open channels 0, 1, and 2 I2CMux.openChannel(0); I2CMux.openChannel(1); I2CMux.openChannel(2); Serial.print("Register after opening 0,1,2: "); Serial.println(I2CMux.readRegister()); // Output: 7 (binary: 00000111) // Close only channel 1, channels 0 and 2 remain open I2CMux.closeChannel(1); Serial.print("Register after closing 1: "); Serial.println(I2CMux.readRegister()); // Output: 5 (binary: 00000101) I2CMux.closeAll(); delay(2000); } ``` -------------------------------- ### Use channel constants for bitmask operations Source: https://context7.com/wifwaf/tca9548a/llms.txt Utilizes predefined hex constants for each channel to perform bitwise operations for channel selection. ```cpp #include #include "TCA9548A.h" // Available channel constants: // TCA_CHANNEL_0 = 0x01 (bit 0) // TCA_CHANNEL_1 = 0x02 (bit 1) // TCA_CHANNEL_2 = 0x04 (bit 2) // TCA_CHANNEL_3 = 0x08 (bit 3) // TCA_CHANNEL_4 = 0x10 (bit 4) // TCA_CHANNEL_5 = 0x20 (bit 5) // TCA_CHANNEL_6 = 0x40 (bit 6) // TCA_CHANNEL_7 = 0x80 (bit 7) TCA9548A I2CMux; void setup() { Serial.begin(9600); I2CMux.begin(Wire); I2CMux.closeAll(); } void loop() { // Example: Open channels 0, 2, 4, and 6 (even channels) uint8_t evenChannels = TCA_CHANNEL_0 | TCA_CHANNEL_2 | TCA_CHANNEL_4 | TCA_CHANNEL_6; I2CMux.writeRegister(evenChannels); Serial.print("Even channels (0,2,4,6): "); Serial.println(I2CMux.readRegister()); // Output: 85 delay(1000); // Example: Open channels 1, 3, 5, and 7 (odd channels) uint8_t oddChannels = TCA_CHANNEL_1 | TCA_CHANNEL_3 | TCA_CHANNEL_5 | TCA_CHANNEL_7; I2CMux.writeRegister(oddChannels); Serial.print("Odd channels (1,3,5,7): "); Serial.println(I2CMux.readRegister()); // Output: 170 delay(1000); I2CMux.closeAll(); delay(2000); } ``` -------------------------------- ### Read control register with readRegister() Source: https://context7.com/wifwaf/tca9548a/llms.txt Reads the current control register value to identify open channels. Returns 255 if a communication error occurs. ```cpp #include #include "TCA9548A.h" TCA9548A I2CMux; void setup() { Serial.begin(9600); I2CMux.begin(Wire); I2CMux.closeAll(); } void loop() { Serial.println("--- Channel State Monitor ---"); // Iterate through all channels for (uint8_t channel = 0; channel < 8; channel++) { I2CMux.openChannel(channel); byte regValue = I2CMux.readRegister(); Serial.print("After opening channel "); Serial.print(channel); Serial.print(": Register = "); Serial.print(regValue); Serial.print(" (binary: "); // Print binary representation for (int bit = 7; bit >= 0; bit--) { Serial.print((regValue >> bit) & 1); } Serial.println(")"); delay(500); } Serial.println("All 8 channels now open"); I2CMux.closeAll(); Serial.println("All channels closed\n"); delay(3000); } ``` -------------------------------- ### Write to control register with writeRegister() Source: https://context7.com/wifwaf/tca9548a/llms.txt Writes a byte value to the control register for precise channel control. Use predefined TCA_CHANNEL_x constants to build the bitmask. ```cpp #include #include "TCA9548A.h" TCA9548A I2CMux; void setup() { Serial.begin(9600); I2CMux.begin(Wire); I2CMux.closeAll(); } void loop() { uint8_t channelMask = 0x00; // Build channel mask using predefined constants // TCA_CHANNEL_0 = 0x01, TCA_CHANNEL_3 = 0x08 // TCA_CHANNEL_4 = 0x10, TCA_CHANNEL_7 = 0x80 channelMask |= TCA_CHANNEL_0; // Add channel 0 channelMask |= TCA_CHANNEL_3; // Add channel 3 channelMask |= TCA_CHANNEL_4; // Add channel 4 channelMask |= TCA_CHANNEL_7; // Add channel 7 Serial.print("Writing channel mask: "); Serial.println(channelMask); // Output: 153 (binary: 10011001) // Write directly to control register I2CMux.writeRegister(channelMask); // Verify the write Serial.print("Register readback: "); Serial.println(I2CMux.readRegister()); // Output: 153 delay(1000); I2CMux.closeAll(); delay(2000); } ``` -------------------------------- ### closeChannel(uint8_t channel) Source: https://context7.com/wifwaf/tca9548a/llms.txt Closes a specific channel on the multiplexer. ```APIDOC ## void closeChannel(uint8_t channel) ### Description Closes a specific channel (0-7) while leaving other open channels unaffected. ### Parameters #### Request Body - **channel** (uint8_t) - Required - The channel index to close (0-7). ``` -------------------------------- ### Write to Control Register Source: https://context7.com/wifwaf/tca9548a/llms.txt Directly writes a byte value to the control register for precise channel control. Each bit corresponds to a channel (bit 0 = channel 0, bit 7 = channel 7). Use the predefined TCA_CHANNEL_x constants for easier bitmask calculation. ```APIDOC ## writeRegister(uint8_t value) ### Description Directly writes a byte value to the control register for precise channel control. Each bit corresponds to a channel (bit 0 = channel 0, bit 7 = channel 7). ### Method `writeRegister(uint8_t value)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **value** (uint8_t) - Required - The byte value to write to the control register. Each bit represents a channel (1 = open, 0 = closed). ### Request Example ```cpp #include "TCA9548A.h" TCA9548A I2CMux; // Example: Open channels 0 and 3 uint8_t channelMask = TCA_CHANNEL_0 | TCA_CHANNEL_3; // 0x01 | 0x08 = 0x09 I2CMux.writeRegister(channelMask); ``` ### Response None (modifies hardware state) ### Response Example None ``` -------------------------------- ### Read Control Register Source: https://context7.com/wifwaf/tca9548a/llms.txt Reads the current value of the control register to determine which channels are currently open. Returns 255 if no data is available (communication error). ```APIDOC ## readRegister() ### Description Reads the current value of the control register to determine which channels are currently open. ### Method `readRegister()` ### Parameters None ### Request Example ```cpp #include "TCA9548A.h" TCA9548A I2CMux; // Assuming channels 0 and 1 are open byte regValue = I2CMux.readRegister(); // Expected value: 3 (binary: 00000011) ``` ### Response #### Success Response (200) - **returnValue** (byte) - The current value of the control register. Returns 255 if a communication error occurs. ### Response Example ```json { "returnValue": 3 } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.