Tuesday 12 May 2015

DC Bot, Product Test, HG7881CP Motor Controller

As always this is for entertainment purposes only but worked perfectly for me when used with the components listed, it won't work with other motor controllers so please don't ask, but have fun with it.

I have had some very small DC motor controllers for some time now and didn't feel confident enough to write my own sketch for them and I couldn't find one that suited the unit so they sat in my parts box not being used. So I have been modifying sketches for a while now and finally I decided to write a sketch for the motor controller, it's the LC Technology HG7881CP Motor Controller, it's a full H Bridge and can carry 800mA total as well as 12V operation if you wish, it will run on 2.5V to 12V. The cost for this controller on eBay is about $2.00 to $5.00 delivered so it's as cheap as chips and will handle those cheap Arduino Robot Kits that use the little yellow geared motors. My tests showed that this motor only drew about 200mA to 600mA at stall on 5V so they should be perfect and that is what I used for this build except I purchased the robot kit from Jaycar Australia mainly because of the wheels that were in the kit so it cost me a lot more than you can buy them from China but I had to support the local bloke at least once, but at $54.00AUD it will be the last time, I'll make my own in future.


                                  This is the Schematic for the Controller

                               The HG7881CP H Bridge Motor Controller

The other components were a HC-SR04 Ultrasonic Sensor, an Arduino Pro Mini Clone, some 3mm MDF, 10mm dowel, assorted small washers, screws and plastic components for insulation and mounting the components not to mention wire, terminals and rechargeable batteries. As I mentioned I purchased the robot kit from Jaycar which came with a switch, two yellow geared motors, caster wheel, plastic chassis and nuts, screws standoffs and encoder wheels which I didn't use.

So, the first thing I did was research the HG7881CP controller and get all the information I could on it, the best bit of information was a Truth table and a list of terminal IDs.
  1. B - IA; Motor B input A, (IA)
  2. B - IB; Motor B input B, (IB)
  3. GND; Ground, 0 Volts
  4. VCC; Supply + Voltage, 2.5 to 12 Volts
  5. A - IA; Motor A input A, (IA)
  6. A - IB; Motor A input B, (IB)
Truth Table:   IA - IB      Motor State
                        L - L      |       OFF
                        H - L     |    Forward
                        L - H     |    Reverse
                        H - H    |        OFF

The other bit of information was that the IA connections should be PWM signals and that the IB connections should be Digital High or Low for Direction Control. With this information I stood on the shoulders of the great women and men that went before me and imported some code that I have used many times for control of the ultrasonic sensor, and went with  NewPing.h and using the truth table I wrote the void moveBackward, move Forward, moveRight and moveLeft as well as moveStop sketch instructions and rats nested the circuit on the bench and loaded the sketch into the Arduino Pro Mini clone and tested the operation of all components and to my surprise it worked perfectly first try, but needed some fine tuning once the robot was built.

                                    The components on the Bread-Board


Okay, the build started with me cutting out the top deck of the robot out of 3mm MDF, I like the robots I build to have a lower and upper deck and I also like to paint the bot to liven it up a bit, which is just what I did for this build. While the Paint was drying I started on the other components.

The HC-SR04 Ultrasonic Sensor needed a bracket and as always I make mine out of some universal PCB and solder the bits together to make up the bracket, see images below.

                                    The PCB cut to form the bracket

                The two parts soldered together so they are at 90 degrees

I then make up some wire supports to hold the Ultrasonic Sensor to the bracket and solder it all together so it becomes a sturdy mounting support,see images below.

                             The mounting wires soldered to the HC-SR04

                                     The finished bracket and HC-SR04

Sensor ready for mounting it now came time to mount the Arduino Pro Mini Clone to some PCB and fit some connection pins on the PCB as well so it can all be wired together and mounted on the robot chassis, doing it this way makes it very easy to mount and wire up. Now fit a 0.1 uF cap across the motor supply terminals to filter the brush noise from the motor to help protect the motor controller, I always do this. The paint was now dry and so I was able to start mounting the components onto the lower chassis, see images below.

                  The Motor with Mounting Bracket and Filter Capacitor


                A shape proof washer is always used on the motor bracket

