Introduction: Arduino Segway
Hello!
More than 3 years ago I started to tinker a homemade segway. Though it's still not really finished (I have to replace the 250W Motors by 500W-models) my children are able to drive around.
If you're going to build one you will Need the following parts:
* wheelset: In my case I use 12 1/2 x 2.75 tires usually taken for dirt bikes (f.e. www.amazon.de/HMParts-REIFEN-MIT-SCHLAUCH-12/dp/B0038X9ODA; http://www.ebay.com/itm/Tire-Rear-Wheel-Mini-Pocket-Dirt-Bike-47cc-49cc-50cc-COOLSTER-QG-50-RX1-Traxxis-/182346539856?hash=item2a74b36350:g:BxUAAOSwh6xTtmwX&vxp=mtr)
* batteries: I use two 12V/12Ah lead accumulators to get 24V for the Motors
* Motors: At the moment two 24V/250W models but the 500W-motors are already waiting for their use
* for the axle I asked a local welder and some of my bicycle-parts (bar, stem) fit perfect too.
Step 1: The Motor Driver
First I tried a homemade full-bridge. To control it I used the analog-out (pwm) of the arduino. But with this variant I didn't succeed. Therefore I bought a commercial product, a 2x60A Motor Driver from sabertooth (www.dimensionengineering.com/products/sabertooth2x60).
The arduino is able to communicate with the sabertooth via a simplified serial code.
Step 2: The Angle Determination
To determinate the current angle I first used a separate accelerometer (ADXL335) and gyroscope (LY530AL). Last week I changed this Setup and the two boards have been replaced by the MPU6050.sensor, which combines the two elements.
The advantage of the gyroscope is, that accelerations don't influence the measurements. The disadvantage is, that the gyro has a drift and after a while the angle goes higher and higher.
The Advantage of the accelerometer is, that it doesn't show any drift like the gyro. But it's being influenced by accelerations, which change the Output.
A filter combines both pro's so the calculated angle is near the real value. This filter can be a simple complementary-filter (like in the Picture) or the more complicated kalman-filter. The Input of the kalman-filter is the actual angle measured by the accelerometer, the angular Speed measured by the gyroscope and the time-step between two measurements. With these Parameters the kalman-filter is able to calculate an angle, which follows the gyro in times of high accelerations and in times of less accelerations it approaches the value of the accelerometer. In the Picture you can see the drift of the gyro and the good Job of the filter.
Step 3: The PID-controller
The segway tries to get the angle down to 0°. If the angle is positive, the segway brakes or accelerates backwards. If it's negative the segway accelerates forward.
But which signal get the motors? This is exactly the task of the pid-Controller.
The proportional-part: The angle should be 0°. So the greater the angle, the higher the speed of the motors.
Mathematically you can write:
angle_error = angle_should - angle_is
p-Motor-value = k_p * angle_error
The integral-part: When the current angle isn't zero, the Controller tries to get the segway horizontally again. But this approach shouldn't be slow and the smaller the angle-values, the slower the changes. Therefore the Controller allows a faster approach with the disadvantage, that the movement doesn't stop at 0° and goes a bit to the other side. This behaviour is guaranteed by the integral-part. All the angles are added (integrated) and this integral over time should be Zero. So if the angle is f.e. positive for a while, the Controller tries to get the seqway into the negative area. This guaranteeds faster approaches...
Mathematically you can write:
error_sum_new = error_sum_old + error
i-motor-value = k_i * (error_sum_new)
error_sum_old = error_sum_new
The derivative-part: Not just the current angle is important but also the changes of the angle. Let me explain: If the angle is f.e. 10° and it doesn't Change a lot, the Motors don't need to correct this shift very fast. But if the angle is 10° and it changes very fast (high derivative), than the motors have to correct this also very fast.
Mathematically you can write:
d-motor-value = k_d * angular Speed
Finally you get: motor-value = p-motor-value + i-motor-value + d-motor-value
Step 4: The Complete Setup
Now it's time to start the segway. To drive not only straight ahead you have to mount two buttons. When you push f.e. the right one, the left Motor increases Speed and the right Motor slows down. To ensure smooth transitions the added and deducted value starts at zero and increases step by step (+0.5 every loop) until he reaches the maximum value (f.e. 30).
To try out your own segway I 've attached the simple program for you. Good luck and don't follow the segway-inventor James Heselden down a cliff ;-)
Maybe you want to take a look at my YouTube-channel: www.youtube.com/user/stopperl16/Videos
more physics projects: https://stoppi-homemade-physics.de/

