Archive for the ‘ Electronics ’ Category

Updated Weather Station

Working on updating my weather station currently. It’s been out of commission for a year or so. The rubbermaid box I had it in didn’t last in the elements so I’m having to rethink my mounting process. I’m also looking into getting a larger battery because the 6000 maH one I’m using doesn’t last that long.

I’m also going to be improving it. I’ve put together a circuit to measure how much battery I have left so I can have it turn itself off when the battery is too low. Also have a few more calculations that I can add in to my reporting like heat index which will be useful for the summer.

I’ve also reordered my posts so you will see the updated weather station posts below this one to try to group them in an order of build process


With this post I wanted to do a full overview of my mast, which I’ve spoken some on before. This will provide instructions on how to build as well as archiving things in case I need to rebuild it in the future. (I just had to re-wire everything and it was pretty difficult remember and figuring out where everything went) The mast is only one part of my weather station; the enclosure, which houses some other sensors as well as the RPi, will be in another post. The way my weather station works is the mast gathers info from the 5 sensors that are contained on/in it via the micro controller. It then sends the data to the Raspberry Pi that resides in the enclosure via I2C to be archived, along with data from the other sensors, within my database tables.

The mast is built out of PVC. This allows me to attach my homemade sensors easily as well as protecting the micro controller that resides inside that runs the sensors.

Main Mast Build:

Most of the mast is made from 1″ PVC with different connectors and such.  There are a few places in which the 1″ is not used, for example, where the micro controller resides.  (You can see it as the larger section). It uses 3/4″ PVC inside 1.5″ PVC.  It is built this way to provide some waterproofing for the micro controller.  The other section is for the temperature & humidity sensors.  It uses some 3″ PVC as a sun shield for the sensors.  As for the wiring of the sensors, I will detail that a little later, but all the wiring for the sensors comes together at a point where the larger section starts.  It’s there where I  “harness” everything together and will detail that later.


As I said, there are 5 sensors within the mast.  3 are sensors I have built myself and two are not.  For the sensors I have built myself, I have linked to the posts in which I detail how to build them.

  1. Wind Vane – Link to post
  2. Anemometer – Link to post
  3. Rain Gauge – Link to post
  4. Temp Sensor – Link to post
  5. Humidity Sensor – Link to post

Microcontroller & Wiring:

In previous versions of the mast, I have used an Adafruit Trinket board, but when I attempted to add some additional sensors (which failed miserably due to waterproofing) it didn’t have the needed pins required, so I switched to a Arduino Pro Mini 3V.  The Pro Mini is slightly larger, more powerful and allows me to power it with 5V coming from the Raspberry Pi.

As I said before, I communicate with the Pro Mini from the RPi via I2C.  To do this, I have a cat 5 cable running from the enclosure to the Pro Mini (I only need 4 of the wires, but it’s nice to have a spare 4 in case something happens) and then another piece of cat 5 that runs from the Pro Mini to the wiring harness.  I’ll make another post regarding the wiring for the micro controller and the wiring harness as I believe this one has gotten a little long. Link to post

Stay tuned for more…


I’ve been meaning to do this for a long time…so without further delay, here is my DIY anemometer.

For those not familiar with what an anemometer is, they are used to measure wind speed. There are certain drawbacks to my design, but after having it active for at least 6 months. I think it works fairly well. The way it works is that it has a reed switch that is triggered with a magnet. In my program, I have it hooked up to an interrupt and count the number of times it triggers within 10 seconds and then plug that into a formula. To come up with this formula, I created a setup on an Arduino that constantly looped the 10 second count. My wife and I took the car out to a county road and did some tests at different speeds. I took the average of 3 tries at 5 different speeds to create the formula I use to convert trigger count to mph.

What you’ll need:


A 7/16″ threaded rod with some nuts and a washer, a 3/4″ to 1″ PVC “T”, a 1 1/2″ PVC cap, a 1″ cap (I think this is the size that fits over one of the sides of the “T”), a slip bushing that will fit in one of the 3/4″ sides of the “T” (that the next item will also fit in), a bearing, 3 ladle spoons (cheap @ Walmart), some small bolts and nuts (I’ll place the size in later), a very small magnet, a reed switch, and some cat 3 (or cat 5) cable.


