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