Bluetooth Toy Car

This is a continuation of our exploration of the Bluetooth module.  In this post we will set up the PIC to drive a little car and steer it using Bluetooth commands sent from a phone or tablet.  I bought a nice 3-wheel car kit on ebay for less than $15 but while I waited for it to arrive from China I made a test platform.  In true junk box fashion, the test car was made out of some scrap plywood, plastic caster wheels from some long forgotten piece of furniture, rubber bands to act as tires on the drive wheels, and a couple of used hobby motors with gearboxes.  If you are thinking that I must have a pretty big junk box, you are correct.  From the outside it looks exactly like a 500 square foot garage.

Motor Drivers

Since the PIC can’t supply enough current to directly drive motors, we need to add a bit of hardware.  There are actually a couple of options to accomplish that task and I will detail two of them here.  The first thing to decide is whether or not you want your car to be able to go in reverse.  Well “Duh!” you might be saying, but hold on.  Adding a reverse capability basically quadruples the part count for only a 33% increase in maneuverability.  In other words, if you only have a forward gear you can still go left or right or even turn the car around.  You can do that by simply running one motor at a time whenever you want to make a turn.  Run it long enough and you will be facing back in the direction you came.  What you can’t do is back out of a corner.  With that in mind, let’s go on to the next step and see how we can make a simple drive circuit.

Forward, Ho!

Transistor Motor Drive

Since we have one motor on each drive wheel of our 3-wheel car, we need two copies of the circuit shown in the diagram.  The heart of the circuit consists of a power transistor that can handle the current required by a motor.  We also need a resistor between the PIC and the base of the transistor to limit the drive current out of the PIC.  That all makes sense but what about the diode shown in the diagram?  Well, this is one time that we might say “May the force NOT be with you”.  The force I’m talking about is the reverse electromotive force (EMF) that the motors generate.  When you apply power across the motor windings it acts like a motor, which is what we expect.  But at the same time it is also acting as a generator and trying to feedback a voltage that is opposite of the polarity of the input voltage.  All is well until we remove power from the motor because the reverse EMF doesn’t stop until the motor stops spinning.  During that short interval there is no opposition for the reverse EMF so there is a voltage spike of the wrong polarity feeding back into our precious electronics.  Just think of the reverse polarity diode as our Jedi Knight protecting us from the evil force that is trying to destroy our transistor.

The parts I used are listed in the diagram but pretty much any NPN power transistor can be used and any member of the 1N400x family of diode (or equivalent) will work.  The resistor needs to be large enough to limit the current from the PIC while being small enough to fully turn on the transistors.  For instance, the TIP41C has a minimum current gain of 30 when driving up to a 300ma load.  Something in the range of 470 to 680 ohms should work and will keep the PIC current safely below the 25ma per pin maximum.

Backward Thinking

L298N Board

In the previous step we saw the basics of what we need to drive each wheel.  In order to add reverse we need to quadruple the logic so that we can control the voltage polarity on each side of each motor.  This is commonly known as an H-Bridge and there are lots of examples out on the web of how to build one from discrete components.  Thankfully, someone actually put together an integrated circuit version that can drive two motors.  Even better, there are complete circuit boards available with connectors and the supplemental components needed for our application.  And, yes, they are cheap.  Just search for L298N.  That happens to be the designation of the integrated circuit but it is also commonly used for the complete circuit card.  There are a couple of different versions but one of the most common is the one shown in the picture.  You can tie the +5V and the +12V inputs together to your +5V source but then you need to remove the jumper labeled “5V enable”.  When the “5V enable” jumper in on, the +12V input is routed to a +5 volt regulator.  That eliminates the need for a separate +5 volt input.  The “A enable” and “B enable” jumpers remain on as shown.  “Output A” and “Output B” each provide the connections to drive one motor.

PIC Hardware Connections

PIC Bluetooth and Motor Interface

The connections to the PIC are a bit different than in the previous Episode on Bluetooth.  First, we have no need to connect to the RXD input of the Bluetooth so that connection is gone.  Second, we need four I/O port outputs to control the drive motors.  These lines connect to the “Input” terminals on the L298N board to control the forward and reverse for each motor.  The battery pack I used consists of 4 AA batteries and puts out a nominal +6VDC.  All of the circuits used here (including the L298N) are within spec at this voltage so there is no need to reduce it to +5 volts.


The software link is listed below.  While it is targeted for the 16F688, it is easily ported to other versions of the PIC.  You will need to change the line that identifies the PIC version (LIST=) and the INCLUDE file but those are intuitive changes.  The __CONFIG line may also need tweaking just because one or two of the labels used are spelled differently in some of the INCLUDE files.

The software is really pretty simple because we just wait for single character inputs from the Bluetooth and then turn on/off the appropriate motor direction controls.  As mentioned in the previous Episode, the Bluetooth program I use is called “Bluetooth Serial Controller” by Next Prototypes.  I set mine up with five buttons to send commands for “Forward”, “Reverse”, “Left”, “Right”, and “Stop”.  I also set it up so that it would automatically repeat the command every 200ms if I continued to press the button.  That’s helpful when turning left or right.  You will also notice that in order to turn in a particular direction you turn off the motor on that side and turn on the motor (forward direction) on the opposite side.  It makes sense if you think about it.  If you want to experiment, you can do it the other way around by turning off the motor on the opposite side and reversing the motor for the side you want to turn toward.  Because I originally built the test car with no reverse, I just left the software directional control the same when I got the L298N and added reverse.


The Cars



Picture 1 is the junk box beauty, but with an Ultrasonic Module instead of the Bluetooth Module.  The socket for the Bluetooth is on the right side of the picture.  I tried to use the Ultrasonic Module to make the car autonomous but ran into some serious limitations of the module when trying to avoid obstacles around the house.  Picture 2 is the kit car I bought.  It came with the motors, wheels, platform, and battery holder but no on/off switch.  Still, it’s not a bad kit for less than $15.  In the back of the picture you can see the Bluetooth Module and then the L298N board behind it.  Both of these cars are front-wheel drive and both evoke a high interest level in our cats.  That’s it for this post.  Check out my other electronics projects.