MicroPython: ESP32/ESP8266 with Anemometer (Wind Sensor)

In this guide, you’ll learn how to interface an anemometer to measure wind speed with the ESP32 and ESP8266 NodeMCU boards programmed with MicroPython. We’ll cover how to power and connect the sensor to the ESP32 and ESP8266 boards, and write a simple code to get wind speed values in different units.

MicroPython ESP32 ESP8266 NodeMCU with Anemometer Wind Sensor

Table of Contents:

Throughout this guide, we’ll cover the following topics:

New to MicroPython? Check out our eBook: MicroPython Programming with ESP32 and ESP8266 eBook (2nd Edition)

Prerequisites

To follow this tutorial you need MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:

Learn more about MicroPython: MicroPython Programming with ESP32 and ESP8266

Introducing the Anemometer

An anemometer is a device that allows us to measure wind speed. It is commonly used in weather stations.

Using this sensor is quite easy. It outputs an analog signal, whose voltage is proportional to the wind speed. We’re using an anemometer with three cups like the one in the picture below.

Wind Sensor Anemometer

Anemometer Technical Details

Depending on the manufacturer, the anemometer may have different characteristics. For example, these are the characteristics of the anemometer used in this guide:

  • Input voltage: 12-24V DC
  • Output voltage: 0-5V
  • Measurement range: 0-32.4m/s
  • Resolution: +/- 0.3m/s

This means that when the analog signal is 0, the wind speed is 0. However, in my case, after powering the sensor and applying a voltage regular, I noticed that when the anemometer was not moving, the output voltage was 0,033V and not 0V.

ESP32 Measure Voltage from Wind Sensor with no wind

So, I consider this to be the lowest value measured when the sensor is not moving. I recommend you do the same and figure out the minimum value read from the sensor using a multimeter.

These details might be different depending on the manufacturer. So, you need to take that into account when converting the analog signal to wind speed.

Anemometer Pinout

The anemometer comes with three wires:

Blue WireSignal
Black WireGND
Brown WirePower
Anemometer, Wind Sensor Pinout

Connecting the Anemometer to the ESP32 and ESP8266

The anemometer requires an input voltage of at least 12V. So, you can’t power it directly from the ESP32 or ESP8266, you need an external power source.

12VDC Power Adapter

We’re powering the sensor using a 12V power adapter and connecting it to the anemometer using a power jack. You can use any other suitable power source.

DC power jack adapter

Converting the Data Signal from 5V to 3.3V

In the case of my sensor, it operates in the range of 0 to 5V. However, the ESP32 analog pins and the ESP8266 analog pins can only read a maximum of 3.3V. So, we need to convert the 5V signal to a 3.3V signal. To do that, we can use a voltage divider.

Note: if you’re using an anemometer like the one from Adafruit, you don’t need to worry about this because the maximum output voltage is 2V.

A voltage divider is a simple circuit that reduces a large voltage into a smaller one. Using 2 resistors and an input voltage, we can create an output voltage that is a fraction of the input. Below you can see the formula that you need to use to calculate the resistors that you need in your circuit:

voltage divider formula

If we use a 1k Ohm (R1) and a 2k Ohm (R2) resistor, we’ll get a maximum output of 3.3V, which is the maximum that the ESP32 can read.

So, here’s what the voltage divider circuit looks like (in which 5V is the maximum value of the sensor data pin):

voltage divider 5V to 3.3V

You can use any other combination of resistors, but you need to take into account the maximum output voltage allowed by the combination of resistors used.

Learn more here: How to Level Shift 5V to 3.3V

Wiring the Circuit: ESP32 and ESP8266 with Anemometer

ESP32 with Anemometer Circuit

Here’s a list of the parts you need for this tutorial:

You can use the following diagrams as a reference to wire the sensor to the boards. Don’t forget to connect the GND pins together.

ESP32

For the ESP32, we’re using GPIO34 to read the analog signal from the sensor, but you can use any other suitable GPIO for analog reading.

ESP32 with Wind Sensor Anemometer Circuit Diagram

ESP8266

The ESP8266 only has one analog pin, the A0 pin. So, you must use that pin to read the analog signal from the sensor.

ESP8266 with Wind Sensor Anemometer Circuit Diagram

If you’re using an anemometer like the one from Adafruit that outputs a maximum of 2V, you can connect the output pin directly to the ESP analog pin (you don’t need the voltage divider).

Black WireGND of the ESP board and GND of the power source
Blue WireConnect to ESP32/ESP8266 analog pin (via voltage divider, if needed), we’re using GPIO 34 for the ESP32 and A0 for the ESP8266.
Brown Wire12V power source (+)

