Introduction: Arduino Bluetooth Remote Control - Part 3 - Motor Control

After the first two parts I have two Arduino boards communicating via Bluetooth. The connection happens automatically when the Bluetooth modules are powered up.

Next I want to set up remote control of an electric motor.

On the transmitter end I will have a Potentiometer and a switch to control the speed and direction of the motor.

On the receiver end I will have an electric motor and two LEDs to indicate forward and reverse.

I will use the master device for the controller and the slave for the gadget. They could work either way round but is possible to have the master controlling to up to seven gadgets so it makes sense to use the master as the controller.

The first step will be to learn how to control the motor from an Arduino board.

Step 1: Simple Motor Control

Starting with the master Arduino with nothing connected, a potentiometer and a chip will be added.

We will use:

10K potentiometer

H-bridge chip

small electric motor, I will use a "toy" motor which has a very small power requirement.

The H-bridge is a microchip which can take a digital output from the Arduino and use it to control the power to a motor. The chip is type L293D or equivalent. There are a number of chips compatible with L293D, mine is marked SN754410NE.

The Arduino is capable of creating a variable voltage on its output pins but these are intended as data signals and do not have the power to drive a useful electric motor. You can damage an Arduino by trying to draw too much power out of it so we will use a chip that can take a data input and control the flow of useful power.

The L293D specification says it can handle motor power up to 600mA and a voltage range of 4.5v to 36v.

Step 2: The Components

The potentiometer has three leads. The two outer ones are connected inside the case to each end of a resistor. The central lead connects to a movable contact. When you turn the spindle of the potentiometer the contact makes a connection part way along the resistor and the resistance value changes between the centre lead and the outside ones, and this affects the voltage at the centre lead.

By connecting one end of the "Pot" to 5v and the other to ground, the voltage at the centre lead varies from 0 to 5v as you turn the spindle. The Arduino can convert this voltage to a number through one of the analogue pins.

The H-bridge is a sixteen pin device. The top end of a chip has a small "D" indentation so you can tell which way round it goes. Looking at the top of the chip the top left pin is number 1 and they are numbered down the left side and then up the right side. This is standard practice for microchip packages.

Step 3: Wiring Up

Power Supply:

I am powering the Arduino using a 9v battery. The specified power supply is 7-12v so 9v is good. The positive goes to VIN and the negative to GND.


The potentiometer has one end lead connected to the 5v source on the Arduino. The other end lead goes to the ground connection. The middle lead goes to pin A0 on the Arduino.


The H-bridge needs a power supply from the Arduino for its "logic" side and another power supply to drive the motor. The chip is designed to keep the high power motor currents away from the sensitive logic side connected to the Arduino.

Pin16 (top right) is the 5v supply for the logic circuits in the chip.

The two centre pins (4,5 and 12,13) on each side of the chip are connected to ground. These four GND pins are all wired together internally so any of them can be used for ground. They also function as a heat sink to help keep the chip cool.

The positive supply for the motor goes to pin8 (bottom left) and the negative goes to the same ground as above. The potential for disaster can be seen if you turn the chip the wrong way round. Now the high voltage motor supply is connected to the logic side of the H-bridge and could burn it out. Fortunately L293D chips are cheap!

The Enable connection switches the motor on and off.

Inputs 1 and 2 control the direction of the motor as we will see.


The motor connects to pins 3 and 6. It doesn't really matter which way around the leads are connected.

Motor power:

The power supply for the motor is connected here. This can be a separate supply from the Arduino, only the GND connections need to be common to both. I'm taking a positive from the 9v battery to this pin to run the small motor.

Step 4: Controlling the Motor

D6 on the Arduino goes to pin1, the enable pin on the H-bridge. This pin switches the motor on and off.

The speed is controlled by rapidly switching the motor on and off. The more time the motor spends switched on the more power it will receive. By controlling how long it spends turned on compared to how long it spends turned off will control the power going to the motor. This is what the pwm output of the Arduino does.

If the motor power is on most of the time the motor will run near its full speed but if it is off a lot of the time the motor will run more slowly. As an example, a kitchen food blender has a "pulse" button which can give its motor a kick for a short time. If you press and release the pulse button a few times the motor will get going then slow down between pulses but not run as fast as if you hold the button down. This is just like pwm except that pwm switches on and off very quickly.

