Introduction: Self-Balancing PT (homemade Segway)

Picture of Self-Balancing PT (homemade Segway)

This Instructable is intended to share my hobbyist project: a Self-Balancing Personal Transportation (SBPT), also known as "Homemade Segway". It is an interesting project requiring a minimal knowledge in electronics and some handcraft skills. All the components can be bought in internet and the mechanical parts assembly can be done in your home workshop.

The SBPT consists in a platform to stand on vertically with 2 side electrical motors powered by batteries. The controller algorithm ensure the balanced position to not fall forward or backward. The 2-wheeler motion is controlled by the rider with the tilt and the handle for the direction. Therefore additional electronical components like a controller, motor drive and acceleration/gyroscope sensor are required. The mechanical framework is made with material like wood because it is lightweight, electrically isolated and very easy to work for prototyping purpose. The description reflects the current status and will be updated with the coming optimizations and enhancement according to the readers’ comments. The way the hardware configuration and the software purpose are documented is to give you a reference which one you can modify to meet your needs. Finally, it is your homemade project!

Adrian Kundert

Step 1: Application’s Target Definition

The goal here is to state what we want to achieve with the SBPT. This will lead what and how the vehicle has to be build up. In my case, I wanted the following features:

- Enough powerful and rugged to ride outdoors even on gravel path

- At least 1 hour operation range

- Total cost less than 500€

- Wireless communication at debugging to get all motion freedom

- Data logging on a SD card for troubleshooting over a long period

Step 2: System Design

Picture of System Design

The schematic above shows the wiring of the main components as well the worm gear motor. The Arduino controller can be the Uno, Nano or the ATmega328 as single IC. The batteries are connected serial to get 24V which supplies the dual H Bridge for motors. This one is only powered as long the rider holds the ready button. In emergency case, the rider just release the push button to get unpowered the motors. The Arduino controller uses the serial communication in the “Packetized” mode at 38400 baud with the H-Bridge and the wireless module XBee. The tilt and steer are measured by an acceleration/gyroscope InvenSense MPU-6050 sensor in a “GY-521” on 2 separated breakout board and communicate with the Arduino by I2C. The tilt sensor (address 0x68) which is the most important is programmed to sense at every 20 ms and gives an interrupt to the Arduino. The second sensor (address 0x69) is pulled by the Arduino. Finally, the rider loaded limit switch is aimed to detect when the rider stands on the SBPT in order to activate the balancing algorithm.

Step 3: Framework Construction

Picture of Framework Construction

The wheels and gearboxed-motors are hold by screws into 3 pieces of wood assembled in a U form box. The handle is a wood stick hold by a bolt in the front wood piece. Very important is that the weight distribution SBPT is balanced when held vertically, which is the riding position. For this verification, the heavy parts like the batteries must be considered, otherwise it will be more difficult for the balancing algorithm.

In my case, the batteries are placed rear in the wood box to compensate the motor weight which in centered. At the remaining space between the batteries and the front of the wood box is placed the electronics components.

On handle is taped the “rider ready” push button. Sorry about the very prototype status with a lot of tape and Vise Grip usage.

Step 4: Electrical Wiring

Picture of Electrical Wiring

The wiring in the wood box is done according to the system schematic. The Arduino pinout table gives you the wire connection to the sensors, h-bridge and switches.

The tilt sensor is mounted flat horizontally to get the x axe angle. However, the steer sensor is mounted vertically to get the left/right steer angle with the y axis.

Step 5: Testing and Tuning

Picture of Testing and Tuning

Don’t forget: the motors can be powerful! Ensure the trial is done in a wide and safe area to not cause damage or injuries. It is recommended to wear protection pads and a helmet.

Important is to proceed step by step. Begin by programming the Arduino (download the source code), then verify the communication with the sensors and afterward with the H-Bridge. The Arduino Terminal can be used for debugging and to verify the working status. For example, the PID gains must be tuned because it depends of the motor mechanical and electrical proprietries.

The gains can be tuned with this typical method:

1. Kp is mainly for the balancing. Increase Kp till the balancing become instable, Ki and Kp remain 0. Reduce Kp a bit to get stable again.

2. Ki is for the acceleration/deceleration by the tilt. Increase Ki to get the right acceleration to avoid falling when tilting forward, Kp remain 0. The balancing should be now is stable.

3. Kd is used to compensate the integration and get again a stable the balancing.

In Terminal, you can get the differents commands by the command "?".


? - Help for the commands

p,i,d [value] - Set/Get PID gain, value between 0 to 255

r [value] - Force the motor speed, value between -127 to 127

v - Software version


With the command "p", you get the Kp. With the command "p 10", you set the Kp to 10.

Once the Arduino controller is powered, the sensors are initialized and finally the waiting state is reached. Pressing the push button will signal to the controller the SBPT has to be prepared by getting the vertical position by activating the motors forward or backward dependant the initial position. From this point, this button has to be held otherwise the motors are unpowered and the controller goes back in the waiting state. Once the vertical reached, the controller is waiting the signal from the “Rider Loaded” limit switch, normally pressed by the foot when get on the platform. When this happen, the balancing algorithm is launched and will activate the motors forward or backward to stay in a vertical position. Leaning forward will create a motion forward, and vice versa. Keep leaning will increase the motion speed. Leaning in the opposite side of the motion will decrease the speed. About the direction left or right, just move the handle in the desired direction.

Step 6: Demo

Picture of Demo

Here you can watch a short video as a demo to see the dynamical behaviour of my SBPT. This should be the funniest step of all!


ajayt7 (author)2017-09-22

Great job

DJH18 (author)2017-01-13

Hello Adrian plzz mail me connection diagram for the relay module u have installed at regards tushar

Giloris (author)DJH182017-07-03


J ai vu tes commentaires et je pense que tu es français

J habite en Isere mais je parle écrit tres mal l anglais

As tu pu expérimenter ce projet ?

Je suis un projet aussi de segway et j aurais besoin d aide au niveau de la programmation et des calculs de pid

Tu peux m envoyer un mail sur

Merci d avance et bonne soirée

Gh lombard

akund (author)Giloris2017-07-09

bonsoir Gh

En effet, je comprend le français. Ai-je dèja répondu en francais sur ce site?

Dis moi plus précisement ce que tu as deja fait et voudrais savoir. Ca sera plus facile de te guider.


Giloris (author)akund2017-07-10

bonsoir et merci pour ton contact

Bravo pour ton projet !!!

J envisage de réaliser le même mais je ne maitrise pas la notion de pid pour contrôler la vitesse des moteurs pour rester en équilibre

Je ne sais pas comment le réaliser sous arduino pour faire auto balancer le gyropode

La partir mécanique est ok , pour la direction gauche/droite j ai prévu d'intégrer un capteur effet hall 5v qui tournera en fonction de l orientation du guidon qui sera monter sur un axe.

c est la partie programmation qui me pose probleme , au niveau des capteurs gyroscopique j ai un mpu 6050 mais je pense qu il faut filtrer les données

Merci de m aider sur le pid et le mpu 6050


akund (author)Giloris2017-07-12

comme j ai deja ecrit, il faut une base en electronique et programmation.

As tu deja fait un essai/tutorial avec un arduino? Essayé de communiquer avec le module MPU050 par I2C?

Giloris (author)akund2017-07-15

bonjour Adrian

J ai telecharge le code source mais je ne comprends pas le fichier SelfBalancing PT.ino ; Je ne sais pas ou trouver le code intégral (mpu sabertooth , contrôle des moteurs ...)

Merci pour ton aide.


akund (author)Giloris2017-07-15

le MPU fait des interrruptions hardware periodique,ligne 82 de SelfBalancing PT.ino.

le calcul du PID se trouve à la ligne 60 du fichier controller.cpp

ligne 80 est le traitement du calcul PID selon l'etat.

control de moteur par el Sabertooth de la ligne 110 à 174

Le lien dessous est un bon tutoriel de PID.

