Self-balancing Skateboard/segw*y Project Arduino Shield

by XenonJohn in Circuits > Arduino

138456 Views, 710 Favorites, 0 Comments

Self-balancing Skateboard/segw*y Project Arduino Shield

FirstPageV3.jpg

General purpose Arduino shield for self-balancing machines.

Why did I make it?
I previously made an Instructable in 2010 on how to build a self-balancing skateboard.
https://www.instructables.com/id/Easy-build-self-balancing-skateboardrobotsegway-/

There are >500 comments on this and many express confusion setting up the balance sensors, software and electronics. On top of that, the analog output inertial measurement units that were commonly available stopped being made.

All my self balancing projects are now on my Youtube channel here: Click

Here, I have taken a low price obscure analog IMU that IS currently still made in China, that IS available on ebay, and used an Arduino prototyping "shield" to mount ALL the parts, including a cable to a basic hand-controller (for steering and fine-tuning the balance point) and a cable with just 2 wires that you connect to a 2 x 25Amp "Sabertooth" motor power controller.
I have tried to make it as easy and in particular, non-confusing as possible to build.
NOTE December 2013: Even these are getting rare now but I have just found the "Grove" series of analog sensors from Seedstudio and added contact details to page 6.

In essence a complete re-vamp of the control system, making it simpler to build at the same time.


NOTE (March 2014): I have finally made something similar to this that actually works with a modern DIGITAL IMU from Sparkfun, the 6dof sensor, code No: SEN-10121. It has its own new Instructable here:
https://www.instructables.com/id/Arduino-Self-Balance-Controller-using-DIGITAL-IMU-/

NOTE(December 2014): I have also done an Instructable using the same digital Sparkfun IMU in a self-balancing scooter. This instructable is most up to date and has the circuit diagram and latest code here:

https://www.instructables.com/id/Raleigh-Chopper-inspired-self-balancing-scooter/



I have included the basics of how to connect this to the Sabertooth motor power controller, which is an off the shelf commercial robot power controller, how to power the Sabertooth and how to connect the motors to it. For a really detailed explanation of the mechanical side of the build, take a look at my original Instructable of 2010, linked at the top of this introduction page.

One gyro is used for balancing (complementary filter with an accelerometer). Another gyro measures rate of rotation laterally (e.g. when steering).
This provides another useful feature for free; when running in a straight line, if it detects rotation faster than 10 degrees per second laterally, it will change power to the motors to resist this effect. For example the motors often have different friction so when you slow to a stop, one stops before the other and you spin off. This feature stops that happening, and means the wheels can be mounted quite close together.
See this video http://www.youtube.com/watch?v=FEaTxahyQxc and you will see this happening at 0.51 mins, the spare gyro is used to reduce this effect.

NOTE: Added 15/03/14. For those who may manage to have one of the old, no longer available, Sparkfun 5dof analog IMU's, I have just attached the same code as written in this Instructable for the Chinese IMU, to step 30 but with the gains changed for the gyro to suit the old 5dof analog Sparkfun IMU.




Main parts list

www.maplin.com part number GBP US$

N39KR RockerSwitch 2.39 3.62
N39KR RockerSwitch 2.39 3.62
GW72P Microswitch with lever 2.49 3.77
FH04E Sub-Min Toggle switch 2.79 4.23
Project Box 3.79 5.74

XR27E 9 way multicore cable 5.14 7.79
2 core screened cable 0.99 1.50
N30KU Arduino Uno 24.99 37.86
N35KU Arduino protoshield 14.99 22.71
5DOF analog IMU 17.81 26.99
NOTE: List of sellers of this updated August 25th 2013 (See Step 6 for the list)
4 x LED’s 2.56 3.88
_________________________________________________________
80.33 121.71

Video clip



Video of latest self-balancing skateboard in action using this IMU and code

Whole Assembly With Hand Controller

WholeThingAssembled.jpg
This Instructable assumes you know basics of how to load a program or "sketch" into an Arduino microcontroller and also know how to solder.

Tip: 
Even if you think you can see just fine, a pair of magnifying glasses or magnifying visor as sold in many hobby shops makes a huge difference when soldering very small parts and will last you your whole life probably.

The Shield has long pins that allow you to push it on top of the Arduino when completed. Take care as quite easy to bend some of them as you do it. It has a square grid of solder-holes on which you can mount your own components. I will mount my IMU, my LED indicator lights and the cables to the sabertooth motor controller and the hand controller securely to it also.

The shield is top left in this photograph. Arduino UNO is lower left. Hand controller, which we shall also make is lower right.

General Build Layout of Machine

BuildLayoutSuggested.png
This is just a guide as to how you might lay out the mechanicals of a machine like this using lead-acid sealed batteries and two rear-wheel drive units from a chain driven electric children's scooter.
See my previous Instructable of 2010 for example of how to arrange the mechanicals.

https://www.instructables.com/id/Easy-build-self-balancing-skateboardrobotsegway-/

Main Parts Laid Out

PartsLaidOut.jpg
Arduino Uno
Arduino protoshield (or something very similar)
Cable with at least 6 wires inside (mine was 9 core in fact).
Battery holder for 6 x AA 1.5V batteries.
Two rocker switches that return to the middle position on a spring when you let go of them

A microswitch with metal lever that we will use as our deadman switch (when you let go of it all power to motors is cut).
Small on/off switch connecting the battery box to the Arduino.
Connector for the battery box.

Plastic project box which we will make our hand controller from.

Making the Shield

ShieldTidiedInFull.jpg
Close up of the shield.

The IMU is mounted on it vertically, more on this later (more wires in this early photo than I eventually needed).

We have 4 indicator LED's.

Multicore cable from hand controller comes in lower left.

2 core cable to Sabertooth motor power controller exits board middle right.

Shield is stacked on top of the Arduino.

IMU Right Angled Header Pins

AngledHeaderPins.jpg
In previous photo the IMU was mounted vertically on the Shield. This was because I used right angled header pins to mount it, which conveniently come with the IMU.

Long ends go through edge of board with solder holes along it, short ends go down through holes in the protoshield.

How We Will Wire Up the IMU

WiringDiagramIMU.jpg
There are not many analog output IMU's left out there.

Here is the only one that seems to still be made.

IMU status (Updated 25th August 2013). Constantly changing situation......................
What you need for this software to work unaltered, is an analog IMU containing an IDG655 gyro and an ADXL335 accelerometer.



This is original one I specified when I wrote the instructable. However now 6 left and price has been quadrupled to $96.99 (thanks guys)
http://www.ebay.com/itm/181004141876?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649

This one looks OK at approx. $17 but note “shipping and handling” is $100 (!) Thanks guys again.
http://www.ebay.com/itm/1PC-5DOF-IDG655-ADXL335-Accelerometer-Dual-axis-Gyro-Instrument-Module/360673380095?rt=nc&_trksid=p2047675.m1851&_trkparms=aid%3D222002%26algo%3DSIC.FIT%26ao%3D1%26asc%3D17214%26meid%3D818663122151966103%26pid%3D100005%26prg%3D8039%26rk%3D3%26rkt%3D5%26sd%3D181004141876%26

This one is OK but listed as sold out
http://www.dhgate.com/product/gy-66-idg655-adxl335-module-5dof-module-twin/156501775.html


Ones that do seem to currently be available at a reasonable price:

GY - 66 5 dof biaxial analog gyroscope sensor IDG655 ADXL335 module
$17.59 Does not ship to UK, does ship elsewhere.
http://www.ebay.com/itm/GY-66-5-dof-biaxial-analog-gyroscope-sensor-IDG655-ADXL335-module-/181115375225

GY-66 IDG655 ADXL335 Module 5DOF Module Twin Screw Analog Quantity Gyroscope Sensor Free Shipping
$14.28
http://www.aliexpress.com/store/product/GY-66-IDG655-ADXL335-Module-5DOF-Module-Twin-Screw-Analog-Quantity-Gyroscope-Sensor-Free-Shipping/406986_729113614.html

This one looks OK. Ships to UK, US etc.
$22.20
http://dx.com/p/gy-66-5dof-idg655-adxl335-double-shaft-analog-quantity-gyroscope-sensor-module-blue-232963

Here it is again at $24.71
http://www.goodluckbuy.com/gy-66-5dof-dual-axis-analog-gyroscope-sensor-module-idg655-adxl335.html

NOTE December 2013: Have just found the "Grove" series of analog sensors from Seedstudio. Details below. The scaling factors in software may need tweaking but at least they are still available to buy:
3 axis analog accelerometer
http://www.seeedstudio.com/wiki/Grove_-_3-Axis_Analog_Accelerometer
Buy here:
http://www.seeedstudio.com/depot/grove-3axis-analog-accelerometer-p-1086.html
and here:
http://www.dawnrobotics.co.uk/grove-3-axis-analog-accelerometer-adxl335/?gclid=CNDQ9fzmqLsCFSoewwod7gYA_w

Also they do an analog 3 axis accelerometer:
Buy here:
http://www.seeedstudio.com/depot/grove-single-axis-analog-gyro-p-1451.html
and here:
http://www.dawnrobotics.co.uk/grove-single-axis-analog-gyro/

