Wind Sensor weird values

Hello. This is my setup. An Arduino Mega 2560 pro and a NPN Pulse Output Anemometer Wind Sensor.

This is the video that I used to make everything work so far.

This is the code that I'm using:

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 1000;    // the debounce time; increase if the output flickers
int pinInterrupt = 2;
int Count = 0;
int Vant;
int VantMax = 0;

void setup() {
  pinMode(2,INPUT_PULLUP);
  attachInterrupt( digitalPinToInterrupt(pinInterrupt), CounterVant, FALLING);
}

void loop() {
if ((millis() - lastDebounceTime) > debounceDelay) {
    lastDebounceTime = millis();
    Vant=((Count * 8.75)/100)*3.6;
    Count = 0;

    Serial.println(Vant);
    if (Vant > VantMax) {
        VantMax = Vant;
        Serial.print("Max:");
        Serial.println(VantMax);
    }    
}
}

void CounterVant()
{
  if ( digitalRead(pinInterrupt) == LOW )
    Count++;
}

The problem is that Max speed is very high, like 100KM/H when the instant speed don't go over 10KM/H.
Furthermore, I'm also using a shielded cable.

Can anyone help me figure out what is going on?

Try making vant and vantmax unsigned long in case you are getting overflow.

Also, print count before you zero it.

const byte pinInterrupt = 2;
volatile uint16_t Count = 0;

void setup() {
  Serial.begin(115200);
  pinMode(2, INPUT_PULLUP);
  cli(); // Disable interrupts
  attachInterrupt( digitalPinToInterrupt(pinInterrupt), CounterVant, FALLING);
}


void loop() {
  static uint16_t Vant;
  static uint16_t VantMax = 0;
  Count = 0;
  sei(); // Enables interrupts
  delay (1000);
  cli(); // Disable interrupts
  Vant = Count * 0.315;

  sei(); // Enables interrupts
  if (Vant > VantMax) VantMax = Vant;
  Serial.print("Vant:");  Serial.print(Vant);
  Serial.print("\tMax:"); Serial.println(VantMax);
}

void CounterVant() {
  Count++;
}

@kolaha
That disables also sending text from the Serial.print(). Have you tried to compile it ?

@georgepa
A Serial.begin(115200); is missing in setup(), and Count should be 'volatile'.
I think that the calculation is okay. To get 100 km/h you need about 320 pulses. Where do those 320 pulses come from ? Do you have a oscilloscope to measure the signal ? Is the GND wire not connected ?

You have to measure somehow if that is a normal 0...5V signal with a few pulses, or just a lot of noise, or really 320 pulses.

If you have another Arduino board and let it output a frequency of 20 Hz and feed that to your Mega board, then I'm sure it will show normal values.

1 Like

thank you. repaired.

IF the shield is also a conductor for your project, it is not a shield.

2 Likes

Hi, @georgepa

Can you please post some images of your project?
So we can see your component layout.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Not a Fritzy picture but your own schematic of your project, reverse engineer your project.

Thanks... Tom... :grinning: :+1: :coffee: :australia:

Your debouncing needs to take place where you actually have a signal being received, not every pass through loop(). Put it in the interrupt code where the signal is actually being received.

Hi,
Did you look at the tutorial that goes with the Youtube video clip.

Have you tried their code without the LCD code?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

2 Likes

If the pulse generator is a Hall effect sensor with NPN open collector output, the signal should not bounce unless the pullup is too weak. Post a datasheet link or brand name and exact part number for your anemometer.

I was also confused by the "debounce", but that should be called the "interval". It is the interval of 1 second to collect the Count from the interrupt. Debounce has nothing to do with it.

Thank you very much for the code, but I have a little problem, this is part of a larger project and I can't add any delays, and also I'm not skilled enough to modify the code to not use the delays. Can you also help me here please?

This does not use delays and will work no matter how much additional code you add

unsigned long lastTime;
unsigned long currentTime;
unsigned long Interval = 2000; 
unsigned long Actual_Interval;
volatile int Count;
float Counts_per_second, Speed, Speed_Max  = 0;
int pinInterrupt = 2;

void setup() 
{
  pinMode(2, INPUT);
  attachInterrupt( digitalPinToInterrupt(pinInterrupt), CounterVant, FALLING);
  lastTime = millis();
  Count = 0;
}

void loop()
{
  currentTime = millis();
  Actual_Interval = currentTime - lastTime;
  if (Actual_Interval >= Interval)
  {
    Counts_per_second = float(Count) * (1000.0 / float(Actual_Interval));
    Speed = Counts_per_second * 0.0875; // meters/sec
    Speed = Speed * 3.281; // feet/sec

    Serial.println(Speed);
    if (Speed > Speed_Max)
    {
      Speed_Max = Speed;
      Serial.print("Max:");
      Serial.println(Speed_Max);
    }
    lastTime = millis();
    Count = 0;
  }
}

void CounterVant()
{
  Count++;
}

Thank you Jim for the code. I have tried your code as you wrote it, plus a Serial.begin(115200) and I'm getting this weird characters:

Screenshot 2023-12-12 172736

I have checked the serial monitor and the baud rate is the same as define in the code.

Yes, I did leave out the Serial.begin
Try a slower rate like 9600 but I don't think that is the problem

I have that code running on an UNO with the only change being the addition of the Serial.begin(115200) and it works OK.
I'm using IDE 1.8.13

In your ISR, this may be interfering with count, try:

void CounterVant()
{
      ++Count;
}

pinInterrupt has to go from HIGH to LOW to get here to start with.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.