About the mechanical structure
I built this inverted pendulum using some standard mechanical structure except the 3D printed parts.
You can find all the standard component in hardware store.



About the PID control algorithm
You may familiar with the following picture. Yeah, It's a sketch map about PID control algorithm.

The beginners always spent a lot of time to figure out how PID control algorithm work. To make things simpler. I write a inverted pendulum arduino library. You can get the library from my github repository. Using that library you can build your own inverted pendulum without struggling with the code.
Here is the repository
You can config your inverted pendulum by using this single line code.
inverted_pendulum(float A_Kp, float A_Kd, float P_Kp, float P_Kd, long A_st, long P_st, int A_limit, int P_limit)
Then you can get the PID controller's output by using this function.
output = Inverted_pendulum_controller.InvertedPendulumUpdate(Angle_encoder, Position_encoder);
Pretty easy right?
About the sensor and motor
I use Grove - 12-bit Magnetic Rotary Position Sensor /Encoder (AS5600) and Grove - Ultrasonic Distance Sensor to collect the angle and position data as the feedback of contorl algorithm.

We all know about the potentiometer, quoted from Wikipedia "A potentiometer is a three-terminal resistor with a sliding or rotating contact that forms an adjustable voltage divider." Simply put, the potentiometer converts relative position information into electrical signals. But the traditional potentiometer needs to be in contact with the object being measured. What about scenes that need to be contactless, such as high-speed motor, or high-precision robot arm? Well, the answer to the contactless potentiometer solution is the Grove - 12-bit Magnetic Rotary Position Sensor (AS5600).
The Grove - AS5600 is a programmable 12-bit high-resolution contactless magnetic rotary position sensor. The Grove - AS5600 can work as magnetic potentiometer or magnetic encoder with excellent reliability and durability. Compared with the traditional potentiometer/encoder, the Grove - AS5600 has significant advantages: high precision, non-contact, no rotation angle limitation. All those advantages make it is perfectly suitable for non-contact angle measurement applications, such as the robot arm, tripod head, motor closed-loop control, machine tool axis positioning.

AS5600 is based on the Hall Effect, the build-in Hall sensor can detect changes in the direction of the magnetic field, thus, there is also no rotation angle limit. Then the magnetic field direction information is amplified by the amplifier, with the help of the build-in 12 bit A/D, the AS5600 module can output 4096 positions per round. The output is selectable, you can either use the I2C interface to output the RAW data or output the PWM wave/Analog wave via the OUT pin. Meanwhile, the maximum angle is also programmable, you can set the maximum angle from 18° to 360°, which means that the measured angular accuracy is up to 18/4096.

AS5600 is excellent for the non-contact rotary position sensor, so seeed made this AS5600 Breakout Board in the Grove form factor. With the Grove I2C connector, you can easily connect this AS5600...
Read more »
Nice work. I recently built something similar - https://youtu.be/iM3StpZzGmg - it still requires work like PID tuning but it's already capable of keeping the arm vertical for 10-15 seconds and it will hold it again full rotation if it drops (my rails are short so I only have 12 cm position limit to play with). However, it only works if I start with the arm in the up position, it is not capable of reaching the top position on it's own if I start at the stable point with the arm down. Do you think it's simply due to non-optimal PID parameters? (an alternative I am considering is to use fixed logic to bring the arm up and then switch to PID logic). Thanks