Controlling motors with an Arduino is a fun and generally integral part of the learning process for most up-and-coming embedded electronics enthusiasts. Or quite simply, using motors is fun ’cause you can make robots, tanks and stuff that moves. And thanks to Freetronics we have their new HBRIDGE motor shield for Arduino to review, so let’s check it out and get some things moving with it.

Arriving in retail-friendly packaging, the HBRIDGE can be stored with the included reusable packaging, and also has a quick-start guide that explains the technical specifications and URLs for tutorials:

The shield is compatible with the latest R3-series Arduino boards including the Leonardo and of course the Freetronics Eleven board:

Specifications

The HBRIDGE shield is based on the Allegro A4954 Dual Full-Bridge DMOS PWM Motor Driver. For the curious, you can download the data sheet (pdf). This allows very simple control of two DC motors with a maximum rating of 40V at 2A, or one bipolar stepper motor. Unlike other motor shields I’ve seen, the HBRIDGE has a jumper which allows the power supply for the motor shield to be fed into the Arduino’s Vin line – so if your motor power supply is under 12V DC you can also power the Arduino from the same supply. Or you can run the motors from the Arduino’s power supply – if you’re sure that you won’t exceed the current rating. Frankly the former would be a safer and this the preferable solution.

The motor(s) are controlled very simply via PWM and digital logic. You feed the A4954 a PWM signal from a digital output pin for motor speed, and also set two inputs with a combination of high/low to set the motor direction, and also put the motor controlled into coast or brake mode. However don’t panic, it’s really easy.

The chassis is pretty much a standard tank chassis with two DC motors that run from an internal 9V battery pack. Search the Internet for “Dagu Rover 5” for something similar. Connection is a simple manner of feeding the power lines from the battery and the motor wires into the terminal block on the HBRIDGE shield.

Next, take note of two things. First – the slide switches below the jumpers. Using these you can select the maximum amount of current allowed to flow from the power supply to each motor. These can be handy to ensure your motor doesn’t burn out by drawing too much current in a stall situation, so you can set these to the appropriate setting for your motor – or if you’re happy there won’t be any issues just leave them both on 2A.

The second thing to note is the six jumpers above the switches. These control which digital pins on your Arduino are used to control the motor driver. Each motor channel requires two outputs and one PWM output. If you leave them all on, the Arduino pins used will be the ones listed next to each jumper, otherwise remove the jumpers and manually wire to the required output. For the purposes of our demonstration, we’ll leave all the jumpers in. A final word of warning is to be careful not to touch the A4954 controller IC after some use – it can become really hot … around 160 degrees Celsius. It’s the circled part in the image below:

So back to the DC motors. You have two digital outputs to set, and also a PWM signal to generate – for each channel. If you set the outputs to 1 and 0 – the motor spins in one direction. Use 0 and 1 to spin the other way. And the value of the PWM (0~255) determines the speed. So consider the following sketch:

Arduino

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

// Freetronics HBridge shield demonstration

intmotora1=4;

intmotora2=7;

intmotoraspeed=6;

intmotorb1=3;

intmotorb2=2;

intmotorbspeed=5;

voidsetup()

{

pinMode(motora1,OUTPUT);

pinMode(motora2,OUTPUT);

pinMode(motoraspeed,OUTPUT);

pinMode(motorb1,OUTPUT);

pinMode(motorb2,OUTPUT);

pinMode(motorbspeed,OUTPUT);

delay(5000);

}

voidallOff()

// turns both motors off

{

digitalWrite(motora1,LOW);

digitalWrite(motora2,LOW);

digitalWrite(motoraspeed,LOW);

digitalWrite(motorb1,LOW);

digitalWrite(motorb2,LOW);

digitalWrite(motorbspeed,LOW);

}

voidgoForward(intspeed)

{

digitalWrite(motora1,HIGH);

digitalWrite(motora2,LOW);

digitalWrite(motoraspeed,speed);

digitalWrite(motorb1,HIGH);

digitalWrite(motorb2,LOW);

digitalWrite(motorbspeed,speed);

}

voidgoBackward(intspeed)

{

digitalWrite(motora1,LOW);

digitalWrite(motora2,HIGH);

digitalWrite(motoraspeed,speed);

digitalWrite(motorb1,LOW);

digitalWrite(motorb2,HIGH);

digitalWrite(motorbspeed,speed);

}

voidturnRight(intspeed)

{

digitalWrite(motora1,LOW);

digitalWrite(motora2,HIGH);

digitalWrite(motoraspeed,speed);

digitalWrite(motorb1,HIGH);

digitalWrite(motorb2,LOW);

digitalWrite(motorbspeed,speed);

}

voidturnLeft(intspeed)

{

digitalWrite(motora1,HIGH);

digitalWrite(motora2,LOW);

digitalWrite(motoraspeed,speed);

digitalWrite(motorb1,LOW);

digitalWrite(motorb2,HIGH);

digitalWrite(motorbspeed,speed);

}

voidloop()

{

goForward(255);

delay(1000);

turnLeft(200);

delay(1000);

goBackward(255);

delay(1000);

turnRight(200);

delay(1000);

}

Instead of chasing the tank chassis with a camera, here it is on the bench:

Now to try out a stepper motor. You can control a bipolar motor with the HBRIDGE shield, and each coil (pole) is connected to a motor channel.

Hint – if you’re looking for a cheap source of stepper motors, check out discarded office equipment such as printers or photocopiers.

For the demonstration, I’ve found a random stepper motor from a second-hand store and wired up each pole to a channel on the HBRIDGE shield – then run the Arduino stepper motor demonstration sketch by Tom Igoe:

Arduino

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

/*

Based on example by Tom Igoe included in Arduino IDE

at File -> Examples -> Stepper -> stepper_oneRevolution

Modified to suit pinouts of Freetronics HBridge Shield

*/

#include <Stepper.h>

constintstepsPerRevolution=240;// change this to fit the number of steps per revolution

// for your motor

// initialize the stepper library using the default pins on the HBridge Shield:

SteppermyStepper(stepsPerRevolution,4,7,3,2);

voidsetup(){

// set the speed at 200 rpm:

myStepper.setSpeed(200);

// initialize the serial port:

Serial.begin(38400);

}

voidloop(){

// step one revolution in one direction:

Serial.println("clockwise");

myStepper.step(stepsPerRevolution);

delay(1000);

// step one revolution in the other direction:

Serial.println("counterclockwise");

myStepper.step(-stepsPerRevolution);

delay(1000);

}

With the following results:

Considering it was a random stepper motor for which we didn’t have the specifications for – it’s always nice to have it work the first time! For more formal situations, ensure your stepper motor matches the power supply voltage and so on. Nevertheless it shows how easy it can be to control something that appears complex to some people, so enjoy experimenting with them if you can.

Competition

Thanks to Freetronics we have a shield to give away to one lucky participant. To enter, clearly print your email address on the back of a postcard and mail it to:

H-Bridge Competition, PO Box 5435 Clayton 3168 Australia.

Entries must be received by the 20th of September 2013. One postcard will then be drawn at random, and the winner will receive one H-Bridge shield delivered by Australia Post standard air mail. One entry per person – duplicates will be destroyed. We’re not responsible for customs or import duties, VAT, GST, import duty, postage delays, non-delivery or whatever walls your country puts up against receiving inbound mail.

Conclusion

As demonstrated, the HBRIDGE shield “just works” – which is what you need when bringing motorised project ideas to life. The ability to limit current flow and also power the host board from the external supply is a great idea, and with the extra prototyping space on the shield you can also add extra circuitry without needing another protoshield. Very well done. For more information and to order, visit the Freetronics website. Full-sized images are on flickr. And if you made it this far – check out my new book “Arduino Workshop” from No Starch Press.

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitter, Google+, subscribe for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other – and we can all learn something.

Note – The motor shield used in this article was a promotional consideration supplied by Freetronics.