Fan with Arduino

How to turn led on and count fan rotations with blynk?
If I rotate fan manually I want blynk app to record fan rotations.

There are some examples of how to do this if you search the forum for “RPM”.

This isn’t a Blynk question… you do it by Googling for proper code and circuitry (what I see here is just the potential to kill your device with inductive feedback :anguished:)… and then once you have that working, you can use Blynk to display the result.

I solved the problem. How can I send the variable number to virtual pin V0.

void switchstate(){
number=7;}

void loop()
{
Blynk.run();
switchstate();
}

http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynkvirtualwritevpin-value

Blynk.virtualWrite(V0, number);

Is this correct?
number is variable

As log as you have declared it as a numerical variable (most common is integer) then yes, that will work.

https://www.arduino.cc/en/Tutorial/Variables

int led = 7;
int reelSwitch = 6;
int switchState;
int number = 0;
void setup() 
{
  pinMode (led, OUTPUT);
  pinMode (reelSwitch, INPUT);
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}
void switchstate(){
  switchState = digitalRead(reelSwitch);
  if (switchState == LOW)
  {
    digitalWrite(led, LOW);
  }
  else 
  {
    digitalWrite(led, HIGH);
    number=number++;
  }
    delay(100);   
}
BLYNK_READ(V0)
{
  Blynk.virtualWrite(V0,number);
}
void loop()
{
Blynk.run();
switchstate();
}

This is whole program (except wifi, pass and auth part). Blynk part doesn’t work.
I want to display “number” in Blynk app. Variable “number” is counter for sensor activation.

I’m struggling to work out what you’re hoping to achieve in the long run with this project, and what the extra bit of circuitry (a resistor and transistor?) is actually doing.
You’ve said that you want to count the fan revs when you rotate it by hand, but is this your ultimate goal, or are you hoping to measure wind speed or airflow?

The process you’re using, of calling the switchstate function from void loop is never going to give consistent results with Blynk. If your fan creates one or more voltage spikes for each revolution then the best solution is probably to use an interrupt which is triggered by each of the spikes and to measure how many of these occur in a set period of time, such as one second.
Whether this is scalable to cope with high revolution speeds will depend on what your final intentions for the project.

If you take a look at code designed for use with an anemometer for a weather station then you’ll see how interrupt counting can work.

Pete.

I wanted to count fan revs when I rotate it by fan or with something else. But because Arduino can’t process negative voltages I disconnected fan. I want to measure up to 3 fan rotations per second. It’s not for wind speed.

Fan is disconnected. It has one peace of magnet glued to one fan blade. I connected Arduino with a magnetic sensor and now Arduino knows when the fan does one rotation. It works.

I only want to show number of rotations in Blynk. My code doesn’t work. If I select Value Display and V0 pin it doesn’t show number of fan rotations. I don’t know if this part is correct

number=number++;
BLYNK_READ(V0)
{
Blynk.virtualWrite(V0,number);
}

The principal of what you’re doing is wrong. You’re looking at the pin that has the fan sensor connected to it each time the code goes around the void loop cycle. If you happen to be looking at it when the magnet is aligned with the sensor then your counter will be incremented, if the fan is rotating slowly, or the fan stops with the magnet next to the sensor then you’ll get multiple counts, or one count per cycle of the loop (this could be hundreds per second).

The way to do it is to use an interrupt on the pin that your sensor is connected to. When the sensor contacts are made (or broken, depending on your wiring and how you define the interrupt) then you will get one interrupt per revolution. When the ESP detects the interrupt it processes the function you named in your interrupt specification, which will be whet you increment your counter.

As I said before, search for weather station anemometer code. The anemometer works in exactly the same way, with a magnet and a reed switch.

Pete.

1 Like
  • fan is not moving slowly (it rotates about 1,2,3 rotations per second)
    HIGH/LOW positions are shown correctly in Blynk (D6 pin)
  • magnet can’t stop next to the sensor (https://i.imgur.com/D5cr6WB.png)
    (this is when force isn’t applied). Fan stops with blade that has magnet at the bottom.

You are saying that another function with Blynk.run() in void loop() isn’t alowed. Did I assumed that correctly?
Because when I tested it with

Serial.print(number);

it count’s correctly.

Not advised… mainly since that loop runs thousands of times a second… and having other functions in there can potentially slow that to a crawl… which causes timing issues for Blynk… leading to disconnections.

{
digitalWrite(led, HIGH);
number=number+1;
Blynk.virtualWrite(V0,number);
}

I tested with only this change and everything works (one day testing). Problem is solved.

I am interested how can we have only one function Blynk.run() but also another function that needs forever loop. Can you point me in right direction.

Not quite following your question… but I think what you are missing is the understanding of timers…

Aside from Blynk.run() and timer.run() rarely does any other function or function call need to sit in the void loop().

An interval timer can call a function every X milliseconds, looping forever as needed, without causing any delays in the blynk library.

Interval timer is like scheduler.

Thanks for the help (project is finished). I changed current program to have only two recommended functions.

This helps with my future projects.