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.
-
Looks like the voltage is too low to be read as a HIGHGerben– Gerben2017年01月07日 09:45:44 +00:00Commented Jan 7, 2017 at 9:45
-
@Gerben means pulseIn() not working for this ?Hasan– Hasan2017年01月07日 10:11:09 +00:00Commented Jan 7, 2017 at 10:11
-
If digitalread doesn't work on this signal then pulseln won't work either.Gerben– Gerben2017年01月07日 10:28:56 +00:00Commented 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– Majenko2017年01月07日 10:46:25 +00:00Commented Jan 7, 2017 at 10:46
1 Answer 1
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);
}
-
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– Hasan2017年01月07日 10:08:43 +00:00Commented 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– Hasan2017年01月07日 10:09:50 +00:00Commented Jan 7, 2017 at 10:09 -
1The timeout is global this means in these 10 seconds, the signal must first go HIGH then go LOW. If you call
pulseIn()
att0
, and the signal gets HIGH att1
, then gets back LOW att2
, then you must havet2 - t0 < timeout (10s)
, thenpulseIn()
will returnt2 - t1
; if evert2 - t0 > timeout
, then the pulse will not be recognized andpulseIn()
will return0
.jfpoilpret– jfpoilpret2017年01月07日 10:13:50 +00:00Commented 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– Kirby2018年06月08日 23:34:10 +00:00Commented Jun 8, 2018 at 23:34