Gemma Neopixel Jewel Earrings

I like projects! I NEED projects!

I’ve wanted to do something in the wearables category for a while, but couldn’t think of something. I was out at the mall one evening with my beautiful wife looking through some of the department stores. I decided to call my sister-in-law (for a reason I can’t remember right now) and the idea popped into my head that I should make her some of the neopixel earrings I’ve seen in the Adafruit Learning System. So I told her I was going to make them and when I got home I went onto Adafruit’s site to purchase what I would need.

The original plans for the earrings include neopixel rings, but I noticed that they had just released a new neopixel “platform” called the “Jewel”. This has 7 neopixels arranged in a circle with 1 more in the middle. I thought these would be more fun to have than the rings, so they were added instead. The only other parts you need are a tiny lipo battery and of course an Adafruit Gemma. (Don’t forget the charger for the batteries as well if you don’t have one)

I feel bad that I didn’t take pictures of the entire process, but it’s really, really simple. The first thing I did was shorten the battery leads. There’s no need for all that length of wire. After that, I wired the Jewel to the Gemma with silicon wire with enough room to slide the battery between the 2. (They are back to back.) 3vo on the Gemma to +5V on the Jewel, Ground to Ground & D0 on the Gemma to input on the Jewel.

To keep them together, I did a no-no and used hot glue. You slide the battery in position and make sure it all will align well (I aligned one of the mounting holes on the Jewel to D1 on the Gemma). Slightly lift the battery on one side so you have room to place the hot glue on the back side of the Gemma, then press the battery down into place. Repeat the same process with the Jewel on the battery making sure it’s aligned.

For the code, I took what was given on the Adafruit tutorial for the ring earrings and modified it to only sparkle and to sparkle specifically in teal (my sister-in-law’s favorite color).

// Low power NeoPixel earrings example. Makes a nice blinky display
// with just a few LEDs on at any time…uses MUCH less juice than
// rainbow display!


#define PIN 0

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(7, PIN);

uint8_t mode = 0, // Current animation effect
offset = 0; // Position of spinny eyes
uint32_t color = 0xffae00; // Start red
uint32_t prevTime;