Just found these as well (17th Dec 2013)
These also seem to have them in stock:
http://www.dawnrobotics.co.uk/grove-single-axis-analog-gyro/?gclid=COyekqahuLsCFRMRtAodlVMAQA

Here is one from Italy (March 2014 in stock):
http://www.robot-italy.com/it/gyro-breakout-board-lpy503al-dual-30-s.html



This one also might work, 10 left on ebay.
http://www.ebay.co.uk/itm/Single-axis-gyroscope-analog-gyro-module-ENC-03MB-module-For-Arduino-MWC-/180956189552?_trksid=p2054897.l4275

Here is an analog dual axis gyro (Feb 2014):
http://www.rhydolabz.com/index.php?main_page=product_info&products_id=1328
Note that it is surface mount so the soldering of wires to it will need magnifying glasses and a very steady hand.


You only need one gyro to balance. I use the second one for direction stability but that one is not essential (or buy 2 single axis gyros.

The accelerometer can be bought separately:

Example here:
http://www.ebay.co.uk/itm/ADXL335-3-axis-Analog-Output-Accelerometer-Module-angular-transducer-/281048570799?pt=LH_DefaultDomain_0&hash=item416fccdbaf







Check out how the original one I used was wired up to the Arduino via the protoshield:

Power to the VCC connection is 3.3V from Arduino 3.3V pin, NOT the 5V pin which will blow up the gyros, I know, I have done it before. Take care, these pins and GND are close together on the Arduino and respective protoshield pins, do not let solder bridges form between them! (A good case for the magnifying glasses I mentioned earlier).

GND goes to either GND pin on the protoshield.

Only 3 more connections need to be made:
X4.5 goes to Analog Pin 3 on the Arduino protoshield.
Y4.5 goes to analog pin 2
Z-acc goes to analog pin 1 

The rest of the IMU holes you do not have to worry about, not needed..................not too bad was it?


Notes on this IMU:
ACCELEROMETER (ADXL335) notes: 300mV (0.3V) per G i.e. at 90 degree angle
GYRO NOTES on the Chinese IMU we only have available in 2013 which uses the IDG655 gyro module: Gyro outputs x 4.5 on this Chinese IMU: 2.27mV per degree per sec up to 500deg per sec. 

3.3V Power to the IMU (NOT 5V)

3.3VtoIMU.jpg
This is how I did it. So long as the wires go to the right pins you can do this any way you like.

I put the long ends of the right angled header pins through the IMU and soldered them all to the respective IMU holes.

From the ends of the long pins (now sticking out horizontally above the Protoshield) I ran small wires carefully to the Analog Pins 1, 2 and 3, you can see this better in the next photograph.

Two longer wires run to the 3.3V power supply pin and also to one of the GND pins.

The holes in the protoshield underneath the IMU are used just to hold the IMU upright and act as a mounting for it. I soldered two of them to hold the IMU rigidly at 90 degrees to the protoshield, i.e. vertically.

The holes on the protoshield just inboard of the black pin sockets are in continuity with them. Therefore you can put header pins in the sockets and solder your wires to them (as seen here with my IMU wires to analog inputs 1,2 and 3) or, you can solder your wires into the holes next to each pin (as seen with the 3.3V wire and GND wire in this photo).

Rest of IMU Wiring, Just 3 Wires

IMUwiring.jpg
As described on previous page, I ran 3 short wires from airborne ends of the IMU long header pins (z-acc, X4.5 and Y4.5) to analog inputs 1, 2 and 3 respectively.

2 more wires go from VCC and GND to 3.3V and GND pins on the protoshield respectively.

Mounting the IMU on the Protoshield

IMUmounting.jpg
Another view just to make how I did this really really clear. Solder 2 pins on the IMU to the protoshield holes underneath just to mount it securely.

LED Wiring and the Two Serial Wires to the Sabertooth Motor Controller

11.12image.jpg
Now we need to wire up some LED's as they are useful as indicators.

The wire next to the curves side of the LED goes to the +ve power (i.e. the Arduino Pin) and the flat side is the -ve and goes VIA  A RESISTOR, to a GND pin (ground) on the Arduino.

So, the +ve round side of the 4 LED's we see here are connected to Digital Pins 6,7,8 and 9 of the Protoshield. I soldered them into the holes just next to the respective black socket holes in the Protoshield.

The -ve side of each LED was pushed through a convenient hole in the protoshield and soldered into the hole.



In this picture we also see the 2 wires of the 2-core screened cable soldered to the holes for Pins 11 and GND.
This cable is the Serial communication cable carrying control data on motor speeds for each of the 2 motors to the Sabertooth motor power controller.

LED Wiring (2)

step11.jpg
Here is another view of the LED's and how they are soldered in, plus the 2 wires of the serial communications cable to the Sabertooth.

Flat side of each LED is facing to the right in this image.

Resistors for the LEDs

LEDwiresOnBack.jpg
Each LED needs a resistor mounted in series with it, otherwise too much current would flow through it when the respective pin on the Arduino goes live. These should be about 100 Ohms each.

I ran mine from the projecting wire of the flat side of each LED (through the hole in the shield), on the underside of the shield, flat along the underside of the shield so they ran next to each other (no wires touching though).
I then joined all the ends of the 4 resistors together on right side of this image and ran a wire from them to the nearest GND connection on the Protoshield.

So, every time one of pins 6,7,8, or 9 goes live (+5V) current flows through the respective LED, through its respective resistor, to GND and so that LED will light up.

Keep the resistors fairly flat along the underside of the shield and you will find it does not then touch the Arduino when you mount the Shield on top of the Arduino later on.

Making the Handheld Controller Box

HandControllerExterior.jpg
Here is a the hand controller, held in palm of your hand.

The deadman switch is on the end and you press it down with your index finger all the time. If you let go than as a safety feature, all power to the motors is cut (after half a second actually).

The rocker switch steers you left or right.

The rocker switch on the side is for fine-trim of the balance position. For example to go up a slope you might want the board (if it is a skateboard) a little "nose-up" before you start.

This is connected via a multicore screened cable to the Arduino.

When any switch contact is made, the respective pin on the Arduino is connected to GND on the Arduino.

Hand Controller Internals

HandControllerInternals.jpg
Here are the internals we want to fit into the hand controller.

Two rocker switches which have a spring that returns them to the mid-position when you are not pressing either end.

One microswitch which is to be used as the deadman switch.

Cut Holes in Box for the Switches

CuttingHandControlBox.jpg
I use a Dremel with an abrasive cutting disc for everything like this.

Cut slots for your switches.


Solder Up Wires to the Respective Switches - Wiring Diagram

HandControllerWiringDiagram.png
Here is the wiring diagram.

Put a wire on each switch contact and write down the colour of each wire and what it does on a chart, along with number of the Arduino Pin the same wire at other end of the cable needs to go to.

See how all the GND wires can join together to go down one GND wire back to the GND of the Arduino

Internals Wired Up

HandInternalsWiredUp.jpg
Here are the hand controller internals wired up.

Switches are held in with hot glue gun glue. Careful where you put the glue else your rocker switches will not rock any more!

The microswitch (deadman) is held to box lid by 2 small 3mm bolts and nuts so you do not break it loose if pushing it too hard (with fear for example).

Keeping Hand Controller Cable Securely Fixed to Shield

HandWiresConnectedToBoard.jpg
See how I have used 2 small cable ties through spare mounting holes in the Protoshield to hold the multicore cable securely to it so you do not put any stress on the soldered pin connections for each of the little wires inside.

Things like this are important for reliability. Loose wires = chaos.

Wire Up the Serial Cable From Shield to the Sabertooth Motor Controller

F4SE3B6HJAI0J4A.MEDIUMb.jpg
We have one more cable left to go.

A 2 core screened cable from Pins 11 and GND (any GND on the protoshield) of the Protoshield/Arduino to the Sabertooth motor power controller.

These carry commands telling the Sabertooth how much power, and in what direction, to apply to each of the 2 motors. It uses a Serial communications protocol.

Photo of How This Cable Is Connected by Just 2 Terminals to the Sabertooth

saberwiringUp.jpg
Pin 11 on the Arduino is connected via this cable to the S1 screw terminal of the sabertooth.

Any GND pin on the Arduino/protoshield is connected via this cable to the 0V screw terminal of the Sabertooth.


In this image of the Sabertooth 2 x 25 Amp power controller, I have also shown the connections to the main 24V battery and also the connections to each of the motors.

Keeping Serial Cable Tidy and Securely Fixed to Shield

securing cable.jpg
This is how I kept my serial cable neatly secured to the protoshield.

Two loops of insulated wire soldered into 2 spare holes in the protoshield about 1 inch apart hold the 2 core cable securely. Neater than a blob of glue gun glue, but that would also work. Careful with the soldering iron, if you hold it on for ages you might melt through everything and damage the cable itself.

Setting the DIP Switches on the Sabertooth for Serial Communication

DIPswitches.jpg
Consult the Sabertooth downloadable instructions but there is a set of DIP switches on the Sabertooth that need to be set to tell it what sort of communications it is receiving and what type of batteries are connected to it.

Main Power Wires and Motor Wires to Sabertooth (for Completeness)

Saberwiring2.jpg
For completeness here is how you connect the two motors to the Sabertooth motor power controller.

250 Watt to 400Watt motors are about right for this controller (remembering that if a motor stalls or is held jammed so it cannot turn, the current consumption greatly exceeds its "rated" value). This is why motor controllers often burn out on "Robot Wars" when robots are pushing against each other but hardly moving.

I have added a "Motor tester" sketch with the main code on page 27. Allows you to check your serial communications with the Sabertooth are all OK and working before worrying about the self-balancing part of things.

Main Power Supply to the Sabertooth for Motors

BatteryConnections.jpg
Suggested simple way to connect the main two 12V batteries to the Sabertooth.

Power Supply to Arduino + Shield

BatteryPack.jpg
To keep it all really simple I power my Arduino + IMU with a completely separate battery pack. AA size batteries are cheapest and last longest (compared to a 9V PP3 type for example). Therefore I use a simple 6 battery holder and a small on/off switch.

Arduino CODE

Codelogo.jpg
Code is attached as a text file to the NEXT page.

Copy and paste it into an new open empty Arduino sketch, compile it and save it. Then load it into your Arduino.


Code Attached As Text File.

This all was written using Arduino 22.

THIS CODE IS FOR USE WITH THE CHINESE ANALOG OUTPUT IMU MENTIONED ON THE TITLE PAGE.
If you are using an old Sparkfun 5dof analog IMU (no longer available) then you need to go to step 30 for code that works with this.

This is a bit old now and some commands do not work in the latest Arduino environment.

You may have to load Arduino 22 to your computer. If so, do it from here (scroll down the page for the historical downloads).

http://arduino.cc/en/main/software


NOTE also added now (Aug 2013) is a MOTOR TESTER sketch (as text file again) which allows you to connect just the deadman switch and the 2 wire serial cable to the Sabertooth.
When you press the deadman in, the code will spin the motors one way at different speeds, the back the other way at different speeds at 10 sec intervals.
It allows you to test the deadman switch and make sure you have the DIP switches on the Sabertooth correctly set up for serial communication (step 22) and that the Saber is working properly.

Orientation of IMU and Shield on Your Machine

IMU orientation.jpg
Take note of the orientation of the IMU and the Shield relative to the plane of tilting of your board or Segway type machine.

Only mistakes here I cannot control are the way you wire up your motors.

Rule of thumb is that if you tilt board one way, both wheels should start turning in direction you have tilted it towards. i.e. if left end of board is down, then wheels should be moving board to left (to counteract this and bring board level again). It is easy to have one motor wired back to front for example in which case they do the right thing but spin opposite ways and so on.
Do initial tests holding board tightly at each end, with wheels ON the ground but be ready to lift board into the air and release deadman if it goes crazy.

- Tip machine one end on ground and do not move it about.
- Turn on Arduino.
- Turn on power to Sabertooth.
- Wait for 1 LED to light up.
- Press in deadman switch.
- Wait for all 4 LEDs to light up, it has now zeroed everything and is ready to go.
- Bring machine level SLOWLY. When level the self-balancing code will become active.
- For first few seconds the gain will be low and it will feel "mushy" in terms of ride quality.
- After a few seconds the gain automatically increases. This is the "soft-start" function.
- If you let go of the deadman switch all power will be cut within 0.5 seconds.
- Use the fine-adjust rocker switch to fine tune balance position if board is not quite level.

Adjusting Variables in the Code

CodeTweakPoints.jpg
Near top of the Arduino sketch is a box with all the key variables in that you might want to change.

You have P, I and D values of the classic PID controller.
The values I have chosen are by trial and error and suit my machine.

The overallgaintarget is the value the gain ramps up to in the first few seconds after you first bring it level.

Therefore for a taughter system you increase it (too much and machine will oscillate wildly). For a "mushier" ride quality then reduce it a little.

Code for People Using an Old Sparkfun 5dof Analog IMU (no Longer Available)

This is exactly the same code as used in this Instructable for the Chinese IMU that is just about still available (Step 26), but altered to take the old Sparkfun analog 5dof IMU that they no longer make, for people who may have one of these.
The gains have just been changed for the gyros as they have a different output to the ones in the Chinese IMU.

It is attached as a text document to be pasted into an empty Arduino sketch then compiled and saved.

The pin connections between IMU and Arduino are included in the sketch for you to see.