1

I have 3rd party device that create PWM output which I want to measure it with Arduino, the problem I have is when I shared ground between the two boards which cause the Pulsein output to become zero. (why this is happening, and how to fix it)

The reason I am connecting the ground since I need measure other signals on that board. (All other signals are responding fine)

schematic

simulate this circuit – Schematic created using CircuitLab

code:

#define pulse_ip 3
unsigned long duration;
int ontime,offtime,duty;
float freq,period;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(pulse_ip, INPUT);
}

void loop() { // put your main code here, to run repeatedly:

ontime = pulseIn(pulse_ip, HIGH); offtime = pulseIn(pulse_ip, LOW); period = ontime + offtime; freq = 1000000.0 / period; duty = (ontime / period) * 100; Serial.print(offtime); Serial.print("-"); Serial.print(ontime); Serial.print("-"); Serial.print(period); Serial.print("-"); Serial.print(freq); Serial.print("-"); Serial.print((duty * 255) / 100); Serial.print("-"); Serial.println(duty); delay(1000);

}

Python Schlange
  • 426
  • 1
  • 4
  • 18
Shahreza
  • 165
  • 9
  • what is your question? – jsotola Aug 02 '21 at 16:21
  • 1
    why I am getting zero when I share the ground – Shahreza Aug 02 '21 at 16:32
  • are you sure you don't get only random pulses without common ground? – Juraj Aug 02 '21 at 16:41
  • @Juraj that is good point maybe I am getting random, let me double check – Shahreza Aug 02 '21 at 16:44
  • @Juraj Yes I believe you are correct I am getting random numbers and as soon as I share ground become zero – Shahreza Aug 02 '21 at 16:50
  • does calling pulseIn twice make sense? check the reference https://www.arduino.cc/reference/en/language/functions/advanced-io/pulsein/ – Juraj Aug 02 '21 at 16:56
  • 1
    The reason I am connecting the ground since I need measure other signals on that board. -- no, the reason you connect the grounds is because you have to. https://majenko.co.uk/blog/our-blog-1/the-importance-of-sharing-grounds-12 – Majenko Aug 02 '21 at 17:55
  • @Majenko you are of course correct, that was reason I was getting random number – Shahreza Aug 02 '21 at 17:57
  • 1
    What (approximate) frequency is the PWM signal? And what voltage levels? – Majenko Aug 02 '21 at 18:02
  • @Majenko 3.3V is the voltage, 14.56k freq of pwm – Shahreza Aug 02 '21 at 18:21
  • you should provide a timeout argument so that it runs faster. even then, it's pushing the limits of hardware and CPU speed and will likely be fuzzy/noisy if it works at all. It would be better to use an RC filter to convert the PWM into an analog voltage, then divide that down to 0-5v, then sample the resulting signal. Since you only seem to need 1 sample a second, you can easily use a large capacitor and sample averaging to get a very smooth analog voltage. – dandavis Aug 02 '21 at 22:59

1 Answers1

1

Without connected grounds the signal on the input pin you read with pulseIn must be random noise.

Two pulseIn one waiting for HIGH change and second waiting for LOW don't make sense because for parameter HIGH, pulseIn waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing. Then it returns the length of the pulse in microseconds.

Juraj
  • 18,037
  • 4
  • 29
  • 49