Introduction: Self-balancing Robot
In this Instructable we’ll show you how to build the self-balancing robot we made as a school project. It’s based off some other robots, such as the nBot and another Instructable. The robot can be controlled from an Android smartphone via a Bluetooth connection. As this Instructable only covers the building process, we’ve also written a document to cover the technical background of the code and electronics. It also contains links to the sources that were used, so you can take a look at them if the document isn’t comprehensive enough for you.
To follow all steps of this project you’ll need some 3D printing skills or some other clever way to attach the wheels to the motors.
Step 1: Requirements
The robot is based on a Martinez brushless gimbal controller board. There are some slight variations of this board, but as long as you have one with an ATmega328 chip and L6234 motor controllers you should be fine. If you search for “Martinez board” on Google Images, you’ll see there are some boards with an easy connector for the IMU chip and/or battery, instead of pin headers or holes. In the last case, it’ll come in handy if you order a pack of header pins, which you can then solder into the holes.
Some of the items on this list contain links to webshops.
- Controller: Martinez Board
DX.com (also comes with the IMU and some header pins).
- IMU: MPU6050
- Battery (450 mAh 3S LiPo battery) Note: you’ll also need a 3S LiPo charger
- 2x Motor: Brushless motor 2208, KV100
- Wheels (you can get these from existing toys or LEGO)
- 6x M2 screw 5 mm
- 8x M3 screw (length depends on the material for your exterior, one needs to be extra long)
- Bluetooth chip HC-05 (be sure to get one with a serial interface board attached, not just a bare chip)
IMPORTANT: Make sure the chip has a pin labelled KEY.
- Wires: Female to female DuPont
Buying a pack of 20 wires will be more than enough
- Velcro tape
- USB Cable to connect controller to PC
- Optional: Header pins
DX.com (you can cut or break these to the desired length)
- Plastic washers and spacers
Finally, you need some acrylic, wood or cardboard—accompanied by glue or tape—to create a structure that holds all components.
Step 2: Bluetooth Chip Configuration
Once you get a hold of all the parts, it’s time for the configuration of the Bluetooth chip. You’ll need a USB cable to connect the connect the controller board to your computer as well as the Arduino IDE to communicate with the components.
For this, you’ll need to download the file:
Then follow these steps:
- Connect the controller to your computer with a USB cable.
- Open the .ino file with the Arduino IDE.
- In the IDE, go to Tools, Board and make sure it’s set to Arduino/Genuino Uno.
- Now go to Tools, Port and set it to the COM port that the controller is connected to.
Usually there is only one port. If there’s multiple, check the Device Manager (in Windows) to find out which one is the controller.
- Now, hit the Upload button in the IDE and wait for the upload to finish. Then, disconnect the USB cable from either the computer or the controller.
After you’ve done this, connect the HC-05 using DuPont cables as follows:
HC-05 Controller KEY +5V GND GND TXD RX RXD TX
Now plug the USB cable in again, then connect the HC-05’s VCC pin to another +5V on the controller. The LED should flash with a ~1 second interval.
In the Arduino IDE, select the correct COM port, then go to Tools, Serial Monitor.
Set the Line Ending option in the Serial Monitor to Both NL & CR. Set the Baud rate to 38400. Now you can use the Serial Monitor to send setup commands to the Bluetooth chip. These are the commands:
AT Check connection AT+NAME Get/set Bluetooth display name AT+UART Get/set baud rate AT+ORGL Reset factory defaults AT+PSWD Get/set Bluetooth password
To change the name, password and baud rate of the Bluetooth device, send the following commands:
AT+NAME="Example Name" AT+PSWD="PassWord123" AT+UART="230400,1,0"
The Name and Password options can be set to whatever you want, just be sure to set the baud rate using the exact same command as listed above. This sets it to 230400 baud, with 1 stop bit and no parity. After setting everything up, reconnect the USB cable (to exit the setup mode) and try pairing your phone to the chip. If everything works disconnect the USB cable and move on to the next step.
Step 3: Attaching the Wheels to the Motors
The wheels that were used in this project have an unknown origin (they were laying around in a drawer with a lot of other stuff). In order to attach the wheels to the motors, we 3D printed a piece that matched the screw holes on the motors. The pieces were screwed on using three 5-millimeter 2M screws per motor. Both pieces have a pin that fits the holes in the wheels’ axes.
The SolidWorks model is included. You’ll probably have to modify it for your wheels, or find a different practical solution to fit the wheels. For instance, you could use a Dremel to carve a hole the same size as the motor (or a tiny bit smaller to give it a snug fit), then you can press the motor into the wheel. Just be sure to get appropriate wheels for this job if you’re planning to do this.
Step 4: Creating the Exterior
For the exterior, two pieces of wood were used and cut to the same shape. To begin, we marked the motor’s circumfence on the bottom centre of the piece. We then marked each corner with a 45 degree line, making sure to leave enough room for the motor to sit at the bottom centre. We then clamped the two pieces of wood together and sawed the corners off. To finish things off, we sanded the corners to make them less sharp and remove splinters.
Now it’s time to drill holes for the screws and the axis that’s bulging out of the motor’s back. If you clamp the pieces of wood together when drilling, you only have to drill each hole once.
To create the layout for the screw holes, we used a piece of paper and placed it on the back of the motor and used a pencil to press into the screw holes, right through the paper. The piece of paper with the four screw holes was then placed on the wood so we could mark the location of the to-be-drilled holes. To drill the holes, use a 3,5 mm drill. Now, use a pencil and a ruler to find the centre of these holes and create the hole for the axis using a 5 mm drill. Attach the motors with M3 screws, but leave one of the wider spaced screws out of one motor.
To get the motor connector and wire inside of the robot, we also drilled an 8 mm hole a little above the motor. Make sure there’s enough room for the wires to bend without stressing them too much.
It’s important to work as precise as possible to create a (near-)perfect symmetrical exterior.
Step 5: Fitting the Components
Mark a vertical centre guideline on the wood so you can place the components in the centre. You can attach everything to the wood using velcro tape. In our robot we used small bolts and nuts to secure the controller board, but you can also use velcro tape (we didn’t have it yet at the time we attached the controller). Make sure you can plug in a USB cable after you finish the construction.
We placed the controller in the centre with the USB port pointing down, so we could plug the cable in from between the wheels. You can also point it to one of the sides.
Place the battery as high as possible, so the robot becomes top heavy. Also place the charging port in an easy-to-reach place near the edge.
Connect the Bluetooth chip VCC pin to +5V on the controller, and the Bluetooth GND to the controller’s GND. The controller’s TXD pin goes to the Bluetooth RX and the RXD pin on the controller goes to the Bluetooth TX pin. Then just stick the Bluetooth chip somewhere on the wood panel using velcro tape.
The motion chip has two screw holes, so we attached the chip using a spacer, in such a way that the centre of the chip falls over the centre of the motor. The orientation doesn’t matter, as the robot calibrates itself when booting. Be sure to use a plastic washer under the screw head to prevent shorting the circuit.
Then use DuPont wires to connect the pins to the controller. Each pin is labelled the same on the controller as it is on the motion chip, so connecting it is pretty self-explanatory.
Connecting a power switch is easy. We took one from an old device, and desoldered it from its circuit board. To use it as a power switch for the robot, you connect the battery’s positive wire to the pin (assuming it’s a three-pin switch) on the side you want to make the on position of the switch. Then, connect the center pin to the controller’s positive power input. We soldered DuPont wires to the switch, so that the battery itself isn’t permanently attached to the switch.
Connecting the sides
Now you know the location of the components and you've got the two sides of the robot. The final step in constructing the robot will be connecting the two sides with each other. we used four sets of three pieces of wood glued together and screwed it to the sides so that our motion chip was on the middle axis of the robot. It should be said that the material used, provided it is strong enough, doesn't matter too much. You can even use a heavier connection on the top to increase the height of the center of mass even more. But unlike the vertical position of the center of mass the horizontal position of the center of mass should be kept in place as much as possible, above the wheel-axis, as coding the code for the motion chip would become rather hard were the horizontal center of mass displaced.
Now you’re ready to upload the code and tune the controller.
Step 6: Uploading and Tuning the Code
To upload the code, you need a computer with the Arduino IDE. Download the .ino file below and open it up with the Arduino IDE. Uploading it to the controller is done in the same way as you did with the code from the Bluetooth setup.
In order to make the robot work, you need to download the app ‘Joystick bluetooth Commander’ from the Play Store. Turn the robot’s power on and place it on the floor, on either its front or back. Launch the app and connect to the Bluetooth chip. Datafield 1 will go from XXX to READY once the robot has calibrated itself (5 seconds to place it on its side, followed by 10 seconds of calibration). You can turn the robot on by switching button 1 in the app. Now place the robot vertically on the ground and let go once you feel the motors powering on. This is when the robot starts balancing itself.
The robot is now ready to be tuned, as its stability probably isn’t great. You may try if it works without additional tuning, but you have to make the robot pretty identical to ours for it to work properly. So in most cases you should tune the controller to work best with your robot. It’s quite easy, despite being quite time-consuming. Here’s how to do it:
Tuning the controller
Somewhere in the code you’ll find 4 variables, starting with a k. These are kp, kd, kc and kv. Start off by setting all values to zero. The first value to set is kp. The default kp value is 0.17. Try setting it to something a lot lower like 0.05. Turn the robot off, upload the code and see how it tries to balance. If it falls forward, increase the value. The smartest way to do this is by interpolating:
- Set the value to something low and try it out
- Set the value to something high and try it out
- Set the value to the average of the two and try it out
- Now try finding out if it balanced better on the low or high value and average the current value and the one at which it worked better.
- Continue until you find a sweet spot
The sweet spot for the kp value is when it’s around the edge of under- and overcompensating. So sometimes it will fall forward as it can’t keep up with its falling speed, and other times it will fall backwards because it overshoots in a different direction.
After you’ve set the kp value, set the kd. This can be done in the same way as you did with kp. Increase this value until the robot is almost balanced, so that it will swing back and forth until it falls over. If you set it too high, you can get it to balance pretty neatly already, but when the balance is disturbed too much, it’ll fall over (like when you give it a push). So try finding the spot on which it’s not quite balanced, but pretty close.
As you may guess, tuning the controller may take several tries as it’s getting more difficult with each new variable introduced. So if you think it’s not going to work, start over.
Now it’s time to set the kv. Interpolate this until you find a value at which the robot stops swinging, stays balanced and can handle a light push. When set too high, it negatively impacts the stability. Try playing around with kv and kp to find a point at which it’s the most stable. This is the most time-consuming step of the tuning.
The last value is kc. This value makes the robot return to its last position after compensating for a push or something else. You may try the same interpolation method here, but 0.0002 should work pretty well in most cases.
That’s it! Your robot is now ready. Use the joystick on your smartphone to control the robot. Beware, though, as going forward at maximum speed may still make the robot fall over. Play around with the controller variables to compensate for this as much as possible. The most logical step would be to look at the kp value for this, as that directly compensates for the robot’s current angle.
Important note on LiPo batteries
It’s recommended that you regularly check the voltage of your LiPo battery. LiPo batteries shouldn’t be discharged to less than 3 volts per cell—measuring 9 volts on a 3S LiPo. If the voltage drops below 3 volts per cell, there will be permanent loss of capacity of the battery. If the voltage dips below 2.5 volts per cell, dispose the battery and buy a new one.
Charging a LiPo cell with less than 2.5 volts is dangerous bcause the internal resistance becomes very high, resulting in a hot battery and a potential fire hazard while charging.