xicarusx
Posts: 4
Joined: Thu Jul 18, 2013 2:24 am

Reed Switch Anemometor

Thu Jul 18, 2013 2:36 am

I am trying to wire a reed switch based anemometer to the GPIO. I have had no luck so far in being able to read anything from it. I was hoping to find help here.

I have tried a few different ways of doing this and having a python script count the pulses over a period of time. I have had no luck what so ever.

My Anemometor and Wind Direction hardware can be found here https://www.sparkfun.com/products/8942

Wiring the Anemometor - How do I go about this using the GPIO? I have tried a couple combinations with mixed results.

Python Code - How do I read the status of the circuit, open and closed over a period of time? I have tried a few different combinations and have failed miserably. I am almost certain my circuit isn't the problem.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reed Switch Anemometor

Thu Jul 18, 2013 7:14 am

How have you connected the device to the Pi? Which pin on the device to which pin on the Pi etc?

xicarusx
Posts: 4
Joined: Thu Jul 18, 2013 2:24 am

Re: Reed Switch Anemometor

Thu Jul 18, 2013 2:10 pm

I have the Anemometor plugged in to the wind vane. Then the wind vane rj11 in to a an RJ11 jack on an Adafruit breadboard.

The four leads from the RJ11 jack are as follows


Pin 4 -----------------------> unused, but is output for Windvane
Pin 3 --------(1k resistor)------3.3v pin on GPIO
Pin 2 ------------- GPIO 4 (I use BCM naming)
Pin 1 ---------------------> unused for now, but is ground for Windvane

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reed Switch Anemometor

Thu Jul 18, 2013 2:51 pm

So you are measuring the anemometer wind speed on gpio4 (pin P1-7) and expect a pulse per second per 1.492 mph of wind speed?

How long is the wire? Have you tried using 5V instead?

How are you checking gpio4?

xicarusx
Posts: 4
Joined: Thu Jul 18, 2013 2:24 am

Re: Reed Switch Anemometor

Thu Jul 18, 2013 3:03 pm

Exactly! The wire isn't long at all maybe 4 or 5 feet. It is working if I put a led on the gpio side between the switch and the GPIO pin. The LED will flash as the reed closes.

I guess that is my biggest question.

How would I go about coding that? something like countibg how many times GPIO.HIGH occurs in a period of time?

xicarusx
Posts: 4
Joined: Thu Jul 18, 2013 2:24 am

Re: Reed Switch Anemometor

Thu Jul 18, 2013 3:12 pm

I had also thought if I used 5.5v across my circuit I would break the Raspberry Pi. (fry the procesor, or so I am told.)

i tried reading input like this.


import RPi.GPIO as io
io.setmode(io.BCM)

io.setup(4, io.in)

input_value = io.input(4)

while True:
print "--" + str(input_value)

that gave me a long list of 0 and then a long list of 1 and never back to 0. Coding isn't my strong suit, but outting together the circuits is.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reed Switch Anemometor

Thu Jul 18, 2013 3:33 pm

You are quite right about the 5V being bad for the Pi's gpios.

You are on the right lines. By the way if you enclose the code within code quotes (button above text entry area) it preserves the spacing. Spacing is important in Python programs.

In your code you are only reading the gpio once.

Code: Select all

input_value = io.input(4).
You need to put that code in the while loop, e.g.

Code: Select all

while True:
        input_value = io.input(4)
        print "--" + str(input_value)

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Reed Switch Anemometor

Thu Jul 18, 2013 3:47 pm

xicarusx wrote:I have the Anemometor plugged in to the wind vane. Then the wind vane rj11 in to a an RJ11 jack on an Adafruit breadboard.

The four leads from the RJ11 jack are as follows


