This is a ballandbeam control system I designed and 3D printed to learn about PID control.
I've posted the CAD and code for this project on my Github page here:
https://github.com/IVProjects/Enginee...
The control electronics consist of an Arduino Uno, a CNC shield, and a TMC2209 stepper motor driver. The TMC2209 motor driver allows the stepper motor to operate very quietly by interpolating step signals into 1/256 microstepping.
The distance sensor I used is an HCSR04 ultrasonic distance sensor. This sensor typically provides rather noisy data, so I used a moving average to act as a lowpass filter and attenuate the high frequencies. I also added some simple outlier rejection to remove the occasional erroneous measurement.
I used a 12bit AS5600 absolute magnetic encoder on the stepper motor to track the position of the motor. These sensors operate on the Hall effect and use I2C protocol to communicate with microcontrollers. I designed and printed a housing that attaches to the back of a NEMA17 stepper motor and holds the encoder board in the correct location relative to the magnet. The encoder magnet is diametrically magnetized and is attached to the back of the motor shaft.
I originally planned to use a ping pong ball as the rolling element of the system, but the distance sensor was not able to reliably provide clean data. I think the reflected sound from the sphere was too small in this case. That's why I made the cart with a large acoustically reflective surface.
To set the PID target position, I mapped the output of a 10k potentiometer to the position along the beam.