Pulse generation and read the time delay between sent pulse and received pulse

Hi.., I am new to the Arduino coding. I want to generate a one pulse when I press a button. After microseconds I will receive several pulses (distorted signal). I want to measure the time delay of each and every receiving pulses respect to the generated single pulse. Can anyone help me on this...?

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

LOL!

I can’t decide whether it’s a good thing or a bad thing that Phil Spector can’t work his magic on this mix…

Oh, nice middle 8…

a7

  1. What Arduino are you using?
  2. What range of times do you expect?
  3. What timing accuracy do you need?

'''
void setup() {
pinMode(2, OUTPUT);
pinMode(6,INPUT);
Serial.begin(9600);
}

// the loop function runs over and over again forever

void loop() {
long duration;
digitalWrite(2, HIGH); // turn on (HIGH is the voltage level)
delayMicroseconds(10); // pulse high for 10 us
digitalWrite(2, LOW); //
delay(5000); // wait for a 5 seconds

duration = pulseIn(6,HIGH);
Serial.print(duration);

}
'''

1.Arduino Uno R3 ( I can buy any if you suggest other than this).
2. I need microseconds range
3. Microseconds accuracy.
Thank you for your reply on this. It is highly appreciate if you can help me on this

Are you using an HC-SR04 ultrasonic range finder?
It only generates one pulse. Can't you use "pulseIn()" like everyone else?

That is not a range. What is the shortest length of pulse you need to measure? What is the longest length of pulse you need to measure? What is the maximum time between the start of the first pulse and the end of the last pulse?

Sorry, I didn't get what you mean

when I send one pulse, actually I will receive pulse train ( less than 20 pulses)

pulseIn() - Arduino Reference

An ESP32 has the PCNT, Pulse Counter (PCNT) - ESP32 - — ESP-IDF Programming Guide latest documentation (espressif.com), which would be ideal for this project.

It is not Ultrasonic one. It is a fiber related project

shortest length of pulse - 5 us
longest length of pulse - 20 us

what did you mean by the last pulse. is it about receiving pulse?

On an Arduino UNO your best bet on precision timing is the Input Capture feature of Timer1. Here is an example of how this feature can be used to measure the Echo pulse from an HC-SR04 ultrasonic rangefinder. The prescale is set to 8 so the timer counts in 1/2 microsecond intervals.

// Measure the HC-SR04 echo pulse on Arduino UNO Pin 8 (ICP1 pin) and calculate distance

const byte TriggerPin = 9;
const byte EchoPin = 8; // MUST be 8 (ICP1)

// Note: Since this uses Timer1, Pin 9 and Pin 10 can't be used for
// analogWrite().

// Speed of sound:
// 343 meters per second 
// == 0.002915452 seconds per meter
// == 2915.452 microseconds per meter
const float MicrosecondsPerCM = 29.15452;
const float MicrosecondsPerRoundTripCM = MicrosecondsPerCM * 2;  // ~58.3
const float HalfMicrosecondsPerRoundTripCM = MicrosecondsPerRoundTripCM * 2; // ~116.6

// TCCR1B values to start Timer 1, prescale = 8
// Input Capture Edge Select (1=Rising, 0=Falling)
const uint8_t TCCR1BCaptureRisingEdge  = _BV(CS11) |  _BV(ICES1);
const uint8_t TCCR1BCaptureFallingEdge = _BV(CS11);

uint16_t RisingEdgeTime = 0;
unsigned long TimeOfLastPing = 0;

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  Serial.println("Ready");

  pinMode(TriggerPin, OUTPUT);

  noInterrupts ();  // protected code
  // reset Timer 1
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;
  TIMSK1 = 0;

  TIFR1 = _BV(ICF1); // clear Input Capture Flag
  TCCR1B = TCCR1BCaptureRisingEdge;
  interrupts ();
}

void loop()
{
  // Is the Input Capture Register set?
  if (TIFR1 & _BV(ICF1))
  {
    uint16_t edgeTime = ICR1;
    TIFR1 = _BV(ICF1); // Clear the flag

    if (RisingEdgeTime)
    {
      // This must be the falling edge
      uint16_t elapsed = edgeTime - RisingEdgeTime;
      // Look for the next rising edge
      RisingEdgeTime = 0;
      TCCR1B = TCCR1BCaptureRisingEdge;

      // Now calculate distance:
      float cm = elapsed / HalfMicrosecondsPerRoundTripCM;
      Serial.println(cm, 3);
      tone(4, cm*10);
    }
    else
    {
      // This is the rising edge
      RisingEdgeTime = edgeTime;
      TCCR1B = TCCR1BCaptureFallingEdge;
    }
  }

  // Be sure to leave 20 to 30 milliseconds between pings so 
  // the sensor doesn't catch old echoes from the prevous ping.
  if (millis() - TimeOfLastPing >= 30)
  {
    TimeOfLastPing = millis();
    digitalWrite(TriggerPin, HIGH);
    digitalWrite(TriggerPin, LOW);
  }
}
1 Like

I will try this and let you know. Thank you again for the support

Thank you for your reply. I will search on this

I think this is not for Arduino Uno .

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