{"id":163102,"date":"2024-10-29T18:21:32","date_gmt":"2024-10-29T18:21:32","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=163102"},"modified":"2025-03-24T11:45:28","modified_gmt":"2025-03-24T11:45:28","slug":"esp32-anemometer-wind-speed-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-anemometer-wind-speed-arduino\/","title":{"rendered":"ESP32 with an Anemometer: Measure Wind Speed (Arduino IDE)"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to interface an anemometer with the ESP32 to measure wind speed. We&#8217;ll cover how to power and connect the sensor to the ESP32 board, and write a simple code to get wind speed values in different units.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-with-Anemometer-Measure-Wind-Speed.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with an Anemometer: Measure Wind Speed (Arduino IDE)\" class=\"wp-image-163180\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-with-Anemometer-Measure-Wind-Speed.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-with-Anemometer-Measure-Wind-Speed.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-with-Anemometer-Measure-Wind-Speed.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-with-Anemometer-Measure-Wind-Speed.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-with-Anemometer-Measure-Wind-Speed.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>We have a similar guide for the&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-anemometer-wind-speed-arduino\/\">ESP8266 board with an Anemometer: Measure Wind Speed (Arduino IDE)<\/a><\/p>\n\n\n\n<p><strong>Table of Contents:<\/strong><\/p>\n\n\n\n<p>Throughout this guide, we&#8217;ll cover the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#anemometer-intro\" title=\"\">Introducing the Anemometer<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#anemometer-details\" title=\"\">Anemometer Technical Details<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><a href=\"#anemometer-pinout\" title=\"\">Anemometer Pinout<\/a><\/li>\n\n\n\n<li><a href=\"#anemometer-wiring-esp32\" title=\"\">Connecting the Anemometer to the ESP32<\/a><\/li>\n\n\n\n<li><a href=\"#esp32-anemometer-arduino-code\" title=\"\">ESP32 with the Anemometer &#8211; Measure Wind Speed Code<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"anemometer-intro\">Introducing the Anemometer<\/h2>\n\n\n\n<p>An anemometer is a device that allows us to measure wind speed. It is commonly used in weather stations.<\/p>\n\n\n\n<p>Using this sensor is quite easy. It outputs an analog signal, whose voltage is proportional to the wind speed. We&#8217;re using an anemometer with three cups like the one in the picture below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Wind-Sensor-Anemometer.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Wind Sensor Anemometer\" class=\"wp-image-163185\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Wind-Sensor-Anemometer.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Wind-Sensor-Anemometer.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"anemometer-details\">Anemometer Technical Details<\/h3>\n\n\n\n<p>Depending on the manufacturer, the anemometer may have different characteristics. For example, these are the characteristics of the anemometer used in this guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Input voltage: 12-24V DC<\/li>\n\n\n\n<li>Output voltage: 0-5V<\/li>\n\n\n\n<li>Measurement range: 0-32.4m\/s<\/li>\n\n\n\n<li>Resolution: +\/- 0.3m\/s<\/li>\n<\/ul>\n\n\n\n<p>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 <a href=\"#voltage-regulator\" title=\"\">voltage divider<\/a>, I noticed that when the anemometer was not moving, the output voltage was 0,033V and not 0V.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Measure-Voltage-Wind-Sensor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Measure Voltage from Wind Sensor with no wind\" class=\"wp-image-163183\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Measure-Voltage-Wind-Sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Measure-Voltage-Wind-Sensor.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"anemometer-pinout\">Anemometer Pinout<\/h2>\n\n\n\n<p>The anemometer comes with three wires:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><span class=\"rnthl rntcblue\">Blue Wire<\/span><\/td><td>Signal<\/td><\/tr><tr><td><span class=\"rnthl rntcblack\">Black Wire<\/span><\/td><td>GND<\/td><\/tr><tr><td><span class=\"rnthl rntcbrown\">Brown Wire<\/span><\/td><td>Power<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/anemometer-pinout.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Anemometer, Wind Sensor Pinout\" class=\"wp-image-163187\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/anemometer-pinout.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/anemometer-pinout.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"anemometer-wiring-esp32\">Connecting the Anemometer to the ESP32<\/h2>\n\n\n\n<p>The anemometer requires an input voltage of at least 12V. So, you can&#8217;t power it directly from the ESP32, you need an external power source. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/12VDC-Power-Adapter.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"12VDC Power Adapter\" class=\"wp-image-163182\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/12VDC-Power-Adapter.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/12VDC-Power-Adapter.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>We&#8217;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.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DC-Power-Jack-Adapter.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DC power jack adapter\" class=\"wp-image-163181\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DC-Power-Jack-Adapter.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DC-Power-Jack-Adapter.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"voltage-regulator\">Converting the Data Signal from 5V to 3.3V<\/h3>\n\n\n\n<p>In the case of my sensor, it operates in the range of 0 to 5V. However, the <a href=\"https:\/\/randomnerdtutorials.com\/esp32-adc-analog-read-arduino-ide\/\" title=\"\">ESP32 analog pins<\/a> 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.<\/p>\n\n\n\n<p><strong>Note:<\/strong> if you&#8217;re using an anemometer like the one from Adafruit, you don&#8217;t need to worry about this because the maximum output voltage is 2V.<\/p>\n\n\n\n<p>A voltage divider is a simple circuit that reduces&nbsp;a large voltage into a smaller one. Using 2&nbsp;resistors and an input voltage, we can create an output voltage that is a fraction of the input. Below you can see&nbsp;the formula that you need to use to calculate the resistors that you need in your circuit:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"349\" height=\"97\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/voltage-divider-equation.png?resize=349%2C97&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"voltage divider formula\" class=\"wp-image-12438\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/voltage-divider-equation.png?w=349&amp;quality=100&amp;strip=all&amp;ssl=1 349w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/voltage-divider-equation.png?resize=300%2C83&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 349px) 100vw, 349px\" \/><\/figure><\/div>\n\n\n<p>If we use a 1k Ohm (R1) and a 2k Ohm (R2) resistor, we&#8217;ll get a maximum output of 3.3V, which is the maximum that the ESP32 can read.<\/p>\n\n\n\n<p>So, here\u2019s what the voltage divider circuit looks like (in which 5V is the maximum value of the sensor data pin):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"408\" height=\"151\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/voltage-divider-circuit.png?resize=408%2C151&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"voltage divider 5V to 3.3V\" class=\"wp-image-12441\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/voltage-divider-circuit.png?w=408&amp;quality=100&amp;strip=all&amp;ssl=1 408w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/voltage-divider-circuit.png?resize=300%2C111&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 408px) 100vw, 408px\" \/><\/figure><\/div>\n\n\n<p>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.<\/p>\n\n\n\n<p>Learn more here: <a href=\"https:\/\/randomnerdtutorials.com\/how-to-level-shift-5v-to-3-3v\/\" title=\"\">How to Level Shift 5V to 3.3V<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Wiring the Circuit: ESP32 with Anemometer<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-with-anemometer-circuit.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with Anemometer Circuit\" class=\"wp-image-163184\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-with-anemometer-circuit.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-with-anemometer-circuit.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Here&#8217;s a list of the parts you need for this tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP32 board<\/a> &#8211; <a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noopener\" title=\"\">read Best ESP32 development boards<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/anemometer-wind-speed-sensor\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Anemometer Wind Speed Sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/12v-2a-power-adaptor-dc-jack\/\" target=\"_blank\" rel=\"noopener\" title=\"\">12V DC Power Adapter<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/dc-power-male-plug-jack-adapter-2-1mm\/\" target=\"_blank\" rel=\"noopener\" title=\"\">DC power jack adapter<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">1k Ohm resistor and 2k Ohm resistor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Breadboard<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Jumper wires<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/best-multimeters-under-50\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Multimeter<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can use the following diagram as a reference to wire the sensor to the ESP32. Don&#8217;t forget to connect the GND pins together:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-wind-sensor-anemometer-circuit.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"442\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-wind-sensor-anemometer-circuit.jpg?resize=1024%2C442&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with Wind Sensor Anemometer Circuit Diagram\" class=\"wp-image-163191\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-wind-sensor-anemometer-circuit.jpg?resize=1024%2C442&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-wind-sensor-anemometer-circuit.jpg?resize=300%2C129&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-wind-sensor-anemometer-circuit.jpg?resize=768%2C331&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-wind-sensor-anemometer-circuit.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div>\n\n\n<p>If you&#8217;re using an anemometer like the one from Adafruit that outputs a maximum of 2V, you can connect the output pin directly to the ESP32 analog pin (you don&#8217;t need the voltage divider).<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><span class=\"rnthl rntcblack\">Black Wire<\/span><\/td><td>GND of the ESP32 and GND of the power source<\/td><\/tr><tr><td><span class=\"rnthl rntcblue\">Blue Wire<\/span><\/td><td>Connect to ESP32 analog pin (via voltage divider, if needed), we&#8217;re using <strong>GPIO 34<\/strong>.<\/td><\/tr><tr><td><span class=\"rnthl rntcbrown\">Brown Wire<\/span><\/td><td>12V power source (+)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp32-anemometer-arduino-code\">ESP32 with the Anemometer &#8211; Measure Wind Speed Code<\/h2>\n\n\n\n<p>The following code reads the analog signal from the anemometer and converts it into wind speed.<\/p>\n\n\n\n<p><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp32-arduino-ide-2-0\/\">If you have the ESP32 Board installed in your Arduino IDE<\/a>, 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.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-anemometer-wind-speed-arduino\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Softwar\n*********\/\n\n\/\/ Constants (Change the following variables if needed)\nconst int anemometerPin = 34;  \/\/ GPIO pin connected to anemometer (analog pin)\nconst float minVoltage = 0.033;  \/\/ Voltage corresponding to 0 m\/s\nconst float maxVoltage = 3.3;  \/\/ Voltage corresponding to 32.4 m\/s (max speed) (when using voltage divider)\nconst float maxWindSpeed = 32.4; \/\/ Maximum wind speed in m\/s\n\n\/\/ Conversion factors\nconst float mps_to_kmh = 3.6;   \/\/ 1 m\/s = 3.6 km\/h\nconst float mps_to_mph = 2.23694; \/\/ 1 m\/s = 2.23694 mph\n\nvoid setup() {\n  Serial.begin(115200);  \n}\n\nvoid loop() {\n  \/\/ Read analog value from anemometer (ADC value between 0-4095 on ESP32 for 0-3.3V)\n  int adcValue = analogRead(anemometerPin);\n  \n  \/\/ Convert ADC value to voltage (ESP32 ADC range is 0-3.3V)\n  float voltage = (adcValue \/ 4095.00) * 3.3;\n  \n  \/\/ Ensure the voltage is within the anemometer operating range\n  if (voltage &lt; minVoltage) {\n    voltage = minVoltage;\n  } else if (voltage &gt; maxVoltage) {\n    voltage = maxVoltage;\n  }\n  \n  \/\/ Map the voltage to wind speed\n  float windSpeed_mps = ((voltage - minVoltage) \/ (maxVoltage - minVoltage)) * maxWindSpeed;\n\n  \/\/ Convert wind speed to km\/h and mph\n  float windSpeed_kmh = windSpeed_mps * mps_to_kmh;\n  float windSpeed_mph = windSpeed_mps * mps_to_mph;\n\n  \/\/ Print wind speed\n  Serial.print(&quot;Wind Speed: &quot;);\n  Serial.print(windSpeed_mps);\n  Serial.print(&quot; m\/s, &quot;);\n  Serial.print(windSpeed_kmh);\n  Serial.print(&quot; km\/h, &quot;);\n  Serial.print(windSpeed_mph);\n  Serial.println(&quot; mph&quot;);\n  \n  delay(1000); \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_Anemometer_Wind_Sensor.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How does the Code Work?<\/h3>\n\n\n\n<p>First, define the pin where you&#8217;re reading the sensor, the minimum and the maximum output voltage of the sensor, and the maximum wind speed.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Constants (Change the following variables if needed)\nconst int anemometerPin = 34;  \/\/ GPIO pin connected to anemometer (analog pin)\nconst float minVoltage = 0.033;  \/\/ Voltage corresponding to 0 m\/s\nconst float maxVoltage = 3.3;  \/\/ Voltage corresponding to 32.4 m\/s (max speed) (when using voltage divider)\nconst float maxWindSpeed = 32.4; \/\/ Maximum wind speed in m\/s<\/code><\/pre>\n\n\n\n<p>These are the parameters for our sensor. Yours might be different. When we&#8217;re using the voltage divider, the maximum voltage that the ESP32 will read is 3.3V, which will correspond to the maximum wind speed. When the sensor is not moving, it outputs a voltage of 0.033 (read on the voltage divider output), so we consider that the minimum value.<\/p>\n\n\n\n<p>Then, we have the conversion factors to convert the wind speed from m\/s to km\/h and mph.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Conversion factors\nconst float mps_to_kmh = 3.6;   \/\/ 1 m\/s = 3.6 km\/h\nconst float mps_to_mph = 2.23694; \/\/ 1 m\/s = 2.23694 mph<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, we initialize the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void setup() {\n  Serial.begin(115200);  \n}<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we get new readings from the sensor every second.<\/p>\n\n\n\n<p>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. So, we can convert the value to a voltage using the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Convert ADC value to voltage (ESP32 ADC range is 0-3.3V)\nfloat voltage = (adcValue \/ 4095.00) * 3.3;<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\">Related content: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-adc-analog-read-arduino-ide\/\" title=\"\">ESP32 ADC \u2013 Read Analog Values with Arduino IDE<\/a><\/p>\n\n\n\n<p>Then, we have the following condition to check if the values read are within the defined range.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (voltage &lt; minVoltage) {\n  voltage = minVoltage;\n} else if (voltage &gt; maxVoltage) {\n  voltage = maxVoltage;\n}<\/code><\/pre>\n\n\n\n<p>Next, we can easily map the obtained voltage to a wind speed value (alternatively, you can use the Arduino <span class=\"rnthl rntliteral\">map()<\/span> function).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float windSpeed_mps = ((voltage - minVoltage) \/ (maxVoltage - minVoltage)) * maxWindSpeed;<\/code><\/pre>\n\n\n\n<p>Then, we convert the values obtained to km\/h and mph.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Convert wind speed to km\/h and mph\nfloat windSpeed_kmh = windSpeed_mps * mps_to_kmh;\nfloat windSpeed_mph = windSpeed_mps * mps_to_mph;<\/code><\/pre>\n\n\n\n<p>Finally, we print the obtained results.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>  \/\/ Print wind speed\n  Serial.print(\"Wind Speed: \");\n  Serial.print(windSpeed_mps);\n  Serial.print(\" m\/s, \");\n  Serial.print(windSpeed_kmh);\n  Serial.print(\" km\/h, \");\n  Serial.print(windSpeed_mph);\n  Serial.println(\" mph\");\n  \n  delay(1000); \n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the Code<\/h2>\n\n\n\n<p>Upload the code to your ESP32. Make sure you have the sensor wired and powered properly.<\/p>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n\n<p>Spin the sensor to mimic some wind, and see the values being printed in the Serial Monitor every second.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"276\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Wind-Speed-Testing-Serial-Monitor.png?resize=666%2C276&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Testing Wind Sensor with ESP32 - Serial Monitor Demonstration\" class=\"wp-image-163189\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Wind-Speed-Testing-Serial-Monitor.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Wind-Speed-Testing-Serial-Monitor.png?resize=300%2C124&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<p>And that&#8217;s it. Now, you can read the wind speed with your ESP32. You can now add an anemometer to your weather station.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to interface an anemometer with your ESP32. This is a fundamental sensor to add to your weather station to get wind speed data.<\/p>\n\n\n\n<p>We have tutorials for more than 20 sensors with the ESP32. You can check them below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-guides-sensors-modules\/\">ESP32: 26 Free Guides for Sensors and Modules<\/a><\/li>\n<\/ul>\n\n\n\n<p>We hope you found this tutorial useful. You may also like other related tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme680-web-server-arduino\/\">ESP32 Web Server with BME680 \u2013 Weather Station (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/cloud-weather-station-esp32-esp8266\/\">DIY Cloud Weather Station with ESP32\/ESP8266 (MySQL Database and PHP)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-tft-lvgl-weather-station\/\">ESP32 TFT with LVGL: Weather Station (Description, Temperature, Humidity)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to interface an anemometer with the ESP32 to measure wind speed. We&#8217;ll cover how to power and connect the sensor to the ESP32 board, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 with an Anemometer: Measure Wind Speed (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-anemometer-wind-speed-arduino\/#more-163102\" aria-label=\"Read more about ESP32 with an Anemometer: Measure Wind Speed (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":163180,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,277,299,264],"tags":[],"class_list":["post-163102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-with-Anemometer-Measure-Wind-Speed.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=163102"}],"version-history":[{"count":24,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163102\/revisions"}],"predecessor-version":[{"id":168251,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163102\/revisions\/168251"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/163180"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=163102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=163102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=163102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}