tomroyal.com

Mousebot, Part Three: Motors

Armed with some plans and a bunch of miscellaneous parts, the first big challenge is to get Mousebot moving.

The obvious answer would be to hook up two small DC motors to the Arduino’s digital output pins, then simply set those to HIGH to move it forward and LOW to stop. This has a couple of problems, though: there would be no way to reverse the motors, and so no way to steer the Mousebot, and the Arduino can’t supply much current, so attempting this would probably destroy it.

So there are two problems to solve: how to run the motors from a separate power supply, merely switching it from the Arduino, and how to rig the motors so that current can be supplied in both directions.

Isolating the motor power

In principle, isolating the motor’s power supply and switching it from the Arduino should be pretty easy. A transistor can be used as a kind of switch, and I had a few of those to hand, so after checking out which pin is which (thanks, internet) I connected one up like this (excuse the dodgy diagram):

So far so good – when the Arduino digital pin goes high the LED lights, and nothing catches fire. Stick a motor in there instead of the LED and resistor and it should go and stop on command. But how on earth to control the motor in both directions? This turned out to be a bit fiddlier.

Two way control

A look on the web tells me that, in order to control a motor in both directions, you need something called an H Bridge. It’s a simple concept – something like this:

Close switches 1 and 4 and the motor runs one way, or chose 3 and 2 and it runs the other way. Substitute four transistors for the four switches, rig them to Arduino outputs and I’d have two way control from the 3V supply. But then I need to control two motors independently, so that’d take eight transistors, eight outputs. That’s going to take quite a bit of breadboard.

Buying it in

One way around the effort of making two H-bridges is to buy them: you can get several bridge-on-a-chip components that include the entire thing, ready to solder. The problem is that most are designed to handle motors that take at least five volts – mine only require 1.5 to 3v. Eventually, however, I came across this: the Pololu Micro Dual Serial Motor Controller.

Everything you need to know about this little chip is in the name. It’s tiny, it controls two motors and it connects via Serial. Sold. I grabbed one, picked up a second identical motor (Maplin WC68) and started to connect the whole lot up. To connect to an Arduino Duemilanove, I used (pins numbered left to right when the component-side of the board is facing you):

1) Motor +ve (3v, for me)

2) Ground (common: connect Arduino ground and 3v battery -ve)

3) Arduino +5v

4) Serial – I used Arduino pin 3

5) Reset – Arduino pin 2

6 and 7) Motor one. I also connected a .1F capacitor across these

8 and 9) Motor two – again, with capacitor

Note that I used pin 3 for the serial transmission, which is not the Arduino’s serial out pin (that’s 1) – I had difficulty getting it to work with the standard Arduino serial.write command, so used the SoftwareSerial command to send serial over pin 3, but you could use any digital pin. The resulting mess looks a bit like this:

Once it’s all connected, you need to program the Arduino.

Coding it up

Programing the Pololu isn’t tricky, but does require a little preparation. The key is to reset it (pin 3 low, wait, high, wait while it initialises) then send four-byte commands. I found it far easier to use softSerial.print() rather than serial.write(), which seemed to generate surprisingly erratic results, hence using pin 3. Here’s my test code: each motor is run for two seconds, in turn:

#include <SoftwareSerial.h>
#define rxPin 4
#define txPin 3
SoftwareSerial softSerial =  SoftwareSerial(rxPin, txPin);
int resetPin =  2;

void setup() {
pinMode(rxPin, INPUT);
digitalWrite(txPin, HIGH);
pinMode(txPin, OUTPUT);
softSerial.begin(9600);
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, LOW);
delay(1000);
digitalWrite(resetPin, HIGH);
delay(1000);
}

void loop() {
// motor 1
softSerial.print(0x80,BYTE); //start command
softSerial.print(0x00,BYTE); //sets device type
softSerial.print(0x03,BYTE); //set motor and direction (0,1,2,3)
softSerial.print(0x7F,BYTE); //full speed
//  stop
delay(2000);
softSerial.print(0x80,BYTE); //start command
softSerial.print(0x00,BYTE); //sets device type
softSerial.print(0x03,BYTE); //
softSerial.print(0x00,BYTE); //stop
delay(2000);
// motor 2
softSerial.print(0x80,BYTE); //start command
softSerial.print(0x00,BYTE); //sets device type
softSerial.print(0x00,BYTE); //set motor and direction (0,1,2,3)
softSerial.print(0x7F,BYTE); //full speed
//  stop
delay(2000);
softSerial.print(0x80,BYTE); //start command
softSerial.print(0x00,BYTE); //sets device type
softSerial.print(0x00,BYTE); //
softSerial.print(0x00,BYTE); //stop
delay(2000);
}

Next steps will be to tidy this up into a more usable set of commands, work out how to sense from the microswitches and actually fit the motors onto some sort of chassis.

All © 2019 Tom Royal. Theme available here.