Weather station project, it's getting windy

Believe it or not but this thing right here is a wind vane for my weather station:

ws-wind-dir-01_small.jpg


"A wind vane"

Ok, at least it’s some kind of mock-up of a wind vane. It’s just missing the vane part. What it has is the absolute rotary encoder done with a 4-bit binary-reflected Gray code (http://en.wikipedia.org/wiki/Gray_code) printed on paper. I cut off all white areas from the disk and then laminated it to make it a bit more rigid. Then I made a hole right through the center and placed a piece of aluminum tube in there. Pieces of paint sticks made a nice “sensor array” with IR leds and phototransistors. You can see leds in the picture above and phototransistors are on the other side of the Gray code disk facing those leds. Picture of the disk and schematic below.

ws-gray-code_small.png


4-bit reflected-binary Gray code disk

ws-wind-measure-schema_small.png


Gray code reader schematic

With 4-bits I can get 16 different positions which is enough for me to get wind direction (I don’t have a good place for wind vane anyway). Code for getting the direction is quite simple (AVR C):

#include <avr/io.h>

#include <util/delay.h>

void wind_measure_init(void) {

    /* Set port directions: PD0-PD3 = input for Gray code, PD4 = Led control /

    DDRD |= _BV(PD4);

}

uint8_t wind_measure_get_dir() {

    uint8_t dir_code;

    / Leds on /

    PORTD |= _BV(PD4);

    / Give phototransistors some time to react /

    _delay_us(20);

    / Get Gray code for direction /

    dir_code = PIND & 0x0f;

    / Leds off */

    PORTD &= ~(_BV(PD4));

    return dir_code;

}

 

To read the Gray code you just get 4 least significant bits from port D. Of course you have to map the Gray code value to actual direction. Works like a dream. I’ll just have to make a bit more “finished” version for real use. In case you are wondering why I did this Gray code thing: I just wanted to try it out after spotting it here: https://www.robotshop.com/letsmakerobots/node/23260#comment-55291 (thanks RobotFreak) :slight_smile: