PWM input

Hi guys, I am new to Arduino world.. I've been assigned a task to develop a pwm reader to read the

output (single channel) of a pressure transducer. Wondering if Arduino supports that functionality.

Secondly, if it does, are there any accuracy concerns associated with that. The output waveform of the

transducer is attached. Many thanks in advance.

That's not PWM, its pulse-width coding. You'll probably want an ISR to decode that, checking the delays
between edges (ie reading micros() and comparing to the previous time and seeing if its > or < than 60us).

You need to watch out for other ISRs skewing your measured times though.

volatile unsigned long prev_ts = 0 ;

void my_isr ()
{
  sense = digitalRead (pin) ;
  ts = micros () ;
  if (ts - prev_ts > 60)
  {
    if (sense)
    { // just seen long LOW
    }
    else
    { // just seen long HIGH
    }
  else
  {
    if (sense)
    { // just seen short LOW
    }
    else
    { // just seen short HIGH
    }
  }
  prev_ts = ts ;
}

void setup ()
{
  attachInterrupt (digitalPinToInterrupt (pin), my_isr, CHANGE) ;
  ...
}

Thank you for your prompt response. Information about ISR on Arduino website suggests that micros()

works initially but then starts to behave erratically after 1-2 ms. Wondering if that suggests that Arduino is

notcapable of reading pwm when events occur at few microsecond intervals. Please advise. Thanks.

Information about ISR on Arduino website suggests that micros()

works initially but then starts to behave erratically after 1-2 ms

Huh? You must be confused, micros() just returns a value immediately.

[ perhaps you reading something about why you shouldn't stay inside an ISR for more
than a few microseconds? - not relevant. ]

Hi,

Welcome to the forum.

Can you post a link to the spec/data of your pressure transducer please.

Thanks .. Tom... :slight_smile:

The Arduinos runs on a resonator, not a crystal.
These drift with temperature by some amount.
If you need better timing, you need to calibrate to
a crystal clock reference. I've used a DS3231 board
and a 1 second interrupt to calibrate the processor speed.
It is good to better than 1x10-5 within any 1 second span
and long term better than 1x10-7.
Dwight

Thank you guys for your effort. Please find attached datasheet that shows the output waveform of the

transducer I am trying to read. I read about the resonator drift issue that is why my initial question was to

know if Arduino is a suitable platform to use for this type of application or not. Please have a look at the

attached document and advise. Many thanks in advance.

Datasheet.pdf (44.4 KB)

Hi,

Sorry, we need the complete manual, what is the make and model of the pressure sensor so we can try and locate some information.

Thanks, Tom... :slight_smile:

For decoding see DHT22 library, it is the same principle = detecting the length of 1s and 0s.

Warning digitalRead () function is very slow compared to the time to measure.

Best to do is using Rob Tillaert's library available on the playground, or even better to use direct reading of the registers of the micro controller but in this case you must use the Atmel I/O name , see datasheet.

FYI

  • DigitalRead () ~ 60/80 clock cycles, depending if I/O is PWM capable or not.
  • Method Rob T ~ 30 clock cycles, not possible to make faster because Wiring/Arduino store variables in Flash with PROGMEN.
  • Reading micro registry = 8 clock cycles, totaly independant of PWM capability.