Simplest PID Ball Balancer

by Joshuar9202 in Circuits > Arduino

185 Views, 0 Favorites, 0 Comments

Simplest PID Ball Balancer

20240401_111312.jpg

I made a PID ball balancer to test out a simple PID controller. Unlike most other ball balancers on the internet which have 3 or more motors, I only used two motors for the project which made the programming more simple and easy to understand for someone like me who doesn't have too much experience with more complicated mathematics in programming. Additionally, I used a pixy 2 camera to calculate the position of the ball and the documentation and resources available for it makes it really simple to use.

Supplies

cnc_shield_v3_2.jpg
download.jpg
  • Arduino UNO
  • Pixy 2 Camera
  • 2 stepper motors
  • CNC shield (not necessary, it is possible to wire it without it, but the shield makes it a lot easier)
  • A4988 Stepper Motor Drivers
  • 3 jumpers
  • Jumper wires
  • Lego or anything else to build the whole thing out of
  • 12v power supply

Building the Structure

Screenshot (3).png

Download the pdf instructions and follow them.

I used a blue piece of construction paper that I taped to the balancing plate so that the white marble I was trying to balance would stand out from everything else. The pixy 2 camera mostly senses things from color, not as much from shape, so it is important to have the color of the object that you are using to be distinct from everything else that the pixy can see.

Attaching the Electronics

20240324_082650.jpg

Insert a Lego 16 tooth clutch gear onto the shaft of each stepper motor. The friction between the shaft and the gear is the perfect amount to be able to fit on the shaft while still transferring power to the gear. The 16 tooth gear should mesh nicely with the 20 tooth double bevel gear when the motor is inserted into the space shown in the picture above.

The pixy 2 camera should fit into the camera mount as shown in the picture.

The wire that comes with the pixy 2 should be fitted onto the camera and then attached to the ICSP pins on the Arduino in such a way that the rest of the cable goes farther away from the board. This also prevents any shields from being attached to the Arduino, I attached all of the pins from the CNC shield to the Arduino with male to female jumper wires.


Configuring the CNC Shield

20240323_220124.jpg
20240323_220856.jpg

The wires from the stepper motor should be run to the x and y motor slots on the shield. A jumper should be placed connecting the middle pin out of the 3 sets of 2 pins under the capacitor so that the motor is in double stepping mode. A jumper should be placed on the left of the shield connecting the two pins "EN/GND." I used A4988 stepper motor drivers which attach on to the shield in a way that the pin that says "EN" on the driver is in the top left of the shield. The current for the stepper motors should be properly limited by using the screws on the bottom of the A4988 board. HowToMechatronics has a good guide on how to do this at https://howtomechatronics.com/tutorials/arduino/stepper-motors-and-arduino-the-ultimate-guide/ Finally, once everything is properly connected, a 12 volt power source should be inputted into the blue terminal block near the bottom left of the shield.

Configuring the Pixy 2

Screenshot (1).png
Screenshot (2).png

After opening the pixyMon software, attach the micro usb cable from the pixy to your computer. The device should be recognized by the software. First go under action > toggle lamp. I found the lamp to be helpful in helping the camera distinguish between the ball and the background, but it is not necessary. From there, place the object you want to balance on the balancing plate, and then go under the action > set signature 1. The video should freeze and then your cursor will become a crosshair. Drag the crosshair over the object. Finally, in order to tune the camera to be able to see only the object you are trying to balance, go under file > configure and then change the slider that says "signature 1 range." Moving the slider higher will result in more things being recognized and moving the slider lower will result in less things being recognized. Changing the "camera brightness" can also help.

Programming

The code is a pretty basic PID controller. For more information and a detailed explanation of how a PID controller visit this link: https://pidexplained.com/pid-controller-explained/

Here is a quick summary of the program: First the stepper motors and the pixy camera are defined and enabled. The stepper motors start in a position where the arms are as far down as they can go, so it has a homing sequence where it goes to an approximate 90 degree angle. Then it goes to the loop section where the camera measures the position of the ball in the x and y axis and subtracts that from the target x and y values and that value is stored in the error. The error is then multiplied by the proportional gain and stored in the proportional variable. The integral is the old integral added to the error from the previous step, multiplied by the integral gain and the cycle time. The derivative is the current error minus the old error which is then multiplied by the derivative gain and divided by the cycle time. Then the proportional, integral, and derivative are added together to create the final output value. This is done for both the x and y axis.

Downloads

Done!

Simplest PID Ball Balancer

Video of it in action: