Pages: [1]   Go Down
Author Topic: [SOLVED] How to read a pulse  (Read 3354 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good afternoon,

I am having a rain sensor which will send a pulse every time it reaches a certain volume (tipping bucket).
I did create a serial link with my computer following the instructions found on: http://www.weather-above.com/Rain%20Tracker%20Rain%20Gage.html

Using CoolTerm as a serial port listener you can see the DSR light blink every time a pulse comes in and thus you can say that it works.
However now I want to read this pulse on my arduino and not anymore on the computer. If I place one wire from the DSR pin of the female serial connector to the arduino digital pin then I can retrieve the length of each pulse with PulseIn. However when I remove the connection to the computer the whole thing goes crazy printing lots of pulse times which are all wrong as I am not pouring water, and thus should not receive anything.

I am using a Arduino Ethernet with PoE as I would like to eventually send the pulse information via the network to my server and display this information on my website.

My code:
Code:
int pin = 2;
unsigned long duration;

void setup()
{
  pinMode(pin, INPUT);
  Serial.begin(9600);
  Serial.println("Started");
}

void loop()
{
  duration = pulseIn(pin, HIGH);
  Serial.println(duration, DEC);
}

So basically my question is: How do I connect this sensor so that I can count it's pulses.
« Last Edit: October 05, 2012, 04:26:39 am by Ruurd » Logged

Offline Offline
Sr. Member
****
Karma: 6
Posts: 429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One Wire?, any sensor needs at least two connections.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How do I connect this sensor so that I can count it's pulses.

Too many different ways: first, you need to decide if you want to polling or interrupt. Polling is easy but is very sensitive to loop timing. Interrupt frees up the mcu but is a little bit more difficult to program.

With interrupt, you can use an external interrupt, or a port change interrupt, or a timer interrupt.

Without you deciding which approach you would like to try, it is impossible to give you more details.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One Wire?, any sensor needs at least two connections.

Yes I managed to get the PulseIn function working with just one wire in the digital port smiley
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How do I connect this sensor so that I can count it's pulses.

Too many different ways: first, you need to decide if you want to polling or interrupt. Polling is easy but is very sensitive to loop timing. Interrupt frees up the mcu but is a little bit more difficult to program.

With interrupt, you can use an external interrupt, or a port change interrupt, or a timer interrupt.

Without you deciding which approach you would like to try, it is impossible to give you more details.


I would like to use a port change interupt.
For example:
Code:
attachInterrupt(0, some_function, RISING);

However for debugging purposes I was thinking about polling as there will be no other code executed at this time.
Logged

UK, Southwest
Offline Offline
Full Member
***
Karma: 5
Posts: 138
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post your entire circuit.

The statement that it works and "However when I remove the connection to the computer the whole thing goes crazy printing lots of pulse times which are all wrong as I am not pouring water, and thus should not receive anything." seem mutually exclusive.

One wire will not work; there is a sneaky earth coming in somewhere.

If your bucket tips quite slowly (say, as slow as 10ms which is 1/100 second), you have plenty of time to poll
for a change, double or triple check the signal is stable to allow for debounce, increment your counter, then continue to poll until the end of the pulse.  An arduino runs its main loop about 20000 times a second.  Polling allows debounce,
interrupts make that quite a bit harder.  Try a quick polling sketch to see.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would be fairly easy.

In your isr, you update a static variable that keeps count of the pulses. You can then raise a flag upon each update to signal to a function running in loop to send the updated count variable.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post your entire circuit.

The statement that it works and "However when I remove the connection to the computer the whole thing goes crazy printing lots of pulse times which are all wrong as I am not pouring water, and thus should not receive anything." seem mutually exclusive.

One wire will not work; there is a sneaky earth coming in somewhere.

If your bucket tips quite slowly (say, as slow as 10ms which is 1/100 second), you have plenty of time to poll
for a change, double or triple check the signal is stable to allow for debounce, increment your counter, then continue to poll until the end of the pulse.  An arduino runs its main loop about 20000 times a second.  Polling allows debounce,
interrupts make that quite a bit harder.  Try a quick polling sketch to see.

I created a quick scheme how I connected everything. I have a Serial2USB converter on the female serial connector that can be seen in the scheme. When the USB is connected to the computer the Arduino reads more or less the correct result. For each pulse I generate (by pouring water over the sensor) it displays 3 pulses via the serial port of the Arduino which I am listening to. So this might not be totally correct either. I read in the RG-11 documentation that each generated pulse stays HIGH for 50ms.

If I remove the USB from my computer then the Arduino registers hundreds of pulses as can be seen via the Serial listener. When I put a wire from the ground pin to the arduino ground pin then everything seems fine with the detail that it never will sense any pulses at all anymore ...
Also when I place the COM output into the Arduino GND pin and the NO output into the Arduino digital pin 2 then nothing will happen. Same behaviour as the last sentence above.

So somehow I am putting everything wrong together.

Logged

UK, Southwest
Offline Offline
Full Member
***
Karma: 5
Posts: 138
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The female 9 way d-type, which I understand is part of a USB-RS232 converter is NOT compatible with your arduino.  Stop connecting it.
The output is +10 to -10V, which might blow the Arduino input pins.

Try this:
COM to Arduino ground (0V).
NO to an Arduino digital input pin.  Any will do (but not the dedicated serial 0 or 1).

Set the Arduino input pin to INPUT and write the value to HIGH.  This will turn on the internal pull up.  So the value read
will be 1 when the bucket is not tipped, and 0 when the bucket tips.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The female 9 way d-type, which I understand is part of a USB-RS232 converter is NOT compatible with your arduino.  Stop connecting it.
The output is +10 to -10V, which might blow the Arduino input pins.

Try this:
COM to Arduino ground (0V).
NO to an Arduino digital input pin.  Any will do (but not the dedicated serial 0 or 1).

Set the Arduino input pin to INPUT and write the value to HIGH.  This will turn on the internal pull up.  So the value read
will be 1 when the bucket is not tipped, and 0 when the bucket tips.

Yep, that's the way to do it. The gauge is providing you with a 'dry contact' so direct connection to arduino ground and a digital input pin with the pin's internal pull-up enable will work fine. However if it's a long run of cable from the gage to the arduino I would be sure to use shielded cable. You might also have to do some software debouncing on the pin's readings.

Lefty
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your help guys, I managed to get it working.
Much appreciated!
Logged

Pages: [1]   Go Up
Jump to: