Sunday, February 15, 2015

Micro Servo Motor - calibrating position angles

I want to use a rather typical micro servo motor to move the eyes of my rover to "look" side to side and determine if there is an object present. I received the 3D prints this week for the servo mounting and the ultrasonic distance sensor so it's time to make sure my micro servo control works as planned. I'm going to use a Arduino Micro and some simple code with a basic bread board to bring the servo to life and tweak the positioning.
Parts Needed: 
1 Breadboard
1 Arduino Micro (or other Arduino board) ($24.95)
3 wires (female to female)
1 Micro Servo Motor  ($12.99)
1 USB to Micro USB cable ($9.99)  you might already have one from your smart phone


There are only three wires to connect so the hook up is easy... 
Here is the pin mapping:

Micro Pin    Servo Connection
Grd         --> Brown Wire
5V          --> Red Wire
 9            --> Yellow Wire

Insert the Arduino Micro into the breadboard and connect the ground, 5 volt and pin 9 to the servo motor as shown above. 

Upload the code to and and tweak the servo position values until they match your setup.. Get the code here.. MicroServoCalibration Code




Sunday, January 4, 2015

SainSmart Mega 2560 R3 4WD Mobile Car Robot Kit Build


Assembled Rover
I got a SainSmart Mobile Car Robot Kit for Christmas so I thought I'd share the build experience. The kit costs about $95 and it doesn't come with any instructions. However, thanks to some other makers out there, I found some helpful instructions posted on Instructables and also a useful post about it on Thingiverse.

There were a few problems with this kit. I wouldn't recommend getting this kit unless you have some experience. Some of the screw holes in the metal frame don't line up very well, but that was pretty minor. The biggest problems are that it doesn't come with enough wire and several of the parts in the kit were bad. In my kit, the Mega 2560 wouldn't accept code uploads and the reset button on it didn't work at all. Also, the battery pack had a short so when I hooked it up it started smoking.

Fortunately, I was able to use an Arduino Mega, some 9 volt battery connectors, and extra wire I already had. Hopefully SainSmart will send me a functional Mega to replace the non-working board in my kit. There is also nothing to mount the distance sensor and no hook up wires for it, so you'll have to get creative if you want to use it.

Parts List from the Kit: 

4 wheels
4 motors
2 rounded metal plates fro the front and back
2 side plates
2 flat plates for the top and bottom of the case
A bunch of screws
2 wires (1 red, 1 black)
1 4-wire female to female connection harness
1 battery pack
1 toggle switch
1 re-charge connector
1 USB cable
1 SainSmart MEGA 2560 R3
1 SainSmart Sensor Sheild V5
1 SainSmart L298N Dual H Bridge DC motor driver
1 HC-SR04 Distance Sensor




Other Items you'll need: 

2 9 volt batteries
2 9 volt battery connectors
extra wire
solder and a soldering iron
electrical tape
wire strippers
A volt meter

Build it ..

STEP 1: Hook up the Toggle Switch and connect the battery leads to the L298N Motor Driver

  1. Place the toggle switch in one of the holes on the top plate and use the washer and nut (only one fits) to secure it in place. 
  2. Secure the L298N Motor Driver to the bottom plate using the screws provided. (The wholes in the plate should line up pretty will with the mounting wholes on the board.)
  3. Connect the battery connectors together in series by soldering the ends of the black lead on one connector to the red lead on the second connector. Use electrical tape to cover the connection.
  4. Connect/solder the remaining red lead on the first battery connector to the center lead on the toggle switch.
  5. Connect the remaining black lead on the other battery connector to the screw connector on the Motor Driver labeled Gnd (The ground pin). 
  6. Cut a 4 inch piece of the red wire that came in the kit and strip away the sheath on the end to expose the wire on the cut side. Connect one side of this wire to one of the side leads on the toggle switch. Connect the other end to the to the screw connector labeled Vcc on the Motor Driver board. 
  7. Test the toggle switch connections and make sure it works.
    With the switch in the off position, connect the batteries. Then flip the switch to the on position. If your connections are good, the red led on the Motor Driver board should light up. if it doesn't, check the connections and try again. 
  8. Turn the switch off and disconnect the batteries. 


STEP 2: Connect the Motors to the Motor Driver


  1. Cut 2 4 inch wires from the red wire, and 2 4 inch wires from the black wire. 
  2. Cut 2 6 inch wires from the red wire and 2 6 inch wires from the black wire. 
  3. Strip the ends of each of the wires. 
  4. Solder the 4 inch black wire to one of the leads on the first motor, and the 4 inch red wire to the other lead on the first motor. 
  5. Solder the the 6 inch wires (one red and one black) to the second motor for the same side as shown in the picture. Caution: Take care to make sure that the wire colors match the orientation in the picture so that the wheels will both spin in the same direction. 
  6. Secure The motors to the side plate and secure the side plate to the bottom plate using the screws provided. 
  7. Connect the red leads for the two motors to one of the screw connection ports on the Motor Driver board labeled out. 
  8. Connect the black leads for the two motors to the other out port on the same side of the Motor Driver board. 

