Code in Arduino IDE for Nano I2C Protocol with 6 TIC Motor Drivers

I2C Protocol, which stands for Inter-Integrated Circuit [3], is an excellent way for beginners to make useful connections between their motor drivers and Arduino microcontroller. The protocol relies on a common ground and two pins: signal clock (SCL) and signal data (SDA). There are two main steps.

  1. Hardware: SDA and SCL are considered open-drain or open-collector, meaning the devices can drive the SDA and SCL lines either high or low [5]. The I2C bus can transmit information via these lines [2]. Multiple master devices can control multiple slave devices (adding complexity, although the I2C hardware setup remains simple) [1]. Two wires is needed to create the setup, however, the hardware itself must be more sophisticated. For example, many microcontrollers have the pullup resistors built in, making I2C wiring convenient and out-of-the-box ready.
  2. Code: The code below is a sample of how to operate the ArmBot with Arduino IDE. Its movement commands are shown in Figure 1 below. Arduino provides a Wire Library to enable I2C synchronous protocol code to run properly [4]. Their sample code includes sending data, receiving data, requesting data, and a few interesting examples, such as controlling a potentiometer and reading an ultra-sonic range finder [4].

Figure 1: Demonstration of the I2C code uploaded to Arduino Nano via Arduino IDE.

A future step for intermediate to advanced coders includes modifying the below code. Notice that each motor driver is initialized with a different value (14-19). This can be changed in the Tic Control Center, downloaded via Pololu [6]. 

#include

TicI2C tic1(14);
TicI2C tic2(15);
TicI2C tic3(16);
TicI2C tic4(17);
TicI2C tic5(18);
TicI2C tic6(19);

float Maxpos = 100;
float minpos = -100;

float Maxspeed = 2000000;
float minspeed = -2000000;

int x = 0;
int flag = 0;
float y = 0;

float myspeed = 0.;

float spdnow;

void setup()
{
// Set up I2C.
Wire.begin();
delay(20);

tic1.exitSafeStart();
tic2.exitSafeStart();
tic3.exitSafeStart();
tic4.exitSafeStart();
tic5.exitSafeStart();
tic6.exitSafeStart();

pinMode(6, INPUT_PULLUP);
Serial.begin(9600);
}

void resetCommandTimeout()
{
tic1.resetCommandTimeout();
tic2.resetCommandTimeout();
tic3.resetCommandTimeout();
tic4.resetCommandTimeout();
tic5.resetCommandTimeout();
tic6.resetCommandTimeout();
}

// Delays for the specified number of milliseconds while
// resetting the Tic’s command timeout so that its movement does
// not get interrupted by errors.
void delayWhileResettingCommandTimeout(uint32_t ms)
{
uint32_t start = millis();
do
{
resetCommandTimeout();
} while ((uint32_t)(millis() – start) <= ms);
}

void loop(){

// all motors work together

// travel in one direction, half-speed, for a second
tic1.setTargetVelocity(-1000000);
tic2.setTargetVelocity(-1000000);
tic3.setTargetVelocity(-1000000);
tic4.setTargetVelocity(-1000000);
tic5.setTargetVelocity(-1000000);
tic6.setTargetVelocity(-1000000);
delayWhileResettingCommandTimeout(1000);

// stop for a second
tic1.setTargetVelocity(0);
tic2.setTargetVelocity(0);
tic3.setTargetVelocity(0);
tic4.setTargetVelocity(0);
tic5.setTargetVelocity(0);
tic6.setTargetVelocity(0);
delayWhileResettingCommandTimeout(1000);

// travel in opposite direction, half-speed, for a second
tic1.setTargetVelocity(1000000);
tic2.setTargetVelocity(1000000);
tic3.setTargetVelocity(1000000);
tic4.setTargetVelocity(1000000);
tic5.setTargetVelocity(1000000);
tic6.setTargetVelocity(1000000);
delayWhileResettingCommandTimeout(1000);

// turn off again for a second
tic1.setTargetVelocity(0);
tic2.setTargetVelocity(0);
tic3.setTargetVelocity(0);
tic4.setTargetVelocity(0);
tic5.setTargetVelocity(0);
tic6.setTargetVelocity(0);
delayWhileResettingCommandTimeout(1000);

}

References

[1] Campbell, Scott (2016). BASICS OF THE I2C COMMUNICATION PROTOCOL. Circuit Basics. https://www.circuitbasics.com/basics-of-the-i2c-communication-protocol/

[2] How I2C Hardware Works. I2C Bus. https://www.i2c-bus.org/i2c-primer/how-i2c-hardware-works/

[3] I2C – What’s That?  I2C Bus. i2c-bus.org

[4] Master Reader/Slave Sender. Arduino. https://www.arduino.cc/en/Tutorial/LibraryExamples/MasterReader

[5] Sparkfun. I2C. Sparkfun Start Something. https://learn.sparkfun.com/tutorials/i2c/all

[6] Tic Software and Drivers for Windows. Pololu. https://www.pololu.com/docs/0J71/3.1