Introduction: Arduino Nano Segway
A few days ago I got an arduino nano and what better way to test it than to make a NANO SEGWAY !!
What is a segway?
So a segway is a robot that has two wheels on which it rolls vertically. Since it has just two wheels the robot tips over in front or back but using some sensors and electronics the robot adjusts to the tilt to get its center of gravity under the wheel base and manages to balance. (further explained in the next step)
Please like and vote for it in the contest.
Hope you enjoy this instructable.
You can watch the video to see it in action.
Step 1: How the Robot Manages to Balance?
Simple, we use this neat little sensor that is known as an accelerometer. (this device measures the acceleration of a moving body) Using this we can find how much the body of the robot has accelerated (in other words "moved") and compensate by spinning the motors in that direction to regain balance. The picture on top explains this idea very clearly.
Step 2: Parts Needed...
- arduino (I had decided to make the segway as small as possible so i used the arduino nano, but depending on the size of your segway any will work)
- l293d motor driver (i had already built mine earlier but in step 7, i will show u how to make it)
- adxl 345 accelerometer (i would suggest using this one, if not you will have to download other libraries and change the code according to your accelerometer)
- 2 dc motors (again mine were these tiny ones cause i had decided to keep it small)
- batteries (to power arduino, and the 2 dc motors)
- arduino's programming software
Step 3: Motor-driver
The motor driver I used, I built myself and I will be soon putting up how I made it as a separate instructables. But till then you can either buy one ready-made or google it saying something like "diy l293d motor driver".
Step 4: Let's Get Building
So as I have told you I had in mind to make the segway as small as possible so I dint make any extra housing I tried to use the parts and components themselves as the main body. So in my case I drilled a small hole in the motor housing and screwed it onto the arduino nano's pre-built holes so the arduino itself acted as the body. Then I stacked up the motor-driver on top and finally the accelerometer right on top. I didn't solder the pins on my nano cause it would become much more bulky.
Things to remember :
The main things to remember is that the higher you place your accelerometer the more accurate the results from the accelerometer will be. So I placed my accelerometer right on top.
Step 5: Finish Building
To finish the building process attach the motor driver and the accelerometer to some firm spot on the body of the segway.(remember to attach the accelerometer right on top). Now attach the signal wires from the motor driver to the arduino and the motor wires to the motors. The motors usually require an external power supply but the small motors I was using ran on 5 volts so I used the arduino's 5 volt supply to power the motors.(Follow the diagram above to attach the motor-driver). Next attach the accelerometer. Again follow the second diagram.
Step 6: Test Your Accelerometer
This is the first step that you would want to try. First upload the program linked below onto your arduino and attach the accelerometer to the arduino following the diagram above. The code is well commented and you can follow the comments to understand the code better. Once you have uploaded the program and attached the accelerometer open the serial monitor in the arduino software. The values that the accelerometer collects are printed out there. move around your accelerometer and observe whether the value changes. One last thing is to check the values that you are getting when your accelerometer is totally flat and remember that value look. You wont need the values of each axis just the one that you will be using which depends on the way that you place your accelerometer on the segway.
Reasons why it might not have worked:
- there might be a loose connection
- the accelerometer I was using communicates with the arduino with 3.3 v so DON'T plug it into the arduino's 5 v supply.
Step 7: Coding Again
Now open the program attached below and connect your motor driver to the arduino .(look at the diagram on top for any help). Then recall the value that you had kept in mind and plug it into the code in the line 14 where it says 0. (picture on top to make it clearer (picture 2)). Now upload the program onto your arduino and you should have a segway that can balance by itself.
Reasons why it might not have worked :
- The motors might be weak or not very fast at changing direction. So i would recommend starting of with good motors to avoid this problem. I too was facing this problem and then i decided to change to these mini quadcopter motors which worked great for me.
- Or another way you could try to solve this problem is by doing it through code. What you will have to do is reduce the forward_thresh and backward_thresh in lines 16 and 17 till you reach a balance. +10 and -10 were working great for me reducing it was making my segway over compensate and falling over in the opposite side.
Step 8: DONE !!!
You could go much further and do much more such as controlling it via a smartphone or making it full sized for a human to sit in it basically there no limits.
If you have questions watch the video everything is said very clearly if you still have questions i will be glad to answer them in the comments section.