I2CMotor/README.md
2025-05-12 10:04:11 +03:00

7.6 KiB
Raw Blame History

I2CMotor Arduino Library

I2CMotor is an all-in-one packed motor with I2C connection that offers a compact solution for precise motor control, allowing you to easily adjust both speed and direction via simple I2C commands. The ability to change the slave address means you can connect multiple motors to the same I2C bus and control them independently, making it highly scalable for robotics or automation projects. This product simplifies wiring, reduces microcontroller pin usage, and enables seamless integration and expansion in complex systems, all while providing reliable and configurable motor management in a single module. Additionally, when paired with an ESP32-C3-based controller, it can be used through Wi-Fi and Bluetooth connectivity, expanding control options beyond wired I2C communication for wireless operation and enhanced flexibility in IoT and robotics applications.

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

image

download STEP file


Features

  • Vertical Load Handling: Utilizes a high-quality thrust bearing to efficiently support vertical (axial) loads, ensuring reliable performance in demanding applications.
  • Donut Shape: Innovative donut-shaped design allows for pass-through of wires or shafts, simplifying cable management and enabling unique integration options.
  • I2C Connectivity: Supports I2C communication for easy and precise control of motor speed and direction using simple digital commands.
  • Configurable Slave Address: Allows the slave address to be changed, enabling multiple motors to operate independently on the same I2C bus.
  • All-in-One Design: Combines motor, controller, and interface in a single compact unit, reducing wiring complexity and saving space.
  • Scalable and Versatile: Ideal for robotics, automation, and projects requiring multiple motors or compact, integrated solutions.
  • Easy Integration: Streamlined installation process with minimal wiring and configuration needed, perfect for both prototyping and production environments.

Hardware List

Component Purpose
ESP32C3 Super Mini Stamp size microcontroller with BLE and WiFi Connectivity
L298n Dual H-Bridge With Digital Control - For DC Motor Control
Standard DC Motor Standard Brushed DC Motor With Gear Box
Magnetic Encoder Optional - to leverage the use of a motor to work as a servo

Installation

  1. Download or clone this repository.
  2. Copy the I2CMotor, and related library folders to your Arduino libraries directory.
  3. Restart the Arduino IDE.

File Overview

File/Folder Purpose
I2CMotor.h/.cpp Arduino class for controlling a DC motor via I2C
Master.ino Example sketch for I2C master (controller)
Slave.ino Example sketch for I2C slave (motor driver)
keywords.txt Arduino IDE syntax highlighting
fablab_motor.jpg Project image

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.


Usage

1. Include the Library

#include "I2CMotor.h"

2. Master Example

  • Upload Master.ino to your Arduino master device.

  • Use the following functions to control and configure your motor:

    • motor.identify();
      Scan and identify the connected motor.

    • motor.setDeviceName("Name");
      Assign a new name to the motor.

    • motor.setMinMax(Min, Max);
      Set the minimum and maximum speed limits for the motor.

    • motor.setMotorDirection(MotorNum, Direction);
      *Set the rotation direction for a specific motor.

      • MotorNum: The number of the motor you want to control.
      • Direction: Use 1 for clockwise, 2 for counter-clockwise.*
    • motor.setMotorSpeed(MotorNum, Speed);
      *Set the speed for a specific motor.

      • MotorNum: The number of the motor you want to control.
      • Speed: Desired speed value.*

3. Slave Example

  • Upload Slave.ino to your Arduino slave device.
  • The slave listens for I2C commands to set motor speed/direction or change its own I2C address (persistently via EEPROM).

API Overview

Function Description Parameters Return Type
I2CMotor() Default constructor.
I2CMotor(uint8_t sda, uint8_t scl, uint32_t freq) Constructor with I2C pin and frequency configuration. sda (I2C SDA pin), scl (I2C SCL pin), freq (I2C frequency)
void begin() Initializes I2C communication and scans for the motor controller.
bool found() Checks if a motor controller was found. bool
uint8_t address() Returns the detected motor controller I2C address. uint8_t
String identify() Requests and returns the identification string from the motor controller. String
void setDeviceName(const char* newname) Sets a new name for the motor controller. newname (C-string)
void setMinMax(uint8_t min_pwm, uint8_t max_pwm) Sets minimum and maximum PWM (speed) values. min_pwm, max_pwm
void setMotorDirection(uint8_t motor, uint8_t dir) Sets direction for a specified motor. motor (motor number), dir (1: CW, 2: CCW)
void setMotorSpeed(uint8_t motor, uint8_t speed_percent) Sets speed (as percentage) for a specified motor. motor (motor number), speed_percent
void emergencyStop() Sends an emergency stop command to the motor controller.
uint8_t scanForMotorController() Scans the I2C bus for a compatible motor controller and returns its address. uint8_t
String queryIdentification(uint8_t addr) Requests and returns identification string for a given I2C address. addr (I2C address) String

Notes

  • The default I2C address is 0x08. You can change it using the provided command.
  • The slave stores its address in EEPROM and will remember it after reset.
  • Modify pin definitions in Slave.ino as needed for your hardware.

Credits

Developed by FABLAB Bahrain.


License

This project is open-source; see the LICENSE file for details.


Project Image