# Relating position sensor voltage input to Arduino PWM % output

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

• Check out this guide on scaling ADC result, really helpful. May 3, 2012 at 8:56