For a project, I have to measure a PWM signal that has an upper voltage 3 to 5V and a lower voltage of between 0 and 2V.
Signal is 1kHz and has a variable duty cycle of 10% to 90%.
Now my question: How can I measure the two voltage levels?
What I tried is: I read the signal with an analog pin (A0) on the arduino. I measure the voltage 100 times in a for loop to make sure that I measure both levels and store the highest and the lowest values I get from the arduino.
So the code looks something like this:
pLow = 1023;
pHigh = 0;
value = 0;
for (int i = 0; i < 100; i++)
{
value = analogRead(A0);
if(value < pLow) {pLow = value}
else if(value > pHigh) {pHigh = value}
}
I got this code from the OpenESVE project and it seems to work there....
my problem is, that i always get the same pHigh and pLow, that is, they are all equal to the upper value.
How can I measure pLow?
That compiles with no ; in the if-else statements?
{pLow = value}
{pHigh = value}
or is this just example code?
It seems to me that you measuring what is basically a randomly occurring signal with respect to the code.
I am thinking maybe you could take advantage of the Analog comparator vs taking a direct reading.
Use a filtered arduino PWM output to create a DC level as one input to the comparator.
Use your 1KHz signal as the other.
Slowly bring the DC up from 0V, note the level when the comparator shows the 1KHz starts going lower than your DC, then keep raising the DC and note the level when the comparator shows the 1KHz stops going higher than your DC.
This is indeed just example code, the actual code has all the ; in the right place
Regarding your suggestion: it is a clever idea! However, it requires some additional circuitury... If i can avoid that by a more intelligent code and direct voltage reading, i would prefer this....
Is there any chance this might work?
Are you talking the average voltage or the max? And, what is downstream of the PWM signal? (If it's a cap or a battery things may not be what you expect.)
The "additional circuitry" is only a resistor and capacitor to smooth out the Arduino PWM into a DC level.
Can you go into a tight loop and take 100 samples and store in an array, then manually reveiw the data to if reflects the waveform? Using the internal ADC you should see 9K to 10K samples/second.
fat16lib has written some very fast data loggers that store data to SD card, so I guess it should be possible.
Processing in real time as your are attempting,I don't know.
@cptdondo: I want to measure the max voltage and the min. voltage. of the signal.
The signal is acctually a -12V/12V PWM signal, scaled with a (bit comlicated) voltage divider to 0-5V. downstream of the signal are only resistors that connect the signal to ground.
@CrossRoads: thank you for your suggestions! I will keep it in mind and will defenately give it a try if i can't measure the signal directly.
So if you know the ranges, then test each reading against the range:
if(V > 0 && V <2) then {{we have a low signal; do some stuff}}
else if(V > 3 && V< 5) then {{we have a high signal; do some stuff}}
else {{we have a bogus signal; do some stuff}}
no?
You will have to do this enough times to get make sure you have both. You could use a counter for each high, low, and bogus. If you run enough iterations you will get some idea of how often you're hitting each.
You're really doing Monte Carlo modeling (look it up).
Thanks, with your code modifications it works! However, I still need (usually) less than 100 cycles for this... so I don't really understand why the previous code didn't work.
Monte Carlo Modeling is a method of analyzing data if you have clear boundaries and uncertain source. Look up the article in wikipedia. You have an uncertain source (your PWM signal level) and a clear test for boundaries.