Hey guys. I know there are already a lot of balancing robot projects on the internet,
but I wanted to make one as well. Actually, I wanted to make something special and make
a mini balancing robot, but I kind of failed with the stepper motors and I will explain
the problem in this video, so stay tuned. So what I’ve done in the end, is to use the same
PCB I’ve prepared for a mini balancing robot, design a small 3D printed body and use bigger
stepper motors and like that be able to finish my idea. The robot should get information from
a homemade remote with Bluetooth connection and like that we could move around. But internally,
the robot should also get information from an IMU module and calculate a PID value of the
angle so it will never fall and always try to stay horizontal. That’s pretty interesting and I think
you will learn a lot with this video because I will explain each step of the circuit but also
the code. So what do you think, will this work? Stay tuned to find out, so let’s get started.
What’s up my friends, welcome back. This here is the PCB I’ve made for this project.
My plan was to use this PCB with this kind of stepper motors.
I have these from old DVD writers and I thought they would work great connected
to my PCB. Is very easy to control them with some stepper drivers so that’s not the problem.
The problem was mechanical. You see, these motors have this shaft connected to the magnets inside.
This shaft is supported on both sides, here and here. In order to add a wheel to this shaft,
I had to cut it smaller, and in that way, we have support only on one side, and that’s the problem.
Without support on both sides, this shaft will move around and automatically break all the small
coils and magnets inside. I’ve tried gluing a bearing at the output but without success.
Inside, the shaft is kept in the center by a metal ball, and once you move that,
you can say goodbye to the hole motor. I’ve then ordered these motors here. But this time they are
too small, don’t have enough torque and guess what. Their shaft is also weak and will break
very easy just as the other ones. So I had no other option but to use NEMA 17 motors.
These are one of the smaller NEMA17 steppers. So, on the side of the PCB I’ve designed this support.
Like this we can attach the motors below, the battery in the middle and the PCB on top kept
in place with some plastic supports. This is not a mini robot anymore but anyway, let’s
make this robot and you will definitely learn something. So, let’s see what we have on the PCB.
If you want the same PCB, get the GERBER files from below and go to PCBWAY. Click quote now and
select your settings such as thickness, amount of PCB, materials and the solder-mask color.
In my case I’ve selected black but you can select any other, they now
have purple or mate black which look amazing. On the next page upload the GERBER zip file
and order the PCBs. I’ve received the PCBs to Spain in around 8 days so let’s check them out.
On the front part we have spaces for two stepper motor drivers. For that you can use
the basic A4988 drivers or even better, use the TMC2225 drives for a better and silent movement.
Below that we have these 4 pins that are used to connect a buck converter for 5V.
I will use a 2S or maybe a 3S battery,
and since the digital part works at 5V, I will connect a buck converter here set to that voltage.
On the bottom sides we have the output for two stepper motors.
Here is where I wanted to solder those tiny motors but unfortunately, they break too easy.
But if you manage to sue them, just solder the motor and use a zip tie like this to
keep it in place. It would be cooler to have a mini robot. Ok, what more do we have on the PCB.
These 4 pins here are for the IMU module and I will use the MPU6050.
We use this to measure the angle. On the top part we have 4 more pins for a Bluetooth module.
I will use the HC-05. On side of that we have 4 more i2c pins for an OLED display.
I might add this as well so we can draw faces for the robot and give it more personality. I’ve also
placed an SMD buzzer here for sound notifications. On the bottom of the PCB we have some LEDs to make
the robot look cooler and also the input from the battery. Now let’s see the back of the PCB.
Actually, the Bluetooth module will go here since the other side will be full of components.
Below here we have the ATMEGA 328 microcontroller and the needed components in order to work.
Is very difficult to see, but we have 3 pairs of pads for each stepper driver and by soldering
these, we can select the micro-stepping amount. And that’s it for the PCB, that’s all we need.
The PCB, the stepper drivers, the buck converter module, the HC 05 Bluetooth Module,
the MPU6050 sensor and two NEMA 17 stepper motors. If you want,
you can also add an OLED screen like this one. As for the tiny components we need the buzzer,
some SMD capacitors, some LEDs and resistors and the ATMEGA chip together with the 16MHz resonator.
For the battery you can use a 3S LIPO like this one. But in my case, since I have a lot
of cellphone batteries, I will connect 3 of this iPhone batteries in series and get a 12V one.
These are very flat and occupy less space. For the body, just download the STL files from below and
print them with PLA material. I’ve used a 0.2mm layer height and 0.4mm nozzle with 3 perimeters
and 20% infill. As for the wheels, the inner part is made out of PLA but the exterior is made out
of flexible a material that must be printed at lower speeds. Like that we can have more grip.
The wheels have these holes so I can pass a screwdriver and tight a screw here.
We have to add a screw nut and then insert the shaft of the motor
and tight that screw. Like that it will stay in place. Then we can add the rubber part on top.
So, this are all the parts we need for the robot, more or less. You have the full part list below on
electronoobs.com Let’s assemble it, it should end up something like this 3D design example.
I get the main 3D printed part and I add the step motors. You just have to apply a little bit of
force and it will snap into place. Then we add some screws and washers and tight the motors in
place. Make sure the motor connector is on the back side of the motor.
Get the plastic wheels and add a small nut inside this hole. Also add a screw.
Now add one wheel to each motor. Tight that screw so it will stay tight on to the motor shaft.
This part was printed in flexible filament. We can add this part on top of the wheel.
The battery will go here above the motors. But first, let’s assemble the PCB so download this
schematic from below and have it in front of you. I first soldered these SMD capacitors.
Then I’ve added some female pins for the drivers so I can remove them if I want.
I get one of these small buck converters and solder the 5V connection on the back.
I connect it to 12V and test if the output is around 5V.
Now we could solder this to the PCB but we will do that later. I also solder the buzzer here on top,
and the LEDs on the bottom of the PCB. On the other side, I solder the ATMEGA microcontroller,
the crystal resonator and the small resistors and capacitors. At this point I connect and
FTDI programmer and test the connection to make sure it works. On the front side, I solder the
MPU6050 accelerometer as well. On the back, I connect female pins for the Bluetooth module.
Now that the ATMEGA chip is in place and it works, I can solder the buck converter on top
so we can supply the PCB with a battery. I might add the OLED screen later when everything works.
Please check this full schematic for all the connections if you’re trying to make
the same project. Remember to also solder together the small pads for MS2 on the back
in order to enable quarter micro stepping. At this point you could download the code from
below and upload it to the PCB using the FTDI programmer. I will go over the code in a moment.
We now have to connect the PCB to the motors and the battery.
For the battery, I’ve soldered in series, 3 of these iPhone batteries.
Like that I would have around 12V and I can also have a very thin battery.
Get this plastic printed leg and screw it on one arm of the PCB
and do the same with the other side but don’t tight the screws yet.
Place the PCB over the main body and glue in place these small plastic arms.
Now we can tight those screws and the PCB is fixed in place.
Solder wires from the stepper motors to the PCB on these pins here.
Between the battery and the PCB I add a switch so I can turn it ON and OFF. Before we go over
the code, I have to say that with this shape, the robot won’t be stable. Why? Well, because this
type of balancing robot needs to have some weight on the upper part in order to have inertia, but at
this moment all the weight is on the bottom side and as you can see, is very difficult to balance.
This robot works exactly the same as trying to balance a stick in your hand.
If the stick is too short, it would be impossible.
When the stick is large enough, you can move the hand in the same direction of the tilt
and you can stabilize it. That’s why I’ve placed some threaded rods on top of the robot.
And on top I have a plywood part and here I will now place the battery. Like this we have more
inertia when moving. With the same code before, I’ve got better results but as you can see,
the robot is still wobbling around. That’s because we have to adjust the PID values in the code.
This was the longest part of this project. I had to manually go and change the PID values
till I got better results and even so, I’m not quite happy with the final results.
If you start only with P values and the I and D are 0, you get this kind of oscillations where
the wheels are rotating in the same direction as the tilt of the robot according to the angle.
Then we add some D values and it will react to the speed of changing of the angle, and try to
stop the oscillation, but that's not enough when the error is very small. Finally, to fine adjust
the position, we add the I that will increase or decrease slowly when the error is small.
After more than 30 tries, I've got more or less good results and the robot is quite stable but
I'm not entirely happy yet. At this point I decided to make a second part for this project
where I will show you how to tune the PID values. Till then I will try to get better results, and
in that video, I will explain the mathematics and the physics behind this kind if project including
the IMU position, center of gravity, inertia of the robot, motor speed and PID algorithm.
Before I end the video, let’s take a look at the code. These are the variables that
you could change in order to tune the PID controller. These is the same as we had for
the brushless PID controller or the ping pong ball controller, but this time we use stepper motors.
Top create the step pulses applied to the stepper drivers, we use a timer,
TIEMR 2 in this case. By setting these registers, we make that timer to click every 20us.
With these lines we read the raw data from the gyro and accelerometer.
To overcome the gyro offset, we make around 500 reads from the IMU module and save that data
as offset and subtract it later in the main loop. Then we calculate the angle each loop
which is set to be each 4000us. Using this gyro angle variable, we can calculate the PID value.
As always, we find the PID error which is the difference between the real
angle of the robot and the desired angle, which should be 0 so the robot would stay horizontal.
Finally, inside the timer routine, depending on the PID output, we calculate the speed of
the motors by creating more or less, faster or slower pulses applied to the stepper motors. To
make the code faster, instead of digital read we use register control where this line for example,
is the same as setting the digital pin D3 to HIGH. For more about this, take a look at my Arduino
register control video, links are below. With this code the robot should be able to stay horizontal.
I lift the robot, it detects the angle and starts the PID code. When it detects a tilt on the front
side, it accelerates the wheels so it will get horizontal again and do the same process backwards
if the tilt is on the other side. Do this fast enough and with a PID control, and you get a
stable horizontal robot. This is not yet perfect, I’ll make it better in a second part of this
project. Also, without a remote, we can’t control it so I will also add that in the second part.
I will use this Wii console Nunchuck to get the movement from the Joystick and send the data using
the Bluetooth module. This actually has an i2c communication, so I might use that to read the
values of the joystick. The schematic would be something like this with a battery and a switch,
the Arduino, the joystick analog outputs and the Bluetooth module.
Stay tuned for more. So guys, that’s how a balance robot works and how to make one.
You have my PCB below for download, you also have the schematics for the robot and the controller,
the part list and the codes, everything is below in the description. If you learned something new,
consider giving me a like or comment below. Thanks again, and see you later guys.