Grove I2C Motor Driver Setup and Example Code

After much deliberation I was finally able to get my Grove I2C motor driver and its example code working. I had originally had problems compiling a set of example code that I found online but now I have fixed the code. Also I have made a drawing showing how to connect the Grove I2C Motor Driver to an Arduino Uno. It seems that very few people have made tutorials on this controller other than the wiki page which has just a lot of technical information which is great but for people who don't want all that extra information I have made this short tutorial. I kept it short sweet and to the point so hopefully if you are trying to use one of these controllers you will be able to use the information on this page.

Setup:
(CLICK TO VIEW IMAGE LARGER)

The example code bellow should simply make the motor spin and then reverse directions repeatedly.
Additional help can be found here:
http://www.seeedstudio.com/wiki/index.php?title=Grove_-_I2C_Motor_Driver
http://www.seeedstudio.com/forum/viewtopic.php?f=17&t=2935
http://www.arduino.cc/en/Reference/Wire

Working Code Example:



#include <Wire.h>

//#define MOTORSHIELDaddr   0x0f
#define SETPWMAB          0x82
#define SETFREQ           0x84
#define CHANGEADDR        0x83
#define CHANNELSET        0xAA
#define MOTOR1            0xA1
#define MOTOR2            0xA5
#define SAVEADDR          'S'
#define NOTSAVEADDR       'N'
byte val = 0; // I put this here to replace the "Wire.write(0);"

static unsigned char MOTORSHIELDaddr = 0x28;

void speedAB(unsigned char spda , unsigned char spdb)
{
  Wire.beginTransmission(MOTORSHIELDaddr); // transmit to device MOTORSHIELDaddr
  Wire.write(SETPWMAB);        //set pwm header
  Wire.write(spda);              // send pwma
  Wire.write(spdb);              // send pwmb  
  Wire.endTransmission();      // stop transmitting
}

void fre_pre(unsigned char pres)
{  
  Wire.beginTransmission(MOTORSHIELDaddr); // transmit to device MOTORSHIELDaddr
  Wire.write(SETFREQ);        // set frequence header
  Wire.write(pres);           //  send prescale
  Wire.write(val);            //  need to send this byte as the third byte(no meaning)
  Wire.endTransmission();
}

void change_adr(unsigned char new_adr, unsigned char save_or_not)
{
  Wire.beginTransmission(MOTORSHIELDaddr); // transmit to device MOTORSHIELDaddr
  Wire.write(CHANGEADDR);        // change address header
  Wire.write(new_adr);              //  send new address
  Wire.write(save_or_not);          //   save the new address or not
  Wire.endTransmission();    //
  delayMicroseconds(100); //this command needs at least 6 us
}

void channel(unsigned char i4) //0b 0000 I4 I3 I2 I1
{
  delayMicroseconds(4);
  Wire.beginTransmission(MOTORSHIELDaddr); // transmit to device MOTORSHIELDaddr
  Wire.write(CHANNELSET);        // channel control header
  Wire.write(i4);                // send channel control information
  Wire.write(val);               // need to send this byte as the third byte(no meaning)
  Wire.endTransmission();
}
void motorAndspd( unsigned char motor_s,unsigned char Mstatus, unsigned char spd)
{
  Wire.beginTransmission(MOTORSHIELDaddr); // transmit to device MOTORSHIELDaddr
  Wire.write(motor_s);        // motor select information
  Wire.write(Mstatus);        // motor satus information
  Wire.write(spd);            //  motor speed information
  Wire.endTransmission();
}

void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  delayMicroseconds(10); //wait for motor driver to initialization
}

void loop()
{

  motorAndspd(0xA1,0B01,225);
  motorAndspd(0xA5,0B01,30);
  delay(2000);

  motorAndspd(0xA5,0B10,225);
  motorAndspd(0xA1,0B10,30);
  delay(2000);

  change_adr(0x12, NOTSAVEADDR );
  MOTORSHIELDaddr = 0x12;

  for( int i = 0;i< 16;i++)
  {
    fre_pre(i);
    channel(i);
    delay(100);
  }
  speedAB(100 , 100); // was 0 , 0
  change_adr(0x28,NOTSAVEADDR );
  MOTORSHIELDaddr = 0x28;

  while(1)
  {
    channel(0b00001010);
    delay(500);
    channel(0b00000101);
    delay(500);
  }
 }


◄ Newer Post Older Post ►
 

Copyright 2011 robot recycled parts is proudly powered by blogger.com