Grove - I2C Motor Driver

From Wiki
Jump to: navigation, search

Grove - I2C Motor Driver V1.0


Introduction

The Grove-I2C motor driver is a new addition to the TWIG series with the same easy-to-use interface. Its heart is a dual channel H-bridge driver chip that can handle current up to 2A per channel, controlled by an Atmel ATmega8L which handles the I2C communication with for example an Arduino. Both motors can be driven simultaneously while set to a different speed and direction. It can power two brushed DC motors or one 4-wire two-phase stepper motor. It requires a 6V to 15V power supply to power the motor and has an onboard 5V voltage regulator which can power the I2C bus (selectable by jumper). All driver lines are diode protected from back EMF.

The easy software interface is not the only easy-to-use feature because the TWIG I2C motor driver is designed to get you up and running in no time. It features an LED for power and four LEDs to indicate which direction each motor is running. Screw terminals facilitate motor and power connections, and the GROVE system plug and I2C interface enables you to daisy-chain the driver with many other devices.


Grove - I2C Motor Driver V1.1.jpg


Feature

Grove compatible I2C interface Motor's speed and direction can control Number of channels: 2 Easy to use Specifications

Item Min Typical Max Unit Working Voltage 6 - 15 VDC Max Output Current per channel 0.7 A Input/output voltage on I2C bus 5 V Communication protocol I2C / Interface Function


I2C Motor Drive Interface.jpg U5: AMS1117 IC,5v voltage regulator U1: L298 IC,dual full bridge driver IC1: Atmega8 IC, Control Motor Rotate. NOTE: Input voltage on screw terminals is regulated to 5v and connected to I2C +5v via a jumper (J4). Remove jumper if both external power via the screw terminals and power via the I2C header is used. Use jumper if 5v should be supplied to the I2C bus.

Application Ideas

This motor driver can be used to drive any brushed electronic motor as long as it doesn't consume more than 2A at 5v. Two motors can be driven simultaneously while set to a different speed and direction. The speed can be set fully proportional and is controlled by the ATmega8 on the board using PWM. It is set by I2C commands sent from e.g. an Arduino. It is perfect for applications like robots, homebuilt RC cars, case fans, high power LED illumination or any other project that involves proportional load control.

Cautions

The board will be very hot while operating over 1Amps. Do keep off your hands! Different Arduino IDE may have difference. I use Arduino-0019 and it works fine, but when I use Arduino - 0022, I need to add some delay() at the end of Wire.endTransmission(). Usage

The I2C Motor Driver can control motor which is based on the chip L298. The L298 isn’t just a dual motor driver, it is a dual H-bridge. An h-bridge is basically a specific setup of transistors that allow you to switch direction of current. So hooked up to a motor, which means you can have it spin in both directions, and with PWM input, you can use your Arduino to make them spin at any speed. Because the L298 has 2 H-bridges, you can not only make a robot go forwards and backwards, but also turn around by having each wheel spin in a different direction. Now, let's use the I2C Motor Driver to control two DC motors rotating in the positive or opposite direction. (图片) Note:

1) The first thing to notice, however, is that you do need an external power source for your motors, the 5v pin on the Arduino just can not source enough power to drive 2 motors, and you could damage your Arduino if you do. 2) It also should be reminded that the module can not drive Stepper Motor. The below is an example program to be used with an Arduino. The code for this is very basic. We created a function for you that makes controlling the L298 from your Arduino easier, but you can also change it up and do it your own way.


