I2CRelay/README.md
2025-05-04 09:49:20 +03:00

4.2 KiB
Raw Blame History

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
  2. Features
  3. Hardware Components Used
  4. Installation
  5. Versatility
  6. Usage
  7. API Overview
  8. Example Serial Commands
  9. Notes
  10. Credits
  11. License
  12. 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 motors 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 projects 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. Heres a simple example:

#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
}