Projects / Wind vane with a magnetic angle sensor

Wind vane with a magnetic angle sensor

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Test the sensor

    Read the raw angle and turn the magnet by hand.

  2. NANO

    Print the shaft

    Make a holder that keeps the magnet 1 to 2 mm above the chip.

  3. NANOBEARIN

    Build the vane

    Add a tail fin on the bearings.

  4. NANO

    Calibrate

    Point it north and press the button.

  5. NANO

    Convert to compass points

    Show N, NE, E and so on.

  6. NANO

    Add the average

    Smooth over about 2 minutes.

  7. NANO

    Mount it outdoors

    Seal the electronics and fix the mast.

In short

How do you build a wind vane with a magnetic angle sensor?

A vane turns a diametric magnet over an AS5600 sensor, which gives the wind direction to under a degree with no contacts to wear out. You need Arduino Nano, AS5600 magnetic angle encoder, Bearings 608ZZ, SSD1306 OLED Display, 3D printer PLA filament 1.75 mm (1 kg) and Tactile push button 6 mm. It takes 7 steps and about 150 minutes.

Level
Intermediate
Time
About 150 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
AS5600 VCC and GNDNano 5V and GNDPower (use 5 V only if your board has an on-board regulator, otherwise 3V3)
AS5600 SDANano A4I2C data
AS5600 SCLNano A5I2C clock
OLED VCC, GND, SDA, SCLNano 5V, GND, A4, A5Shares the I2C bus
ButtonNano D2 and GNDPress with the vane pointing north
USB or 5 V adapterNanoPower

Code

The AS5600 reads the angle of a magnet turning right above its chip, so nothing touches and nothing wears. The magnet must be diametrically magnetised (north on one half and south on the other) and held 1 to 2 mm above the chip, exactly on the axis of rotation; print a shaft and holder for the vane and mount it on two 608 bearings. Point the vane north with a compass, press the button and the offset is saved in memory, so every reading is relative to north. The direction average uses sine and cosine, as averaging angles directly fails around north (359 and 1 degrees would average to 180). Wind comes FROM the direction the vane's head points to. Mount the sensor outdoors in a sealed box away from rain, protect the electronics with a roof, and fix the mast securely so it cannot fall. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Wind vane with AS5600. Libraries: Adafruit SSD1306, Adafruit GFX. Board: Arduino Nano.
#include <Wire.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const byte AS5600_ADDR = 0x36;
const byte BTN = 2;
const char* NAMES[16] = {"N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"};
float offsetDeg = 0, avgX = 0, avgY = 0;
bool haveAvg = false;

int readRaw() {
  Wire.beginTransmission(AS5600_ADDR); Wire.write(0x0C); Wire.endTransmission(false);   // RAW ANGLE register
  Wire.requestFrom(AS5600_ADDR, (uint8_t)2);
  if (Wire.available() < 2) return -1;
  int hi = Wire.read(), lo = Wire.read();
  return ((hi << 8) | lo) & 0x0FFF;                                                      // 12 bits, 0 to 4095
}

float readDeg() {
  int r = readRaw(); if (r < 0) return -1;
  float d = r * 360.0 / 4096.0 - offsetDeg;
  while (d < 0) d += 360; while (d >= 360) d -= 360;
  return d;
}

void setup() {
  pinMode(BTN, INPUT_PULLUP);
  Wire.begin();
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); oled.setTextColor(SSD1306_WHITE);
  EEPROM.get(0, offsetDeg);
  if (isnan(offsetDeg) || offsetDeg < 0 || offsetDeg > 360) offsetDeg = 0;
}

void loop() {
  if (!digitalRead(BTN)) {                              // vane pointing north: store this reading as the offset
    int r = readRaw();
    if (r >= 0) { offsetDeg = r * 360.0 / 4096.0; EEPROM.put(0, offsetDeg); }
    delay(500);
  }
  float d = readDeg();
  if (d >= 0) {
    float x = cos(d * DEG_TO_RAD), y = sin(d * DEG_TO_RAD);
    if (!haveAvg) { avgX = x; avgY = y; haveAvg = true; }
    avgX += (x - avgX) * 0.01; avgY += (y - avgY) * 0.01;      // slow average of the direction as a vector (about 2 minutes)
    float avgDeg = atan2(avgY, avgX) * RAD_TO_DEG; if (avgDeg < 0) avgDeg += 360;
    oled.clearDisplay();
    oled.setTextSize(1); oled.setCursor(0, 0); oled.print("Wind from");
    oled.setTextSize(3); oled.setCursor(0, 14); oled.print(NAMES[(int)((d + 11.25) / 22.5) % 16]);
    oled.setTextSize(1); oled.setCursor(0, 46); oled.print((int)d); oled.print(" deg now");
    oled.setCursor(0, 56); oled.print((int)avgDeg); oled.print(" deg average (");
    oled.print(NAMES[(int)((avgDeg + 11.25) / 22.5) % 16]); oled.print(")");
    oled.display();
  }
  delay(100);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Vane pointed northReads N after calibration
Turn to eastReads E within a few degrees
Gusty windDirection fluctuates, average stays steady
Power off and onNorth offset remembered

If it does not work

Readings jump

Check the magnet is centred and the gap is 1 to 2 mm.

Reads a fixed value

Check I2C wiring and the sensor's address 0x36.

Direction is mirrored

Turn the magnet over or change the sign of the angle.

Vane sticks

Use better bearings and balance the vane.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the wind vane with a magnetic angle sensor?

Arduino Nano, AS5600 magnetic angle encoder, Bearings 608ZZ, SSD1306 OLED Display, 3D printer PLA filament 1.75 mm (1 kg) and Tactile push button 6 mm.

How long does the wind vane with a magnetic angle sensor take to build?

About 150 minutes for a intermediate maker, in 7 steps.

How do I know the wind vane with a magnetic angle sensor works?

Vane pointed north: Reads N after calibration. Turn to east: Reads E within a few degrees. Gusty wind: Direction fluctuates, average stays steady.

Readings jump: what should I check?

Check the magnet is centred and the gap is 1 to 2 mm.

Reads a fixed value: what should I check?

Check I2C wiring and the sensor's address 0x36.

Direction is mirrored: what should I check?

Turn the magnet over or change the sign of the angle.

Vane sticks: what should I check?

Use better bearings and balance the vane.