Introduction: How to Build Your Own Anemometer Using Reed Switches, Hall Effect Sensor and Some Scraps on Nodemcu - Part 2 - Software


This is the sequel to the first post "How to Build Your Own Anemometer Using Reed Switches, Hall Effect Sensor and Some Scraps on Nodemcu - Part 1 - Hardware" - where I show how to assemble the wind speed and direction measurement devices. Here we will exploit the measurement control software designed for use in a Nodemcu using the Arduino IDE.

Project Description

In the previous post, the devices armed and connected to the Nodemcu are able to measure the speed and direction of the wind. The control software was designed to read the rotation of the anemometer for a period of time, calculate the linear velocity, read the direction in which the vane is, show the results in the OLED, publish the results in ThingSpeak and sleep for 15 minutes until the next measurement.

Disclaimer: This anemometer should not be used for professional purposes. It is only for academic or home use.

Note: English is not my natural language. If you find grammatical errors that prevent you from understanding the project, please let me know to correct them. Thank you so much.

Step 1: Installing Arduino IDE, ESP8266 Boards and Libraries and Your ThingSpeak Account

Installing Arduino IDE and Nodemcu

If you have never installed the IDE the Arduino please read the tutorial in the link - How to install Arduino IDE - where you can find the complete instructions.

Next step, to install the Nodemcu board use this tutorial from the Magesh Jayakumar Instructables which is very complete. How to install Nodemcu no Arduino IDE

Installing Libraries

Next step you must install the libraries that the sketch uses. They are common and you can follow the steps shown below.

ThingSpeak Library -

ESP8266 Library -

Creating a ThingSpeak Account