Giloris (author)akund2017-07-16


Merci pour ces renseignements , je vais étudier le Tuto qui va sans doute m'aider à comprendre le pid et l utilisation du mpu.

Par contre il utilise des moteurs avec encodeurs , est ce indispensable ?



Giloris (author)akund2017-07-12


Je connais un peu l arduino j ai deja fait plusieurs petits projets

(Automatisation de poulailler, contrôle électrovanne, boîtes à lettres connectée ...)

Mais c est surtout le pid qui me pose probleme

Pour asservir les moteurs pour que le plateau auto balance

Si tu pouvais m'aider sur ce sujet .



АнатолийХ (author)2017-04-05

Hello Adrian. I want to assemble a Segway according to your drawing. Please help. Can't deal with the connection of the relay and connecting the gyro to the arduino. Send me please circuit diagram your device for email.

MVVSTEM2015 (author)2017-02-03

Can you share where to order the motors and wheels?


tmoir (author)2016-09-06

You use state-space control? ie state-feedback? Or is it PID.

akund (author)tmoir2016-09-07

it is a simple PID.

akund (author)2016-07-10

Dear readers

Here an update after a few month of trial and testing.

- The mechanics, motors and power electronics work like a charm.

- The balancing algorithm is limited when the motor reach is
maximum speed. Pretty simple to understand: if you lean forward or backward,
the speed has to be increased to compensated, but if the motor is already a
max, it is not possible anymore, so you fall! I was aware of that and without
knowing the motor speed, it is always a guest if I can still lean or not.
Therefore, I added a buzzer (or a vibration motor) in the handle activated by the ARDUINO when the motor speed is
over 90%.

- It happened a few time that my ARDUINO NANO had to be reprogrammed
because it lost his program!. This happens when the motors regenerate current
while driving down a hill.

I would say thank you for all shared comments.

BillK27 made it! (author)2016-07-07

Hello Akund, a progress question. My example has been done and running great for a week or so. It died today, and I after exhausting other possibilities, I am wondering if there is an issue with the MPU6050 devices. Currently, as I think you do as well, I operate the arduino at 5v, as 5v is required for high speed 16mHz operation. But the MPU6050 is a 3.3v device. I took a chance and ran it without a fancy level shifter at 5v, and it has up to this point operated fine. Just wondering out loud if that's how yours operates, and if it's given any trouble.

akund (author)BillK272016-07-10

hi Bill

Nice project!

