0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Check out this guide on scaling ADC result, really helpful. \$\endgroup\$ – abdullah kahraman May 3 '12 at 8:56
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ yes I am making a control system, since my error is in voltage from the position sensor I need a way to relate change in voltage (error) to change in PWM to the motor. Not sure what you mean about the % PWM part, basically I was looking at the oscilloscope on each test to get that. so instead of 45% PWM in the graph I put in 0.45 and recalulate? I'm still having trouble relating that to the 0 to 255 number to output the PWM Would it be like 0.45 * 255 = value for PWM ? Basically I'm doing this: read in sensorvalue 0 to 1023, convert to voltage error = sensorVoltage - Setpoint \$\endgroup\$ – zacharoni16 Apr 30 '12 at 14:22
  • \$\begingroup\$ I know the Nominal PWM for the setpoint sensor voltage is 114.5 around 45%.. Now not sure how to scale the error (Change in voltage) to newoutput (Change in PWM) would it be Output = 255 * ((slope*error)/ 5.0) + nomialPWM slope is the slope of the line found in excel \$\endgroup\$ – zacharoni16 Apr 30 '12 at 14:24
  • \$\begingroup\$ I'm not sure what exactly you are asking in some of the cases, but as for PWM output, yes, the maximum PWM duty cycle is scaled by the percent output you want. If the system is set up so that 255 is the maximum PWM duty cycle and you want a 45% output, then you should set the duty cycle to 45% * 255 = 115. \$\endgroup\$ – Olin Lathrop Apr 30 '12 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.