{"id":163194,"date":"2024-11-26T14:53:57","date_gmt":"2024-11-26T14:53:57","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=163194"},"modified":"2024-11-26T15:07:55","modified_gmt":"2024-11-26T15:07:55","slug":"micropython-esp32-esp8266-anemometer-wind-sensor","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-anemometer-wind-sensor\/","title":{"rendered":"MicroPython: ESP32\/ESP8266 with Anemometer (Wind Sensor)"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to interface an anemometer to measure wind speed with the ESP32 and ESP8266 NodeMCU boards programmed with MicroPython. We&#8217;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.<\/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-ESP8266-Micropython-wind-sensor.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP32 ESP8266 NodeMCU with Anemometer Wind Sensor\" class=\"wp-image-163211\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-ESP8266-Micropython-wind-sensor.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-ESP8266-Micropython-wind-sensor.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-ESP8266-Micropython-wind-sensor.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-ESP8266-Micropython-wind-sensor.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-ESP8266-Micropython-wind-sensor.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><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-esp8266\" title=\"\">Connecting the Anemometer to the ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"#esp32-anemometer-micropython-code\" title=\"\">ESP32 with the Anemometer &#8211; Measure Wind MicroPython Speed Code<\/a><\/li>\n\n\n\n<li><a href=\"#esp8266-anemometer-micropython-code\" title=\"\">ESP8266 with the Anemometer &#8211; Measure Wind Speed MicroPython Code<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>New to MicroPython?<\/strong> Check out our eBook: <a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\" title=\"\">MicroPython Programming with ESP32 and ESP8266 eBook (2nd Edition)<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>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:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Thonny IDE:\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>uPyCraft IDE:\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with uPyCraft IDE<\/a><\/li>\n\n\n\n<li>Install uPyCraft IDE (<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Windows<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Mac OS X<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Linux<\/a>)<\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclgreen\">Learn more about MicroPython:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266<\/a><\/p>\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 regular<\/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-esp8266\">Connecting the Anemometer to the ESP32 and ESP8266<\/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 or ESP8266, 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-esp8266-analog-readings-micropython\/\" title=\"\">ESP32 analog pins and the ESP8266 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 and ESP8266 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> or <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP8266 board<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/anemometer-wind-speed-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">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 diagrams as a reference to wire the sensor to the boards. Don&#8217;t forget to connect the GND pins together.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">ESP32<\/h4>\n\n\n\n<p>For the ESP32, we&#8217;re using GPIO34 to read the analog signal from the sensor, but you can use any other <a href=\"https:\/\/randomnerdtutorials.com\/esp32-adc-analog-read-arduino-ide\/\" title=\"\">suitable GPIO for analog reading<\/a>.<\/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=\"1200\" height=\"518\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-wind-sensor-anemometer-circuit.jpg?resize=1200%2C518&#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?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, 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=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=768%2C331&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\">ESP8266<\/h4>\n\n\n\n<p>The ESP8266 only has <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-adc-reading-analog-values-with-nodemcu\/\" title=\"\">one analog pin, the A0 pin<\/a>. So, you must use that pin to read the analog signal from the sensor.<\/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=\"1200\" height=\"705\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP8266-anemometer-wind-sensor-circuit.jpg?resize=1200%2C705&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 with Wind Sensor Anemometer Circuit Diagram\" class=\"wp-image-163212\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP8266-anemometer-wind-sensor-circuit.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP8266-anemometer-wind-sensor-circuit.jpg?resize=300%2C176&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP8266-anemometer-wind-sensor-circuit.jpg?resize=1024%2C602&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP8266-anemometer-wind-sensor-circuit.jpg?resize=768%2C451&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/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 ESP 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 ESP board and GND of the power source<\/td><\/tr><tr><td><span class=\"rnthl rntcblue\">Blue Wire<\/span><\/td><td>Connect to ESP32\/ESP8266 analog pin (via voltage divider, if needed), we&#8217;re using <strong>GPIO 34<\/strong> for the ESP32 and <strong>A0 <\/strong>for the ESP8266.<\/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-micropython-code\">ESP32 with the Anemometer &#8211; Measure Wind Speed MicroPython 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>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-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-esp32-esp8266-anemometer-wind-sensor\/\n \nfrom machine import ADC, Pin\nfrom time import sleep\n\n# Constants\n# GPIO pin connected to anemometer (ESP32)\nanemometer_pin = 34\nmin_voltage = 0.033  # Voltage corresponding to 0 m\/s\nmax_voltage = 3.3    # Voltage corresponding to 32.4 m\/s (max speed when using voltage divider)\nmax_wind_speed = 32.4 # Maximum wind speed in m\/s\n\n# Conversion factors\nmps_to_kmh = 3.6     # 1 m\/s = 3.6 km\/h\nmps_to_mph = 2.23694 # 1 m\/s = 2.23694 mph\n\n# Set up the ADC\nadc = ADC(Pin(anemometer_pin))\nadc.atten(ADC.ATTN_11DB)  # Configure ADC to read full 3.3V range (11dB attenuation)\nadc.width(ADC.WIDTH_12BIT)  # Set resolution to 12 bits (0-4095)\n\nwhile True:\n    # Read analog value from anemometer (ADC value between 0-4095)\n    adc_value = adc.read()\n    \n    # Convert ADC value to voltage\n    voltage = (adc_value \/ 4095.0) * 3.3\n    \n    # Ensure the voltage is within the anemometer operating range\n    if voltage &lt; min_voltage:\n        voltage = min_voltage\n    elif voltage &gt; max_voltage:\n        voltage = max_voltage\n    \n    # Map the voltage to wind speed\n    wind_speed_mps = ((voltage - min_voltage) \/ (max_voltage - min_voltage)) * max_wind_speed\n\n    # Convert wind speed to km\/h and mph\n    wind_speed_kmh = wind_speed_mps * mps_to_kmh\n    wind_speed_mph = wind_speed_mps * mps_to_mph\n\n    # Print wind speed\n    print(&quot;Wind Speed:&quot;)\n    print(&quot;{:.2f} m\/s&quot;.format(wind_speed_mps))\n    print(&quot;{:.2f} km\/h&quot;.format(wind_speed_kmh))\n    print(&quot;{:.2f} mph&quot;.format(wind_speed_mph))\n    print() \n    \n    sleep(1)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/ESP32_Anemometer_Wind_Sensor.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>The code for the ESP8266 is slightly different\u2014continue scrolling for the ESP8266 MicroPython Code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp8266-anemometer-micropython-code\">ESP8266 NodeMCU with the Anemometer &#8211; Measure Wind Speed MicroPython Code<\/h2>\n\n\n\n<p>The following code does the exact same thing as the previous one, but it was adapted for ESP8266 NodeMCU boards.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-esp32-esp8266-anemometer-wind-sensor\/\n \nfrom machine import ADC, Pin\nfrom time import sleep\n\n# Constants\nmin_voltage = 0.033  # Voltage corresponding to 0 m\/s\nmax_voltage = 3.3    # Voltage corresponding to 32.4 m\/s (max speed when using voltage divider)\nmax_wind_speed = 32.4 # Maximum wind speed in m\/s\n\n# Conversion factors\nmps_to_kmh = 3.6     # 1 m\/s = 3.6 km\/h\nmps_to_mph = 2.23694 # 1 m\/s = 2.23694 mph\n\n# Set up the ADC\nadc = ADC(0)\n\nwhile True:\n    # Read analog value from anemometer (ADC value between 0-1023)\n    adc_value = adc.read()\n    \n    # Convert ADC value to voltage\n    voltage = (adc_value \/ 1023.0) * 3.3\n    \n    # Ensure the voltage is within the anemometer operating range\n    if voltage &lt; min_voltage:\n        voltage = min_voltage\n    elif voltage &gt; max_voltage:\n        voltage = max_voltage\n    \n    # Map the voltage to wind speed\n    wind_speed_mps = ((voltage - min_voltage) \/ (max_voltage - min_voltage)) * max_wind_speed\n\n    # Convert wind speed to km\/h and mph\n    wind_speed_kmh = wind_speed_mps * mps_to_kmh\n    wind_speed_mph = wind_speed_mps * mps_to_mph\n\n    # Print wind speed\n    print(&quot;Wind Speed:&quot;)\n    print(&quot;{:.2f} m\/s&quot;.format(wind_speed_mps))\n    print(&quot;{:.2f} km\/h&quot;.format(wind_speed_kmh))\n    print(&quot;{:.2f} mph&quot;.format(wind_speed_mph))\n    print() \n    \n    sleep(1)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/ESP8266_Anemometer_Wind_Sensor.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Does the Code Work?<\/h2>\n\n\n\n<p>We start importing the required modules. The <span class=\"rnthl rntliteral\">ADC<\/span> and <span class=\"rnthl rntliteral\">Pin<\/span> classes from the <span class=\"rnthl rntliteral\">machine<\/span> module to set the GPIOs as ADC pins.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import ADC, Pin\nfrom time import sleep<\/code><\/pre>\n\n\n\n<p>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. In case of the ESP32, we&#8217;re using GPIO 34 to read the analog signal from the wind sensor. <\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># GPIO pin connected to anemometer (ESP32)\nanemometer_pin = 34\nmin_voltage = 0.033  # Voltage corresponding to 0 m\/s\nmax_voltage = 3.3    # Voltage corresponding to 32.4 m\/s (max speed when using voltage divider)\nmax_wind_speed = 32.4 # Maximum wind speed in m\/s<\/code><\/pre>\n\n\n\n<p>In the case of the ESP8266, the only available analog pin is A0. So, we need to pass it to the <span class=\"rnthl rntliteral\">ADC()<\/span> object later on like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>adc = ADC(0)<\/code><\/pre>\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-python\"><code># Conversion factors\nmps_to_kmh = 3.6     # 1 m\/s = 3.6 km\/h\nmps_to_mph = 2.23694 # 1 m\/s = 2.23694 mph<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">ADC<\/span> object on the GPIO you&#8217;re using to read the analog sensor. It&#8217;s like this for the ESP32:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>adc = ADC(Pin(anemometer_pin))<\/code><\/pre>\n\n\n\n<p>And like this for the ESP8266:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>adc = ADC(0)<\/code><\/pre>\n\n\n\n<p>When using the ESP32. we also need to pass the following two lines.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>adc.atten(ADC.ATTN_11DB)  # Configure ADC to read full 3.3V range (11dB attenuation)\nadc.width(ADC.WIDTH_12BIT)  # Set resolution to 12 bits (0-4095)<\/code><\/pre>\n\n\n\n<p>The first line means that we want to read voltage from 0 to 3.3V. This corresponds to setting the attenuation ratio of 11db.<\/p>\n\n\n\n<p>The second line of code sets the resolution to 12-bits. So, we&#8217;ll read a number between 0 to 4095 on the analog pin.<\/p>\n\n\n\n<p>Finally, we have a while loop that gets new wind speed values 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. <\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>while True:\n    # Read analog value from anemometer (ADC value between 0-4095)\n    adc_value = adc.read()<\/code><\/pre>\n\n\n\n<p>So, we can convert the value to a voltage using the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Convert ADC value to voltage\nvoltage = (adc_value \/ 4095.0) * 3.3<\/code><\/pre>\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-python\"><code># Ensure the voltage is within the anemometer operating range\nif voltage &lt; min_voltage:\n    voltage = min_voltage\nelif voltage &gt; max_voltage:\n    voltage = max_voltage<\/code><\/pre>\n\n\n\n<p>Next, we can easily map the obtained voltage to a wind speed value as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Map the voltage to wind speed\nwind_speed_mps = ((voltage - min_voltage) \/ (max_voltage - min_voltage)) * max_wind_speed<\/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-python\"><code> # Convert wind speed to km\/h and mph\n wind_speed_kmh = wind_speed_mps * mps_to_kmh\n wind_speed_mph = wind_speed_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-python\"><code># Print wind speed\nprint(\"Wind Speed:\")\nprint(\"{:.2f} m\/s\".format(wind_speed_mps))\nprint(\"{:.2f} km\/h\".format(wind_speed_kmh))\nprint(\"{:.2f} mph\".format(wind_speed_mph))\nprint() <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Run or upload the code to your board using Thonny IDE.<\/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=\"470\" height=\"114\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/image.png?resize=470%2C114&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE Run Code\" class=\"wp-image-163213\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/image.png?w=470&amp;quality=100&amp;strip=all&amp;ssl=1 470w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/image.png?resize=300%2C73&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n<p>On the MicroPython Shell, you should get wind speed data.<\/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=\"696\" height=\"418\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-wind-speed-micropython.png?resize=696%2C418&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32-ESP8266-Measure-Wind-Speed-MucriPython-Demonstration\" class=\"wp-image-163214\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-wind-speed-micropython.png?w=696&amp;quality=100&amp;strip=all&amp;ssl=1 696w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-wind-speed-micropython.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><\/figure><\/div>\n\n\n<p>You may need to do further sensor calibration to get more accurate results.<\/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 the ESP32 and ESP8266 boards programmed with MicroPython to get data about wind speed.<\/p>\n\n\n\n<p>We hope you found this quick guide useful. We have guides for other sensors and modules that you may find useful:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-neo-6m-gps\/\" title=\"\">MicroPython: ESP32 with NEO-6M GPS Module<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-i2c-lcd-esp32-esp8266\/\">MicroPython: I2C LCD Display with ESP32\/ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-bh1750-esp32-esp8266\/\">MicroPython: BH1750 Ambient Light Sensor with ESP32\/ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-ds18b20-esp32-esp8266\/\">MicroPython: DS18B20 Temperature Sensor with ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-bme280-esp32-esp8266\/\">MicroPython: BME280 with ESP32 and ESP8266 (Pressure, Temperature, Humidity)<\/a><\/li>\n<\/ul>\n\n\n\n<p>To learn more about MicroPython, check out our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32-esp8266-micropython\/\">Free MicroPython projects and guides<\/a> <\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266 eBook<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to interface an anemometer to measure wind speed with the ESP32 and ESP8266 NodeMCU boards programmed with MicroPython. We&#8217;ll cover how to power and &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: ESP32\/ESP8266 with Anemometer (Wind Sensor)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-anemometer-wind-sensor\/#more-163194\" aria-label=\"Read more about MicroPython: ESP32\/ESP8266 with Anemometer (Wind Sensor)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":163211,"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":[310,309,264],"tags":[],"class_list":["post-163194","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-micropython","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-ESP8266-Micropython-wind-sensor.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\/163194","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=163194"}],"version-history":[{"count":11,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163194\/revisions"}],"predecessor-version":[{"id":164226,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163194\/revisions\/164226"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/163211"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=163194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=163194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=163194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}