To use the ThingSpeak ( you must create an account (it is still free for a certain number of interactions) where you can save the data measured in your anemometer and monitor the wind conditions in your home, even via cellphone. By using ThingSpeak, you can give the public access to your collected data to whoever is interested. That is a good advantage of the ThingSpeak. Enter the homepage and follow the steps to create your account.

Once the account is created, enter this tutorial - ThingSpeak Getting Started - to create your channels. It's pretty well explained. In summary, you must create a channel where the data will be stored. This channel has an ID and a Key API that should be referenced in the sketch each time you want to record data. ThingSpeak will store all the data in a bank and will show them every time you access your account, in the way you have configured.

Step 2: Exploring the Sketch


In the diagram, you can understand the sketch's fluxogram. When you wake up (link) the Nodemcu, it will connect to your Wi-Fi network, whose parameters you have configured and start counting 1 minute of time to perform the measurements.
First, it will count the anemometer rotations for 25 seconds, calculate the linear velocity and read the direction of the wind. The results are shown on the OLED. Do the same steps again and for this second reading, it will transmit to the ThingSpeak.

Then Nodemcu sleeps for 15 minutes to save the battery. As I am using a small solar panel it is imperative that I do so. If you are using a 5V source you can modify the program so that it does not sleep and keep measuring the data.

Structure of the programs

In the diagram, you can see the structure of the sketch.


It is the main program that loads the libraries, starts the variables, controls the attach interrupt, calls all the functions, calculates the wind speed, determines its direction and puts it to sleep.


Connect the WiFi and send the data to ThingSpeak.


The keys of your WiFi network and the identifiers of your account in ThingSpeak. This is where you will change your Keys IDs and APIs.


It contains all the variables of the program. This is where you can change the reading times or how long the nodemcu should sleep.


It contains the functions to combine the parameters and read the multiplexer as well as the function to read the rotations of the anemometer.


Show on-screen results of wind speed and direction.

Step 3: Explanations About ...

Attach Interrupt

The rotation of the anemometer is measured by the function attachInterrupt () (and detachInterrupt ()) in the GPIO 12 (pin D6) of the Nodemcu (It has interrupt feature on its D0-D8 pins).

Interrupts are events or conditions that cause the microcontroller to stop the execution of the task that it is performing, work in a different task temporarily and come back to the initial task.

You can read the detail of the function in the link for the tutorial of Arduino. See attachInterrupt().

Syntax: attachInterrupt(pin, callback function, interrupt type/mode);

pin = D6

callback function = rpm_anemometer - counts each pulse on a variable.

interrupt type/mode = RISING - interrupt when the pin goes from low to high.

At each pulse produced by the magneto in the Hall sensor, the pin goes from low to high and the counting function is activated and summed pulse in a variable, during the 25 seconds established. Once the time has expired, the counter is disconnected (detachInterrupt ()) and the routine calculates the speed while disconnected.

Calculating the Wind Speed

Once it has been determined how many rotations the anemometer gave in 25 seconds, we calculate the speed.

  • RADIO is the measurement from the center axis of the anemometer to the tip of the ping pong ball. You must have measured yours very well - (see that in the diagram that says 10 cm).
  • RPS (rotations per second) = rotations / 25 seconds
  • RPM (rotations per minute) = RPS * 60
  • OMEGA (angular velocity - radians per second) = 2 * PI * RPS
  • Linear_Velocity (meters per second) = OMEGA * RADIO
  • Linear_Velocity_kmh (Km per hour) = 3.6 * Linear_Velocity and this is what is going to be sent to ThingSpeak.

Read wind vane direction

To read the position of the wind vane to determine the direction of the wind the program sends low and high signals to the multiplexer with all the combinations of the parameters A, B, C (muxABC matrix) and wait to receive on pin A0 the result that can be any voltage between 0 and 3.3V. The combinations are shown in the diagram.

For example, when C = 0 (low), B = 0 (low), A = 0 (low) the multiplexer gives it the data of pin 0 and sends the signal to A0 that is read by the Nodemcu; if C = 0 (low), B = 0 (low), A = 1 (high) the multiplexer will send you the data of pin 1 and so on, until the reading of the 8 channels is completed.

As the signal is analog, the program transforms into digital (0 or 1), if the voltage is less than or equal to 1.3V the signal is 0; if it is greater than 1.3V the signal is 1. The value 1.3V is arbitrary and for me, it worked very well. There are always small leakages of current and this protects that there are no false positives.

This data is stored in a vector val[8] that will be compared with the address array as the compass. See the matrix in the diagram. For example, if the received vector is [0,0,1,0,0,0,0,0,0] it indicates in the matrix the direction E and corresponds to an angle of 90 degrees; if [0,0,0,0,0,0,1,1] indicates in the matrix the WNW address and corresponds to an angle of 292.5 degrees. The N corresponds to [1,0,0,0,0,0,0,0,0] and angle of 0 degrees.

What will be sent to the ThingSpeak is at the angle because it only accepts numbers.

Step 4: Communications

How to send data to ThingSpeak

The function thingspeaksenddata () is responsible for sending the data.

ThingSpeak.setField (1, float (linear_velocity_kmh)) - Send the velocity data to field1 of my channel

ThingSpeak.setField (2, float (wind_Direction_Angle)) - Send the address data to field2 of my channel

ThingSpeak.writeFields (myChannelNumber, myWriteAPIKey) - Send to my channel myChannelNumber, with the written myWriteAPIKey API indicated by TS. This data was generated by TS when creating your account and channel.

In the pictures above you can see how the ThingSpeak shows the received data.

In this link you can access the data of my project in the public channel of ThingSpeak.

Step 5: Main Variables

wind vane parameters

  • MUX_A D5 - mux pi A to Nodemcu pin D5
  • MUX_B D4 - mux pin B to Nodemcu pin D4
  • MUX_C D3 - mux pin C to Nodemcu pin D3
  • READPIN 0 - Analog input on NodeMcu = A0
  • NO_PINS 8 - number of mux pins
  • val[NO_PINS] - ports 0 to 7 of mux
  • wind_Direction_Angle - Angle of wind direction
  • String windRose[16] = {"N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"} - cardenals, collaterals and sub-collaterals
  • windAng[16] = {0,22.5,45,67.5,90,112.5,135,157.5,180,202.5,225,247.5,270,292.5, 315,337.5} - angles of each direction
  • Digit [16] [NO_PINS] - Directions Matrix
  • muxABC[8] [3] - ABC mux combinations

anemometer parameters

  • rpmcount - count how many full rotations did the anemometer in the allotted time
  • timemeasure = 25.00 - measurement durantion time in seconds
  • timetoSleep = 1 - Nodemcu awake time in minutes
  • sleepTime = 15 - time to keep sleeping in minutes
  • rpm, rps - rotation frequencies (rotations per minute, rotations per second)
  • radius - meters - the measure of the length of anemometer wing
  • linear_velocity - lineal velocity in m/seg
  • linear_velocity_kmh - lineal velocity in km/h
  • omega - radial velocity in rad/seg

Below you can find the complete sketch. Create a new folder on the Arduino folder of your computer with the same name as the main program (Anemometer_Instructables) and put them all together.

Enter the data of your wifi network and the ThingSpeak ID and API Writer Key in the part Credentials.h and save. Upload to Nodemcu and that's all.

To test the operation of the system I recommend a good rotating fan.

To access the data by mobile phone, download the application for IOS or Android called ThingView, which, fortunately, is still free.

Configure your account settings and you will be ready to see your home wind conditions wherever you are.

If you have an interest, access my ThingSpeak Channel ID channel: 438851, which is public and there you will find the wind and direction measurements in my house.

I really hope you have fun.

If you have any doubt don't hesitate to contact me.


Arduino Contest 2019

Participated in the
Arduino Contest 2019