Pin 4 -----------------------> unused, but is output for Windvane
Pin 3 --------(1k resistor)------3.3v pin on GPIO
Pin 2 ------------- GPIO 4 (I use BCM naming)
Pin 1 ---------------------> unused for now, but is ground for Windvane
You need to either set pull down on the GPIO input or add a 10k resistor to 0V. If you do not then your input pin is either connected to 3.3V or floating (which will generate a lot of noise signals).
>)))'><'(((<

hargy001
Posts: 14
Joined: Sun Nov 08, 2015 9:12 pm

Re: Reed Switch Anemometor

Sun May 22, 2016 8:06 pm

Hi There

This is one that I made. You can modify it or use any part of it as you wish.
I used it with a PiTFT screen and a battery so that I can take it outside anywhere.

Here is the code. You may need to adjust the tabs and indents to get it working.

cheers # This programme is written for Raspberry Pi model B+ using Raspbian and the maplin anemometer #N24FR.
# It should work on other Pi's as well however I have only tested it on the older B+ and the Raspberry #PI 2.
# The anemometer has a reed switch and a magnet that switches twice per revolution.
# The RJ11 was removed from the cable and one of the wires is connected to 3.3v pin on the Pi and #the other
# wire connected to PIN 3.
# a 470 ohm resistor is connected between pin3 and GND on the PI
# to act as a pull down resistor to prevent stray voltages.
# (This value resistor just happened to be what I had, it snot critical).
# This appears to achieve good results however I have not tested it against any calibrated #anemometers
# so it can only be used as a rough guide
# During measurements a file will be written on the Desktop "windstats.txt" in which all the readings #will be written
# If the file does not exist it will be created.
# If the file exists the new data will be appended rather than overwritten.
# Please note that this programme only works properly from the terminal screen, not IDLE
# and the card needs to be write enabled

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN)
import datetime
import time
from time import sleep

#setup variables for triggers and units
trigger=0 #this is to stop multiple detection if the anemometer stops in the closed position
pulse=0 # this is the count that we want to know how many actual genuine pulses per revolution
pulsepersecond=0
meterspersec=0
kmh=0
mph=0
beaufort=0
knots=0

#a 1 second switch check loop

print "\033c" #this clears the screen
while True:
endTime = datetime.datetime.now() + datetime.timedelta(seconds=1) #this is the end of the 1 #second loop in seconds
while datetime.datetime.now() <= endTime:
if (GPIO.input(3) ==1):
if trigger == 0 and (GPIO.input(3) ==1):# this stops the switch being read more than once
pulse +=1
trigger=1
else:
trigger=0
pulsepersecond = pulse/2 # divided by 2 because the read switch open and closes twice per revolution
meterspersecond=2*3.142*0.09*pulsepersecond #this is 2 pi r, radius of anemometer arm is 90mm
kmh=meterspersecond*3.6
mph=kmh/1.609344
beaufort=(mph+6)/6
knots=mph*0.86897624190816
pulse=0
if meterspersecond >0: # this just stops the screen printing zeros all the time
print "\033c"
print ("\033[1;37;40m") # bright white colour
print datetime.datetime.now()
print ("\033[1;31;40m"),"____________________________________" #escape code is just to #print red
print ("\033[1;32;40m")," meters per second is ",round(meterspersecond,2),"m/sec"
print ("\033[1;36;40m")," Kilometers per Hour is ",round(kmh,2),"Kph"
print ("\033[1;37;40m")," Miles per Hour is ",round(mph,2),"mph"
print ("\033[1;35;40m")," Beaufort Scale reads ",round(beaufort,1)-0.5 #this seems about as #accurate as I can get with this
print ("\033[1;34;40m")," Knots are ",round(knots,2),"Knots"
print ("\033[1;31;40m"),"____________________________________"

todaysdatetime = datetime.datetime.now()
generatefile=open( '/home/pi/Desktop/windstats.txt', 'a+' ) #a+ means append rather than just #write
generatefile.write(repr(todaysdatetime) +'\n')
generatefile.write( 'meters persecond ' + repr(meterspersecond) + '\n')
generatefile.write( 'Kilometers per hour ' + repr(kmh) + '\n')
generatefile.write( 'Miles per Hour ' + repr(mph) + '\n')
generatefile.write( 'Beaufort Scale ' + repr(beaufort) + '\n')
generatefile.write( 'Knots ' + repr(knots) + '\n')
generatefile.write( "\n")
generatefile.close()

else:

print "\033c" #this clears the screen
print ("\033[1;37;40m") # bright white colour
print datetime.datetime.now()
print ("\033[1;32;40m")
print "Waiting for the wind to blow !!"
sleep (0.2)










Hope this helps
Last edited by hargy001 on Mon May 23, 2016 11:28 am, edited 1 time in total.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reed Switch Anemometor

Sun May 22, 2016 8:45 pm

@hargy001

Could you edit your post and put the code within code quotes? Top of editing screen.

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Reed Switch Anemometor

Sun May 22, 2016 10:20 pm

If you wire it to GPIO05 then there's some ready made code at: https://github.com/raspberrypi/weather-station

https://github.com/raspberrypi/weather- ... _daemon.py reads the anemometer.
https://github.com/raspberrypi/weather- ... rection.py reads the wind direction using channel 0 on an MCP342X ADC to convert the resistance value to a digital signal.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
scruss
Posts: 3212
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Reed Switch Anemometor

Mon May 23, 2016 1:14 am

The anemometer wind speed conversion in both the poster's code and the official weather station code doesn't seem quite right. It assumes that the anemometer cup will accelerate to exactly the incident wind speed and act as a perfect drag device (that is, have a Cd = 1.0). This will not be the case, as the opposite anemometer cup will provide additional resistance and there will also be bearing friction causing the anemometer to turn more slowly than the ‘perfect’ wind speed. I've spent many years working with anemometers, and even though hobbyist weather stations running on Raspberry Pis aren't going to be used for life-critical purposes, we might as well apply the right theory when we make the measurements. A Raspberry Pi has ample processing power to do this; we used to do this in realtime on dataloggers with a 1 MHz Hitachi 6303 8-bit processor (a 6800 clone).

For the official Weather Station, it might be worth the RPF getting a small batch of anemometers type calibrated. City University of London used to have a great and relatively inexpensive calibration service. That way, at least we'd know if the numbers were roughly close, and if the units had any repeatability.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Reed Switch Anemometor

Mon May 23, 2016 6:22 am

scruss wrote:
For the official Weather Station, it might be worth the RPF getting a small batch of anemometers type calibrated. City University of London used to have a great and relatively inexpensive calibration service. That way, at least we'd know if the numbers were roughly close, and if the units had any repeatability.
I'll send a private message to Clive or an email to weather@raspberrypi.org about this. Clive is trying to get the weather station sensors calibrated. The problem we've got at the moment isn't wind speed, wind direction or rainfall, it's that the three temperature sensors aren't consistent. The HTU21D, for example, reads 4.4°C warmer than the DS18B20.

The OP should also take a look at: viewtopic.php?f=112&t=142570 which is the thread for the home build weather watchers.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

hargy001
Posts: 14
Joined: Sun Nov 08, 2015 9:12 pm

Re: Reed Switch Anemometor

Mon May 23, 2016 11:32 am

joan wrote:@hargy001

Could you edit your post and put the code within code quotes? Top of editing screen.
Ok I think I have done that correctly. If not please feel free to edit as necessary. I just posted as maybe it might be of some use to people maybe just to cut/copy the parts that they may need or all of it rather than a fully downloadable programme

hargy001
Posts: 14
Joined: Sun Nov 08, 2015 9:12 pm

Re: Reed Switch Anemometor

Mon May 23, 2016 11:36 am

scruss wrote:The anemometer wind speed conversion in both the poster's code and the official weather station code doesn't seem quite right. It assumes that the anemometer cup will accelerate to exactly the incident wind speed and act as a perfect drag device (that is, have a Cd = 1.0). This will not be the case, as the opposite anemometer cup will provide additional resistance and there will also be bearing friction causing the anemometer to turn more slowly than the ‘perfect’ wind speed. I've spent many years working with anemometers, and even though hobbyist weather stations running on Raspberry Pis aren't going to be used for life-critical purposes, we might as well apply the right theory when we make the measurements. A Raspberry Pi has ample processing power to do this; we used to do this in realtime on dataloggers with a 1 MHz Hitachi 6303 8-bit processor (a 6800 clone).

For the official Weather Station, it might be worth the RPF getting a small batch of anemometers type calibrated. City University of London used to have a great and relatively inexpensive calibration service. That way, at least we'd know if the numbers were roughly close, and if the units had any repeatability.
This was stated in the explanation in the comments section of the python script. It is only a bit of fun for getting a rough guide on wind speed and was never intended to be an exact windspeed meter. I just wrote it as a guide and for teaching my daughter about science etc

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: Reed Switch Anemometor

Mon May 23, 2016 1:06 pm

Hi xicarusx,

You have to place input_value = io.input(4) inside the while loop.

If you are reading zero's means that you don't have a pullup resistor properly installed. Double check your diagram. An external pullup of pulldown resistor is not required because the rpi pins contain pullup/pulldown circuits. To use them you have to active the circuits in your statement
io.setup(4, io.IN, pull_up_down=io.PUD_UP)

Here is the corrected code:

Code: Select all

#!/usr/bin/python

# Anemometer magnetic contact
#
#             /
#            /
# GPIO4 ----o  o------- GND
#

import RPi.GPIO as io

io.setwarnings(False)
io.setmode(io.BCM)

io.setup(4, io.IN, pull_up_down=io.PUD_UP)


while True:
   input_value = io.input(4)
   print "-- " + str(input_value)
Success with your project!
Ferdinand

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: Reed Switch Anemometor

Mon May 23, 2016 8:04 pm

Oeps, very old post. :o
Success with your project!
Ferdinand

PetrSvetr
Posts: 20
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: Reed Switch Anemometor

Fri Jul 06, 2018 9:18 pm

I am struggling (kind of) with anemometer used on meteostations WH1080 WH1090 WS0101 WH2080 (and many others brands probably).
https://www.amazon.de/gp/product/B00GGM ... _qh_dp_hza

In the documentation they say:
The cup-type anemometer measures wind speed by closing a contact as a magnet moves past a switch. A wind speed of 1.492 MPH (2.4 km/h) causes the switch to close once per second.

I have made "calibration" using my car, GPS and anemometer and my findings are different.

Anybody here made similar calibration? What are your results?

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Reed Switch Anemometor

Fri Jul 06, 2018 9:42 pm

There's some code here: https://github.com/RaspberryPiFoundatio ... er-station that uses a speed of 2.36Km/h

The anemometer is read with https://github.com/RaspberryPiFoundatio ... _daemon.py

Code: Select all

class wind_speed_interrupt_watcher(interrupt_watcher):
    def __init__(self, radius_cm, sensorPin, bounceTime, calibration = 2.36):
        super(wind_speed_interrupt_watcher, self).__init__(sensorPin, bounceTime, peak_sample = 5, peak_monitor = True)
        
        circumference_cm = (2 * math.pi) * radius_cm
        self.circumference = circumference_cm / 100000.0 #circumference in km
        self.calibration = calibration
        self.last_time = time.time()
        
    def calculate_speed(self, interrupt_count, interval_seconds):
        rotations = interrupt_count / 2.0
        distance_per_second = (self.circumference * rotations) / interval_seconds
        speed_per_hour = distance_per_second * 3600
        return speed_per_hour * self.calibration
        
    def get_wind_speed(self):
        return self.calculate_speed(self.get_value(), time.time() - self.last_time)
        
    def get_wind_gust_speed(self):
        return self.calculate_speed(self.get_peak(), 5) #5 seconds
        
    def reset_timer(self):
        self.last_time = time.time()
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

pfletch101
Posts: 623
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Reed Switch Anemometor

Fri Jul 06, 2018 9:53 pm

PetrSvetr wrote:
Fri Jul 06, 2018 9:18 pm
I am struggling (kind of) with anemometer used on meteostations WH1080 WH1090 WS0101 WH2080 (and many others brands probably).
https://www.amazon.de/gp/product/B00GGM ... _qh_dp_hza

In the documentation they say:
The cup-type anemometer measures wind speed by closing a contact as a magnet moves past a switch. A wind speed of 1.492 MPH (2.4 km/h) causes the switch to close once per second.

I have made "calibration" using my car, GPS and anemometer and my findings are different.

Anybody here made similar calibration? What are your results?
Was/were your calibration run(s) made on an absolutely windless day?

Your quoting the specifications in switch closures and km/h and recording your results in revolutions per sec and m/sec doesn't make things easier (!), but your calibration numbers actually look about right to me. 2.4 kph is equal to 0.667 m/sec, and there are two switch closures per revolution, so 6.7 m/sec should give you about 5 rps (= 10 switch closure), which is pretty much what your graph shows. What am I missing?

User avatar
scruss
Posts: 3212
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Reed Switch Anemometor

Fri Jul 06, 2018 10:21 pm

Looks like your anemometer puts out twice as many pulses per revolution. When you manually turn it, do you feel/hear four clicks instead of two? Reed switches are pretty quiet, so you may need to use a continuity meter to detect closure.

Notes on your rig:
  1. maybe put the wind vane behind the anemometer. As is, the anemometer's seeing turbulence from the vane
  2. you really don't need to care about wind speeds much over 10 m/s. Cup anemometers are pretty linear at that speed, though I'd worry about the reed switch bouncing. Low wind speed is where calibration matters
  3. you may need to correct for uncertainty in your GPS measured speed, as each point could be ± 5 m
  4. are you correcting for ambient wind speed? Driving into the wind, your anemometer experiences higher wind speeds than with the wind behind you
  5. If you're feeling really advanced — and to be honest, I don't think reed-switch anemometers have the resolution for this — throw out measurements where your car is accelerating or decelerating. Cup anemometers can have considerable inertia, so they can read high as the wind dies down and under-read as the wind picks up. This hysteresis is not a huge problem with small plastic anemometers, but with the big old Casella ones were heavy metal.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

PetrSvetr
Posts: 20
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: Reed Switch Anemometor

Sat Jul 07, 2018 8:12 am

pfletch101 wrote:
Fri Jul 06, 2018 9:53 pm
Was/were your calibration run(s) made on an absolutely windless day?
Kind of. Wind was nearly not possible to observe (grass not moving). To suppress the wind influence, I was going there and back on the same straight road, you can see it in the chart provided.
Your quoting the specifications in switch closures and km/h and recording your results in revolutions per sec and m/sec doesn't make things easier (!),
The switch closure number (2.4 km/h causes the switch to close once per second) is the only document I have found about this anemometer.
GPS is returning knots speed, so I am recalculating to it m/s (as recommended by WMO).
Two small tables showing "official" and "measured" m/s versus r/s are in the spreadsheet, right from the graph.
It looks to me:
  • from documentation 1 r/s => 1.333 m/s
  • my calibration 1 r/s => 1.2376 m/s
but your calibration numbers actually look about right to me. 2.4 kph is equal to 0.667 m/sec, and there are two switch closures per revolution, so 6.7 m/sec should give you about 5 rps (= 10 switch closure), which is pretty much what your graph shows. What am I missing?
I was just worried if observed difference is correct/acceptable.

PetrSvetr
Posts: 20
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: Reed Switch Anemometor

Sat Jul 07, 2018 8:57 am

scruss wrote:
Fri Jul 06, 2018 10:21 pm
Looks like your anemometer puts out twice as many pulses per revolution. When you manually turn it, do you feel/hear four clicks instead of two? Reed switches are pretty quiet, so you may need to use a continuity meter to detect closure.
No, this is not a problem for sure. I am using EXCELLENT pigpio library to detect the reed switch pulses. Moreover I can observe GPIOs pulses using piscope.
Notes on your rig:
  1. maybe put the wind vane behind the anemometer. As is, the anemometer's seeing turbulence from the vane
I was thinking the same and I did it already, you can not see it on my photos, I have realized this later.
[*]are you correcting for ambient wind speed? Driving into the wind, your anemometer experiences higher wind speeds than with the wind behind you
I was riding my car there and back on the same straight road to suppress this. I think it is possible to observe it in the provided chart.
[*]If you're feeling really advanced — and to be honest, I don't think reed-switch anemometers have the resolution for this — throw out measurements where your car is accelerating or decelerating. Cup anemometers can have considerable inertia, so they can read high as the wind dies down and under-read as the wind picks up. This hysteresis is not a huge problem with small plastic anemometers, but with the big old Casella ones were heavy metal.
I understand your comment about resolution. All solutions I have found on the internet are using resolution 2 pulses per revolution for this kind of anemometer (2 magnets in the anemometer head) . Thanks to the pigpio library I have discovered it is possible to use 4 pulses per revolution (as the reed switch stay closed for some short period, while magnet is passing over). So I am detecting both, rising and falling edge. Now I think my observation is close to the manufacturer numbers (documentation 1 r/s => 1.333 m/s, my calibration 1 r/s => 1.2376 m/s) and the interpolation line in the chart looks reasonable to me.

I just wonder why the "official" raspberry software is calculating the wind speed using the radius/perimeter equation.

AFIK this approach does not consider the bearing rolling resistance and so on.

BTW I am calculating wind speed using 1s sampling pulses generated by hw PWM on Raspberry Pi, instead of measuring time difference between pulses as all other solutions, I have found, do. Do you think it is a good/reasonable approach?

pfletch101
Posts: 623
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Reed Switch Anemometor

Sat Jul 07, 2018 3:47 pm

PetrSvetr wrote:
Sat Jul 07, 2018 8:12 am
pfletch101 wrote:
Fri Jul 06, 2018 9:53 pm
but your calibration numbers actually look about right to me. 2.4 kph is equal to 0.667 m/sec, and there are two switch closures per revolution, so 6.7 m/sec should give you about 5 rps (= 10 switch closure), which is pretty much what your graph shows. What am I missing?
I was just worried if observed difference is correct/acceptable.
It represents about a 7% difference from the specs. These are not precision devices, and there are all sorts of real-world sources of error, so I would regard this as an entirely acceptable variance. If you want precise wind speed measurements, you have to spend much more money on (e.g.) ultrasonic transducers and much more effort on 'siting' the transducer to avoid outside interference with the flow of air over/round it.

Return to “Automation, sensing and robotics”