Introduction: Arduino - Grove I2C Motor Driver

Recently I got a few Grove sensors, and as always I tried wiring it up with the Arduino and the Particle Core. Grove sensors are a series of senors form Seeed Studio, they have a wide range of sensors and components and I'm going to show you how to work with a few of these senors and components.

In this instructable I'm going to show you how to connect a Grove I2C Motor Driver, this is a two channel motor driver and communicates to the Arduino via I2C. This project serves as a starter to a more complicated robot project which I will post soon.

So lets get started....

Step 1: Tools and Components

Here are a list of components to gets started with the project -

  • Arduino UNO
  • Grove I2C Motor Shield
  • Motor (3-15v)
  • Suitable power supply to power the motor
  • Jumper wires

Note- No soldering skills are required to build this project, but it is good to know how to solder there are a good soldering tutorials on YouTube that can help you get started.

Step 2: Getting Started

The Grove I2C Motor Driver, has a dual channel H-Bridge (L298) which can handle 2A per channel. Controlled by an Atmel ATmega8L which handles the I2C communication.

I2C is a is 2 pin communication system the 2 lines are the SDA (data line) and SCL (clock line), this reduces the number of pins of the Arduino dedicated to drive the motor by connecting it directly to a L298 IC. There is a good documentation of Ardino I2C in the Arduino website, you can read to that to get started with I2C.

Step 3: Hardware

Now time for the connections the Arduino has the SCL line on analog pin 5 and SDA line on analog pin 4.

Here is how you need to connect he hardware -

  • Arduino analog pin 5 - Grove I2C Motor Driver SCL
  • Arduino analog pin 4 - Grove I2C Motor Driver SDA
  • Arduino +5V - Grove I2C Motor Driver VCC
  • Arduino Gnd - Grove I2C Motor Driver Gnd
  • Power Supply + - Grove I2C Motor Driver VS
  • Power Supply Gnd - Grove I2C Motor Driver Gnd

The Motor can be connected at Output 1 and 2 and if you are using 2 motors the other motor goes at output 3 and 4.

After you connect the hardware it is time to upload the code...

Step 4: Code

The code to be uploaded is a test sketch it just makes the motor run in one direction and then after some delay reverses the direction.

This is just a test sketch and if you need a specific task, leave a comment below and I will be happy to write the code for you.

#include

#define MotorSpeedSet 0x82
#define PWMFrequenceSet 0x84 #define DirectionSet 0xaa #define MotorSetA 0xa1 #define MotorSetB 0xa5 #define Nothing 0x01 #define EnableStepper 0x1a #define UnenableStepper 0x1b #define Stepernu 0x1c #define I2CMotorDriverAdd 0x0f // Set the address of the I2CMotorDriver // set the steps you want, if 255, the stepper will rotate continuely; void SteperStepset(unsigned char stepnu) { Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd Wire.write(Stepernu); // Send the stepernu command Wire.write(stepnu); // send the steps Wire.write(Nothing); // send nothing Wire.endTransmission(); // stop transmitting } /////////////////////////////////////////////////////////////////////////////// // Enanble the i2c motor driver to drive a 4-wire stepper. the i2c motor driver will //driver a 4-wire with 8 polarity . //Direction: stepper direction ; 1/0 //motor speed: defines the time interval the i2C motor driver change it output to drive the stepper //the actul interval time is : motorspeed * 4ms. that is , when motor speed is 10, the interval time //would be 40 ms ////////////////////////////////////////////////////////////////////////////////// void StepperMotorEnable(unsigned char Direction, unsigned char motorspeed) { Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd Wire.write(EnableStepper); // set pwm header Wire.write(Direction); // send pwma Wire.write(motorspeed); // send pwmb Wire.endTransmission(); // stop transmitting } //function to uneanble i2C motor drive to drive the stepper. void StepperMotorUnenable() { Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd Wire.write(UnenableStepper); // set unenable commmand Wire.write(Nothing); Wire.write(Nothing); Wire.endTransmission(); // stop transmitting } ////////////////////////////////////////////////////////////////////// //Function to set the 2 DC motor speed //motorSpeedA : the DC motor A speed; should be 0~100; //motorSpeedB: the DC motor B speed; should be 0~100;

void MotorSpeedSetAB(unsigned char MotorSpeedA , unsigned char MotorSpeedB) { MotorSpeedA=map(MotorSpeedA,0,100,0,255); MotorSpeedB=map(MotorSpeedB,0,100,0,255); Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd Wire.write(MotorSpeedSet); // set pwm header Wire.write(MotorSpeedA); // send pwma Wire.write(MotorSpeedB); // send pwmb Wire.endTransmission(); // stop transmitting } //set the prescale frequency of PWM, 0x03 default; void MotorPWMFrequenceSet(unsigned char Frequence) { Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd Wire.write(PWMFrequenceSet); // set frequence header Wire.write(Frequence); // send frequence Wire.write(Nothing); // need to send this byte as the third byte(no meaning) Wire.endTransmission(); // stop transmitting } //set the direction of DC motor. void MotorDirectionSet(unsigned char Direction) { // Adjust the direction of the motors 0b0000 I4 I3 I2 I1 Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd Wire.write(DirectionSet); // Direction control header Wire.write(Direction); // send direction control information Wire.write(Nothing); // need to send this byte as the third byte(no meaning) Wire.endTransmission(); // stop transmitting }

void MotorDriectionAndSpeedSet(unsigned char Direction,unsigned char MotorSpeedA,unsigned char MotorSpeedB) { //you can adjust the driection and speed together MotorDirectionSet(Direction); MotorSpeedSetAB(MotorSpeedA,MotorSpeedB); } void stepperrun() { Serial.println("sent command to + direction, very fast"); SteperStepset(255); StepperMotorEnable(1, 1);// ennable the i2c motor driver a stepper. delay(5000); Serial.println("sent command to - direction, slow"); SteperStepset(255); StepperMotorEnable(0, 20); delay(5000); Serial.println("sent command to - direction, fast"); StepperMotorEnable(0, 2);// ennable the i2c motor driver a stepper. delay(5000); Serial.println("sent command to + direction,100 steps, fast"); SteperStepset(100); StepperMotorEnable(1,5); delay(3000);

Serial.println("sent command to shut down the stepper"); StepperMotorUnenable(); delay(1000); Serial.println("sent command to - direction, slow, and 10 steps then stop"); SteperStepset(10); StepperMotorEnable(0,40); delay(5000); Serial.println("sent command to shut down the stepper"); StepperMotorUnenable(); delay(5000); } void setup() { Wire.begin(); // join i2c bus (address optional for master) delayMicroseconds(10000); Serial.begin(9600); Serial.println("setup begin"); stepperrun(); }

void loop() { while(1) { MotorSpeedSetAB(100,100); delay(10); //this delay needed MotorDirectionSet(0b0110); //0b1010 Rotating in the positive direction delay(10000); MotorDirectionSet(0b1001); //0b0101 Rotating in the opposite direction delay(500); } }