{"id":646,"date":"2025-07-04T20:43:50","date_gmt":"2025-07-04T18:43:50","guid":{"rendered":"https:\/\/blog.bratfisch.net\/?p=646"},"modified":"2025-07-22T22:10:31","modified_gmt":"2025-07-22T20:10:31","slug":"a-beelogger-on-esp32-basis-part-5-test-setup-the-wind-direction-sensor","status":"publish","type":"post","link":"https:\/\/blog.bratfisch.net\/index.php\/2025\/07\/04\/a-beelogger-on-esp32-basis-part-5-test-setup-the-wind-direction-sensor\/","title":{"rendered":"A beelogger on ESP32 basis, part 5: Test setup: The wind direction sensor"},"content":{"rendered":"\n<p>I bought a wind direction sensor from china, it&#8217;s a spare part for the &#8220;misol&#8221; weather stations, just search for &#8220;wind direction sensor misol aliexpress&#8221; for the product, it was less than 15\u20ac.<br><\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" data-id=\"666\" src=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/winddir-image_inspyrenet.png\" alt=\"\" class=\"wp-image-666\" srcset=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/winddir-image_inspyrenet.png 960w, https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/winddir-image_inspyrenet-300x225.png 300w, https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/winddir-image_inspyrenet-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" data-id=\"667\" src=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/pasted-image_u2net.png\" alt=\"\" class=\"wp-image-667\" srcset=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/pasted-image_u2net.png 960w, https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/pasted-image_u2net-300x225.png 300w, https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/pasted-image_u2net-768x576.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"489\" height=\"460\" data-id=\"771\" src=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/pasted-image_u2net-1-1.png\" alt=\"\" class=\"wp-image-771\" srcset=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/pasted-image_u2net-1-1.png 489w, https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/pasted-image_u2net-1-1-300x282.png 300w\" sizes=\"auto, (max-width: 489px) 100vw, 489px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>I unscrewed it to understand how it works. The picture of the PCB verifies that the hardware matches the <a href=\"https:\/\/github.com\/Nik71git\/stazione-meteo\/wiki\">datasheets I found in this wiki<\/a>:<br><a href=\"https:\/\/github.com\/Nik71git\/ESP32-weather-station\/files\/10190321\/sensor.datasheet.pdf\">datasheet_1<br><\/a><a href=\"https:\/\/github.com\/Nik71git\/ESP32-weather-station\/files\/10190323\/sensor.datasheet.2.pdf\">datasheet_2<\/a><\/p>\n\n\n\n<p>So it&#8217;s a few hall sensors that switch resistors connected in parallel. The magnet switches one or two of them at once:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"432\" src=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/image-1.png\" alt=\"\" class=\"wp-image-689\" style=\"width:auto;height:250px\" srcset=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/image-1.png 440w, https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/image-1-300x295.png 300w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><\/figure>\n\n\n\n<p>The resistor values are given in the datasheet:<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter is-style-stripes has-small-font-size\"><table class=\"has-fixed-layout\"><thead><tr><th>Direction (degrees)<\/th><th>Resistance (ohms)<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td>33K<\/td><\/tr><tr><td>22.5<\/td><td>6.57K<\/td><\/tr><tr><td>45<\/td><td>8.2K<\/td><\/tr><tr><td>67.5<\/td><td>891<\/td><\/tr><tr><td>90<\/td><td>1K<\/td><\/tr><tr><td>112.5<\/td><td>688<\/td><\/tr><tr><td>135<\/td><td>2.2K<\/td><\/tr><tr><td>157.5<\/td><td>1.41K<\/td><\/tr><tr><td>180<\/td><td>3.9K<\/td><\/tr><tr><td>202.5<\/td><td>3.14K<\/td><\/tr><tr><td>225<\/td><td>16K<\/td><\/tr><tr><td>247.5<\/td><td>14.12K<\/td><\/tr><tr><td>270<\/td><td>120K<\/td><\/tr><tr><td>292.5<\/td><td>42.12K<\/td><\/tr><tr><td>315<\/td><td>64.9K<\/td><\/tr><tr><td>337.5<\/td><td>21.88K<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><br>Every second entry in this table is a combination of the entries below and above when both switches are triggered and is defined by the formular for two resistors connected in parallel:<\/p>\n\n\n<p>$$R = \\frac{R_1 \\cdot R_2}{R_1 + R_2}$$<\/p>\n\n\n\n<p>So we have to measure the resistance of the sensor via the ADC. Therefore a voltage divider is needed. A voltage divider looks like this:<br><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"310\" src=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/511ac8f5ce395f5846000000.png\" alt=\"\" class=\"wp-image-811\" style=\"width:auto;height:200px\" srcset=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/511ac8f5ce395f5846000000.png 400w, https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/511ac8f5ce395f5846000000-300x233.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/figure>\n\n\n\n<p>R<sub>2<\/sub> is the variable resistor that is the sensor. R<sub>1<\/sub> has to be chosen correctly so that it matches the range of R<sub>2<\/sub>, the value of V<sub>in <\/sub>and the desired measuring range on V<sub>out<\/sub>.<br><br>According to the datasheets we have to measure resistances for R<sub>2<\/sub> that can be between 688 \u03a9 and 120 k\u03a9. The ADC of the ESP32S3 accepts up to <a href=\"https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/v4.4\/esp32s3\/api-reference\/peripherals\/adc.html\">3100 mV with ADC_ATTEN_DB_11<\/a> (that&#8217;s V<sub>out<\/sub>), V<sub>in<\/sub> is 3.3V from the ESP32.<\/p>\n\n\n\n<p>Now the value for R<sub>1<\/sub> has to be calculated for the biggest possible resistor, so we have R<sub>2<\/sub>=120 k\u03a9, V<sub>in<\/sub>=3.3V and V<sub>out<\/sub><em>\u2264<\/em>3.1V:<\/p>\n\n\n\n<p>The formular for the voltage divider is:<\/p>\n\n\n<p>$$V_{\\text{out}} = V_{\\text{in}} \\cdot \\frac{R_2}{R_1 + R_2}$$<\/p>\n\n\n\n<p>So, for our values of V<sub>in<\/sub>, V<sub>out<\/sub> and R<sub>2<\/sub> we get:<\/p>\n\n\n<p>$$R_1 = R_2 \\cdot (\\frac{V_{\\text{in}}}{V_{\\text{out}}} &#8211; 1) = 120k\u03a9 \\cdot (\\frac{3.3V}{3.1V} &#8211; 1) \u2248 7.74k\u03a9$$<\/p>\n\n\n\n<p>The next fitting standard resistor is 8.2k\u03a9, let&#8217;s calculate V<sub>out <\/sub>for it:<\/p>\n\n\n<p>$$V_{\\text{out}} = V_{\\text{in}} \\cdot \\frac{R_2}{R_1 + R_2} = 3.3V \\cdot \\frac{120k\u03a9}{8.2k\u03a9 + 120k\u03a9} \u2248 3089mV$$<\/p>\n\n\n\n<p>That&#8217;s a good value: with it we get 3.089V on V<sub>out<\/sub> for 120k\u03a9 which is about 99.6% of the maximal voltage of 3.1V the ADC of the ESP323 can handle.<br>So we can get up to 3.089V of the reference voltage of 3.3V, that&#8217;s 93.6% of the theoretical range. The ADC has 12 bits, that means we have a theoretical measuring range of 2<sup>12<\/sup>=4096 values, 93.6% of it are \u22483834, so we have a resolution of round about 120k\u03a9\/3834 \u2248 31.3\u03a9. <br>That&#8217;s sufficient, the minimal difference between the resistors we want to measure is more than 100\u03a9.<\/p>\n\n\n\n<p>So we can wire the sensor up now via a properly chosen voltage divider:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"750\" src=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/beelogger-wind-direction-sensor_bb-3.png\" alt=\"\" class=\"wp-image-959\" style=\"width:auto;height:300px\" srcset=\"https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/beelogger-wind-direction-sensor_bb-3.png 726w, https:\/\/blog.bratfisch.net\/wp-content\/uploads\/2025\/07\/beelogger-wind-direction-sensor_bb-3-290x300.png 290w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/figure>\n\n\n\n<p>Integrating it in esphome is comfortable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>substitutions:\n  update_interval: 5s\n\ntext_sensor:\n  - platform: template\n    name: \"Wind direction\"\n    icon: 'mdi:windsock'\n    id: wind_dir\n\nsensor:\n  - platform: adc\n    id: source_sensor\n    pin: GPIO32\n    name: ADC\n    attenuation: 11db\n    internal: true\n    update_interval: ${update_interval}\n    accuracy_decimals: 1\n\n  - platform: resistance\n    sensor: source_sensor\n    id: resistance_sensor\n    configuration: DOWNSTREAM\n    resistor: 8.2kOhm\n    internal: true\n    name: Resistance Sensor\n    reference_voltage: 3.3V\n    accuracy_decimals: 1\n    update_interval: ${update_interval}\n\n    on_value:\n      then:\n        - lambda: |-\n            struct WindEntry {\n              int lower;\n              int upper;\n              const char* direction;\n              float heading;\n            };\n\n            static const WindEntry wind_table&#91;] = {\n              {  400,    789, \"East-South-East\",  112.5},  \/\/ 688 \u03a9\n              {  789,    945, \"East-North-East\",   67.5},  \/\/ 891 \u03a9\n              {  945,   1205, \"East\",              90.0},  \/\/ 1 k\u03a9\n              { 1205,   1805, \"South-South-East\", 157.5},  \/\/ 1.41 k\u03a9\n              { 1805,   2670, \"South-East\",       135.0},  \/\/ 2.2 k\u03a9\n              { 2670,   3520, \"South-South-West\", 202.5},  \/\/ 3.14 k\u03a9\n              { 3520,   5235, \"South\",            180.0},  \/\/ 3.9 k\u03a9\n              { 5235,   7385, \"North-North-East\",  22.5},  \/\/ 6.57 k\u03a9\n              { 7385,  11160, \"North-East\",        45.0},  \/\/ 8.2 k\u03a9\n              {11160,  15060, \"West-South-West\",  247.5},  \/\/ 14.12 k\u03a9\n              {15060,  18940, \"South-West\",       225.0},  \/\/ 16 k\u03a9\n              {18940,  27440, \"North-North-West\", 337.5},  \/\/ 21.88 k\u03a9\n              {27440,  37560, \"North\",              0.0},  \/\/ 33 k\u03a9\n              {37560,  53510, \"West-North-West\",  292.5},  \/\/ 42.12 k\u03a9\n              {53510,  92450, \"North-West\",       315.0},  \/\/ 64.9 k\u03a9\n              {92450, 150000, \"West\",             270.0},  \/\/ 120 k\u03a9\n            };\n\n            const int resistance = id(resistance_sensor).state;\n\n            for (const auto&amp; entry : wind_table) {\n              if (resistance &gt;= entry.lower &amp;&amp; resistance &lt; entry.upper) {\n                id(wind_dir).publish_state(entry.direction);\n                id(wind_heading).publish_state(entry.heading);\n                break;\n              }\n            }\n\n  - platform: template\n    name: \"Wind Heading\"\n    id: wind_heading\n    unit_of_measurement: \"\u00b0\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I bought a wind direction sensor from china, it&#8217;s a spare part for the &#8220;misol&#8221; weather stations, just search for &#8220;wind direction sensor misol aliexpress&#8221; for the product, it was less than 15\u20ac. I unscrewed it to understand how it works. The picture of the PCB verifies that the hardware matches the datasheets I found [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3,4,5],"class_list":["post-646","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-beelogger","tag-esp32","tag-esphome"],"_links":{"self":[{"href":"https:\/\/blog.bratfisch.net\/index.php\/wp-json\/wp\/v2\/posts\/646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bratfisch.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bratfisch.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bratfisch.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bratfisch.net\/index.php\/wp-json\/wp\/v2\/comments?post=646"}],"version-history":[{"count":175,"href":"https:\/\/blog.bratfisch.net\/index.php\/wp-json\/wp\/v2\/posts\/646\/revisions"}],"predecessor-version":[{"id":1004,"href":"https:\/\/blog.bratfisch.net\/index.php\/wp-json\/wp\/v2\/posts\/646\/revisions\/1004"}],"wp:attachment":[{"href":"https:\/\/blog.bratfisch.net\/index.php\/wp-json\/wp\/v2\/media?parent=646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bratfisch.net\/index.php\/wp-json\/wp\/v2\/categories?post=646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bratfisch.net\/index.php\/wp-json\/wp\/v2\/tags?post=646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}