Inverted Pendulum Project

Inverted Pendulum
Diagram of an inverted pendulum. Source: https://en.wikipedia.org/wiki/Inverted_pendulum

The goal of this project is to design and build a self balancing inverted pendulum to begin learning control systems and PID tuning. A few things I hope to get out of the project include learning how to model in Matlab Simulink, control system calculations including forward and inverese kinematics, how to use the model to tune PID, and learn how to code and setup the hardware.

I wanted the design to be a rail setup so that the motor would move the sled in a linear motion to adjust the pendulum. I figured this would simplify the process later on when I am trying to do PID tuning. To do this, I purchased rails with linear bearings. To determine where the pendulum is, I had purchased a rotary encoder that will allow the pendulum to spin freely and return position data. I plan on 3D printing the structure and the sled for the pendulum. All the parts ordered so far are in the table below.

Item Price Link
Limit Switches $4.89 amazon.com
CNC Shield and Stepper Driver DRV8825 $14.99 amazon.com
Belt and gears $9.99 amazon.com
Linear Bearings $9.99 amazon.com
Rotary Encoder $15.83 amazon.com
Linear Motion Rod $8.99 amazon.com
Stepper Motor $7.99 amazon.com
Power Supply $9.98 amazon.com
Threaded Inserts $14.99 amazon.com
Shaft Coupler $3.99 amazon.com



Process

1. Mechanical Design
I knew I wanted the project to be on a linear sled controlled by the stepper motor. I believed it would be an easier introduction to control over a rotational pendulum. I used Fusion360 for this project as an opportunity to learn the software and add to my portfolio.
The first part that I designed was the sides of the mechanism. The intent was to hold the 8mm rods, have slots for mounting components, space for the control belt, and potential for mounting to the table. The final design is shown below.

Pendulum Stand Side

Sides Printed with Linear Rods

After I printed the sides, the next design was the mount for the stepper motor. I wanted this to attach to the side with the GT2 gear sitting in the middle of the stand. The original print had the legs a little too short, so a spacer was later printed and added onto the bottom. The CAD was adjusted to correct this.

Stepper Motor Mount

Printed Stepper Mount

The next item I designed was the sled for the encoder. To ease the printing process, the attachment for the belt was printed separately. The only issue that arose was I did not include enough tolerance in the mounting holes for the encoder, and these were drilled out for a better fit. To help with attaching the belt to the sled, a threaded insert was placed in the standoff.

Encoder Cart

Printed Encoder Cart

Sled Standoff

Belt Connection

The final design is shown below. As of now, it seems to work well, however, I may need to add a cap to the end of the sides to prevent the rods from sliding under the tension of the belt. Currently, the friction fit seems to enough, but this may worsen over time. The original intent was to add a cap, but this was forgotten in the first iteration.

Final Design

2. Stepper Motor Wiring
To drive the stepper motor with the Arduino, I am using the DRV8825 stepper motor driver. I chose this one for the Nema17 motor because it was capable of providing a higher current than the A4988 and should have better microstepping, although this is not used currently.

DRV8825

The website, Last Minute Engineers, was extremely helpful in wiring and setting the motor driver. To start, I connected my 12V power supply to the breadboard and then the positive to the VMOT pin and ground to the GND MOT pin, bridging the two with a 100μF capacitor to protect against voltage spikes. I then connected the ground of the microcontroller to the GND LOGIC pin of the driver.
The M0, M1, and M2 pins are to adjust for microstepping. I left these alone so that the driver was set to full stepping. To control the motor, I connect pin 5 of the arduino (PWM capable) to the STEP pin of the motor driver, and pin 4 of the arduino to the DIR pin of the driver. Then, I connected the SLEEP, RESET, and FAULT pins of the driver to the 5V supply from the microcontroller so the board will be enabled. These have options to control later to add safety features that may shut down the driver.
The next step was to set the current limiter on the driver before connecting the motor, which is what differed from Last Minute Engineers. I did it differently to avoid burning out the motor accidentally. To set the current limit, there is a potentiometer that allows adjustment when measuring the voltage between the potentiometer and the GND LOGIC pin. The voltage should be set so Vref = Current Limit/2. I set mine to 0.7V for a small buffer of the 1.5A rated Nema17 motor.
After the current limit was set, I connected the motor wires to the A1, A2, B1, and B2 pins of the driver. The phases of the motor must go together (A or B) for the motor to work properly. I had originally had these wired differently (same way it came out of the motor) and the motor tried to move but could not. After swapping the wires around, the motor worked perfectly (an issue that took too long to figure out).

Stepper Motor Wiring

3. Rotary Encoder Wiring

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.




Back to Projects