Repeat this process for the remaining two motors on the other side.



STEP 3: Connect the remaining wires to the Motor Driver board


  1. Cut and strip two wires (black and red preferred) to be used as ground and voltage supply wires for the Arduino. These wires will need to be about 6-7 inches long. 
  2. Connect the black wire to the ground on the Motor Driver board using the screw connector. 
  3. Connect the red wire to the 5V screw connector on the Motor Driver board.
  4. Using the 4 wire harness, connect the wires to the INT pins on the Motor Driver board as follows:
    INT1 -> Red 
    Switch and INT pin Connections
    INT2 -> Brown
    INT3 -> Black
    INT4 -> White


  1. Re-connect the batteries, but leave the switch off for now. 
  2. Fish the power, ground and 4 wire harness wires through the top plate from the bottom side.
  3. Place the top plate on top of the two sides where it will eventually be screwed on.

STEP 4: Program the Mega Using the Arduino IDE

The great thing about the Motor Driver board, is that for on and off control of the motor, you don't need any external libraries, just set the pins HIGH and LOW to control the direction of the motors. 

Copy the following code upload it to the Mega board using the Arduino IDE. 

//declaring the pins for the IN pins on the L298N
const int rightForwardPin = 4;
const int rightBackwardPin = 2;
const int leftBackwardPin = 7;
const int leftForwardPin = 5;

int runTime = 1000;

void setup() {
  //Stating that the pins are OUTPUT
  pinMode(rightForwardPin, OUTPUT);
  pinMode(rightBackwardPin, OUTPUT);
  pinMode(leftForwardPin, OUTPUT);
  pinMode(leftBackwardPin, OUTPUT);
}

//Looping to test the wheels of the car
void loop() {
  forward();
  stopCar();
  backward();
  stopCar();
  left();
  stopCar();
  right();
  stopCar();
}

//Setting the wheels to go forward by setting the forward pins to HIGH
void forward(){
  digitalWrite(rightForwardPin, HIGH);
  digitalWrite(rightBackwardPin, LOW);
  digitalWrite(leftForwardPin, HIGH);
  digitalWrite(leftBackwardPin, LOW);
  delay(runTime);
}

//Setting the wheels to go backward by setting the backward pins to HIGH
void backward(){
  digitalWrite(rightForwardPin, LOW);
  digitalWrite(rightBackwardPin, HIGH);
  digitalWrite(leftForwardPin, LOW);
  digitalWrite(leftBackwardPin, HIGH);
  delay(runTime);
}

//Setting the wheels to go right by setting the rightBackwardPin and leftForwardPin to HIGH
void right(){
  digitalWrite(rightForwardPin, LOW);
  digitalWrite(rightBackwardPin, HIGH);
  digitalWrite(leftForwardPin, HIGH);
  digitalWrite(leftBackwardPin, LOW);
  delay(runTime);
}

//Setting the wheels to go left by setting the rightForwardPin and leftBackwardPin to HIGH
void left(){
  digitalWrite(rightForwardPin, HIGH);
  digitalWrite(rightBackwardPin, LOW);
  digitalWrite(leftForwardPin, LOW);
  digitalWrite(leftBackwardPin, HIGH);
  delay(runTime);
}

//Setting the wheels to go stop by setting all the pins to LOW
void stopCar(){
  digitalWrite(rightForwardPin, LOW);
  digitalWrite(rightBackwardPin, LOW);
  digitalWrite(leftForwardPin, LOW);
  digitalWrite(leftBackwardPin, LOW);
  delay(1000);
}

STEP 5: Connect the Sensor Shield and Mega to the Circuit 

  1. Place the sensor shield on the Mega making sure the connection pins are placed in the correct slots. 
  2. Connect the 5V power line to the Vcc screw connection terminal on the Sensor Shield. 
  3. Connect the Ground line to the GND ground screw connection terminal on the Sensor Shield. 
  4. Connect the 4 wire harness to the following pins on the Sensor Shield:
    Red - > pin 7
    Brown -> pin 5
    Black -> pin 4
    White -> pin 2
Sensor Shield Connections

STEP 6: Test It 

Now with all the connections made, it's time to see if works.
  1. Secure the top plate and secure the Mega board to the top plate (using the small screws). 
  2. Turn the switch to the ON position. 
  3. The motors should start to turn soon after it is turned on.
  4. The green LED on the Mega board should come on and a red LED may be lite or flashing on the Sensor Shield, indicating that is has power. If there is no power,  try pushing the button on the Motor Driver board to turn on the external power.
  5. Verify the direction that the motors are turning in. Both motors on any given side should always turn in the same direction. 

If everything is working, Attach the wheels and finish securing the front and back metal plates.