# I2C Communication Library **I2CRelay** is an 8-relay module built around the ESP32-C3 Super Mini, featuring an I2C interface that allows it to be controlled by another MCU, enabling easy integration into complex control systems. Additionally, when paired with an ESP32-C3-based controller, it can leverage Wi-Fi and Bluetooth LE connectivity, expanding control options beyond wired I2C communication for wireless operation and enhanced flexibility in IoT and robotics applications. This combination of compact size, low power consumption, and versatile communication protocols makes the module indispensable for remote and automated control of multiple devices, offering scalable and reliable switching solutions for smart homes, industrial automation, and advanced robotics. ## Table of Contents 1. [Technical Drawings](#technical_drawings) 2. [Features](#Features) 3. [Hardware Components Used](#hardware_components_used) 4. [Installation](#installation) 5. [Versatility](#versatility) 6. [Usage](#usage) 7. [API Overview](#api-overview) 8. [Example Serial Commands](#example-serial-commands) 9. [Notes](#notes) 10. [Credits](#credits) 11. [License](#license) 12. [Project Image](#project-image) --- ### Technical Drawings --- ### Features ..... --- ### Hardware Components Used - 8 Relay Module. - Esp32C3 Super Mini. - Flywheel. --- ### Installation ## Files The library is composed of several files, each with a specific role: 1. **I2CMasterUtils.h** [3] - This header file defines the `I2CMasterUtils` class which contains static methods for I2C operations such as scanning for devices and changing slave addresses. 2. **I2CMasterUtils.cpp** [2] - Contains the implementation of methods defined in `I2CMasterUtils.h`. 3. **I2CRelay.h** [5] - Defines the `I2CRelay` class, which extends the functionality to control relays over I2C. 4. **I2CRelay.cpp** [4] - Contains the implementation of methods for the `I2CRelay` class. --- ### Versatility The I2CMotor offers exceptional versatility, as it can function as a standalone device and can be adapted to be USB-controlled instead of relying solely on I2C communication. This flexibility allows users to easily integrate the motor into a wide range of projects and control environments. Furthermore, the motor’s firmware can be reprogrammed to support Wi-Fi and Bluetooth connectivity, enabling wireless control and making it ideal for modern applications in robotics, automation, and IoT. This adaptability ensures that the I2CMotor can meet diverse requirements and evolve with your project’s needs. --- ### API Overview - **scanI2C(Stream &output = Serial)** - Scans the I2C bus for connected devices and prints their addresses. - **changeSlaveAddress(uint8_t oldAddr, uint8_t newAddr, Stream &output = Serial)** - Sends a command to change the slave address of a device. - **parseChangeAddressCommand(const String &input, uint8_t &oldAddr, uint8_t &newAddr)** - Parses an input string to extract the old and new slave addresses from a "ch" command. #### Methods - **I2CRelay(uint8_t slaveAddress)** - Constructor that initializes the `I2CRelay` object with the given slave address. - **begin()** - Initializes the I2C bus for communication. - **setRelay(uint8_t relayIndex, bool state)** - Sets the state of a specific relay on the slave device. - **scanDevices(uint8_t* foundAddresses, uint8_t maxDevices)** - Scans the I2C bus for connected devices and returns their addresses up to `maxDevices`. - **setAddress(uint8_t newAddress)** - Changes the address of the relay slave. - **handleI2CCommand()** - Handles incoming I2C commands. ## Usage Example To use this library, you would typically include the necessary files and create instances of `I2CMasterUtils` and `I2CRelay`. Here’s a simple example: ```cpp #include "I2CMasterUtils.h" #include "I2CRelay.h" void setup() { Serial.begin(9600); I2CMasterUtils::scanI2C(); uint8_t newAddress = 0x54; uint8_t oldAddress = CMD_CHANGE_ADDR; // Assuming CMD_CHANGE_ADDR is defined in I2CCommands.h I2CMasterUtils::changeSlaveAddress(oldAddress, newAddress); } void loop() { // Example usage of I2CRelay I2CRelay relay(0x54); // Use the newly assigned address relay.begin(); relay.setRelay(1, HIGH); // Set relay 1 to ON }