DIY Self-Driving - Part 3. Wiring Harnesses

Please Note:  Unlike most of my projects which are fully complete before they are published, this project is as much a build diary as anything else.  Whilst I am attempting to clearly document the processes, this is not a step-by-step guide, and later articles may well contradict earlier instructions.

If you decide you want to duplicate my build, please read through all the relevant articles before you start.

In the previous post we looked at getting a motor driver up and running, and I realised I breezed over the process of how it was all connected.  This article will (hopefully) address that shortcoming.

Because this is a mobile project, vibration and the like will take a toll on connections and as we know, poor connections will lead to poor results.  For this reason, I am going to take you through my process of making up wiring harnesses to suit our car.  Clearly unless you are using the same car and modules as me your wiring is going to vary, but I hope this will get you headed in the right direction.

A bit about wire gauges
Even though this is relatively low power (for an electrical machine), we cannot use light-gauge hookup wire everywhere if we want to have a reliable result.  So I have used three different gauges in this project:

17AWG - For the battery and motor connections.  This is rated at around 8A.
 nnAWG - for the 5V and lighting connections.  Rated around 4A.
Rainbow hookup wire.  I use this for control signals.

Proposed Wire Colours
You don't have to follow my scheme, but at least stick to whatever you do use.  My colour coding is as follows:
  • Red: Unregulated battery Voltage
  • Black: Ground
  • Orange: 5V regulated
  • Blue: Head/Tail Lights
  • Yellow: Indicators
The various sensors are wired using the rainbow cable, and I will call these out as we go along.

The basic electrics in my car are all connected with either bullet or spade connectors.  You can get these from your friendly auto parts store or from an electronics retailer.

The controllers and sensor elements will need DuPont style connectors.  I don't know how many I will need yet, but a pack of 500 is pretty cheap.  Make sure you get both pin and socket types.

While you can terminate all of the above connectors with a pair of pliers, don't do it.  Get a pair of crimpers.  It will take less time and give you a better result.

If you wish to crimp and solder, be my guest.

Measure three times, cut once
Not much is going to be as frustrating as finding out that your beautifully-terminated cable is 1cm too short or twice as long as it needs to be.  I find that using a length of scrap wire to plan out a run works well.  Mark the length on that wire and cut your final cables against it.  This way you can allow for any challenges in your path.

Secure your wiring
Next to the quality of connections in my opinion is making sure the wiring is safe and secure:  
  • Don't pull the cables tight, but keep them neat.
  • Try to avoid attaching cables to things which move.  If you must do this, allow for the movement, and sheath the cable to protect it.
  • Anchor your wiring (those little self-adhesive mounts are great) to keep it out of harm's way
Test as you go
I recommend that you break your wiring into functional sections.  This way you can test each element as you proceed.  Some might argue it's not necessary, but I bell out (do a continuity check) on each cable before installation.

As you add each section, test it completely, or as completely as you can.  Verify each function cable carries the voltage or signal you expect.  Use a voltmeter as well, and check that you are getting the voltage you expected.  If a cable is supposed to carry five volts, but you're seeing half of that then chances are there is a bad connection somewhere in the chain.  By doing things in sections you can more easily backtrack to find the problem.

Finally, don't put problems off.  If something is not working, or is intermittent at this early stage you can be sure it is going to cause you bigger issues as the car gets more complex.  If you can't get your head around a problem by all means take a break or find some stickers for the body or something.  But don't forget to come back and fix the problem before you go on to the next stage.

A note on safety
People will often dismiss safe practices when working on battery-powered equipment.  When working on any kind of vehicle (or even a lot of flying models these days too) the batteries can deliver a very high current, especially with lithium-ion batteries.

While the power levels are not lethal in and of themselves, a short on a lithium battery, or the main battery for that matter can melt wiring and possibly start a fire.  Just be careful as these cars are mostly plastic and therefore quite flammable.

Schematic diagram for stage 1

