Read logic voltage of PWM(not level)?

So I have a quick question. Is there a simple(or not so simple) way to read the logic voltage of a PWM signal with an arduino? Obviously an RC filter could convert it to an analog voltage if I wanted to read the level, but I'm interested in simply acquiring the "HIGH voltage" at any given time. Any ideas?

Hi, If you want to know the logic level, just sample the PWM using analog input for a time period, and store the maximum value found. You will get if your samples are not synced to the PWM, a string of readings, some representing 0V and others representing the max value.

Not sure of your application, don't forget arduino analog input maximum is 5V.

Tom.... :)

Thanks Tom!! You rock. I appreciate the help!

What's the best way to go about that from a code perspective? I have an idea but I'm unsure about specifics. Any ideas there?

My initial thought would be to do a digital read and if its high to set have to do an analog read. Would that work? It seems a bit cumbersome lol

Never mind that last post, I see what you're saying. That makes a lot of sense!

Os use the pulseIn() functions to determine how long the high & low durations are.

So I've been searching for a method of identifying the highest value during the analog read period and can't find anything. Is there any common method for doing so?

Well PWM oscillates between 0 and 5V - from what I read you want to know the logic value so when it is at 0 or when it's at 5

so when it goes from low to high or the opposite you can have an interrupt

As long as your arduino is fast enough to track the frequency of the PWM (so probably need the pre scaler to be pretty high if you generate this from an arduino) then you can indeed track this

Actually, What I need is that actual voltage of the logic, not just the state. I likes Tom's idea of analog reading for a period and storing the highest value. That sounds pretty good.

I don't understand any of this. PWM will be HIGH or LOW (5V or 0V). An analog read is not appropriate here. It will return 255 (high) or 0 (low) or something close to that. Possibly if the PWM is fast enough you will get a reading in-between but that would depend on the moment you sampled it.

Is there a simple(or not so simple) way to read the logic voltage of a PWM signal

You need to clarify that. Do you mean the duty cycle? Otherwise it is meaningless. The voltage will be as I described above. On/off/on/off with varying intervals.

Well the real voltage is either 0 or 5v.

Are you looking for the duty cycle? Is there a load associated to that PWM? Resistive? Inductive? Both?

Okay let me clarify. I don't want the duty cycle. What I want to do is measure how much the voltage of each pulse drops from passing through a resistor. This is why I need to know that actual voltage, not just the duty cycle. I want to compare the voltage before and after the resistor. Obviously we know the logic level of the pulse before it hits the resistor. It's the level after that I'm interested in reading.

Unless I misunderstand you, how difficult can it be ;)

void loop()
{
  // remember max value
  static int maxValue = 0;

  // get current value
  int currentValue = analogRead(A0);

  // if current value greater than last max
  if(currentValue > maxValue)
  {
    // update
    maxValue = currentValue;
  }
}

@sterretje Thanks! Works well!

I appreciate the help guys, thank you.

mdlockyer77: @sterretje Thanks! Works well!

I appreciate the help guys, thank you.

You should thank TomGeorge ;) Code was based on my interpretation of reply #1

Hi, Good to hear.

The thing with that code is it can be 5V logic or 3,3V logic or any logic level attenuated by load or wire, you will get the resultant maximum pulse height.

We need to realize that 5V is not absolute HIGH, there are parameters that dictate the threshold, TTL, MOS, DTL, etc etc.

Tom... :)

TomGeorge: Hi, If you want to know the logic level, just sample the PWM using analog input for a time period, and store the maximum value found. You will get if your samples are not synced to the PWM, a string of readings, some representing 0V and others representing the max value.

Not sure of your application, don't forget arduino analog input maximum is 5V.

Tom.... :)

This sounds strange. PWM consists of pulses, like 0's and 1's. Rather you need to calculate on-time and off-time, and from this ratio construct the intended voltage.

mdlockyer77: Okay let me clarify. I don't want the duty cycle. What I want to do is measure how much the voltage of each pulse drops from passing through a resistor. This is why I need to know that actual voltage, not just the duty cycle. I want to compare the voltage before and after the resistor. Obviously we know the logic level of the pulse before it hits the resistor. It's the level after that I'm interested in reading.

Okay, so the fact that there is PWM involved, is kind of not the point then? :-)

Rupert909: This sounds strange. PWM consists of pulses, like 0's and 1's. Rather you need to calculate on-time and off-time, and from this ratio construct the intended voltage.

I believe the OP is trying to build circuitry based on the actual voltage of each pulse.