Nope, I never had problem with the MPU-6050 / GY-521 break-out boards. This board has his own suppy regulator, it muss be supply with 5V. Just the logic signals (SCL and SDA could be shifted to 3.3v. The arduino input is 3.3v compatible.



BillK27 (author)2016-07-07

Oh, just one more thing. You can see my temporary grey wires to (3) 10k 10-turn pots glued together, I adjusted the code to read on startup via analog inputs, the pot settings, these are translated to my PID settings. Allows a simple adjustment in the field using your fingernail even, a quick reset and you're off with fresh PID settings to test. When done, I'll tuck the cable down inside.

BillK27 (author)2016-07-07

Notes on the pics I added....I installed 12v strip leds on the underside and front. Shines the surrounding area really well operating at night. I didn't want to hold a button down all the time, so added the emergency red cord, it's tied to the operator, and wired to disconnect battery power in the event of an "unplanned departure". Because the motor controller is capable of running on forever with its last speed command if the arduino craps out for some reason. I also added a line of code to include the "rider loaded" switch along with the button press switch (bypassed here) so that if you step off, it shuts down via computer. Works great. Added a dc meter I had lying around, it monitors the 2 lithium polymer series 12v 13AH packs I built for the project, there's a switch to shut it off, as it requires its own 9v battery source. Installed a 12v switching power supply and AC cord in the underside to charge, wired through a DPDT switch wired to parallel the 2 12v batteries for charging. So down position is charging, middle is off, up is ready. I used an old Hoveround for the motors and chassis, they work great. I machined some adapters to mate the small Hoveround hubs to a 4-bolt pattern standard US-style trailer hub, after machining off the unnecessary steel to minimize the weight. Used 2 spare trailer tires I had lying around. This thing is fast! Way too fast to allow it up to full speed, unless you like the risk of faceplanting on the asphalt when something goes wrong, like a pothole or a computer crash. Very happy overall with the project, highly recommended for those with the skillset.

BillK27 (author)2016-03-18

Logging some of my build notes here for others' benefit. One: don't forget the address pins on the MPU6050 devices. Figured that out the hard way. Second, and not sure the answer yet, the PID settings are tricky. Currently I am using 1,28,6 for the PID constants. I revised the code to incorporate (3) 10-turn pots for PID settings. I think it best to be able to revise things on the fly, while out in the street or parking lot without hassling with other devices. Bench testing indicates my PID settings are on the money. A little tweaking once test running for real, should be good to go.

BillK27 (author)2016-03-17

Another interesting note. The code spits out plot data, you can open the serial plotter(instead of the serial monitor in the Arduino IDE) and it plots what appears to be PID gain and some other variables. Interesting to watch.

BillK27 (author)2016-01-25

For the PID settings, I think I would add some active control, whether by mechanical pots or an lcd and joystick, to allow on the fly adjusting. I mean once the code is loaded, that's all that needs adjusting right?

akund (author)BillK272016-01-27

Yes, some tuning control are very

To simplify the amount of component and the wiring, I have
implemented commands with the Arduino Terminal for the PID tuning. In the step
5 is described how to tune the Kp, Ki and Kd separately.

BillK27 (author)akund2016-01-27

I did see that, thanks so much. I guess with the Xbee, over serial. Was studying the code last eve. A couple things I might suggest for others....the motor driver I think, could be swapped to (2) $8 PWM double h-bridges, with little change to the code. Unless there's something I am missing. Also the Xbee is rather pricey, around $38 or so, while a bluetooth Bee is like $5 or so. I would probably change to that.

TechPaul (author)BillK272016-01-31

I'm in a group that has building an almost identical project and the $8 PWM will not last too long. The motors are basically electric wheelchair motors and use a lot of current. I have my doubts about the suggested motor driver but we will use them with fan cooling.

BillK27 (author)TechPaul2016-01-31

1. BTS7960-43A Motor Driver is what I will be testing. It's good for 43 amps and seems to get good reviews. Though I see there are various suppliers, some poorly made and fail early.

akund (author)BillK272016-02-01

At the beginning, I wanted to use the Arduino PWM and drive an external H Bridge. So I bought on eBay a "60A Dual Motor Driver Drive Module H Bridge" with the BTS7960. I had doubt when a saw the size of the screws for the wires. Yeah, it began burning at the very first switching on.

Another con was the highest PWM frequency limited to 8 KHz, which is still audible.

Kurt E. Clothier (author)akund2016-02-27

You'll quickly learn that you can't buy cheap electronics when it really matters. Otherwise - great job!

anuj1993 (author)akund2016-02-26

Is there any alternative for the motor controller? Sabertooth isn't available at stores where I live.

Waiting for your response.


rizqiakanishi (author)2016-02-22

hello.. I want to do this project.. :D but there's no seller in my country which sell sabertooth dual motor driver.. and i want to replace that with BTS7690 motor driver.. do u think it will work or not? please help me.. :)

waiting for your answer.. :)

akund (author)rizqiakanishi2016-02-23

no. Please read the comment I have done about BTS7960 to BillK27, 22 days ago.

BillK27 (author)2016-02-15

Ok, I tried the IBT-2 motor drivers we spoke of. They are fine but noisy at standard Arduino PWM frequencies. Adjusted the PWM to 10kHz, and the driver smoked in seconds. Ugh. Ordered the Sabertooth. :-)

primitivo (author)2016-02-13

hello sir , great job on that clone segway :) is possible to have a decent schematics? im sory but im still a amateur.

BillK27 (author)2016-02-11