Second Prize in the
Arduino Contest 2016
46 Comments
Question 6 months ago on Step 3
Hi,
I have started to build a segway from your design and have all the parts.
I have the control system built and testing my test unit with small motors.
I am confused about the dip switched on the Sabretooth and which type of configuration you are using, I think it is the Simplified Serial. Can you please confirm and what are the dip switch settings.
Take a look at my work https://www.vertogen.eu
Mike
mike@vertogen.co.uk
Hallo,
Ich habe begonnen, einen Segway nach Ihrem Entwurf zu bauen, und habe alle Teile.
Ich habe die Steuerung gebaut und teste mein Testgerät mit kleinen Motoren.
Ich bin verwirrt über den Dip-Schalter am Sabretooth und welche Art von Konfiguration Sie verwenden, ich denke, es ist die vereinfachte serielle. Können Sie bitte bestätigen und wie sind die DIP-Schaltereinstellungen?
Schauen Sie sich meine Arbeit an https://www.vertogen.eu
Mike
mike@vertogen.co.uk
Answer 6 months ago
Hello Mike!
Here's a picture of the sabertooth driver with a higher resolution. There you can see, which switches are high and low... I hope I could help you. Good luck with your project, cheers Christoph (stoppi)
Reply 6 months ago
Hi,
Thanks for quick response
I have just found out I pin 11 to S1 should be TX (arduino) to S1 (Sabretooth)
How do I monitor 3 the pip potentiometers.
I have attached some detailed wiring and schematics with serial output.
Not quite working properly, motors spinning a bit, no changes when MPU6050 is moved, any ideas
I know they work tested with 3 sketches from Sabretooth.
Thanks
Mike Chung
Reply 6 months ago
Hi! I've just taken a look at my segway-circuits. You have to connect digital pin 11 (Tx) with S1 of the motor driver. Don't use the Tx-Pin on your Arduino, this made problems... The Tx-cable is the green one which goes to pin 11 of the arduino. In the code/sketch the serial communication is initialized on pin 11! You can see my shematics above in my instructables or you visit my homepage: https://stoppi-homemade-physics.de/segway/
I add further images of the sabertooth switches as well. Good luck with your project, cheers stoppi
Reply 6 months ago
Hi, thanks for your reply.
I did have it plugged in pin 11, nothing happens. When I look at your photos it does show you using Tx and Rx.
on the serial monitor it does show somethings is happening, but I really understand that.
Any ideas why nothing is happending?
The examples from sabretooth work and they use the Tx pin
Thanks
Mike
Reply 6 months ago
Hi,
I am still stuck, nothing is happening at the motors.
Noticed on your Actual Wiring you have some differences from your diagram, could this be the problem?
Example
Diagram Actual installation
pin 12 to S1 Tx to S1
Nothing Rx to int
The Left and right buttons are working.
Thanks for your help, like to get this working, nobody else can help.
Mike
2 years ago on Step 4
Arduino and Motor Controls are my bread & butter stuffs. I have built a Car Racing simulator and a Go-Kart. This is what I was looking for. Thanks you Sir and Pinterest to send this to me. I already have a geared 500W 24V dc brushed motor, Arduino etc. Only other part that I need to buy is the Sabertooth 2x60. Will order it today.
2 years ago
Why is MPU 6050 not getting result, not showing value and motor not running?
2 years ago
Thanks for your post!!
Question 2 years ago
I am having trouble downloading the .ino code, it show up as a broken link. If you have it and don't mind sending it to me, it would be greatly appreciated. Thx, Dean the_real_dean@yahoo.com
Question 4 years ago on Step 3
https://www.amazon.in/Robodo-Electronics-Motor-Dri...
Is it possible to use this H bridge instead of sabertooth dual 60A motor driver, since its too expensive, so I cant afford it. If not can you suggest me a good one at lower price range?
Answer 2 years ago
i found one on ebay, $11.00 but the heat sink and fan are not included, another $10.00. it is just a generic h bridge with dual motor control.
Tip 4 years ago on Introduction
hello, we are making a thesis, we use your segway software one to one, we have very little time left, we had problems about saberthoot, can we help you if we give you the fee?
Coskunkodr@gmail.com
6 years ago
Yes, you'll need a second button (voltage-divider with the 10 kOhm-resistor)... Pin 12 and 13 are low, if the button isn't pushed. If you push the button for steering, the signal goes high...
Therefore I ask in the program: if (steering-button-right or left) = high then ...
I've changed the Picture of the circuit, because in the first version the status was low if the button was pressed ;-)
Reply 4 years ago
can u explain briefly about it. Also can we use potentiometer for steering the direction?
5 years ago
Can i get full pdf of segways construction
Question 5 years ago on Introduction
i m using 2 350 watt motors for segway.i have two 12 volt batteries that connect in series giving 24 volts.i want 14 amps current for each motor. how can i get that? what possible combination capacity of battery (voltage and current) should i use? i m using sebertooth motor driver 30*2 channels.please guide me for motor driver function and how i couple motors with wheels
Question 5 years ago
Nice to meet you
Your research is very good
I am a teacher of an industrial high school in Japan.
I have a request
I referred to ”Step 4: The Complete Setup”, but the motor does not work.
”FXLSYHKIW6PNEU6.ino” could also be compiled.
I'm sorry.I still need to study more.
It would be greatly appreciated if you could explain the details.
5 years ago
Can i use another motor driver? i would like to use your project as reference in my project but the driver that you used is not available in our country and the cost was too high heheh... can you give me some advice about the motor driver? thanks ^_^
5 years ago
thank you for sharing, you have making the best project for me ..you're actually a professional engineer.!