I've hocked up a futaba rx to pin 3 and I'm executing the code below. But I only get "Duration: 0". I've also connected a oscilloscope to pin 3 and I can see the signal from the rx. But pulseIn can't seem to read it? What am I doing wrong here?
void setup() {
Serial.begin(9600);
pinMode(3, INPUT);
}
long i = 0;
void loop() {
Serial.print("Duration: ");
i = pulseIn(3, LOW);
Serial.println(i);
}
You want to measure the duration of the high pulse, not the low – change LOW to HIGH in your code.
But you should get some kind of reading either way so do check that you have the receiver ground connected to arduino ground
Can you see on the oscilloscope if the pulse is more than 2½ volts?
Perhaps try to test the input using a switch (with a pull-up or pull-down resistor as per one of the many switch tutorials) to see if you can get a pulseIn reading that way.
You may want to try another pin to see if pin 3 is defective.
Its working perfekt with a servo ... hmm could the problem be that Im taking +5 from the arduino board to power the rx when Im trying to use the pulesIn? When I tried the servo now I use a esc with a buildin bec. Maybe the arduino +5 is to week to allow the rx to send a good signal?
Still no progress I tried a external BEC to the rx but still same result.
I also tried to attach a capacitor to the pin from the rx and doing a analog read instead of a pulsein, this works, so I assume the pin is ok.
But why wont pulseIn in work? Im running out of ideas ...
Ground is ok, and I can see the signal om my oscilloscope when I attatch it to same groung and same pin as the arduino. The arduino can read the pin with analogRead but not with pulseIn. ???
The level needs to be more than 2.5 volts for it to be read by digitalRead. AnalogRead can detect voltages as low as 5 millivolts but this is of no use for measuring pulse width.
Did you test pulseIn with a switch and resistor as suggested earlier?
Not sure if this will work but its worth a try – turn on the internal pull-up resistor on your input pin:
after setting pin 3 as input add: digitalWrite(3, HIGH);
Im using a oscilloscope and have a very clear picture of the signal. Its amplitude is 0.5 volts. And I need above 2.5 to read it with pulseIn, thats my problem . :-/
This code give '0' as print out, even when I try with a cable from GND to the pin, plugging it in and out as fast as I can, I also tried to change pin ...
Why did you change from using pin 3 to pin 0? Pin 0 is used by the serial hardware. Try it again using pin 4 if you want to test another pin.
If that doesn't work, replace the pulseIn command with code to light an LED and see if that works, the LED should turn off when you connect pin 4 to ground :
This is almost ebarising. I had connected the signal from the rx to the ANALOG IN pin 0 and 3 ... won't get digital signal there ... Now at digital pin 2 it works PERFECT.