Getting started with the LinkIt One – DC Motors

Written by: admin@makezilla

Picture of Getting started with the LinkIt One - DC Motors22.jpg55.jpg

In the last two instructables I showed you how to get started with the LinkIt one which included controlling servos and LEDs. I also show you how to update the onboard firmware of the LinkIt One, so I highly recommend you go through that first.

In this instructable I'm going to show you how to get started with controlling DC Motors using the LinkIt One and the Grove I2C motor driver. In the next tutorial I'm going to show you how to build a robot using it.

For those of you how do not know the LinkIt One is an arduino compatible board with a lot of features on board like WIFI Bluetooth, GPS, GSM and a audio jack all in one board so there is no requirement to get additional shields and we can save money. This board is great for IoT related projects which I will write about in the future tutorials.

So lets get started.....

Step 1: Requirements

Picture of Requirements 777.jpg

The list of requirements is simple all you need is -

  • LinkIt One Board
  • Grove I2C board
  • A power source to power the motors
  • Some connecting wires

No soldering skills are required for this tutorial as we will be using a breadboard, but in the future tutorials there will be a lot of soldering. And there are a lot of tutorials on YouTube that shows you how to solder. You can also use an Arduino and you can PM me for additional details on how to get started.

Step 2: Hardware

Picture of Hardware

The LinkIt one is a product of Seeed Studio and hence has compatible on board headers which plugs in grove sensors on board. You can go for additional headers by buying a Grove Base shield which is compatible also with an Arduino and an Intel Edison.

The hardware part is fairly simple you first need to connect the power source to the Grove I2C motor driver -

  • VCC goes to +12V of the voltage source
  • Gnd goes to Gnd of the voltage source

Step 3: Firmware

Picture of Firmware

You also need to plug the I2C cable between both the boards.

The LinkIt One can be powered using the Li-ion battery which came in the pack or you can plug in a micro USB cable.

To run this code you need to update the board's firmware if you have already have it to the latest version you are good to go. If not, please refer to my first instructable of the series on how to do that before you proceed.

You also need to install the arduino IDE which can also be found in the first tutorial, if you face any difficulties you can leave a comment or PM me and I would be glad to help.

Step 4: Code

Picture of Code11.jpg

After you have got the hardware setup now it is time for the code, this a test code which makes the motors rotate in one direction for 10 seconds and then reverses the direction. In the next tutorial I will show you how to design a robot using the same logic.

The code can be found below you can copy and paste the code into the Arduino IDE and upload it to the board.

#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 setup()  {  Wire.begin(); // join i2c bus (address optional for master)  delayMicroseconds(10000);  Serial.begin(9600);  Serial.println("setup begin");}void loop()  {    Serial.println("sent DC speed 100");    MotorSpeedSetAB(100,100);//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 "negtive"                                  // make sure M+ and M- is different polatity when driving DC motors.   delay(10000);      MotorDirectionSet(0b0101);  //0b0101  Rotating in the opposite direction    delay(10000);   }

Leave a Reply