Posts from: November '10
The coolest project I've created. EVAR.
Gardening meets PIC assembly.
Where did it all start...:
Meet the Poinsettia. A photo-periodic plant, native in Mexico. Suitable for home, but it needs 14 hours of continuous darkness (starting from October), so it can "sense" winter is coming and trigger blossom.
And it looks good when it does:
Poinsettia meets the student-programmer day/night cycle:
Don't try to figure out any order or logic — there ain't any.
Poinsettia stays green, three years in a row. Since we rarely turn off the lighting here before midnight, the biological clock of the poor plant gets totally screwed up.
I've read from several places in the net, that this problem can be alleviated by placing a large box over the flower in the late afternoon, and lifting it up in the morning. It just needs 14 hours of "night".
However, this needs manual intervention and it's not my taste, really. Waking up early is a no-no for me.
Meet the PIC microcontroller:
Capable of running all kinds of peripherals and electronics. I got the PICKit 2 starter kit - a programmer, a PIC16F690 chip, a Low-pin count demo board, and some software to learn&use all this.
Applying some soldering, relays, wiring...:
We can now drive a DC motor back and forth. Yay.
Meccano building kit:
Generic metal building set. Unleash your imagination. For starters, we did one of the models in the book:
Let's build a crane:
Pure Assembly. Everything else is shit.
A box... using plain wires:
Since a cardboard box that large (32х38х38cm) would be too heavy - usually over 500 g - I built a cage using wires, and wrapped it up in black nylon - the whole box is less than 100 g!
Mounting the crane:
Works surprisingly well. Designed for up to 7 Richter (like, really!)
So finally, the Thing is completed! Run for your lives!
Well. So you are probably a bit confused over here. The idea is like:
The PIC is programmed to run busy loop, counting the time that way. Turns out to be fairly accurate.
At 5pm, the "box" is moved down, so it covers the flower and provides darkness.
At 7am, it is lifted back up.
See this clip of the whole thing in action: Youtube
This son of a bitch is really working. Two weeks now.
On a side thought, I really learned one thing while working on this project: if you set your goals too high, and pursue them by any means that are out of your technical reach, you're going to fail. You really need to keep focused on the important things. It is vital to define your target correctly, and be ready to adjust your standards of success lower and lower as the project wears on. Some ideas aren't really worth pursuing. Consider:
* Being battery-powered (I reviewed all kinds of power sources, 4.5 volts, 9 volts, 4xAA rechargeable, even a LM7805 combo came to my mind...) (didn't need any of that after all. The Thing is inside a fucking house! You can really use a 5V adapter. It's lame. But it works™.
* Detecting the movement endpoints - since I had this current-sensing circuitry anyway, I could've sensed when, e.g., while moving the box up, it comes to halt at the top point, as the current through the motor increases sharply. I had even a more wild idea for the downward movement. Here it is: move the box, in a timed fashion, a bit more than it should be needed. Then start to pick it up. When the slack of the rope is consumed and it starts to pull the box, the current will rise, so stop here. (Of course, got rid of that idea, too. This is far too buggy and fragile. Calibrated timing is all you need.)
* Keeping track of time (throughout the day) - I've came up with all sorts of ideas. For example, I could've used an precise timer, which would trigger interrupts on the PIC. The interrupts would wake the microcontroller from sleep (and sleeping is for... conserving the power from the battery - see point 1).
* Don't use the internal clock of the PIC - use an external quartz resonator instead...
* Various battery efficiency ideas, e.g. - don't run the PIC at 4 MHz; use lower speeds. 32 kHz is sufficient, and the PIC is far more efficient that way.
* All other kinds of crazy ideas, basically avoiding to keep track of the time with the PIC.
(You'd wonder why was all that fear. Well, the datasheet says that the PIC achieves its best clock accuracy, "only" 1% of rated MHz, when we assume room temperature and 5V power supply. But, I thought, even there, the error is huge - we're talking about ~15 minutes per day! Well, with some calibration, the PIC becomes fairly good at tracking time. My current performance is being merely 10 seconds too slow per day, and I can calibrate it better if I want to. Anyway, the plant doesn't care.)
I did implement a few debugging things though. For example, I can trigger the movement, up or down, by holding the button of the demo board for a while - the type of operation being determined by the amount of time I kept it pressed. Another function I coded is that I can ask, "PIC, tell me what time is it", and it would show me. In 4-bit binary. Minutes are displayed like this: first, it prints M div 5, then M mod 5. Another thing that kept me busy for a while was an actual hardware bug that developed out of nothing, some time after I finished my first soldering two years ago. The bug was that the motor refused to turn in one of the directions, and the relays clicked wrong during that. More precisely, one of them wasn't clicking at all. When you touch the pins at some specific place of the board, it suddenly works all right again. Of course, it turned out to be bad soldering. There are no miracles in electrical engineering - only weak joints. But you should've seen what it is like to debug hardware. You still think that text-based gdb debugging is painful, ugly and obsolete? Meh.
P.S. I uploaded the source code of this thing: /code/my_projects/PIC.