The first step is a bit of work. You have to get the bearing inside the bushing and have it flat with the bushing. What you’ll need to do is sand out the bushing some and use a piece of wood and a clamp to squeeze it in. It’s very important that it is as straight as possible.


Next we begin to make the shaft. Place a lock nut at one end of your 7/16″ threaded rod.


Now place the rod through the bearing so that the lock nut is inside the bushing. On the other end of the threaded bushing, you’ll need two nuts. The first nut will need to hold the rod against the bearing, but not so tight that the rod will not spin. The second nut is to make sure that the first nut doesn’t move. (Work the first nut down to a list past the tightness to the bearing you need, then put the second nut on. Tighten the second nut while loosening the first to get it good and locked.)


Now comes another difficult part. You’ll need to put a hole for the rod in the exact middle of the 1 1/2″ PVC cap. I think I went through 5 caps before I got it right. (A drill press is your friend). You also need to place two holes in each of the ladles so that your small screws will fit. (These are stainless steel, so go slow! It’s fairly difficult to drill through it) Make sure they are in the center of the handle and that you have the center line marked the same for each spoon. The other piece of this step, you’ll need to bend the handles of the ladles so that they are perpendicular to the ladle. (The come kind of curvy)



Showing the center lines and the bent handles.


To attach the ladles to the PVC cap, you’ll need to mark and drill 3 holes. They need to be 2 7/16″ from each other and 1″ from the bottom of the cap. (Using a soft sewing tape measure works well for this.


Now here’s where the center line comes in handy. You’ll need to bend the handles of the ladles on this center line as shown.


Now attach the first ladle to the cap and get it level with a table. Mark and drill the hole for the other screw and attach the other side of the ladle to the cap. Continue to do this for the other two ladles. (I used lock nuts to attach these screws.


Hot glue your magnet (or 3) inside the cap.


Now for some of the electronics. Take your reed switch and bend one side like shown. Be VERY CAREFUL, these are delicate.


Now solder a wire to each side as shown and place shrink tube over the joints. I have found that you will need water proof this a bit. There are two ways to accomplish this…carefully place shrink tube over the reed switch and seal up the top, or buy these from Sparkfun.


In this step you’ll need to cut your threaded rod to size. Place two nuts (use the locking feature explained earlier) and a washer on the rod and place the cap on. Adjust the nuts so that the magnet would pass over the reed switch when shown attached above. Give yourself enough room on top side of the cap to place two more nuts on and cut the rod with a hacksaw. Be careful not to mess up the threads. (Too much). Next use hot glue to attach the reed switch and wire as shown. Almost Done!



Finishing up, drill a hole in the side of the “T” so the wire can go back inside. Hot glue around the wire to keep it still. You can use 1″ PVC to connect it to the other parts of the weather station. (as shown in the following picture..) Place the cap on the underside of the “T” to keep things out. Here is a finished version on the desk and attached to the “mast”. I will post the python (and now Trinket as I’ll go into later) code that use later.



Wind Direction Sensor

This is one of the easiest sensors I’ve built. Basically it is a 10k 360 degree potentiometer with a wind vane attached to the top. I set North equal to 0 resistance and then record the values through testing at the other directions and then split the difference between them. (Will explain this more when I do the coding part)



Here are the two main parts to the sensor. A Bourns 6639S-1-103 10k rotary potentiometer and a Davis instruments wind vane with brass tip for Vantage Pro2 anemometer. You’ll also need some cat3 or cat 5 cable, a 1 1/4″ to 1″ PVC “T” and two 1 1/4″ PVC plug.


First you want to drill a hole in the middle of the PVC plug for the pot to go through. (Make sure you can get all the threads through the hole).


Before you tighten the pot into the hole, you’ll need to solder some wires to the bottom of the pot. For the Raspberry Pi, you only need one on peg 1 and one on peg 2 (Power on peg 1 & read on peg 2). For Arduino, You’ll need to attach a ground to peg 3 of the pot. Make sure and use some shrink wrap to protect the joints.


Once your soldering is done, tighten down the pot into the hole and slip your wire through the “T”. Make sure you mark “N” on your plug where 0 resistance is read after the pot is tightened down. Keep your pot adjusted to this position. Slide the plug into the T. (Place another 1 1/4″ plug in the other side).


Finally, you’ll need to attach the vane. The vane has a small Allen screw in it. You’ll need to loosen it up before you slide the vain onto the post of the pot. Adjust the vane so it’s just barely off the plug, point it towards “N” that you marked on the plug and tighten it down.

As far as wiring this sensor, the wire soldered to peg 1 should go to +v, the wire soldered to peg 2 should go to either an read spot on your ADC (if you’re using a Pi, something like the MCP3008) or to an analog read pin on your Arduino, and the wire on peg 3 should go to ground (if it’s connected)

I’ll share the reading code part later when I can get on my main computer at home.

Wind Direction Reading

As I stated in the previous post, the readings of this sensor take a lot of work to setup. You have to get a reading at each direction and then split the difference between them to get a high and low. Somewhere I have an excel file that I use to do all the calculations and print out the code. I’ll load it here if/when I find it.

To use this, you will need to place an include in your python program:

from wind_dir import wind_dir

Then when you want to get the value, you call the function with your ADC reading:

wind_direction = wind_dir(wind_dir_read)

The annoying part of this is setting up this file. All it does is take your reading and see where it falls within the table of max & min values established through testing:

#!/usr/bin/env python


def wind_dir(amt):
if amt <= wind_dir_high_NNE and amt >= wind_dir_low_NNE:
wind_direction = 'NNE'
elif amt <= wind_dir_high_NE and amt >= wind_dir_low_NE:
wind_direction = 'NE'
elif amt <= wind_dir_high_ENE and amt >= wind_dir_low_ENE:
wind_direction = 'ENE'
elif amt <= wind_dir_high_E and amt >= wind_dir_low_E:
wind_direction = 'E'
elif amt <= wind_dir_high_ESE and amt >= wind_dir_low_ESE:
wind_direction = 'ESE'
elif amt <= wind_dir_high_SE and amt >= wind_dir_low_SE:
wind_direction = 'SE'
elif amt <= wind_dir_high_SSE and amt >= wind_dir_low_SSE:
wind_direction = 'SSE'
elif amt <= wind_dir_high_S and amt >= wind_dir_low_S:
wind_direction = 'S'
elif amt <= wind_dir_high_SSW and amt >= wind_dir_low_SSW:
wind_direction = 'SSW'
elif amt <= wind_dir_high_SW and amt >= wind_dir_low_SW:
wind_direction = 'SW'
elif amt <= wind_dir_high_WSW and amt >= wind_dir_low_WSW:
wind_direction = 'WSW'
elif amt <= wind_dir_high_W and amt >= wind_dir_low_W:
wind_direction = 'W'
elif amt <= wind_dir_high_WNW and amt >= wind_dir_low_WNW:
wind_direction = 'WNW'
elif amt <= wind_dir_high_NW and amt >= wind_dir_low_NW:
wind_direction = 'NW'
elif amt <= wind_dir_high_NNW and amt >= wind_dir_low_NNW:
wind_direction = 'NNW'
wind_direction = 'N'

return wind_direction

As you can see from the above, I get all the way down to the minor directions such as “NNE” & “ESE”, etc. You can change it to suit your needs.

Rain Gauge

The rain gauge to me was one of the easiest to do, mostly because I kind of cheated a bit. Since I didn’t think I could create a sensor for cheaper than I could buy one, I spent $20 and bought the Acu-Rite gauge at Wal-Mart and modified it to my needs.


To start off, you’ll need to take apart the “gauge” piece and set the wireless display aside. This is fairly easy, as the main cover and tipping part comes off fairly easy. Next, you’ll need to remove the screws under the battery compartment to remove the circuit board. There are two wires coming out of the circuit board that go to the reed switch within the gauge that need to be clipped.


The reed switch is set within the tipping part, so carefully pull it out and strip the wires. You’ll need to attach wires to each of these. I use some telephone cable/cat3. I usually start with attaching about 10 feet of wire just in case. When attaching, make sure you solder the wires and use shrink tube to protect the connection.


Next, carefully slide the reed switch back into the tipping piece. Real quick explanation of how this works….A reed switch “closes” when a magnetic field is near. So we run current through the reed switch and place a magnet in the tipping mechanism to pass over the reed switch when it tips. When rain fills up a side of the gauge it tips over and the magnet causes the reed switch to complete the circuit for a brief period, which we can count….


We then need to drill a hole in the battery compartment and in the bottom piece to allow the wire to go through. Make sure to put a little hot glue in the hole in the bottom to protect against the elements a bit.

For testing, plug the two wires into a breadboard, one needs to run to power and the other to one of the pins on the Raspberry Pi (I believe I use number 17). In order to count how many times the gauge tips, we have to use GPIO interrupts. Before I forget, each tip is .02 inches of rain….

To keep count, I use a script that runs when the Pi starts up and monitors that pin. When the gauge tips and it detects a voltage “spike” it opens a file, takes the number, adds 1 to it and then rewrites the file with that number. (When I run the script that gets the current weather, I access this file and multiply by .02) At midnight every night, I also run a script that resets the number in the file to 0.

Here is the file I use to monitor the pin…

#!/usr/bin/env python2.7

import time
import os
import RPi.GPIO as GPIO

First bit is to set up the normal stuff…I have to give some credit to Alex Eames @ for the pieces of interrupt code.

# GPIO 17 set up as input. It is pulled up to stop false signals
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

Next we need to set up the pin for interrupts. We’re setting up pin 17 as an input and using an internal resistor to pull it down to 0 when there is no current.

def my_callback(channel):
tfile = open("/home/pi/rain_count.txt")
text = tfile.readline()

rain_count = int(text)
rain_count += 1
tfile = open("/home/pi/rain_count.txt", "w")


Now we create the function that gets called when a “spike” is detected. This function opens the file called “rain_count.txt” and reads the number, adds one and then writes the new number to the file. When doing this, don’t forget to create the rain_count.txt file prior to running this and place a 0 in it.

GPIO.add_event_detect(17, GPIO.RISING, callback=my_callback, bouncetime=300)

Now we add the interrupt event that monitors pin 17 and says that we want to monitor it for a rising event (I call a spike). When it is detected it calls the function “my_callback” and then waits 300 milliseconds before it will try to detect another one. (The bounce time is necessary to keep from getting false positives.)

while True:

The last bit is just an infinite loop to keep the script running. To run this at startup of the Pi, you’ll need to add a line to the crontab file:

@reboot python /home/pi/ &

The clearing file is fairly easy too….
#!/usr/bin/env python2.7
import time
import os

tfile = open("/home/pi/rain_count.txt", "w")
rain_count = 0

And add this to your crontab file to run at midnight…
* 0 * * * /home/pi/

The last bit is grabbing the count when you run the the weather script…
#get rain amount
rain_file = open("/home/pi/rain_count.txt")
rain_gauge = rain_file.readline()

rain_count = int(rain_gauge)

rain_count =float(rain_count) * .02

rain_count = round(rain_count, 1)

The only thing I’ll mention here since it’s pretty straight forward is that you’ll notice I do a round at the end. I’ve experienced some false positives during the day when it’s sunny (usually 1 or 2 a day), so I just get rid of them by rounding to the nearest 10th.

To start off, I wanted to say that I know there are other sensors that can do what the sensors that I use can do and possibly even do it better, but I’ve had these two sensors for several years now and they have done exactly what I’ve needed them to without fail.

For temperature I use a DS18B20 and for humidity I use a Honeywell HIH-5031.  The DS18B20 is a very popular temperature sensor which uses the 1 wire protocol.  the HIH-5031 is an analog humidity sensor that runs on 3V.  The DS18B20 outputs a temp in C with the Arduino code, but the HIH-5031 only outputs a voltage which requires some code to translate into humidity.  I’ll post all of that later when I post about the code I use on the Raspberry Pi.

Sensor Wiring:

For wiring these sensors, each has 3 pins, a power, ground & read pin.  Because each sensor really needs to be exposed to air to reach correctly, I soldered several single wires to their pins and then used shrink tube to cover the connections and then another piece of shrink tube to collect the 3 wires together and cover a bit of the end of the sensor.  (You can see this in the pic above)  The two sets of 3 wires are brought together into a single cat 3 cable (4 wires).  I wire the power of each sensor to a red wire, the ground of each sensor to a black wire, then the read pins of each sensor to it’s own wire.  This allows me to be able to have only 4 wires in the wiring harness for these two sensors instead of 6.


Sensor Mounting:

As you can see above, I use an offshoot of my main mast to hold these two sensors.  The sensors themselves sit within a piece of 1 inch PVC that has holes drilled in it to allow for air flow.  This is covered with a 3″ PVC pipe that protects from solar radiation as well as rain.  I don’t have many of the details on all the PVC connections I used to make this happen, but all I did was go to the hardware store and start piecing things together to get what I wanted.

In a previous post I mentioned that I would make a separate post regarding the micro controller wiring as well as the wiring harness.  Again, for the micro controller I am using an Arduino Pro Mini 3V.  Link to code  Below are a few pics and wiring details:

Microcontroller Pics:

A few pics of the micro controller wiring and waterproofing tube.  As you can see in the below pic, the micro controller sits within some 3/4″ PVC with caps on each end that have holes for the wires to come out of.  My suggestion would be to make the “top” hole waterproof by putting silicon or something around the cat 5 cable coming out and leave the bottom one free to slide in case you need to get to your micro controller for repair reasons.

Micro Controller Wiring:

I’ll include the micro controller pin as well as the wire color and “direction” of wire in this format: Pin on Pro Mini – Wire/direction – (wire color.)  (Some will have multiple wires attached)

  • Raw – 5V from RPi – (blue)
  • GND – GND from RPi – (blue-white)
  • A4 – SDA from RPi – (green)
  • A5 – SCL from RPi – (green-white)
  • Vcc
    • Power to wiring harness – (brown)
    • 4.7k Ω resistor for DS18B20 to Pin 10
  • GND
    • Ground to wiring harness – (orange)
    • Ground to wiring harness – (brown-white)
    • 65k Ω resistor for HIH-5031 to Pin A2
  • A1 – read pin for wind vane in wiring harness – (green)
  • A2
    • read pin for HIH-5031 in wiring harness – (orange-white)
    • 65k Ω resistor for HIH-5031 to GND
  • D2 – interrupt for rain gauge in wiring harness – (blue-white)
  • D3 – interrupt for anemometer in wiring harness – (blue)
  • D10
    • read pin for DS18B20 in wiring harness – (green-white)
    • 4.7k Ω resistor for DS18B20 to Vcc

Wiring Harness Pics & Wiring:

The wiring harness is where all the wires from the sensors come together with the wire coming from the Pro Mini.  I solder the wires together and use shrink tube to protect them, but I ran out of shrink tube and had to use these other connectors…don’t use these connectors.  It’s a mess…

For the wiring, I’ll list the wire color coming out of the micro controller and what wires are attached to it from the sensors.

  • Brown-white
    • GND for rain gauge
    • GND for anemometer
  • Brown
    • These two are tied together and I’ll explain in another post
      • 3V for DS1B20
      • 3V for HIH-5031
    • 3V for wind vane
  • Blue-white
    • 3V for rain gauge
  • Blue
    • 3V for anemometer
  • Green-white
    • read pin for DS18B20
  • Green
    • read pin for wind vane
  • Orange-white
    • read pin for HIH-5031
  • Orange
    • These two are tied together and I’ll explain in another post
      • GND for DS1B20
      • GND for HIH-5031
    • GND for wind vane


In my original version of my weather station, the enclosure sat inside my greenhouse and was protected from the elements as well as being powered by electricity coming from the house.  I’ve since gotten rid of the greenhouse and attempted to use a rubbermaid enclosure and purchased some solar power equipment to make it wireless.  I had issues with the battery not lasting long in the winter time due to the reduced sunlight as well as the rubbermaid could not handle the elements.  This has led to me not having my weather station for several years now.  With a renewed interest, I am attempting a new enclosure and adding battery monitoring. I’ll be listing the parts that I’m using as well as linking to where you can purchase them.


Power Equipment:

Electronics & Sensors

  • Raspberry Pi Zero W
  • MCP 3008 ADC – could use something smaller, I just had this so I used it
  • BMP180 Pressure Sensor – this has been discontinued, but linking to a newer version that should work
  • MOD-1016 Lightning Sensor – I really want this sensor to work correctly, but I’m still ironing out the details

Mounting Hardware:

As you can see above, the cable gland is placed on one side of the junction box, the solar panel requires a hole in the top to run the wire, but I use velcro to attach. (Have not placed the velcro on the lid of the junction box yet)  The electronics are mounted to a piece of the clear acrylic sheet using the nylon standoffs arranged in a way that everything fits nicely.  The battery sits underneath the acrylic sheet.

There are some wires that come from the different hardware pieces that I wanted to call out now and will discuss more in the next post regarding the perma-proto board and the sensor communication with the Raspberry Pi.  As you can see there is a cat 5 cable that runs from the cable gland to the perma-proto board.  This is the communication from the mast.  There are also two wires that come from the power boost to the perma-proto board.  One is from the low battery indicator pin, and the other is the ground pin.

Solar Charging circuit:

Just a quick explanation of how this is wired.

  1. Solar panel plugs into the USB/DC/Solar Li/LiPO charger as well as the battery into the “BATT” terminal
  2. Using a 2 female JST cable from “LOAD” on the Li/LIPO charger to the black JST connector on the Powerboost 1000 Basic
  3. USB A to USB micro from the Powerboost 1000 to the raspberry pi.  I used a small 6″ cable.



With this new enclosure, I have created a new sensor board that brings all my sensors together in one location as well as setting up 10 pins on the board for bringing together like wires for communication back to the Pi.  Due to the need of the mast needing 5V, I am using 2 separate voltages on the opposite rails.  The rail on the left is 5v and the one on the right is 3.3v

There are 3 sections that I’m going to cover:

  • Battery Circuit – for monitoring of the battery charge
  • Sensors – The wiring of the sensors within the enclosure as well as the communication back from the mast
  • Sensor board to RPi communication

Battery Circuit Wiring:

  • GND from Powerboost to GND on board
  • Low Battery indicator from Power boost to voltage divider on board.  Resistors: R1 = 2.2kΩ, R2 = 5.1kΩ (If you’re not familiar with how this works, LBI goes to rail containing one side of R1, Other side of R1 goes to rail that contains one side of R2 as well as read wire.  Other side of R2 goes to ground.  This is used to cut down voltages.  The powerboost is outputting 5V, but the MCP30008 ADC can only handle 3.3v which is why the divider circuit)
  • Voltage divider read wire to CH0 on MCP3008
  • MCP3008 wiring
    • Vdd & Vref to pin 10 on board (3.3V power rail)
    • AGND & DGND to pin 9 on board (3.3v ground rail)
    • CLK to pin 5 on board
    • Dout to pin 6 on board
    • Din to pin 7 on board
    • CS to pin 8 on board

Sensor Wiring

  • BMP180
    • Vin to pin 1 on board (5V power rail)
    • GND to pin 0 on board (5V ground rail)
    • SCL to pin 3 on board
    • SDA to pin 2 on board
  • MOD-1016
    • Vcc to pin 1 on board (5V power rail)
    • IRQ to pin 4 on board (interrupt pin on RPi)
    • SCL to pin 3 on board
    • SDA/MOSI to pin 2 on board
    • GND to pin 0 on board (5V ground rail)
  • Mast
    • Blue-white to pin 0 on board (5V ground rail)
    • Blue to pin 1 on board (5V power rail)
    • Green-white to pin 3 on board
    • Green to pin 2 on board

Board pinout to Rasberry Pi

  • Pin 0 (5V GND) to pin 7 on Pi
  • Pin 1 (5V Power) to pin 4 on Pi
  • Pin 2 (SDA) to pin 3 on Pi
  • Pin 3 (SCL) to pin 5 on Pi
  • Pin4 (Int) to pin 11 on Pi (GPIO 17)
  • Pin 5 (SPI CLK) to pin 12 on Pi (GPIO 18)
  • Pin 6 (SPI OUT) to pin 16 on Pi (GPIO 23)
  • Pin 7 (SPI IN) to pin 18 on Pi (GPIO 24)
  • Pin 8 (SPI SC) to pin 22 on Pi (GPIO 25)
  • Pin 9 (3V GND) to pin 20 on Pi
  • Pin 10 (3V Power) to pin 17 on Pi


it will take a while before I post the Python code on the Pi.  I am still testing/working on the code for the battery monitoring.  Once I’m done, I’ll post it all here as well as on Github.