I have a weather station project with wind measurement to complete. I got two wind speed sensors for it, one of them is WH-SP-WS01 and the other is PCE-WS P from PCE instruments.
For the first one with two wires that are serial to Reed Swtich, I builted simple program with interrupts to check when state is RISING with added 10ms debbounce time:
- #include <Arduino.h>
- #define Pin GPIO_NUM_33
- volatile uint16_t speedCnt;
- uint32_t timer1;
- volatile uint16_t debbTimer;
- void IRAM_ATTR isr()
- {
- if (millis() - debbTimer > 10)
- {
- debbTimer = millis();
- speedCnt++;
- }
- // speedCnt++;
- }
- void setup()
- {
- Serial.begin(9600);
- pinMode(Pin, INPUT_PULLUP);
- attachInterrupt(Pin, isr, RISING);
- timer1 = millis();
- }
- void loop()
- {
- if (millis() - timer1 > 1000)
- {
- Serial.println(speedCnt);
- speedCnt = 0;
- timer1 = millis();
- }
- }
The problems is when I read pulses on first one, there are lot of them. I added RC filter to first one, and it goes even worse. Most of edges were round and long. So I moved to the second more "professional" instrument and changed code a little bit to read HIGH state only when previous was LOW on Pull_Down resistor.
- #include <Arduino.h>
- #define Pin GPIO_NUM_33
- volatile uint16_t speedCnt;
- volatile bool state;
- uint32_t timer1;
- volatile uint16_t debbTimer;
- void IRAM_ATTR isr()
- {
- if (millis() - debbTimer > 10 && state != digitalRead(Pin))
- {
- debbTimer = millis();
- state = digitalRead(Pin);
- speedCnt++;
- }
- }
- void setup()
- {
- Serial.begin(9600);
- pinMode(Pin, INPUT_PULLDOWN);
- attachInterrupt(Pin, isr, HIGH);
- timer1 = millis();
- state = digitalRead(Pin);
- }
- void loop()
- {
- if (millis() - timer1 > 1000)
- {
- Serial.println(speedCnt);
- speedCnt = 0;
- timer1 = millis();
- }
- }
According to documentation it has already filter and third wire to give pulses:
But problem still occure. I look on my small osciloscope to check how signals goes in 1 second and it look like this:
Rising edges are pretty sharp so I don't know why EPS printed me on console values 13-17 but when I see there are no 13 Rising edges in one second.
I added addictional filter according to documentantion...
it changes falling edges to more smoother ones, but it changes nothing. I tried to measue Pulse Hz with osciloscope and my hair dryer for repeatable result, but on osciloscope was 30 HZ on counter there was about 140-170. Even if osciloscope have constant value, values on ESP hesitated between 140-170.
I don't know how to fix that problem. I read about Schmitt triggers, that can solve problem of values of pulses but,
PCE-WS was pretty expensive( about 140euros) and I don't know how they can work on PLC but not on ESP? Did PLC have some other stuff that can works well?
Have anyone have any idea how to solve that problem? I don't rule out that my oscilloscope is bad, but most of time values can be high even if Fan is rotates very slow.
This is how I connected to LOLIN32 Lite. Test was also on esp32 doit devkit v1 with the same result. DC connector was for power supply and White wire was to Yellow on Anemometer. Capacitor are 100nF and resistor is 1k
Regards,
Lucas