Measuring times between pin state changes

I'm receiving a signal in an arduino and I want to measure the period with which said signal goes HIGH to LOW and vicecersa. I know what this message will be (I wrote it in manchesterEncoded) so I wrote this code

  // find carrier wave
  unsigned long carrierDuration = pulseIn(RX, HIGH);
  if (carrierDuration > 40000) {
    // wait for LOW
    if (digitalRead(RX) == LOW) {
      delayMicroseconds(1000);
      //Verifica serial Code
      for (int i = 0; i < 18; i++) {
        if (manchesterEncoded[i] == 0) {
          periods[i] = pulseIn(RX, LOW);
          state[i]=0;
        } else {
          periods[i] = pulseIn(RX, HIGH);
          state[i]=1;
        }
      }

The problem is that when i sand the code 010101010101... I get random times (0.01ms to 10 ms) and not the ones I know are the actual periods (1ms).

The usual way is with a interrupt and millis().
Can you give values for the "on" and "off" time, how long are they ? If they are 0.5 and 2 ms then I doubt if it is possible.

Which Arduino board do you use ? Can you tell more about your project ?
There are libraries to measure a frequency and there are libraries to read a ESC signal.

1 Like

That is basically rise and fall time. Use an oscilloscope to make that measurement. It will have to have a bandwidth of 500MHz or greater.

if it is the period you require have a look at determination-of-wheel-acceleration-deceleration-for-use-in-integrated-stability-control-systems
it is for an ESP32 but the technique can be used on an UNO

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.