Something of concern, but maybe you have it handled. What about rolling over uneven surfaces? The steering gyro will feed back incorrect tilt since it only sees overall tilt. How is this effect seen in real life operation? Most builders seem to opt for potentiometer steering for that reason. Maybe it's not an issue overall.

akund (author)BillK272016-02-12

Your remark is very good. The concept here (like for the real Segway), is that the rider stays vertically whatever the position of the PT. This mean when the left wheel is higher than the right wheel, the rider has to lean the stick to the left (get vertical) to not turn right. For me this behaviour is more "natural", then the poti steering.

BillK27 (author)akund2016-02-12

So I think you are saying, this works more naturally with the tilt sensor, and possibly even the real Segway uses this concept?

miragempro (author)2016-02-11

Hello, I'm Edward's Braziu, thanks for posting this detailed well and it is possible to do, do.

But you have the link to purchase the engine and batteries etc?

Thanks and a hug

BillK27 (author)2016-01-25

Also you mention using Xbee for tweaking settings, updating code, but I don't see any drivers or mention of Xbee in the code?

akund (author)BillK272016-01-25

It is the xbee default configuration. You take 2 Xbee modules (1 connected to Arduino, the other your PC). They talk together to bridge the serial communication.

TechPaul (author)akund2016-01-31

Could ESP8266 modules be used, then the tweaking vould be done on a phone

akund (author)TechPaul2016-02-01

it should. See

I had already the XBee module, that s the reason of my choice.

quintesse (author)2016-02-01

Any idea where to get those motors? I can find the specs and the site of the manufacturer but am not having any luck finding a place where they actually _sell_ them.

NitroRustlerDriver (author)2016-01-23

Are you able to stop and have it balance in place while standing on it?

akund (author)NitroRustlerDriver2016-01-25

yes. almost standing. it moves slightly back and forth. The perfect standing is not possible for any self-balancing system.

John LS (author)2016-01-24

Great device. Love the video.
Is there a full parts list? How is the wireless sensor used?
Can you provide more detail of how the broom handle is attached and connected to the tilt switch to achieve steering/


akund (author)John LS2016-01-25

there's no wireless sensor. Wireless is the serial communication instead via the Arduino USB interface.

I have added in the step 4t he mounting schematic of the steer sensor to the stick.

The tilt sensor is pinned in the bread board.

BillK27 (author)2016-01-25

Looks like I can build it with the info provided. I downloaded the code, it assembles fine. Question is, is the device reliable enough, from one use to another that the PID constants don't need tweaking? I see some others are unsure of their devices once constructed, they're always playing with the settings.

akund (author)BillK272016-01-25

If it is one thing that cannot by

"copied", it is the PID gains. It depends fully on the motor
electrical proprieties and the mechanic like the weight distribution. But don't
worry, the gains can be tuned with the common method.

1. Kp is mainly for the balancing. Increase Kp till balancing
become instable, Ki and Kp remain 0. Reduce
Kp a bit to get stable again.

2. Ki is
for the acceleration/deceleration by the tilt. Increase Ki to get the right
acceleration to avoid falling when tilting forward, Kp remain 0. The balancing
should be now is stable.

3. Kd is used to compensate the
integration and get again a stable the balancing.

That roughly summarized. The
tuning can be tricky if you don’t understand the dependencies.

BrunoF (author)2016-01-22

Hello akund, i made one in a little time ago, but i have problems with my self-balance. chash all the time. i think your project more simple. my ask is, what happens when the battery is discharging ?

akund (author)BrunoF2016-01-23

Both batteries provide my 24VDC. The 5VDC electronics (Arduino and auxiliary) are supply by a converter 24V to 5V. The Sabertooth H-bridge works in 6-28VDC range.

The batteries voltage is more than 26V when full charged. Then the voltage drops slowly during operation till reaching 23VDC. At this point the voltage drops massively when demanding current, enougth to causing failure operation in the H-bridge. So the electronic is still working, but H-bridge do not respond anymore. Time to recharge!

About This Instructable




More by akund:Self-Balancing PT (homemade Segway)
Add instructable to: