Archive for the attiny Category

Beehive scale build details. finally

Posted in attiny, avr, electronics, i make things on September 15, 2011 by makingthingswork

I’ve put this off for a while, but here’s all the details!

Summary: I made (and you probably can too!) a scale that is readable via USB with better than 1lb resolution for less than $50. I have been using it to track the weight of my beehive, and year-to-date, they have produced over 150lb of honey!

Long version (pictures/schematics/code near the bottom):
A while back, I hacked together a bathroom scale, instrumentation amp, and attiny85 to make a device that could weigh my beehive and report how much it weighed. It taught me a lot about electronics and bees, so i thought i’d share the details with everyone.

Surprisingly it was very inexpensive ($40-$50 US) for the parts, and has been working well for 9 months or so.

The project works as follows:

* An attiny85 (8 pin) is the brains of the operation, and uses the V-usb project to emulate USB. This is the interface to the computer. ~$3
* A standard bathroom scale is taken apart and wired up to the electronics, and placed under the hive. ~$25
* A 15′ usb cable has the end cut off of it, and is wired to the attiny. ~$5 (this is connected to the PC, for power and scale readings)
* A small fet used as a low-side switch turns the scale/amp on and off, to prevent it from warming up (probably not necessary), but also saves power ~$1
* an instrumentation amp (INA136 or INA138 i believe) amplifies the signal from the scale to the level where it can be read by the 10 bit ADC on the attiny. ~$6
* a fixed voltage divider (potentiometer) provides a reference for error compensation, and feeds into a different ADC channel ~$1
* when a read is requested, the attiny turns the fet on, waits a few seconds, then reads the scale and the voltage reference 32 times, and stores that data
* later, the results are read by the PC and graphed.

Weighing setup:
The scale is just a standard bathroom scale, which i removed the brains from and am using only the analog electronics… basically just the strain gauges and their attached wires.

The scale has a wiring harness which connects all the strain gauges, and terminates in 4 wires: E+, E-, S+, S-. E stands for ‘excitation’, and S stands for ‘sense’.

Apply a voltage difference across E+ and E- (eg 3V, 5V, or 10V), then read the voltage difference between S+ and S-. It’s very basic and quite simple. The main problem to solves is that the maximum difference between S+ and S- is about 6 mV, and needs to be amplified several hundred times to be usable on a 10-bit adc. The scale I got (cheapie on eBay) happened to report 6mV at the maximum reading, and go all the way down to 0mV as weight was added. I happened to get a little more resolution out of the scale by turning the gain up on the amp so that it read max ADC voltage at around 60lbs. Since the beehive will always weigh over 60lbs, this won’t impact my data at all and i get better resolution out of it.

Read some my other, older blog posts for more info, but i had to spend a long time calibrating the scale. the system has a /very/ strong temperature coefficient. I probably spend 5 weeks with the scale in the garage with between 45 and 90 lbs on it, trying to figure out how to get it to produce a flat reading.

In the end, I determined that the voltage swings were primarily due to the attiny, and by setting up a voltage divider to produce a fixed voltage, i was able to compensate for the fluctuations. With the current setup, i am able to get readings at about +/- .5 lb of accuracy, even though the resolution is much better than that. For the purposes of beekeeping, this is just fine. My next version will be much better, I hope :)

There are a few critical things i will do differently for my next scale (currently under design)
* get a really clean voltage source. The current model uses the 5v signal from the USB bus for power. This is a noisy signal, and may fluctuate with temperature (or it may be the voltage reference on the attiny). Any voltage fluctuation will alter the reading on the scale somewhat.
* I am also using the internal voltage reference on the attiny. This does not seem to be a good idea.
* don’t use the voltage reference or ADC on the attiny. It’s not good enough for precision work, at least in my experience. It also seems to have an affinity for even-numbered readings (eg binary numbers ending in 0).. so call it a 9bit ADC :)
* use an ADC with a low thermal coefficient, and/or built in voltage reference. I have been experimenting with a purpose-built chip from TI which is made to be a strain gauge ADC. It is 24bit and costs about the same as the instrumentation amp i used. So far it is working very well. This simplifies things somewhat.

Eagle files:
scale-no-vreg-2.sch
scale-no-vreg-2.brd

Code
hostcode.tar.gz libusb interface
tinycode.tar.gz attiny code

Some pictures:
Raw scale, showing strain gauges. Why yes, that is an old macbook power supply case

Ready for outdoor use

Scale is the yellow thing at the bottom of the hive.. sorry the picture is sideways

.. and some graphs:

Nice day in spring.. 5lb of weight (nectar/honey) gained
They made about 5lb of Honey that day

They swarmed.. 7lb of bees went out, 5lb of which were later recaptured

As of may, here’s a cleaned up graph of overall honey production

More error correction, aka the attiny reference voltage sucks.

Posted in attiny, avr, electronics on February 9, 2011 by makingthingswork

After much curve fitting and data analysis, I decided that the fluctuations in the readings were too much, and i had to find another way of fixing things.

I had previously made up my mind that the attiny board i made was the source of the error, not the scale itself. After working on it one evening, I saw the readings move quickly, then stabilize after i put the unit back in the garage (in the cold) from the nice warm house. The scale stayed out there the whole time.

So i figured it was the resistors changing and affecting the gain on the amplifier.

I ran some calculations and determined that the gain should not have changed anywhere near what it did in reaction to the temperature fluctuations, and the changes in resistance that might bring. So that leaves one thing: the ADC unit on the attiny85 itself.

I have been using the 1.1V reference voltage on the attiny as the basis for the readings. It’s a ‘bandgap’ based thing, which sounds fancy and accurate, but let’s give it a test. To test it, i got a 5k potentiometer and put the end pins on +5v and GND, and the wiper was set so it read about 1 volt. I connected the 1volt output to a spare pin on the attiny, and started taking readings to see if it would exhibit the same pattern as the scale.

About a day later, i’m pleased to report that the constant voltage varies EXACTLY the same way the scale does.

Here’s a picture:

After some analysis and curve-fitting, the coefficient is -1.0, which means that the error in the voltage reading is exactly the same as the scale reading, just opposite (which is to be expected, since i am storing the scale reading as 1024 – raw_reading). So it’s basically a matter of adding them together then subtracting an offset.

As you can see, it works really well, and the total range is just 2 ticks!

Also note that I was able to use a potentiometer (could easily have been 2 resistors) as a MUCH more effective temperature sensor than the internal temperature sensor of the attiny itself. That is somewhat sad.

For extra accuracy, i may someday try using an external VREF input, and see if it’s the ADC itself or if it’s the bandgap voltage moving around. For now, this is good enough.