Further Reading
Part 1 - Introduction
Part 2 - Preparing the car
Part 4 - Steering
Part 5 - Sensors
Part 6 - Software
Part 7 - Final assembly and testing

DIY Self Driving - Part 2. Preparations

Please Note:  Unlike most of my projects which are fully complete before they are published, this project is as much a build diary as anything else.  Whilst I am attempting to clearly document the processes, this is not a step-by-step guide, and later articles may well contradict earlier instructions.

If you decide you want to duplicate my build, please read through all the relevant articles before you start.

Last time we were discussing the challenges of building your own self-driving car (albeit at about 10% scale).  I've now moved past the daydream phase of the project and it's time to get something happening.

From this...
To this...
First handy hint, get the car up high so you can move around and work on it without bending over all the time.  I borrowed a guitar stool from one of my sons and balanced the car quite nicely on that.  A portable workbench would also do the job nicely.

I will pay tribute to the people at Lovbio, because this car really is beautifully made.  Everything is screwed together, so taking it apart was much easier than I anticipated.  I also discovered a really useful storage space under the bonnet ("hood" to my American friends).

One of the other goals I have is to re-use as much of the original wiring and components as practical.  Everything is connected with bullet or spade connectors so this should not be too difficult, and the wire gauges used seem more than adequate for the task.  This thing was built to last.

Original battery and wiring.  See space for 2nd battery and motor?
It made sense to start by working out how the motor was connected.  This turned out to be pretty straightforward.  The battery is switched on/off by the foot pedal and the motor direction is determined by a rocker switch on the dashboard.  The nice thing is that the switch was fitted into a block socket which presented all the wiring I needed in one spot.

To confirm, I pulled the switch and jumpered out the pins which I thought corresponded to "Forward", and pushed the pedal.  I was rewarded by the motor rotating the proper direction.  For future reference, I labelled the connector.

Testing the motor connector

Sorting out this little bit of wiring also made locating the motor driver module obvious.  Fit it to the kick panel near this connector.  From there its outputs go to the "Forward" pins on the motor connector.