I like to use a shake proof or star washer on the motor brackets and flat washers under the head of the screws to spread out the load of the screw over a larger area, helps prevent breakage especially on the plastic chassis.

           Flat washers under the head of the mounting screws for the motors


                 Both motors mounted to the underside of the lower chassis

                     Mounting screws and standoffs on the battery holder

I use plastic screws and standoffs on the battery holder to mount the holder to the lower chassis, safer so far as rubbing through the batteries etc and very easy to trim the head down so the batteries lay flat in the holder, works very well.

           Screws with flat washers to hold the standoffs for the rear caster


            Rear caster mounted on the standoffs with two flat washers per nut

The holes in the caster wheel are a bit big so I always put a flat washer both sides of each hole so it is a much safer and secure mount. You can also use a thread locker if you wish, up to you.

                              Caster mounted to rear of the lower chassis


               Pro Mini Clone and HG7881CP mounted to the lower chassis

The mounting of the other components depends on how you make up your bits and I also think about balance of the robot so it has good traction and weight distribution for stability on rough outside ground, yes this bot is intended for outdoor use in the sun.

                   With the components mounted the wiring can now be done


        The ON/OFF switch and Ultrasonic Sensor mounted on the upper chassis

The ultrasonic sensor and on/off switch were mounted on the upper chassis or top of the bot, no real reason other than it made it easy to get to the switch and to also get to the arduino for sketch uploading during the fine tuning process. Believe me it will take some fine tuning, no two motors are the same so you will need to adjust the speed of each motor etc.

                                    HC-SR04 mounted right at the front


                               Top deck on and wiring cable tied together

                                       Finished product ready for play

The sketch is a very simple one, the forward speed is controlled by PWM with respect to the positive part of the signal, if you look at the truth table you can see that it needs a High on IA and a Low on IB for forward BUT reverse needs the exact opposite so it needs more negative on IA to go faster in reverse, seems strange but you will see it as you play with the sketch, and do play with the sketch to make your bot perform better than the basic setup. Anyway here is the basic sketch;


//Code by Terrence J MacCabe and Arduino World Family
//Arduino ProMini is used in my Robot but most Arduinos could be used
//HG7881CP Motor Controller from LC Technology is used
//Max Motor Voltage is 12Volts with the HG7881CP but don't exceed motor safe voltage
//Max Current for the HG7881CP is 800mA (0.800A), test the current of your motors
//Motor1 IS Motor A and Motor2 IS Motor B
//For this code you need to install the NewPing libraries to your arduino software

#include <NewPing.h> 


#define TRIGGER_PIN  A4 //Trigger pin of Ultrasonic sensor connected to pin A4

#define ECHO_PIN     A5 //Echo pin of Ultrasonic sensor connected to pin A5 

#define MAX_DISTANCE 100 //The maximum distance we want the sensor to look for is 1m


const int Motor1PinIB = 8; //This selects forward or reverse via Digital High or Low

const int Motor2PinIB = 9; //This selects forward or reverse via Digital High or Low

const int Motor1PinIA =10; //This sets the speed via Analog PWM

const int Motor2PinIA = 11; //This sets the speed via Analog PWM



NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //Create Ultrasonic Sensor Object


unsigned int time; //Variable to store how long it takes for the ultrasonic wave to come back          

int distance; //Variable to store the distance calculated from the sensor               

int triggerDistance = 20; //The distance we want the robot to look for a new path  

int fDistance; //Variable to store the distance in front of the robot              

int lDistance; //Variable to store the left distance from the robot

int rDistance; //Variable to store the right distance from the robot



void setup()

{

  pinMode(Motor1PinIB, OUTPUT);  

  pinMode(Motor2PinIB, OUTPUT);  

  pinMode(Motor1PinIA, OUTPUT);  

  pinMode(Motor2PinIA, OUTPUT);

}


void loop()

{

  scan();                               

  fDistance = distance;                 

  if(fDistance < triggerDistance){   

    moveBackward();                     

    delay(1000);

    moveRight();                        

    delay(600);

    moveStop();                         

    scan();                            

    rDistance = distance;               

    moveLeft();

    delay(1000);                        

    moveStop();                        

    scan();                            

    lDistance = distance;              

    if(lDistance < rDistance){          

      moveRight();                     

      delay(1000);

      moveForward();                   

    }

    else{

      moveForward();                    

    }

  }

  else{

    moveForward();                     

  }

}


void scan(){

  time = sonar.ping(); //Send out ping and store the time it took to return               

  distance = time / US_ROUNDTRIP_CM; //Convert time to distance  

  if(distance == 0){ //If no ping was received                  

    distance = 100; //Set the distance to Max Distance of 1m                   

  }

  delay(10);

}


void moveBackward(){

  digitalWrite(Motor1PinIB, HIGH); //Truth Table for the HG7881CP IB Reverse

  digitalWrite(Motor2PinIB, HIGH); //Truth Table for the HG7881CP IB Reverse

  analogWrite(Motor1PinIA, 100); //For reverse the Truth table indicates IA must be low most of the time for max speed

  analogWrite(Motor2PinIA, 100); //For reverse the Truth table indicates IA must be low most of the time for max speed

}


void moveForward(){

  digitalWrite(Motor1PinIB, LOW); //Truth Table indicates Low for the HG7881CP IB Forward

  digitalWrite(Motor2PinIB, LOW); //Truth Table indicates Low for the HG7881CP IB Forward

  analogWrite(Motor1PinIA, 170); //Speed set to 170 out of 255, Truth Table indicates IA must be High (PWM) for forward, change as required but do not exceed 255

  analogWrite(Motor2PinIA, 200); //Speed set to 200 out of 255, Truth Table indicates IA must be High (PWM) for forward, change as required but do not exceed 255

}


void moveRight(){

  digitalWrite(Motor1PinIB, LOW); //Truth Table for the HG7881CP IB Forward

  digitalWrite(Motor2PinIB, LOW); //Truth Table for the HG7881CP IB Forward

  analogWrite(Motor1PinIA, 10); //Right motor low speed, change as required

  analogWrite(Motor2PinIA, 200); //Left motor higher speed, change as required

}


void moveLeft(){

  digitalWrite(Motor1PinIB, LOW); //Truth Table for the HG7881CP IB Forward

  digitalWrite(Motor2PinIB, LOW); //Truth Table for the HG7881CP IB Forward

  analogWrite(Motor1PinIA, 200); //Right motor higher speed, change as required

  analogWrite(Motor2PinIA, 10); //Left motor low speed, change as required

}


void moveStop(){

  digitalWrite(Motor1PinIB, LOW); //Truth Table for the HG7881CP IB Stop

  digitalWrite(Motor2PinIB, LOW); //Truth Table for the HG7881CP IB Stop

  analogWrite(Motor1PinIA, 0); //Truth Table for the HG7881CP IA Stop

  analogWrite(Motor2PinIA, 0); //Truth Table for the HG7881CP IA Stop
 
  //Truth Table indicates all Pins on the HG7881CP must ALL be LOW OR HIGH for Stop

}


If you copy and paste the above code into your Arduino IDE it should work OK with the LC Technology Motor Controller HG7881CP and most Arduino Boards, it is intended for the above components only and will need to be rewritten for other components. As I said the sketch is very basic and was written for me to test the HG7881CP which was a great success, the motor controller works very well and doesn't get very warm at all so I highly recommend the HG7881CP for small low current motors and according to the information can be used to control a 4 wire stepper motor so it has many uses and is very cheap, I'll be getting a few more to add to my parts list. Hope this helps and have fun playing, Cheers, TJ.
This is a very short video of the above rolling robot, DC Bot.







No comments:

Post a Comment