2

Waveform I want to find the length of upper one signal (purple one). So for that, i used pulseIn() function for that. But i just get 0 as a result in serial monitor.

int pulsepin = 8;
int value = 0;

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

void loop()
{
  value = pulseIn(pulsepin, HIGH);
  Serial.println(value);
  delay(1000);
}

Here, above is my arduino code which i used. I don't know why I don't get any reading except 0.

4
  • Looks like the voltage is too low to be read as a HIGH
    – Gerben
    Jan 7, 2017 at 9:45
  • @Gerben means pulseIn() not working for this ?
    – Hasan
    Jan 7, 2017 at 10:11
  • If digitalread doesn't work on this signal then pulseln won't work either.
    – Gerben
    Jan 7, 2017 at 10:28
  • @Gerben Too low to be read as a HIGH, or too high to be read as a LOW...? Looks like it's a ~2V pulse with a ~3V offset. Hard to work out from that trace, it doesn't give much information.
    – Majenko
    Jan 7, 2017 at 10:46

1 Answer 1

5

pulseIn() returns an unsigned long (32 bits) not an int (16 bits), so you may have some truncation occurring in your program leading to a 0 value.

Hence you should modify your program as follows:

int pulsepin = 8;
unsigned long value = 0;

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

void loop()
{
  value = pulseIn(pulsepin, HIGH);
  Serial.println(value);
  delay(1000);
}

Also beware that pulseIn() has a timeout (it will not wait forever). This timeout is used for the whole completion of the pulse, i.e. first waiting for the signal to get HIGH, then waiting for it to get back LOW. If no pulse is detected and complete (LOW -> HIGH -> LOW) within this time, then pulseIn() will return 0.

Default timeout value is 1 second, but you can set any value (in microseconds) you want that can hold in an unsigned long:

e.g. if you need 10 seconds, then:

  value = pulseIn(pulsepin, HIGH, 10000000UL);

I am not sure why you use delay(1000) in your loop(), I would remove it as a pulse may occur during that time and it would not be seen by your program.

Here is a suggested update (I also used const where it made sense but that won't change program behavior):

const unsigned long PULSEIN_TIMEOUT = 10000000UL;
const int pulsepin = 8;
unsigned long value = 0;

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

void loop()
{
  value = pulseIn(pulsepin, HIGH, PULSEIN_TIMEOUT);
  Serial.println(value);
}

You may also want to avoid printing 0 values since they mean no pulse was detected:

void loop()
{
  value = pulseIn(pulsepin, HIGH, PULSEIN_TIMEOUT);
  if (value > 0)
    Serial.println(value);
}
4
  • Thank you for explanation. Here, i used delay(1000) because every time whenever readings are serially printed in serial monitor, it's very hard to identify the changes between those readings. So, delay(1000) used only for my convenient purpose, so i can see every readings after every 1 second,nothing else.
    – Hasan
    Jan 7, 2017 at 10:08
  • And as you say, value = pulseIn(pulsein, HIGH,10000000UL ) that mean pulseIn() function wait for 10 seconds until signal goes LOW or gets that 10 second long pulse. I can't understand. Can it possible to get 10 seconds or any seconds pulse means length of pulse is 10 seconds or any seconds length with help if this line of code.
    – Hasan
    Jan 7, 2017 at 10:09
  • 1
    The timeout is global this means in these 10 seconds, the signal must first go HIGH then go LOW. If you call pulseIn() at t0, and the signal gets HIGH at t1, then gets back LOW at t2, then you must have t2 - t0 < timeout (10s), then pulseIn() will return t2 - t1; if ever t2 - t0 > timeout, then the pulse will not be recognized and pulseIn() will return 0.
    – jfpoilpret
    Jan 7, 2017 at 10:13
  • It works strange anyway. It works for 10s and doesn't for 1s. It's always returning zero... pulseIn(buttonPin, HIGH, 1000000UL); - 1s.
    – Kirby
    Jun 8, 2018 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.