I wanted to add a "master" switch for the motor driver.  Rather than drill a hole somewhere, I used the existing light switch (which had been bypassed for reasons I don't understand).  Turning this on now supplies power to the motor driver module, and you will be rewarded with a healthy red glow from it's power indicator.

First signs of power!
You can also see in this photo where I removed the foot pedal in order to bypass that switch without cutting things up.

Whilst pulling the wiring apart I found the lights had been hard-wired to the switched power, so they will come on automatically when the pedal is pressed.  This didn't seem too lifelike to me, so I wanted to do it better.  The first discovery was that they were tiny lilliput globes, which would have trouble competing with a candle for brightness.  Since the lamp assemblies come apart easily it's easy to replace the head and tail lamps with LEDs.

I also discovered that the assemblies already had a hole for an "indicator", which fits a 5mm LED just nicely.

Reworking the headlights
Note the much more modern appearance now
When I removed the bonnet I was happy to discover a nice shelf which ran the full width of the chassis.  This means I can mount the Raspberry Pi, regulator and control relay as well as the auxiliary battery together and out of sight.

"What's this about Auxiliary Battery?" I hear you ask?  I discovered there is a lot of noise from the electric motor on the power rails, which is not good for microprocessors.  Also, since the electrics a six-volts I will be marginal on power at the best of times, and I need the power to be stable.

My electronics don't need massive current, so I figure a small Lithium-ion pack will suffice.  A dual cell pack will give me 7.4V which I can regulate down nicely. 

The relay is driven off the master switch, so that the electronics are powered on automatically, yet kept isolated.  I haven't quite figured the shutdown part yet, but I'll get there.

Testing the electronics battery
At this point, we can start to test something useful.  Load up the "" file from my github repo ( and the motor should alternate between forward and reverse direction.

I found that I had some unreliability in the motor drive controls.  I suspect this was due to the motor driver being designed for a 5V input while the Raspberry Pi is 3.3V.  In theory, this should have been OK, but in my case it was not.  I installed a level-converter between the Raspberry Pi and the driver module and things have been stable ever since.

Not bad for an afternoon's work!

Running the first update

Further Reading

Part 1 - Introduction
Part 3 - Wiring Harness
Part 4 - Steering
Part 5 - Sensors
Part 6 - Software
Part 7 - Final assembly and testing

DIY Self-Driving

There's nothing like  a ridiculous challenge to keep me amused, and this one is huge.  There are no instruction manuals or how-to guides, and definitely more questions than answers at this point.

I am trying to build a self-driving car.  

Not a road-going car exactly, but I have taken one of those kid's electric cars (about 10% size licensed copies of real cars), and I intend to make it capable of driving itself, following a road, avoiding obstacles and such like.

UPDATED:  Watch a timelapse (kind-of) video of the build up:

But why use that?  Surely it would be easier to use a remote control chassis as a starting point?

Yes, you're right.  In a lot of ways it would be easier.  The biggest problem is that those chassis are rather small, which makes packaging difficult and it can limit the options I want to add.  My method gets around that, and I get more battery power for no cost as well.  Plus it really looks like a car (if you squint hard enough, anyway).

By way of explanation, my white convertible was made some years ago by a Taiwanese company called "LovBio", and it is called a "V6".  Externally it looks like a mid-80's Mercedes to which somebody added a BMW-esque kidney grill.

A six-volt electric motor drives the right side rear wheel, and the electronics consists of a collection of switches.

Just like using a real car, the mechanical challenges are at least as big as the computational ones.  For starters, I have to rebuild the steering mechanism.

The steering assembly is primitive to say the very least (see the next photo) with a "J-Hook" at the end of the steering column which pushes/pulls on steering links.  There is a lot of free play in the unit, which is probably not going to be an issue in this project.  At least in the early stages.
The plan at this stage is to remove the steering column and replace the J-Hook with a heavy-duty servo.  The car is quite light, so I am guessing that a servo intended for a 1/10 or 1/8 scale model will be a good starting place.  I am going to use a "20Kg" rated model, because I have some.

I have no brakes and only one driving wheel so the dynamics might be a bit of a challenge.  We can most likely make it two-wheel drive by installing an axle in the rear, and this will probably happen sooner rather than later.

The motor in my car states 4A which I presume is the stall current.  This means I can use a L298D H-Bridge driver module but it's going to need a bigger heatsink. Ideally I would like to use PWM to drive the motor so I can control its speed directly.  Given the light weight and single driving wheel, this would help a lot with stability.  As tempting as build my own DSC system is, I don't want to get carried away with solving the wrong problem.

Astute readers will note I have not mentioned any computing solutions yet.  I will get to that later, but for now there is more than enough groundwork to do.

Further Reading
Part 2 - Preparing the car
Part 3 - Wiring Harness
Part 4 - Steering
Part 5 - Sensors
Part 6 - Software
Part 7 - Final assembly and testing

* The sticker reads "Honk if you're autonomous"

Build an obstacle-avoiding robot 1

I love building robots.  They are a great test of your creativity and problem-solving skills.  They are also a good way to introduce kids to electronics and software.

These days they are also very inexpensive to get started with, thanks to Arduino and low-cost modules.  Even if you don't know how to solder, chances are you could build the machine I describe here in an afternoon.

What is it?

It's been named "The Meerkat".  If you watch the accompanying video it's not hard to see how the kids named it.

This is a 4-wheel machine which uses sonar to detect and avoid obstacles.  To make life a little more interesting, the ultrasonic modules is steered using a small servo motor.  This allows the machine to more effectively find a clear path.

The Design

At the heart of the robot is an Arduino Mega2560.  This board has all of the I/O we need and is programmed with the Arduino IDE application.

The Arduino board cannot drive the motors by itself, the I/O ports cannot source or sink the necessary current.  To solve this problem, we use an L298 H-Bridge driver module to drive the motors.  Using an H-Bridge means we can reverse the polarity (and thus the direction) or a motor without needing relays.  It also allows us to use Pulse-Width Modulation (PWM) to control the motor speed.  So we need three control lines per motor.  There are four motors to drive, so we need twelve connections and two motor driver chips.

At present the motors on each side only ever move in the same direction, and at the same speed.  So you might wonder why not just use a two-channel driver.  You could do this if you want to save a little money.  But I wanted to build something a little more future-proof.  These low-cost motors don't always run at the same speed, so we can do a calibration and store a speed offset in the EEPROM of the board for each motor.  We could also vary the speed of motors to improve steering as well.  We're not doing either of these today, but keep watching the code releases.

Obstacle direction is via a standard HC-SR04 ultrasonic rangefinder module under the control of the "NewPing" library.  This is capable of quite fine resolution, and is more than adequate for our purpose.  We want to detect an object within a few centimetres of the chassis.

Power comes from a pair of battery packs in parallel.  If you use convention batteries, the 6-cell packs specified will supply 9 volts.  If you go the rechargeable path, then they will supply 7.2 volts.  Of course there is nothing to stop you from using lithium-ion or lithium-polymer batteries instead, however you need to make provision for charging those batteries as they have more complex requirements.

The parallel packs means plenty of current is available.  Running four motors at normal speed requires around 180mA, so the extra capacity means plenty of run time at the cost of extra weight.

There is also the ability to add a small OLED display to the unit, as shown in the pictures.  This is entirely optional but I found it very useful while I was debugging the software since it enabled me to see plainly what steps were executing. 

What will I need?

To get you started, you will need the following.  Most of my parts came from Jaycar electronics and Altronics in Australia, but you can find much the same devices anywhere.

  • 1 x 4WD Motor platform (Jaycar KR3162, Altronics K1092)
  • Arduino Mega2560 (Various sources)
  • 1 x 4-way L298 Motor Driver (Jaycar XC4460) or 2 x 2-way modules
  • 2 x 6-cell AA Battery holders
  • 1 x HC-SR04 Ultrasonic module
  • 1 x small servo motor (Altronics Z6392)
  • 1 x Bracket to suit ultrasonics
  • 1 x DPDT switch 
  • Hook-up wire
  • M3 screws and nuts (10-15mm)
  • M3 spacers 
  • Heat Shrink tubing
  • The source code
Optional Items (Good, but not essential)
  • Rechargeable batteries in place of standard AA-cells
  • 2 x  banana posts
  • Zip ties
  • SPI Compatible OLED Display (AdaFruit)
You're also going to need a PC or Mac with the Arduino software loaded, screwdrivers and a soldering iron.  A hot-glue gun would also be useful.

Assuming you had to buy all the components, the total cost should be around 150 AUD.  Not bad when you consider what it can do.

Getting Started

The best place to start is with the chassis.  There's a couple of useful things to consider when putting this together.

Motors and Wheels
If your motor have wires attached, it's almost certain that they will not be long enough to reach your driver board and you will need to extend them.  My motor driver went on the top deck at the rear, as there were two holes located there already to take it.  Solder and heat-shrink the connections.

Bolt your motors into place according to your platform.  In same cases you can choose to put the motors above or below the bottom plate.  I chose to put mine below because it frees up room for the battery packs, but it does make the finished product rather tall.

With a 3V battery, test each motor and determine the polarity which causes the motor to turn in a "forwards" direction.  Label the leads as + and - according to the battery.  This will make it easier when it comes to hooking up later.

Take some time now to properly align your wheels.  Place a straight-edge (such as a metal rule) across the wheels on each side to and align them before finally tightening up the mounting screws.  This might take a little time, but eliminating wheel drag improves battery life for free.

Mounting the modules
We have to mount the motor driver and Arduino Mega on the top deck.  As noted before, the motor driver I used matched holes in the rear part of the top deck, so I put it there.  The Arduino Mega needed new mounting holes, so work out and drill everything you need before you start assembling.  While you're there drill a hole for the power switch and battery charging terminals if you're using them.

Mounting the ultrasonics and servo
This requires a little thought.  Since only a tiny servo is required it doesn't take up much space, but at the same time this makes it tricky to mount since most commercial spacers won't clear the body of the servo.  Because it's got so little mass on it I used industrial double-sided tape (the kind for sticking badges to cars) under the servo.  Place it in the centre and running down the long axis of the chassis.  (See photos)

Attaching the servo to the ultrasonic module requires a bracket of some kind.  I made my own from acrylic.  Turned out a bit on the large side but it works well.  If you can find a long enough screw of the appropriate size just screw the bracket to the horn wheel on the servo.

Mounting the batteries
Make a design decision here. If you are using rechargeable batteries, you will need to add a connector for the charger (I used banana posts as per the photos), and the DPDT switch should isolate the electronics from the battery when charging for added protection.

However if you are using non-rechargeable batteries, make sure you have a way of getting at the battery holder(s) to change batteries without having to disassemble the whole machine.  It might not look elegant, but a long cable (zip) tie is ideal for the job.

At this point, you should have everything mounted, but not wired.  It looks pretty good for now, so let's move on to the wiring.


The source code is the definitive guide to wiring the robot, and it will override any instructions here.  All the information you require is in the file "rover_hw.h"

The motors are arranged as follows (looking from the top)
Back Right: Motor A
Back Left: Motor B
Front Right: Motor C
Front Left: Motor D

Some L298 driver boards jumper out the "Enable" pin for each motor channel.  Refer to the documentation for your board and check if there is a jumper which needs to be removed.

The "A" and "B" (or "1" and "2") inputs on the motor driver control the direction.

The "A" and "B" (or "1" and "2") inputs on the motor driver control the direction.

Connections for the motor driver are as follows:

Motor A (1)
A      => 22
B      => 24
Enable => 2

Motor B (2)
A      => 26
B      => 28
Enable => 3

Motor C (3)
A      => 30
B      => 31
Enable => 4

Motor D (4)
A      => 32
B      => 33
Enable => 5
 The motor driver board outputs are normally labelled "+" and "-".  Connect the positive and negative leads from the motors to the outputs.

If you compile the code with "BURN_IN" defined, the software will cycle through each motor in turn.  This is useful for checkout out the motor and servo wiring.

The motor driver I used also has a servo driver on-board so I used this to remove the load from the Arduino.  However, the servo is small enough that it can be driven directly if required.

The servo needs +5V, Ground and Signal.  The Signal comes from Output 6 on the Arduino.  If your driver board supports servo motors, connect Output 6 to the control or signal input on the driver and connect the servo to the driver board instead.

The HC-SR04 needs +5V and Ground, as well as a Trigger.  The detection result is returned via the Echo pin on the module.

Trigger is connected to pin 7, and Echo to pin 34.

Congratulations.  Your robot is now fully assembled.  If you wish, use some cable ties to make everything neat and secure.

The software

Like the rest of this project, the software is rather simple but you can still do a lot with it.  First of all, grab the latest release from:

Load up the Arduino IDE and open up "features.h".  For now make sure that the line:

#define OLED

has been commented out.  We will come back to that one later.  If you want to test out your motor wiring, add the line:

#define BURN_IN

to "features.h"

But in either case, make sure the robot's wheels are clear of the ground before uploading the code.

Once you have confirmed that all the motors turn in the correct direction, removed the BURN_IN from the code and upload again.

Next Steps

I don't think any hobby robot is ever really "finished", and Meerkat certainly is proof of this.  I have a lot of further plans for this little platform:
  • Wireless data transmission so we can tell what it's doing from afar.
  • Navigation
  • Battery Management
  • Additional sensors for "experiments"

Some of this code (The power monitoring) is done already, and is on the "develop" branch on  If you're feeling adventurous you can pull this branch down and see what we're up to.

Wasting your and my time

I had a really interesting experience recently which I hope might enlighten others as much as it did me: I was approached (via LinkedIn) by ...