oliver
Posts: 26
Joined: Sun Mar 30, 2014 4:53 pm

raspberry pi weather station multiple spins

Tue Jun 16, 2015 7:55 pm

Hi,
I have been making a raspberry pi weather station and I am trying to measure wind speed using an anemometer, however when I try to count the number of spins, it counts one spin multiple times. I am using a Maplin replacement anemometer which sends a pulse every time it is spun around half a turn. I was wondering if there is a way of avoiding it from counting a spin multiple times especially when it is going slowly. Also when it is stopped and the reed switches are closed it counts multiple times.
Here is my code,

Code: Select all

import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN)
i = 0

while True:
    input_value = gpio.input(17)
    if input_value == False:
        i += 1
        print i
        while input_value == False:
            input_value = gpio.input(17)
If anyone has any ideas on how I could solve this please comment below,

Thanks in advance
Olly

ghp
Posts: 1516
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: raspberry pi weather station multiple spins

Wed Jun 17, 2015 3:24 am

Hello,

if I am right, this anemometer just provides a reed switch. Did you provide a pullup resistor in your setup, as needed to connect buttons/switches/reeds ? There is a nice tutorial in https://www.cl.cam.ac.uk/projects/raspb ... _switches/
You should have connected one wire to ground, and the other to a gpio pin. Then the pullup (around 10k) goes from the gpio pin to 3.3V.
If you do not have resistors at hand, you can enable an internal pullup resistor for the raspberry processor.

Code: Select all

GPIO.setup(port_or_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
But although this internal pullup works, it is a good idea to have external pullup and a current limiting resistor too in order to protect the pi.

Your code looks ok. It is basically an 'edge detector'.

Next problem is bouncing of the contact. Real world contacts will not just close or open, but toggle open-close-open-close while beeing operated. This is in a short time, for rmilliseconds.
If you want to keep your hand crafted edge-detector, you could add something like 'when 5 consecutive inputs, taken at 1ms intervals, are low', then you have low level for shure. Similiar for high level. This is a nice exercise in coding.
The RPI.GPIO library has a feature for edge detect logic, which provides debouncing.
You could also place a 0.1uF capacitor along the switch, but then use an external pullup and a current limiting resistor in order to prevent damage to the pi.

Another faint possibility is that the magnet-reed combi in the anemometer is causing trouble. Magnetic field and mechanical alignment of the reed switch could cause multiple triggers, but this should be reproducible and not random.
And keep the reed away from external magnets like loudspeekers, solenoids (or earth...).

Hope this helps,
Gerhard

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: raspberry pi weather station multiple spins

Wed Jun 17, 2015 4:02 am

I'd concur with the contact bounce. And the pull-up.

Add a small delay in your loop and you can ignore the bounce period. 10 or 20 ms should do.

Code: Select all

import RPi.GPIO as gpio
import time
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN)
i = 0

while True:
    input_value = gpio.input(17)
    if input_value == False:
        i += 1
        print i
        while input_value == False:
            time.sleep(0.02)
            input_value = gpio.input(17)

    time.sleep(0.02)

oliver
Posts: 26
Joined: Sun Mar 30, 2014 4:53 pm

Re: raspberry pi weather station multiple spins

Wed Jun 17, 2015 6:25 am

Sorry that didn't seem to work when the anemometer was spinning fast because the 20ms delay would stop it from being counted.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: raspberry pi weather station multiple spins

Wed Jun 17, 2015 6:29 am

oliver wrote:Sorry that didn't seem to work when the anemometer was spinning fast because the 20ms delay would stop it from being counted.
"did stop it" or "would stop it"?

Ok. Try a 10ms delay. Or 5ms.

Or figure out the fastest wind speed you ever expect to see, and therefore calculate the shortest time between pulses. Set the debounce time a little shorter than that.

Return to “Python”