ESP32 with the Anemometer – Measure Wind Speed MicroPython Code

The following code reads the analog signal from the anemometer and converts it into wind speed.

You can upload the following code to your ESP32. You may need to modify some of the variables depending on the parameters of your anemometer.

# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/micropython-esp32-esp8266-anemometer-wind-sensor/
 
from machine import ADC, Pin
from time import sleep

# Constants
# GPIO pin connected to anemometer (ESP32)
anemometer_pin = 34
min_voltage = 0.033  # Voltage corresponding to 0 m/s
max_voltage = 3.3    # Voltage corresponding to 32.4 m/s (max speed when using voltage divider)
max_wind_speed = 32.4 # Maximum wind speed in m/s

# Conversion factors
mps_to_kmh = 3.6     # 1 m/s = 3.6 km/h
mps_to_mph = 2.23694 # 1 m/s = 2.23694 mph

# Set up the ADC
adc = ADC(Pin(anemometer_pin))
adc.atten(ADC.ATTN_11DB)  # Configure ADC to read full 3.3V range (11dB attenuation)
adc.width(ADC.WIDTH_12BIT)  # Set resolution to 12 bits (0-4095)

while True:
    # Read analog value from anemometer (ADC value between 0-4095)
    adc_value = adc.read()
    
    # Convert ADC value to voltage
    voltage = (adc_value / 4095.0) * 3.3
    
    # Ensure the voltage is within the anemometer operating range
    if voltage < min_voltage:
        voltage = min_voltage
    elif voltage > max_voltage:
        voltage = max_voltage
    
    # Map the voltage to wind speed
    wind_speed_mps = ((voltage - min_voltage) / (max_voltage - min_voltage)) * max_wind_speed

    # Convert wind speed to km/h and mph
    wind_speed_kmh = wind_speed_mps * mps_to_kmh
    wind_speed_mph = wind_speed_mps * mps_to_mph

    # Print wind speed
    print("Wind Speed:")
    print("{:.2f} m/s".format(wind_speed_mps))
    print("{:.2f} km/h".format(wind_speed_kmh))
    print("{:.2f} mph".format(wind_speed_mph))
    print() 
    
    sleep(1)

View raw code

The code for the ESP8266 is slightly different—continue scrolling for the ESP8266 MicroPython Code.

ESP8266 NodeMCU with the Anemometer – Measure Wind Speed MicroPython Code

The following code does the exact same thing as the previous one, but it was adapted for ESP8266 NodeMCU boards.

# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/micropython-esp32-esp8266-anemometer-wind-sensor/
 
from machine import ADC, Pin
from time import sleep

# Constants
min_voltage = 0.033  # Voltage corresponding to 0 m/s
max_voltage = 3.3    # Voltage corresponding to 32.4 m/s (max speed when using voltage divider)
max_wind_speed = 32.4 # Maximum wind speed in m/s

# Conversion factors
mps_to_kmh = 3.6     # 1 m/s = 3.6 km/h
mps_to_mph = 2.23694 # 1 m/s = 2.23694 mph

# Set up the ADC
adc = ADC(0)

while True:
    # Read analog value from anemometer (ADC value between 0-1023)
    adc_value = adc.read()
    
    # Convert ADC value to voltage
    voltage = (adc_value / 1023.0) * 3.3
    
    # Ensure the voltage is within the anemometer operating range
    if voltage < min_voltage:
        voltage = min_voltage
    elif voltage > max_voltage:
        voltage = max_voltage
    
    # Map the voltage to wind speed
    wind_speed_mps = ((voltage - min_voltage) / (max_voltage - min_voltage)) * max_wind_speed

    # Convert wind speed to km/h and mph
    wind_speed_kmh = wind_speed_mps * mps_to_kmh
    wind_speed_mph = wind_speed_mps * mps_to_mph

    # Print wind speed
    print("Wind Speed:")
    print("{:.2f} m/s".format(wind_speed_mps))
    print("{:.2f} km/h".format(wind_speed_kmh))
    print("{:.2f} mph".format(wind_speed_mph))
    print() 
    
    sleep(1)

View raw code

How Does the Code Work?

We start importing the required modules. The ADC and Pin classes from the machine module to set the GPIOs as ADC pins.

from machine import ADC, Pin
from time import sleep

Define the pin where you’re reading the sensor, the minimum and the maximum output voltage of the sensor, and the maximum wind speed. In case of the ESP32, we’re using GPIO 34 to read the analog signal from the wind sensor.

# GPIO pin connected to anemometer (ESP32)
anemometer_pin = 34
min_voltage = 0.033  # Voltage corresponding to 0 m/s
max_voltage = 3.3    # Voltage corresponding to 32.4 m/s (max speed when using voltage divider)
max_wind_speed = 32.4 # Maximum wind speed in m/s

In the case of the ESP8266, the only available analog pin is A0. So, we need to pass it to the ADC() object later on like this:

adc = ADC(0)

Then, we have the conversion factors to convert the wind speed from m/s to km/h and mph.

# Conversion factors
mps_to_kmh = 3.6     # 1 m/s = 3.6 km/h
mps_to_mph = 2.23694 # 1 m/s = 2.23694 mph

Create an ADC object on the GPIO you’re using to read the analog sensor. It’s like this for the ESP32:

adc = ADC(Pin(anemometer_pin))

And like this for the ESP8266:

adc = ADC(0)

When using the ESP32. we also need to pass the following two lines.

adc.atten(ADC.ATTN_11DB)  # Configure ADC to read full 3.3V range (11dB attenuation)
adc.width(ADC.WIDTH_12BIT)  # Set resolution to 12 bits (0-4095)

The first line means that we want to read voltage from 0 to 3.3V. This corresponds to setting the attenuation ratio of 11db.

The second line of code sets the resolution to 12-bits. So, we’ll read a number between 0 to 4095 on the analog pin.

Finally, we have a while loop that gets new wind speed values every second.

First, we read the value on the ADC pin and convert it to a voltage value. The maximum value read on the ESP32 ADC pin is 4095 that corresponds to 3.3V.

while True:
    # Read analog value from anemometer (ADC value between 0-4095)
    adc_value = adc.read()

So, we can convert the value to a voltage using the following line:

# Convert ADC value to voltage
voltage = (adc_value / 4095.0) * 3.3

Then, we have the following condition to check if the values read are within the defined range.

# Ensure the voltage is within the anemometer operating range
if voltage < min_voltage:
    voltage = min_voltage
elif voltage > max_voltage:
    voltage = max_voltage

Next, we can easily map the obtained voltage to a wind speed value as follows:

# Map the voltage to wind speed
wind_speed_mps = ((voltage - min_voltage) / (max_voltage - min_voltage)) * max_wind_speed

Then, we convert the values obtained to km/h and mph.

 # Convert wind speed to km/h and mph
 wind_speed_kmh = wind_speed_mps * mps_to_kmh
 wind_speed_mph = wind_speed_mps * mps_to_mph

Finally, we print the obtained results.

# Print wind speed
print("Wind Speed:")
print("{:.2f} m/s".format(wind_speed_mps))
print("{:.2f} km/h".format(wind_speed_kmh))
print("{:.2f} mph".format(wind_speed_mph))
print() 

Demonstration

Run or upload the code to your board using Thonny IDE.

Thonny IDE Run Code

On the MicroPython Shell, you should get wind speed data.

ESP32-ESP8266-Measure-Wind-Speed-MucriPython-Demonstration

You may need to do further sensor calibration to get more accurate results.

Wrapping Up

In this tutorial, you learned how to interface an anemometer with the ESP32 and ESP8266 boards programmed with MicroPython to get data about wind speed.

We hope you found this quick guide useful. We have guides for other sensors and modules that you may find useful:

To learn more about MicroPython, check out our resources:



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

1 thought on “MicroPython: ESP32/ESP8266 with Anemometer (Wind Sensor)”

  1. Sarah,

    Although the wind speed sensor is nice, using the highly nonlinear ADC on the ESP32 or ESP8266 is not a good idea. I notice you did not show any calibration data, which will be very inaccurate due to the ADC hysteresis and nonlinearity.

    May I suggest that you find a pulse output anemometer and use pulse counting, which eliminates the ADC nonlinearity. However, you really need more than one pulse per revolution in order to have accuracy, and something like a grey code rotation sensor. (so you can have near real time response, and not have to count say 100 rotations for 1% accuracy, which would limit the response time).

    The other option is to do the same thing but use an Arduino. The old R3 10 bit ADC is much more accurate/linear than the ESP devices, but you could try the R4 WiFi, 14 bit accuracy. The problem here is that the R4 appears to have a lot of self noise on the ADC, so probably the old faithful R3, 10 bit Arduino would be good, if you scale the output voltage properly to fill up the entire 0-5 Volt range of the Arduino ADC.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.