Intro: BaW-Bot Part 2: Build the Motor-controller & Body
This is the second part of our BaW-Bot (Bells-and-Whistles Bot) build – 5 separate instructables that look into different Arduino-related technologies, combining to create a Bot with all the bells-and-whistles.
Part 1: Build an Arduino on a Board
Part 2: Build the motor-controller & body
Part 3: Adding Sight and Touch
Part 4: Blinging up the BaW-Bot
Part 5: Taking it to the Next Level
In this instructable, we’ll be putting a simple motor-controller together, hooking together a simple body to prototype it on, and connecting it to the Arduino from Part 1 to test it. This instructable is a simplification of a previous one, Using the Sparkfun Motor Driver 1A Dual TB6612FNG - a Beginner's Guide.
Step 1: The Parts
Collect the following parts together. As this is part 2 in a series, it assumes that you'll be using the Arduino on a Board from Part 1 - if you're not, then you'll need an Arduino Board (I recommend an Uno R3, as the pin count and configuration will match this series)
1 x Sparkfun Motor Driver 1A Dual TB6612FNG
1 x set of Header pins
1 x half-size Breadboard plus connecting wires
8 x 1N4001 rectifier diodes (or similar)
1 x 6V power supply (I used 4x 1.2V NiMH rechargeables in a holder)
2 x DC motors not exceeding the driver's current and voltage rating (eg. Pololu's 250:1 micro metal gearmotor)
2 x Mounting brackets and wheels (Pololu's extended gearmotor brackets and 32x7 wheels)
1 x Castor
1 x Corrugated Card Box for a simple body
Step 2: Solder Connections
If you haven’t already, you’ll need to solder a few connections:
Solder the header pins onto the TB6612FNG
Solder pins onto the battery holder, to allow it to connect to the breadboard. This battery holder is for the motor power (we need to keep the power for the motors separate from the power that runs the Arduino)
Solder jumper cables onto the motors
Step 3: Prepare Your Breadboard
The motor driver is on a separate half-size breadboard, so that it can stack above the Arduino on a Board. You could run this off the same board as the Arduino, if you used a full-size board.
I’m using one power rail on the breadboard to power my motors, and the other power rail to carry the 5V regulated power from the Arduino on a board. We need to connect the GND from both sources so that we’re working off a common GND – but never the positive supplies!
Place your Motor Driver at one end of the board, giving you space on the rest of the board to place the diodes and other connections.
Connect Arduino Power to your Motor Driver board. Connect the 5V from the Arduino on a Board to the VCC on the motor driver, and the GND to the GND. Make sure you are connecting these to the power rail that doesn’t provide power to the motors.
Step 4: Connect the Motor Control Pins
We’re now connecting the motor power and control pins (A01 and A02 / B02 and B02).
The motor driver switches the polarity on the 01 and 02 pairs for motor A and B to control the direction each motor spins. PWM pulses through the same connectors control the speed of each of the motors.
Connect the A01/A02 & B01/B02 from the motor drive to points on the breadboard. Space the jumper wires 2 points apart to allow us to connect the diodes in the next step, as well as to keep it compact.
Step 5: Connect the Motors to the Board
Motor Driver Power
Connect the motor power rail (6V separate source) of the breadboard to the motor driver VM pin.
Back EMF Protection
In order to protect the driver and circuit against back EMF, we need to connect 2 rectifier diodes to each connection that the motors make to the breadboard. This setup provides Back EMF protection regardless of the direction the motor may spin.
One diode connects the motor terminal to Positive, the other diode connects the motor terminal to GND. It's important to get the polarity right, so connect as follows:
- Motor Terminal ---> Positive: white stripe of diode to the positive power rail
- Motor Terminal ---> Ground: white strip of diode to the motor terminal
Then, connect the motor to the breadboard.
Repeat this for each of the 3 remaining motor outputs
Motor Power Source
Finally, connect the Motor Power connector to the Motor power rail
Step 6: Connect the Control Pins
We’ll now connect the pins on the Arduino that tell the motor driver what to do.
As we’re connecting the Arduino on a Board that we built (and not an Uno etc.), you need to remember that the pin numbers on the ATmega below are the logical, not physical ones. To remind you, I’ve included the logical pin numbers in a diagram above, the ones in blue square parentheses (eg. logical pin 4 = physical pin 6).
Connect as follows:
- ATmega Pin 5 --> PWMA (Speed for motor A)
- ATmega Pin 6 --> AIN1 (Direction #1 for Motor A)
- ATmega Pin 7 --> AIN2 (Direction #2 for Motor A)
- ATmega pin 8 --> STBY (Disconnect / Connect Motors)
- ATmega Pin 9 --> PWMB (Speed for motor B)
- ATmega Pin 10 --> BIN2 (Direction #2 for Motor A)
- ATmega Pin 11 --> BIN1 (Direction #1 for Motor B)
Step 7: Assemble the Body
To get things going quickly, we’re only using a cardboard box as a body. It works as a prototype body, allowing you to build a more robust better-designed body once you've seen where all the sensors etc fit. All you need to do here is to:
- Connect the wheels to the motor shafts
- Bolt on the two motors to the front of the box
- Bolt on the castor to the rear of the box
Step 8: Code to Drive a Simple Pattern
Now it’s time to test that we’ve connected everything correctly, before moving on to the next part in the series. Use the Arduino IDE, and upload the sketch as set out in Part 1 of the BaW-Bot series.
I’ve written a few key functions to simplify the control of the motors:
motorDrive – Drives a specified motor in a clockwise/counter-clockwise direction at specified speed
motorBreak – applies short-braking to stop the motor dead
motorStop – stops motor, allowing it to coast to a stop
motorsStandby – stops / starts both motors.
The sample code using these functions drives a simple pattern, allowing you to test that everything works. Tweak and play, as we’ll be using these functions in Part 3 of the BaW-Bot series, when the bot reacts to its environment.
Once you test the motors, you may need to switch the wire connections from the motor to the breadboard - if a motor turns forward instead of backwards, switch the contacts to correct.
Once you've got this going, add sight and touch to your BaW-Bot, in part 3 of the series.