/*

 Grove- i2C motor driver demo v1.0
 by: http://www.simgin-chip.com
  • /
 //  Author:LG
  1. include <Wire.h>
  1. define MotorSpeedSet 0x82
  2. define PWMFrequenceSet 0x84
  3. define DirectionSet 0xaa
  4. define MotorSetA 0xa1
  5. define MotorSetB 0xa5
  6. define Nothing 0x01
  7. define Stepernu 0x1c
  8. define I2CMotorDriverAdd 0x0f // Set the address of the I2CMotorDriver

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

}

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

}

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 direction and speed together

 MotorDirectionSet(Direction);
 MotorSpeedSetAB(MotorSpeedA,MotorSpeedB);  

}

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(255,255);//defines the speed of motor 1 and motor 2;
   delay(10); //this delay needed
   MotorDirectionSet(0b1010);  //"0b1010" defines the output polarity, "10" means the M+ is "positive" while the M- is "negative"
   // make sure M+ and M- is different polarity when driving DC motors.
   delay(1000); 
   MotorDirectionSet(0b0101);  //0b0101  Rotating in the opposite direction
   delay(500);
 }
 

} Upload it to Arduino. Please click here if you do not know how to upload. Then the motors will rotating in the positive or opposite direction in cycle. Note: Each motor has 3 control pins, 2 for direction, and one for speed. When one direction pin is HIGH and the other is LOW the motor will spin one direction, flip them and it spins the other direction (both HIGH or both LOW and the motor stops). The PWM pin allows you to analogWrite to this pin to control the speed of that one motor. analogWrite 0 and the motor stops, 255, and it will go full speed.

Version Tracker

Revision Descriptions Release v0.9b Initial public release July 21st, 2011 v1.1 Initial public release May 17th, 2012 v1.2 Initial public release June 15th, 2012











Grove - I2C Motor Driver V1.2


Introduction

The Grove I2C motor driver is a new addition to the Grove series with the same easy-to-use interface. Its heart is a dual channel H-bridge driver chip(L298N)that can handle current up to 2A per channel, controlled by an Atmel ATmega8L which handles the I2C communication with for example an Arduino. Both motors can be driven simultaneously while set to a different speed and direction. It can power two brushed DC motors or one 4-wire two-phase stepper motor. It requires a 6V to 15V power supply to power the motor and has an onboard 5V voltage regulator which can power the I2C bus and the Arduino(selectable by jumper). All driver lines are diode protected from back EMF.

The easy software interface is not the only easy-to-use feature because the Grove I2C motor driver is designed to get you up and running in no time. It features a LED for power and four LED's to indicate if and to which direction each motor is running. Screw terminals facilitate motor and power connections, and the Grove system plug and I2C interface enables you to daisy-chain the driver with many other devices.


I2CMotorDriver-2.jpg

Feature

Grove Compatiple I2C Interface Motor's speed and direction can control Number of channels: 2 Changeable slave address by Hardware Specifications

Item Min Typical Max Unit Working Voltage 6 - 15 VDC Max Output Current per channel 0.5 A Maximum Total current 1.0 A Input/output voltage on I2C bus 5 V Communication protocol I2C / Interface Function

I2CMotorDriver-1.jpg 78M05 IC: 5v voltage regulator L298 IC: dual full bridge driver Atmega8 IC: Control Motor Rotate. NOTE: Input voltage on screw terminals is regulated to 5v and connected to I2C +5v via a jumper (J4). Remove jumper if both external power via the screw terminals and power via the I2C header is used. Use jumper if 5v should be supplied to the I2C bus.

Application Ideas

This motor driver can be used to drive any brushed electronic motor as long as it doesn't consume more than 2A at 5v. Two motors can be driven simultaneously while set to a different speed and direction. The speed can be set fully proportional and is controlled by the ATmega8 on the board using PWM. It is set by I2C commands sent from Arduino or www.simgin-chip.com. It is perfect for applications like robots, homebuilt RC cars, case fans, high power LED illumination or any other project that involves proportional load control.

Cautions

The board will be very hot if while operating over 1Amps. Do keep off your hands! Arduino IDE(1.0 Or higher version) are supported. Usage

The I2C Motor Driver can control motor which is based on the chip L298, The L298 isn’t just a dual motor driver, it is a dual H-bridge. An h-bridge is basically a specific setup of transistors that allow you to switch direction of current. So hooked up to a motor, that means you can have it spin in both directions, and with PWM input, you can use your Arduino to make them spin at any speed. Because the L298 has 2 H-bridges, you can not only make a robot go forwards and backwards, but also turn around by having each wheel spin in a different direction. Now, let's use the I2C Motor Driver to control two DC motors or a stepper rotating in the positive or opposite direction.

Set the address of the I2C Motor Driver Set the address by dial switch as a new function added to the new I2C Motor Driver. I2CMotorDriver-9.jpg Then keep the address setup in the program is same to the address setup on the I2C motor driver. The default address setup in the program is 0x0f.

  1. define I2CMotorDriverAdd 0x0f // Set the address of the I2CMotorDriver

How to drive 2 DC motors I2CMotorDriver-4.jpg Note:

The first thing to notice however, is that you need an external power source your DC motors, the 5v pin on the Arduino can not source enough power to drive 2 motors, you may damage your Arduino if you do. And then program your Arduino as below:

  1. include <Wire.h>
    .......
    .......
   < Driver functions >
    .......
    .......

void setup() {

 Wire.begin(); // join i2c bus (address optional for master)
 delayMicroseconds(10000); //wait for motor driver to initialization

}

void loop() {

 while(1)  {
   MotorSpeedSetAB(100,20);
   delay(10); //this delay needed
   MotorDirectionSet(0b1010);  //0b1010  Rotating in the positive direction 
   delay(1000); 
   MotorDirectionSet(0b0101);  //0b0101  Rotating in the opposite direction
   delay(500);
 }

} In this program, Arduino first set the speed of the 2 DC motors with the MotorSpeedSetAB()command, and then set the DC motors work directions with MotorDirectionSet() command. please refer to the Function Reference for details, you can download all the demo code in the Resource.

How to drive a 4-wire stepper The I2C motor Driver can be also used to drive a 4-wire stepper. connect your stepper to the Output Pins of I2C motor driver, and then connect motor driver to your Arduino/www.simgin-chip.com with I2C bus. Program your Arduino as blows:

  1. include <Wire.h>
    .......
    .......
   < Driver functions >
    .......
    .......

void setup() {

 Wire.begin(); // join i2c bus (address optional for master)
 delayMicroseconds(10000); //wait for motor driver to initialization

}

void loop() {

while(1)  {
   MotorSpeedSetAB(100,100);//when driving a stepper, the speed should be set to 100;
   delay(10);
   MotorDirectionSet(0b0001);
   delay(4);
     MotorDirectionSet(0b0011);
   delay(4);  
   MotorDirectionSet(0b0010);
   delay(4);
     MotorDirectionSet(0b0110);
   delay(4);  
   MotorDirectionSet(0b0100);
   delay(4);  
   MotorDirectionSet(0b1100);
   delay(4);
     MotorDirectionSet(0b1000);
   delay(4);
     MotorDirectionSet(0b1001);
   delay(4);
 }

} This connected 4-wire stepper will rotate, you can adjust the rotate speed or step number in your Arduino program. You can also use some other stepper libraries to control it, you can download all the demo code in the Resource. 2.gif

Function Reference

1. void MotorSpeedSetAB(unsigned char MotorSpeedA , unsigned char MotorSpeedB) Description: defines the speed of motor 1 and motor 2 MotorSpeedA: the DC motor A speed, should be 0~100; MotorSpeedB: the DC motor B speed, should be 0~100; Usage:

Serial.println("sent DC speed 100"); MotorSpeedSetAB(100,100);//defines the speed of motor 1 and motor 2; delay(10); //this delay needed 2. void MotorPWMFrequenceSet(unsigned char Frequence) Description:set the prescale frequency of PWM, 0x03 default

Frequence: the prescale frequency of PWM

3. void MotorDirectionSet(unsigned char Direction) Description: Adjust the direction of the motors Direction:can be Forward/Reverse rotating. Usage:

MotorDirectionSet(0b1010); //"0b1010" defines the output polarity, "10" means the M+ is "positive" while the M- is "negative"

                           // make sure M+ and M- is different polarity when driving DC motors.

delay(1000); MotorDirectionSet(0b0101); //0b0101 Rotating in the opposite direction delay(500); 4. void MotorDriectionAndSpeedSet(unsigned char Direction,unsigned char MotorSpeedA,unsigned char MotorSpeedB) Description: Adjust the direction and speed together of Motors.

Version Tracker

Revision Descriptions Release v1.0 Initial public release May 17th, 2012 v1.2 I2C address set by hardware July 2th, 2012




Grove - I2C Motor Driver V1.3


Introduction

The Grove - I2C Motor Driver V1.3 (latest version) can directly control Stepper Motor or DC Motor. Its heart is a dual channel H-bridge driver chip(L298N)that can handle current up to 2A per channel, controlled by an Atmel ATmega8L which handles the I2C communication with for example an Arduino. Both motors can be driven simultaneously while set to a different speed and direction. It can power two brushed DC motors or one 4-wire two-phase stepper motor. It requires a 6V to 15V power supply to power the motor and has an onboard 5V voltage regulator which can power the I2C bus and the Arduino(selectable by jumper). All driver lines are protected by diodes from back-EMF.

Contrast to the Grove - I2C motor driver V1.2, the V1.3 enables users to control the stepper more easily. You do not need to control the steppers all the time anymore, simply send a command to I2C motor driver V1.3 to drive a stepper, and it will act as your command, which would save your Arduino resource and simplify your code.


I2CMotorDriver-2.jpg



Features

Grove Compatible I2C Interface Adjustable motor speed and rotation direction Changeable slave address by hardware Specifications

Item Min Typical Max Unit Working Voltage 6 - 15 VDC Max Output Current per channel 0.5 A Maximum Total current 1.0 A Input/output voltage on I2C bus 5 V Communication protocol I2C / Interface Function

I2CMotorDriver-1.jpg 78M05 IC: 5v voltage regulator L298 IC: dual full bridge driver Atmega8 IC: Control Motor Rotate. NOTE: Input voltage on screw terminals is regulated to 5v and connected to I2C +5v via a jumper (J4). Remove jumper if both external power via the screw terminals and power via the I2C header are used. Use jumper if 5v should be supplied to the I2C bus.

Application Ideas

Robots Homebuilt RC cars Case fans High power LED illumination

Cautions

The board will be very hot while operating over 1Amps. Do keep off your hands! Usage

The I2C Motor Driver can control motor which is based on the chip L298. The L298 isn’t just a dual motor driver, it is a dual H-bridge. An h-bridge is basically a specific setup of transistors that allow you to switch direction of current. Hooking up to a motor means you can have it spin in both directions; and with PWM input, you can use your Arduino to make them spin at any speed. Because the L298 has 2 H-bridges, you can make a robot turn around by spinning each wheel in different directions, and of course go forwards and backwards. Now, let's use the I2C Motor Driver to control two DC motors or a stepper rotating clockwise and anticlockwise.

Set the address of the I2C Motor Driver Set the address by dial switch is a new function added to the new I2C Motor Driver. I2CMotorDriver-9.jpg Then keep the address setup in the program the same as the address setup on the I2C motor driver. The default address setup in the program is 0x0f.

  1. define I2CMotorDriverAdd 0x0f // Set the address of the I2CMotorDriver

How to drive 2 DC motors I2CMotorDriver-4.jpg Note:

The first thing to notice however, is that you need an external power source for your DC motors. The 5v pin on the Arduino cannot provide enough power to drive 2 motors, you may damage your Arduino if you do so. And then program your Arduino as below:

  1. include <Wire.h>
    .......
    .......
   < Driver functions >
    .......
    .......

void setup() {

 Wire.begin(); // join i2c bus (address optional for master)
 delayMicroseconds(10000); //wait for motor driver to initialization

}

void loop() {

 while(1)  {
   MotorSpeedSetAB(100,20);
   delay(10); //this delay needed
   MotorDirectionSet(0b1010);  //0b1010  Rotating in the positive direction 
   delay(1000); 
   MotorDirectionSet(0b0101);  //0b0101  Rotating in the opposite direction
   delay(500);
 }

} In this program, Arduino first set the speed of the 2 DC motors with the MotorSpeedSetAB()command, and then set the DC motors work directions with MotorDirectionSet() command. please refer to the Function Reference for details, you can download all the demo code in the Resource.

How to driver a stepper using I2C motor driver V1.3 As the upgraded version of I2C motor DriverV1.2, You can drive stepper via 2 methods for I2C motor driver V1.3. 1. Control the stepper directly by Arduino The I2C motor Driver can also be used to drive a 4-wire stepper. Connect your stepper to the output pins of I2C motor driver, and then connect motor driver to your Arduino/www.simgin-chip.com with I2C bus. Program your Arduino as below:

  1. include <Wire.h>
    .......
    .......
   < Driver functions >
    .......
    .......

void setup() {

 Wire.begin(); // join i2c bus (address optional for master)
 delayMicroseconds(10000); //wait for motor driver to initialization

}

void loop() {

while(1)  {
   MotorSpeedSetAB(100,100);//when driving a stepper, the speed should be set to 100;
   delay(10);
   MotorDirectionSet(0b0001);
   delay(4);
     MotorDirectionSet(0b0011);
   delay(4);  
   MotorDirectionSet(0b0010);
   delay(4);
     MotorDirectionSet(0b0110);
   delay(4);  
   MotorDirectionSet(0b0100);
   delay(4);  
   MotorDirectionSet(0b1100);
   delay(4);
     MotorDirectionSet(0b1000);
   delay(4);
     MotorDirectionSet(0b1001);
   delay(4);
 }

} This connected 4-wire stepper will rotate; you can adjust the rotation speed or step number in your Arduino program. You can also use some other stepper libraries to control it. For all the demo code please refer to Resource. 2.gif

2.Control the Stepper using the I2C motor Driver V1.3 on-chip Atmega8L. Take 24BYJ48 as an example, The hardware installation as show below: I2C Motor Driver control a Stepper Motor.jpg Note: The connection between 24BYJ48 Stepper Motor and I2C Motor Driver is shown as blow: I2C Motor Driver Connector.jpg Download the Grove-I2C motor driver V1.3 demo code, and open the StepperControlMode2.ino:

  1. include <Wire.h>
  2. define MotorSpeedSet 0x82
  3. define PWMFrequenceSet 0x84
  4. define DirectionSet 0xaa
  5. define MotorSetA 0xa1
  6. define MotorSetB 0xa5
  7. define Nothing 0x01
  8. define EnableStepper 0x1a
  9. define UnenableStepper 0x1b
  10. define Stepernu 0x1c
  11. define I2CMotorDriverAdd 0x0f // Set the address of the I2CMotorDriver

// set the steps you want, if 255, the stepper will rotate continuously; 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 

}

    .......
    .......
    .......
    .......

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() {

} In this demo code, Arduino sends stepper-control command to I2C motor driver via I2C bus, with SteperStepset() to set the step number, and StepperMotorEnable() to set the direction and speed. Please refer to the Function Refrence for the details. Note that if you have I2C motor driver V1.2 and want to use the off-line Stepper control methods, you will need to upgrade your firmware in your V1.2 motor driver with a AVRISP and upload the .hex file to your I2C motor driver. Please download the .hex file and source code and related tips in the [http://www.simgin-chip.com/wiki/Grove_-_I2C_Motor_Driver_V1.3#Resources Resource

Function Reference

1. void SteperStepset(unsigned char stepnu) Description: Set the steps you want.

stepnu: the Parameter can be 1~255. if 255, the stepper will rotate continuously;

Usage:

Serial.println("sent command to + direction,100 steps, fast"); SteperStepset(100); 2. void StepperMotorEnable(unsigned char Direction, unsigned char motorspeed) Description: Enable the IIC motor driver to drive a 4-wire stepper.

Direction: Stepper direction 1/0

motorspeed: defines the time interval the i2C motor driver, Change it output to drive the stepper. The actual interval time is : motorspeed * 4ms. That is , When motor speed is 10, the interval time would be 40 ms. Usage:

StepperMotorEnable(1, 1);// enable the i2c motor driver a stepper. 3. void StepperMotorUnenable() Description: Uneanble IIC motor drive to drive the stepper. Usage:

StepperMotorUnenable(); 4. void MotorSpeedSetAB(unsigned char MotorSpeedA , unsigned char MotorSpeedB) Description: defines the speed of motor 1 and motor 2

MotorSpeedA: the DC motor A speed, should be 0~100; MotorSpeedB: the DC motor B speed, should be 0~100; Usage:


Serial.println("sent DC speed 100"); MotorSpeedSetAB(100,100);//defines the speed of motor 1 and motor 2; delay(10); //this delay needed 5. void MotorPWMFrequenceSet(unsigned char Frequence) Description:set the prescale frequency of PWM, 0x03 default

Frequence: the prescale frequency of PWM

6. void MotorDirectionSet(unsigned char Direction) Description: Adjust the direction of the motors

 Direction:can be Forward/Reverse rotating. 

Usage:

MotorDirectionSet(0b1010); //"0b1010" defines the output polarity, "10" means the M+ is "positive" while the M- is "negative"

                        // make sure M+ and M- is different polarity when driving DC motors.

delay(1000); MotorDirectionSet(0b0101); //0b0101 Rotating in the opposite direction delay(500); 7. void MotorDriectionAndSpeedSet(unsigned char Direction,unsigned char MotorSpeedA,unsigned char MotorSpeedB) Description: Adjust the direction and speed altogether.

Version Tracker

Revision Descriptions Release v1.0 Initial public release May 17th, 2012 v1.2 modify the I2C address set by hardware July 2nd, 2012 v1.3 modify the firmware to support off-line Stepper Feb 18th, 2013