Uno pulseIn problem

I am trying to read a pulse using pulsIn.

This is the code

// Arduino: UNO
  
  unsigned long pulseHi = 0, pulseLo = 0;
  #define sensorPin 6

void setup() {
 Serial.begin(9600);
  pinMode(sensorPin, INPUT); 
}

void loop() {
  pulseHi = pulseIn(sensorPin,HIGH);
  pulseLo = pulseIn(sensorPin,LOW);          
  Serial.print(pulseHi);Serial.print("__");Serial.println(pulseLo); 
}

This is the pulse to pin 6

This the output......

pulseIn

The signal is "shaped" by 74HC14.

I really dont understand why the measuring result is so wrong?
Help please

Is the scope showing the "shaped" or "unshaped" signal?

This is the pulse after 74HC14 - measured at pin 6

The shape doesn't look great. Have you calibrated your probes? (Maybe your scope has a square wave output to help with that).

The figures in the 66000 range look ok, but they are not consistent.

Can you zoom right in on the leading edge of one of the pulses? There might be multiple edges we can't see on the image above.

What is this signal? Something from an engine/motor?

I adjusted the probe - that changed the mesuarement.
and I zoomed in as you suggested.

This the zoomed in

The source of the signal is an IR sensor.

That picture shows some AC superimposed on both the 5 volt part of the signal and the 0 volt part of the trace. That noise, particularly on the ground must be eliminated. Are you using a breadboard, by any chance?

We are still at 1ms/div? Can you zoom any closer to that rising edge? We are looking for multiple edges that would not be visible at this level. They would only be 5-10us.

Paul_KD7HB
I am using a breadboard.
I will tomorrow add a low pass filter.
Would you think that cound solve the problem?

I would add additional ground wires that DO NOT go through the bread board connections. Yes, parallel with the ones you have right now. IF that improves, then remove the breadboard ground wires.

Perhaps the shaping of the signal is wrong. Are you showing us the signal at the Arduino pin?

@johnwasser see post #4

The signal is measured at pin 6.
And the problem is similar without the 74HC14. I implemented the 74HC 14 to be sure the signal was shaped and full 5V.

I have now changed the grounding wirering and that improved the signal but not much.

Then I added a 1kHz RC filter.
That solved the problem.

And I also removed the 74HC14 and the signal is fine and no problem.

So the solution is the 1 kHz filter.
I am measuring frequences of 31Hz (max) .

The signal from the IR sensor KY033LT must be "very dirty".

Thanks to you all for the suggestions.
If you have more ideas for improvements I am listening.

It might help other forum users with a similar problem, but less experience than yourself, if you were to show the "dirty" signal on the scope, and share a little schematic of the RC filter you used, including the component values.

I suspect the "dirty" signal is caused not so much by the sensor as what it is sensing. For example, if it is detecting the edge of some mark on a rotating shaft, the edge of the mark may not be perfectly sharp, and/or the surface may be rough, causing small variations in the received IR signal. These could cause very short pulses not visible on the scope images above, but which the pulseIn() function is able to measure.

It may be possible to fix this without the RC filter, by detecting the first edge of each transition and then using short delays (using delayMicroseconds() function) to ignore any other edges that arrive very quickly after. The micros() function could be used to measure the time between the first rising edges of each detection. This would have a similar effect to your RC filter.

What is the output now?

The measurement is stable using the lowpass filter.
The filter is 470ohm / 100nF.
I am right now working on a signal "cleaning".
If that is possible - the filter can be removed and a better technical solution.
I dont know if it will work.

Just to be on the same page, the sketch is not measuring every pulse.

Please refer to the picture above, which is the one you posted, with names on the transitions and pulses.

Suppose the loop() first starts executing during 0LOW

This is what hapens:

  1. pulseIn(sensorPin,HIGH) is called and it waits for next low-to-high transition to occur. 1LH is detected. It then starts counting microseconds elapsed.
  2. pulseIn waits for next high-to-low transition (1HL). We are now in 1LOW
  3. pulseHi receives the duration of 1HIGH
  4. pulseIn(sensorPin,LOW) is called and it now waits for next high-to-low transition. 1HL is water under the bridge, and pulseIn patiently waits for 2HL to happen and then it starts counting microseconds during 2LOW.
  5. pulseIn waits for next low-to-high transition to occur (3LH)
  6. pulseLo receives the duration of 2LOW. 1LOW and 2HIGH are not measured.
  7. Output (is sent to the Serial engine during 3HIGH.
  8. loop() restarts. Per the figure, 3HIGH lasts more than 6 milliseconds, so we should still be in 3HIGH.
  9. pulseHi receives the duration of 4HIGH
  10. pulseLo receives the duration of 5LOW

Etcetera

So in the monitor you see the durations of
1HIGH__2LOW
4HIGH__5LOW
and so forth.

To test this, I loaded your sketch in an Arduino Mega ("A") and the following sketch in a second Arduino Mega ("B"):

// pulse generator to test pulseIn()
// port 142401
//https://forum.arduino.cc/t/uno-pulsein-problem/965774

const int N = 15;

const int outputPin = 3;
const unsigned long tLow = 67; //milliseconds

void setup() {
  pinMode(outputPin, OUTPUT);
}

void loop() {
  for (int i = 0; i < N; i++) {
    digitalWrite(outputPin, HIGH);
    delayMicroseconds(1000*(i+1));
    digitalWrite(outputPin, LOW);
    delay(tLow);
  }
}

I had to use varying durations for the HIGH pulse because otherwise the output would not give any clues. In this case, the high pulse durations are a repeating sequence of 1000, 2000, thru, 15000 microseconds.

This is the output I get:


The first few lines occur during the reset and setup of Arduino "B".

You can see that it measures one HIGH pulse and it skips the next two. In this run it gets pulses "numbered" 1,4,7,10,13,1,4,7,10,... and misses 2,3,5,6,8,9,11,12,14,15,...

The same happens with the LOW pulses, but the output does not reveal this because all LOW pulses have the same duration (approx).

mancera1979
I am impressed by all the work you have done to help. Thank you very much.
I ahve to attend to my daytime business this week. This si the reason for not getting any furthe right now.
I will be back at the weekend.

I have now received the parts for what I hoped could be the solutionm and I have a solution.
The problem was:

  1. the pulse shape
  2. surface for the reflektion of the IR signal (noise)

I desided to use an optocoupler with schmitt trigger.
My choise is H11L2

The IR sensor I am using is HW-201.
I needed a booster for the H11L2 and used BC547.

The measurement is now stable.

I got the following by using this solution

  1. Pulse shaping
  2. Noise elimination
  3. Level shift (from 5V of the IR sensor to 3V3 of the ESP32 D1)