Relating position sensor voltage input to Arduino PWM % output

arduino

This is a general question, I believe it maybe a math question but it is for the Arduino platform.

I'm doing a ball levitation project that will keep a ping pong ball suspeneded at a setpoint in a tube, the balls position is read with a laser position sensor that gives an analog voltage related to the balls position. The sensor output is pretty linear for the operating range of the tube's length.

Then I have a fan controlled with PWM as a %, this is manually adjusted with a pot, and I marked a scale on the tube in one inch increments.

Basically I made a table of sensor voltage output values, related to PWM% values of the motor for different positions in the tube.

I made a graph relating the two variables and it is surprisingly pretty linear. I did a trendline in excel and got a line equation of Y = 3.0265X + 15.05

X is the sensor position(in volts) and Y is the PWM value in %,

The part I'm having is taking the analog voltage from the analog pin that is a value of 0 to 1023 in the arduino and relating it to a PWM to analogWrite to the motor (A value from 0 to 255).

I know the analogRead() is and ADC so do I convert the 0 to 1023 value to a voltage by (5.0 * analogvalue)/ 1023;

That will give me volts, but I dont understand how how to relate this to PWM, like a change in sensor voltage to a change in PWM to the motor. I think I'm on the right track but not connecting the dots together.

Any help will be appreciated!

Thanks

Best Answer

All you need to do is to scale the equation based on the new units. If X was previously Volts and now is in units of 5 V / 1023. So scale your coefficients by 5 / 1023 to get the same Y with the new X units.

While you're at it, you probably want to scale the coefficients so that Y is in something more useful than percent. Percent may be appropriate for displaying a fraction to humans but is pretty useless and inconvenient otherwise. Either make it a fraction from 0 to 1 so you can easily multiply it by whatever maximum PWM value your hardware needs, or incorporate this into the equation in the first place. So start by scaling the coefficients down by 100, then up by the maximum hardware duty cycle value. After scaling to the new X as above, you have the equation from A/D reading to PWM output value.

That answers your question, but none of this is what you realy want. It seems you want to implement a control system. For that it is good to know that the control output (the PWM duty cycle value) produces a roughly linear system output (the ball position). The feedback gains you use then are a function of the system dynamics. Your X coefficient represents open loop gain which will effect the feedback coefficients, but it's probably simpler to find the right values by imperical tweaking.

A simple control scheme for which there is much information out there is called "PID". That stands for Proportional, Integral, and Derivative. A PID controller is a whole subject onto itself, but there is much available out that out there so there is little point repeating it here.