Pulse Width Modulation (pwm) is the official name for this rapid switching on and off. The pwm output from D6 will be used to control the motor speed.

The motor direction is simpler. This is determined by pins 2 and 7 on the H-bridge. These are wired to D5 and D4 on the Arduino.

If pin D5 is high and pin D4 is low the motor will turn in one direction. If D5 is low and D4 is high it will turn in the opposite direction.

Notice that there is another set of unused pins on the other side of the chip. With suitable wiring these could be used to control another motor.

Step 5: Program Control of the Motor

The motor speed is controlled by the Potentiometer connected to A0.

The Arduino reads the Potentiometer value as a number in the range 0 to 1023, so that's the input range.

The motor speed is controlled by using a digital output and a value in the range 0 to 255. This is the range needed for pwm output.

By dividing the Potentiometer reading by 4, the value will give the range for pwm to the motor.

The program begins by defining some constants. This gives names to the pins used for output, for example:

const int Enable1 = 6;

allows pin6 to be identified by the name "Enable1". Why bother? Because in a larger program pin6 may be mentioned many times. Now if you decide it should have been pin10, the correction can be made to the program by changing the definition at the beginning of the program listing rather than going through the whole program looking for where pin6 is mentioned.

The constant definitions don't take up any space in the Arduino because they are removed when you run Verify/Compile on the program.

The compiler translates the human readable program you see on the screen into a machine readable version. It goes through the program and replaces any mention of Enable1 with "6" so the instruction

pinMode(Enable1, OUTPUT); becomes pinMode(6, OUTPUT);

The compiler also removes any comments so you can put as many as you want in the listing and it won't make the program any bigger when it is loaded into the Arduino.

PotValue = analogRead(A0); // returns a number in the range 0 to 1023

speedValue = PotValue/4; // converts the number to range 0 to 255

digitalWrite(direct1, HIGH); // set the direction
digitalWrite(direct2, LOW);

Notice that the motor doesn't always start immediately. A lot of motors won't start on a low current. They need a "kick" to get them going then they can run slowly once they are turning.

If everything is connected up correctly, give the motor a spin with your fingers if it doesn't start.

Step 6: Adding Direction Control

The circuit has a switch and two LEDs connected.

The pin connected to the switch has the 10K resistor to GND so that the pin is held LOW when the switch is off.

Closing the switch connects 5v to the pin to bring it HIGH.

The two LEDs each have their 220R resistors to control current flow. Higher values can be used such as 550R or 1K to reduce power consumption on battery powered devices.

In the program constants are declared for the LED pins and the switch. The pin numbers match the wiring in the diagram. The LEDs are output and the switch is an input device.

A variable sw1state is used so that the switch can be tested for on or off and the value remembered.

The speed control is the same as before.

An "if" statement is used to switch things depending on the state of SW1. It says:

If the switch is on (HIGH), set the direction forward, turn on the forward LED, turn off the reverse LED


set the direction to reverse, turn off the forward LED, turn on the reverse LED

Step 7: Improved Circuit

The motor circuit can be improved by adding a small value capacitor across the motor leads. This helps to reduce sparking and electrical noise in the motor and make the motor run more smoothly.

Brushless motors have less of the problems of stalling and interference that come with cheap brushed motors.

It is a good idea also to put 1K resistors on the output connections from the Arduino. These digital output pins should not be supplying more than about 5mA of current. If there was a short circuit on a digital pin it could cause a high current flow which is unhealthy for the Arduino. Putting a 1K resistor on the connection limits the maximum current from a 5v source to 5mA. The resistance does not affect the digital signals to the L293D because these signals are communicated by the presence of a voltage at the pin even though the pin draws virtually no power.

Now we have an Arduino controlling the speed and direction of the motor. The inputs to this are the Potentiometer and the Switch. The outputs are the H-bridge controls and the LEDs. In Part 4 the inputs will be on one Arduino and the outputs on another, and Bluetooth will be used to pass information between controller and the gadget.