void setup() {
pixels.setBrightness(60); // 1/3 brightness
prevTime = millis();

void loop() {
uint8_t i;
uint32_t t;

switch(mode) {

case 0: // Random sparks – just one LED on at a time!
i = random(7);
pixels.setPixelColor(i, pixels.Color(0,128,128));;
pixels.setPixelColor(i, 0);



Add some earring pieces from Walmart and they should sparkle like these:

Youtube Video

I2C sensors using Adafruit Trinket

I finally completed migrating the weather sensors for the weather station that is at my father-in-law’s farm. I had issues with the rain gauge not working too well with the pi directly, so I decided to use an Adafruit Trinket to capture the values of the sensors and send them to the pi for tracking. The communication between the two is accomplished using I2C.


Here’s the completed mast with sensors attached. (This is actually a pic of it at my house for testing) What you’ll need for this is 1 of each of an anemometer, wind vane & rain gauge. It works with the sensors I built, but I’m sure you could incorporate it to use any sensors you have as well.

I’ve used PVC to build all my sensors as it’s easy to connect together. In my anemometer & wind vane posts I have used certain PVC pieces to allow for connection to my mast. For the rain gauge, it’s a little different. I used a piece of cedar fence board to secure the sensor, then some u-bolts to attach the board to a 1 inch pvc pipe, plugged one end and drilled a hole for the wire to run through.

For the rest of the top part of the mast I use a 1″ T-connector at the top with about 8 or so inches of 1″ pipe to connect the anemometer & wind vane. I run the wires down through the pipes. I then use a small piece of 1 inch pipe (about 3 or so inches) to connect another T-connector for the anemometer. I then use another portion of 3 or so inches of 1″ pvc pipe to connect to the lower portion. (Sorry, but I don’t have any pics of this…)

The lower mast is where the trinket comes in…I connect all the sensor wires to a single cat 5 cable to connect to trinket and then another wire for the I2C communication. The idea is to house the trinket in some 3/4″ pipe that is waterproofed and have it sit inside some 1 1/2″ pipe. (You can see this bulge in the pic above) I use some connectors to move from 1″ to 1 1/2″ pipe. Then a portion of 1 1/2″ pipe (I think it’s 12 inches long) and then connect it back to another 1″ piece of pipe to mount it.

Before wiring up your trinket, make sure you upload the code to it. Here is what I use which tracks the sensors and sends the data via I2C:

#include avr/interrupt.h
#include TinyWireS.h

// setup cbi & sbi for interrupts
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

//Define address of device
#define I2C_SLAVE_ADDRESS 0x04

// Set up registers to hold data to send
volatile uint8_t reg[] =


// Set up variables used

volatile byte reg_position; // keep track of reg position sent from master
volatile int rainGauge = 0; // keep track of interrupts from rain gauge
volatile int windSpeed = 0; // keep track of interrupts from anemometer
volatile int windDirection = 0; // keep track of value of wind direction sensor
volatile int windSpeedSend = 0; // used to send 10 second interrupt value to pi
long debouncing_time_rain = 300; //Debouncing Time in Milliseconds for rain gauge
volatile unsigned long last_micros_rain; // micros since last rain gauge interrput
long debouncing_time_wind = 100; //Debouncing Time in Milliseconds for anemometer
volatile unsigned long last_micros_wind; // micros since last anemometer interrupt
volatile int val1 = 0; // pin 1 value at interrupt
volatile int val2 = 0; // pin 4 value at interrupt

// Function to send data when it is requested. Sends 2 register positions
// for each request
void onRequest()

// Load all values into registers. (even though only 2 will go to master)
reg[0] = lowByte(windDirection);
reg[1] = highByte(windDirection);
reg[2] = lowByte(windSpeedSend);
reg[3] = highByte(windSpeedSend);
reg[4] = lowByte(rainGauge);
reg[5] = highByte(rainGauge);

// send 2 reg positions to master

// Function to set which register positions you wish to receive
void receiveEvent(uint8_t howMany)
reg_position = TinyWireS.receive();

// if requested position is 7, reset rain gauge variable. Will be sent at midnight each day
if(reg_position == 7) {
rainGauge = 0;


// Setup for connection
void setup()

sbi(GIMSK,PCIE); // Turn on Pin Change interrupt
sbi(PCMSK,PCINT1); // Which pins are affected by the interrupt. Turn on Pin 1
sbi(PCMSK,PCINT4); // Which pins are affected by the interrupt. Turn on Pin 2

void loop()

// anemometer counts interrupts for 10 second interval. This is sent to master for calculation
windSpeed = 0;
windSpeedSend = windSpeed;

// read pin 3 analog value of wind direction sensor
windDirection = analogRead(3);

// Check for connection to be stopped.

// debounce rain gauge. make sure new reading is greater than old reading + debounce rain micros
void debounceRainGauge() {
if((long)(micros() – last_micros_rain) >= debouncing_time_rain * 1000) {
last_micros_rain = micros();

// debounce anemometer. make sure new reading is greater than old reading + debounce wind micros
void debounceWindSpeed() {
if((long)(micros() – last_micros_wind) >= debouncing_time_wind * 1000) {
last_micros_wind = micros();

// function to increment rain gauge count
void rainGaugeFunc() {
rainGauge = rainGauge + 1;
} // end of rainGauge

// function to increment anemometer count
void windSpeedFunc() {
windSpeed = windSpeed + 1;
} // end of rainGauge

// interrupt service routine. What to do when an interrupt occurs as all pin change interrupts
// on ATTiny85 trigger PCINT0 vector.
ISR(PCINT0_vect) {
// get value of pin 1 @ interrupt
val1 = digitalRead(1);
// get value of pin 4 @ interrupt
val2 = digitalRead(4);

// if pin 1 is HIGH (caused the interrupt), proceed with rain gauge increment functions
if (val1 == HIGH) {

// if pin 4 is HIGH (caused the interrupt), proceed with wind speed (anemomether) increment function
if (val2 == HIGH) {


So, now to the wiring:

Here is the wiring diagram I drew out that shows how everything is connected. When connecting, make sure you use solder and shrink tube to protect the joints.


After all the wires are connected, drill a hole in a 3/4″ pvc plug and push the wire through that hole prior to connecting to the trinket. You may want to run it through a piece of 3/4″ pipe as well (about 8 or so inches).

All 3 sensors will need 1 wire connected to the +3v on the sensor. I managed to get all 3 of the cat 5 wires into the hole on the trinket. The read wires will need to go different pins. For the anemometer and rain gauge, you will need to connect a 10k resistor to ground to pull the pin to ground. The rain gauge read wire should go to pin 1 and the read side of the anemometer should go to pin 4. The read wire for the wind vane should go to pin 3. Connecting all the ground wires was fun. What I did was run 1 wire out of the trinket and connected all the ground wires to it and put shrink tube over all of them. (You can see it in the pics below.)



I didn’t take a lot of single step pics on this and I apologize. You can see the connections for the I2C wire as well in these pics. The last part is connecting the wires for the I2C communications. You’ll want to put the wire through hole in another 3/4″ pvc plug before you connect them to the trinket to allow for waterproofing. Connect the +v to the battery pin (I use 5 volts out of my raspberry pi to power, but you can power it any way you want. I used a 3 volt trinket for this setup). Ground to your ground wire, SDA to pin 0 and SCL to pin 2. Once this is done, slide it all into the 3/4″ pvc and plug the other side. It should look like this:


Place some hot glue on each plug where the wires go in to keep the water out, then shove this into the 1 1/2″ pvc pipe and then connect the rest of your mast.

This is really set up to communicate with a pi, but I’m pretty sure it would work with an Arduino as well. In the code you basically send the trinket a number and it will give you the response. Sending 0 will give you the wind direction reading, 2 will give you the wind speed reading and 4 will give you the rain gauge reading. Sending it 7 will cause the rain gauge count to reset. Here’s a quick bit of code you can use for the pi:

# Import needed libraries
import smbus
import time

# Set I2C address of device you wish to access
DEV_ADDR = 0x04

bus = smbus.SMBus(1)

# Request values from device. Number is start register position.
while True:
print 'Testing for connection...'
while True:
wind_dir_read = bus1.read_word_data(DEV_ADDR, 0)
wind_count = bus1.read_word_data(DEV_ADDR, 2)
rain_count = bus1.read_word_data(DEV_ADDR, 4)
except IOError:
print 'No connection...'
if wind_dir_read < 1030: break else: print 'Value too high.' time.sleep(5) # Use this to reset a variable: #d_val = bus.read_word_data(DEV_ADDR, 7)

It doesn't seem that wordpress knows how to deal with indents in it's code sections, so make sure you indent it properly. Sometimes the trinket can give you some funny business which I've handled via the error trapping above. If you keep this bit of code you shouldn't run into any problems.

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.

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.

Anemometer Reading

It was too late last night when I finished the post about building the anemometer that I didn’t want to get into the code to read the wind speed. As I said before, basically what I do is I count the number of “triggers” within a 10 second period and then feed that into the formula that came from testing it with our car.

Here is the code I use for Raspberry Pi…I run this every 5 minutes (with the rest of my measurements) to get the wind speed at that time.

# calculate windspeed
# set up the GPIO to be an input and activate the internal resistor to pull down the pin to low
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# This is the interrupt function. It opens a file, pulls the number from the file and adds one to it, then rewrites it to the file.
def wind_callback(channel):
windfile = open("/home/pi/wind_count.txt")
windtext = windfile.readline()

wind_count = int(windtext)
wind_count += 1
windfile = open("/home/pi/wind_count.txt", "w")


# Here is the interrupt setup. We are setting it up on pin 22, looking for it to rise, calling the function "wind_callback" when it is triggered and
# debouncing it by 100ms
GPIO.add_event_detect(22, GPIO.RISING, callback=wind_callback, bouncetime=100)
# Before we start the count, we open the file and set the number to 0
windfile_clear = open("/home/pi/wind_count.txt", "w")
wind_count_clear = 0

# now we sleep for 10 seconds to get the readings

# After sleeping, we open the file and read the number
wind_file = open("/home/pi/wind_count.txt")
wind_count = wind_file.readline()

# in order to use it in some math, we need to convert it to an int
wind_count_comp = int(wind_count)

# here is the formula created from testing. Basically it is .0023427x^2 + .46244x
windspeed_a = wind_count_comp * wind_count_comp * .0023427
windspeed_b = wind_count_comp * .46244
windspeed = windspeed_a + windspeed_b

# round it off to 1 decimal.
windspeed = round(windspeed, 1)

I am actually in the middle of a redesign on my weather station due to the Raspberry Pi not being the best at interrupts for the rain gauge. For the one at my father-in-laws farm, I will be using an Adafruit trinket to capture all the data from the anemometer, rain gauge, and wind direction sensor before relaying it to the Pi. This type of sensing is better for a micro controller than it is for a Linux computer such as the Pi. For my home weather station, I’ll be using a Arduino Pro Mini as I have a lot more sensors on my mast. The Trinket code is a little more difficult due to the ATTiny85 that runs it. I’ll post info on how to connect it to a Raspberry Pi later.

Here is the code for the same principal as above on a trinket. The big difference here is that this runs constantly. Every 10 seconds I have a wind speed reading, although I only pull it every 5 minutes.

#include avr/interrupt.h

// setup cbi & sbi for interrupts
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

// Set up variables used
volatile int windSpeed = 0; // keep track of interrupts from anemometer
volatile int windSpeedSend = 0; // used to send 10 second interrupt value to pi
long debouncing_time_wind = 100; //Debouncing Time in Milliseconds for anemometer
volatile unsigned long last_micros_wind; // micros since last anemometer interrupt
volatile int val2 = 0; // pin 4 value at interrupt

void setup()
// Setup Pins for interrupts

sbi(GIMSK,PCIE); // Turn on Pin Change interrupt
sbi(PCMSK,PCINT4); // Which pins are affected by the interrupt. Turn on Pin 2


void loop()

// anemometer counts interrupts for 10 second interval. This is sent to master for calculation
windSpeed = 0;
windSpeedSend = windSpeed;


// debounce anemometer. make sure new reading is greater than old reading + debounce wind micros
void debounceWindSpeed() {
if((long)(micros() - last_micros_wind) >= debouncing_time_wind * 1000) {
last_micros_wind = micros();

// function to increment anemometer count
void windSpeedFunc() {
windSpeed = windSpeed + 1;

// interrupt service routine. What to do when an interrupt occurs as all pin change interrupts
// on ATTiny85 trigger PCINT0 vector.
ISR(PCINT0_vect) {
// get value of pin 4 @ interrupt
val2 = digitalRead(4);

// if pin 4 is HIGH (caused the interrupt), proceed with wind speed (anemomether) increment function
if (val2 == HIGH) {



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.



When I finished building my weather station it was on a large full size breadboard. I didn’t like that it there was so much space on there that was just wasted and I had a half size sitting around that I thought would work well. Unfortunately, the ADC wouldn’t let everything else fit properly. I did some research and found that Microchip also produced a 2 channel ADC called the MCP3002. Since I was using only 2 channels on the MCP3008, I thought this would work nicely and I counted all the slots on the half size breadboard and it would fit perfectly! So I ordered a couple from Newark…

When they came in I had to test to make sure it was going to work, so I placed in the half size breadboard, wired up a TMP36 analog temp sensor, and connected the breadboard to my spare Pi. Well, come to find out, the driver I had didn’t work. Neither did the other one I found online nor the others I found. I was a bit depressed because I really wanted it to work.

My last hope laid on the Adafruit forums. (Love Adafruit by the way!) I was a bit worried as they don’t sell the MCP3002 and they have messages on there about only supporting their products. So, reluctantly I posted a question on the forum asking if the driver they provide for the MCP3008 would work for the MCP3002. At first their people informed me that it would work, but I told them I had tested it and it didn’t. In one of my responses I made sure that I mentioned that I had wired it as per the datasheet and added a link.

Come to find out, the reason their driver wouldn’t work is because the 3002 only wanted 4 bits sent to it for the request and the 3008 wanted 5. So, Rick (who I think works for Adafruit) helped with adapting that part of the driver. When I added his piece to the 3008 driver, it still didn’t work. I then took another look at the datasheet and found another difference in the 3008 vs the 3002. The 3002 only provided a 11 bit response vs the 12 that the 3008 provided. I then updated the code to account for that and voila! it worked like a charm.

After I got it working, I posted about having to make the other change. Rick then asked me to post the full driver so that it could be added to their python library. So yeah, that’s the story of writing my first driver…

Here it is by the way if you need it:

def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 1) or (adcnum < 0)): return -1 GPIO.output(cspin, True) GPIO.output(clockpin, False) # start clock low GPIO.output(cspin, False) # bring CS low commandout = adcnum << 1; commandout |= 0x0D # start bit + single-ended bit + MSBF bit commandout <<= 4 # we only need to send 4 bits here for i in range(4): if (commandout & 0x80): GPIO.output(mosipin, True) else: GPIO.output(mosipin, False) commandout <<= 1 GPIO.output(clockpin, True) GPIO.output(clockpin, False) adcout = 0 # read in one null bit and 10 ADC bits for i in range(11): GPIO.output(clockpin, True) GPIO.output(clockpin, False) adcout <<= 1 if (GPIO.input(misopin)): adcout |= 0x1 GPIO.output(cspin, True) adcout /= 2 # first bit is 'null' so drop it return adcout # change these as desired - they're the pins connected from the # SPI port on the ADC to the Cobbler. There are 2 Chip Select pins. # one for each ADC SPICLK = 18 SPIMISO = 23 SPIMOSI = 24 SPICS = 25 # set up the SPI interface pins GPIO.setup(SPIMOSI, GPIO.OUT) GPIO.setup(SPIMISO, GPIO.IN) GPIO.setup(SPICLK, GPIO.OUT) GPIO.setup(SPICS, GPIO.OUT) Read the rest of this entry

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.

Weather Station


I know it’s been a while since I posted something, I guess it was just getting difficult to find time to post something after completing a sensor before bed. My most recent project has been building my own weather station. This sort of came from the greenhouse automation project. Once I was able to measure humidity, that was it; I wanted to measure everything else about the weather that I could.

In building this, I feel I learned a lot about some other aspects of the raspberry pi, coding and some other sensors (reed switches specifically). In some places, I kind of took the easy route and took something someone else made and re-wired it to work for me, (rain gauge & wind direction pointer) but this still involved a lot of innovation on the coding side to use it. I plan to write other posts detailing the building of the other sensors; just plan to space them out some as some of them are pretty involved. I think eventually I might make a wireless self-contained version (wifi, batteries, & solar power), but that’ll be a while 😉

Oh…..forgot to mention the first time I published it… can see the current weather at my house by clicking on the “Current Weather” page on my blog…

Today, I put the cover on the greenhouse to get ready for the winter. I know it’s a couple weeks early (as I don’t think we have the first frost here in Texas until around mid to late November. This year, I decided to use zip ties instead of rope to attach all the pieces and I think it worked a lot better. For those who haven’t seen it before here is my little greenhouse with all my citrus trees…


I’ve recently came to the understanding that I probably don’t need my automatic watering system nearly as much in the winter time, so I’m not in a rush anymore to get it built. When I will need it though is next summer. Now the fact that I need it most when I don’t have the cover on the greenhouse means that I need to find a way to waterproof everything. I’ve been looking into waterproof boxes I can put the computer in and have some ideas, but one of the other things I’ll need are waterproof moisture/light sensors for the trees. I had a revelation the other day that I can probably use some PVC to accomplish this. When I was at Lowes picking up the zip ties for the greenhouse, I went to the the plumbing section and found my parts.


So, here it is, the new and improved waterproof (mostly. You can’t dip the whole thing in and expect it to survive. I haven’t really tested it’s waterproofness yet, but I believe it is)


What you’ll need (besides many of the parts from the previous sensor) are a 1 1/2″ plug & a 1 1/2″ cap. (These should fit into each other.) The plug has a flat end and the cap has a rounded end. At first I had a hard time finding something that would work. I had planned on using some 1″ pipe with caps on either end, but I just didn’t think that the cap would be the right shape. When I was about to give up, I went back through one more time and found these!!! I was pretty excited that I found this combo.


The first step to building this new sensor is to drill some holes in the cap and plug. You’ll need to drill a 7/32″ hole in the middle of both the cap and the plug. The plug also needs 2 1/8″ holes drilled on either side of the middle hole for the moisture sensor wire. The middle hole of the plug is for the cat 5 cable and the middle hole in the cap is for the light sensor.


Again like the previous sensor, you’ll need to use some 12 gauge wire (10″ of romex is what I use). Remove some of the insulation from both sides like before and put some solder (tin) the side that will go into the dirt. When this is complete you’ll need to make your wires look like the above picture. The way to do this is the put the wire through it’s hole and then press the end against the side of the plug and hold it there. While holding the inside, bend the outside. You’ll want the part of the wire that has the insulation removed to be above the end of the plug. (I think this is shown later).


After you have the wires bent to the correct shape, you’ll need to “attach” the probes to the plug. Put them through the hole and then place some hot glue at around the probe where it meets the hole.


Next, flip the plug over and put a good amount of glue in the plug to hold the wire in place. (You can see in this picture how I said to place the part of the wire with the insulation removed above the top of the plug.) Do this with both sides one side at a time.


Now we go to the wiring part. Just like in the previous sensor, we’ll need a LDR and a 10 ohm resistor. Green to one side of the LDR, blue and one side of the 10 ohm resistor to the other side of the LDR, and green/white to the other side of the 10 ohm resistor. Solidify this connections with some solder and cover them with some shrink tube. (Make sure when you remove the insulation from the cat 5 cable that you have a good amount to work with.)


The next step is really annoying. You need to place the LDR in the hole in the cap and secure it. Do do this, I pushed the LDR through the hole and used some hot glue to tack it down. (Wait for it to dry so you have some hold.) Once it’s try, turn it over and place a lot of hot glue into the cap. Let this dry, then fill in the holes around the LDR on the other side of the cap. (It took me 5 tries to get this right.


Almost finished. Lets solder the brown wire to the black probe and the brown/white wire to the white probe. Should be pretty easy if you gave yourself enough wire and placed the top of the probe above the top of the plug. After you finished this you can carefully press the plug inside the cap. Once together, hot glue around where the cat 5 cable went enters the plug.



Finally, just as last time, place the rj45 jack on the end. I use the above pattern even though it doesn’t match the “standard” ethernet pattern. I’ve had this sensor going all day now and it seems to be working fine. I’d like to make another one to have two to test. I’ll need to do this fairly quickly as it’s getting close to when I need to take the system outside and actually work…