Categories
Experimentation! Project Logs

Vegetable Garden

A few months ago I decided it would be quite fun to keep my own little vegetable garden in my spare time, and just grow a whole assortment of things that could be eaten. This idea quickly spiraled out of control into the monstrosity it is now… A pic micro-controlled, serially data-logged, moisture and temperature monitored automated growing area. xD

I started off wanting a simple automated irrigation system, since I often forget to water it! and when I do remember, I forget to go out and turn the water off which results in floods ๐Ÿ™ … I had a scout around the irrigation sites on the net and found that my best option for this garden was simple Microjets of which I could run 10-12 on residential pressure, I would also need some irrigation tubing, a few fittings and the most important part; an electronically controlled solenoid water valve allowing me to turn the water on and off electronically…

Off I went the the local irrigation wholesaler and acquired the needed materials including the valve for ~R300 total. The valve needs 24VAC to open, thus I would need a 24v AC transformer that I grabbed from work.

The valve, with a nice garden hose adapter.. ๐Ÿ˜€

The circuit: I decided the easiest way to switch the 24VAC would be with a relay as I have a couple in the misc parts box, I also decided I’d use a PIC micro-controller to open and close the relay and allow me to set timing values. I opted for a PIC16f88 mostly because I have some lying around but also because they are easy to use with their internal oscillators and multiple I/O pins. I also had some 7-segment LED displays that I have really wanted to play with for a while now so that was to be my interface, along with some tact switches.

Schematic of the controller in its present state.. (This is the first schematic I’ve made in Eagle so its not great…)

The transformer is a 12-0-12v transformer, so I took the center tap and one of the 12v wires and put it through a full-bridge rectifier that I salvaged out of the power supply of some other misc electronics that was broken (I collect peoples old electronics/computers to either try repair or reuse for parts ๐Ÿ™‚ ) I also used a big smoothing cap (25v 4700uf) over-kill but not a bad thing ๐Ÿ˜› then I put this through an lm7805 and another smaller smoothing cap to provide the PIC and other components with regulated 5V. The common & normally open pins of the relay are connected between one side of the valve and one of the 12v outputs of the transformer, the other side of the valve is connected to the other side of the transformer. The PIC closes the relay through a small NPN transistor that connects the relay to ground, the other side of the relays coil is connected to the ~12VDC available from the rectifier..

The code: originally the plan was simple; display the time in minutes between 0&9 on the 7-seg display, have an up and down tact switch to increase/decrease the time and an ‘activate’ tact switch to open the valve and activate the timer, then close the valve after the elapsed time. I set this all up initially on a bread-board and started programming the PIC which was tricky but alot of fun as I had to learn how to use the PIC’s internal timers to time relatively accurately.. I found the PIC data-sheet and This post invaluable to getting the timer working just right.

I managed to get everything working after a bit of troubleshooting, but I felt it was a bit underwhelming to use an entire PIC 16f88 to just read some switches control one output and some led’s and count down a few minutes, considering it has many other features like ADC’s, more timers, USART module etc… I also decided this would be a great opportunity to expand my knowlage of PIC’s..

I started by using the ADC to read the value of a MCP9701A temperature sensor that I had lying around (from back in the day when you could actually get FREE samples from Microchip… ๐Ÿ™ ) I also made a soil moisture sensor thanks toย  cheapvegetablegardener.com ๐Ÿ˜€ but what to do with this awesome new data we can read?

Send It to a PC of course! ๐Ÿ˜€ 8) since I recently learnt serial communication between a PC and a PIC I decided to create an nice monitoring system for the garden that sends data to the PC which is then logged into a SQL database and can be graphed etc. I also added functionality to set the time on the timer and activate/deactivate the pump. Eventually I’d like this to be controlled from a web-page but until I get around to implementing that I can still log in using remote desktop over the internet and water my garden! ๐Ÿ˜€ something I actually managed to do recently while I was in another city checking out a university I’m interested in attending, using my windows mobile phone ๐Ÿ˜€ was quite a cool feeling ๐Ÿ˜€

Just a very basic GUI for testing purposes, works thou! thats the important part ๐Ÿ™‚

I still need to do alot of coding to get it ‘right’ but for the most part it works ๐Ÿ˜€

After I managed to get everything working on the bread-board I used some perf board to assemble a prototype, It does the job but eventually I’ll make a proper PCB for it once I have all the code/features worked out how I like.. I used a chip socket so I can remove it for programming but on a proper PCB I’ll be able to implement ICSP properly..

Small project box I found to house it, will eventually stick it on the wall with a nice perspex cover..

I need to do some more work on the actual garden now, and make a better moisture sensor because the one I made was a bit.. erratic… and I need to do alot of work on the code, as well as hopefully get it controllable from a webpage, and possibly even use things like the moisture sensor data to water automatically at certain levels. I could even implement a watering schedule controlled by the PC and configurable over the internet! something that commercial irrigation controllers costing many thousands of rands dont offer… ๐Ÿ˜€

Anyways let me know what you think in the comments below! I’ll upload a video of it in action and the source code in another post soon once I’v cleaned it up a bit ๐Ÿ˜€ oh I’d also like to mention http://www.cheapvegetablegardener.com/ again, which is an awesome site and provided alot of inspiration for this project and my garden in general… why spend hours on farmville when you could spend those hours in a real garden of your own? so much more rewarding ๐Ÿ˜‰

~Rob.

Categories
Experimentation! Project Logs

LFS Dashboard Pt1

Well where to start on this one… I suppose my initial inspiration came from this post on HackADay, This lead me to the discovery of Live For Speed and my new gaming addiction! It wasnt long before I had a C# program connecting to the LFS out-gauge API and displaying tachometer info on a small LED display I hacked together on the LPT port… (I will detail this in another post some time ๐Ÿ™‚ ) But it wasn’t long before I decided to move onto, ah, bigger ideas ๐Ÿ˜›

The plan is to have a C# app connecting to LFS and sending data to a PIC micro through a USB-RS232 converter, and then have the PIC control a real instrument cluster! Initially I connected a pic up and wrote a lil C# app to test the theory and mannaged to get the pic to output a variable duty PWM signal dependent on the car’s RPM!! ๐Ÿ˜€ So onto step 2!

Yesterday I ordered a Logitech G27 to really get some realism going! This should be arriving in a week or two so that’s my time-frame to try get the instrument cluster finished. After ordering the wheel I set off into the depths of the Verulam scrap yards to try find a cheap workable gauge cluster, Initially everywhere was too expensive wanting R500-R700 for the cheapest 2ND HAND!! cluster… But just as I thought all hope was lost I pulled in to a place called “Mandos used spares” where I found this:

BMW Instrument Cluster…

At first they wanted R750 for item, but after some bargaining I managed to get it for R400! Unfortunately I didn’t know if or how easy it would be to use, but I kinda guessed Alex Rosiu was using a similar BMW cluster so how hard could it be!?

Immediately after getting home I located the part number and started a 3 hour long Google quest!:

Part Number: 6932894…

Unfortunately I only found small scattered amounts of info on this cluster, which I now believe to be a cluster off of a 2002 BMW E46. Eventually, after some creative Googling, I tracked down this pin-out which seems to match. However I didn’t see any simple inputs for speed/tachometer etc that I could just stick a PWM signal into and get the needles to move, by the look of things I figured I may have to talk to to the on-board cluster computer somehow and send it the right info serially, something I half expectedย  I would need to do this being typically BMW… But time to open her up and take a look:

The cluster open revealing lotsa LED’s and even a small LCD!! ๐Ÿ˜€

Yes I had all ready de-soldered the black connector and hacked in some power and ground pins to test with…

Things were WAY more complicated on the inside than I had originally expected.. I connected it up to 12v from the bench supply and things came to life! but this didn’t help much as I had no way of getting data into it yet… Back to Google and I eventually discovered that MOST of the data was sent to the cluster, not over some simple serial protocol as I had originally hoped but over the vehicles CAN serial interface from the ECU… BIG complications. At this point after a bit of thinking I figured I was stuck with two options:

1) Get my PIC to talk to the cluster over the CAN interface. The problem with this idea is I dont know what ‘Language’ the cluster talks over the CAN interface, AND I dont have any experience making PICs communicate anything other than with the UART over RS232… As far as I can tell, unless I could find a very well documented example of talking to the cluster over the CAN on the net, which doesn’t seem very likely but if you have any ideas PLEASE let me know! my only other option would be to get a Bus Pirate and an E46 BMW and sniff the traffic.. and then inject it into my cluster. All very possible but a huge amount of work and a bit out of my league for the moment and for a relatively simple project… OR

2) Figure out how to control the mechanics that move the needles and implement my own controller from scratch with a PIC. Possibly easier than option 1 although still alot more work than I originally anticipated.. and it would mean basically destroying my R400 cluster!

To make a decision I decided I needed to see what mechanics drove the needles.. I quickly de-soldered one of the ‘Motors’ to have a look hoping it would be similar to an analog Volt or mA meter that could be driven directly from PWM..

No such luck.. Turns out they are miniature Bipolar Stepper motors! It just keeps getting more complicated! Time for some more thinking…

At first I had NO idea how I would drive this thing, I’m sure I could make It move with a PIC but how would I tell what position It was at? Then I realized its a bipolar STEPPER motor, It moves in steps! It has a small tab internally that stops it at a maximum and minimum position and I’d say its range is about 300′. Theoretically I could just set it to the minimum position and move it forward and backwards with the PIC while keeping a counter for how far away it is from the ‘Zero’ position and thus know ‘where’ it is. EXCITING!

Each coil measured 275ohm so at 5v they would pass about 18.5mA of current and according to the data-sheet a PIC 16f88 can sink or source up to 25mA on each I/O pin, so I could drive it directly at-least. After reading up on how to control Bipolar Steppers I connected it to the PIC and through together some rough code.

From the 12 o’clock position going counter-clockwise PIN A,B,C,D

Basically the control scheme for 1 ‘step’ is to connect the pins in the following order AB,CD,BA,DC (where BA is the reverse polarity of AB) some code:

#include <stdio.h>
#include <htc.h>
#include <delay.h>
#include <delay.c>
#include <usart.c>
#include <usart.h>
#include <stdlib.h>

__CONFIG(0x3F30);
__CONFIG(0X3FFC);

//define
#define _XTAL_FREQ=8MHZ
#define Aย ย  ย ย ย  ย RA1
#define Bย ย  ย ย ย  ย RA0
#define Cย ย  ย ย ย  ย RA7
#define Dย ย  ย ย ย  ย RA6
#define dlyย ย ย ย  3
#define TRUEย ย ย  1
#define FALSEย ย  0

volatile char speed_current, count;
char data_in

void init(void)
{
IRCF2=1; //
IRCF1=1; // These set the internal osc @ 8Mhz. Prob overkill ๐Ÿ˜€
IRCF0=1; //

TRISA=0b00000000;
TRISB=0b01001111; // make RB6/3/2/1/0 input

GIE=1;
PEIE=1;
init_comms();ย ย  ย // set up the USART – settings defined in usart.h

// Output a message
printf(“\rHello There ๐Ÿ™‚ I’m online! :D\n”);

}

void interrupt my_isr(void)
{
if((RCIE)&&(RCIF)) // Data has arrived via usart
{
data_in = getch();
//No clearing RCIF, must clear RCREG
RB4 = !RB4;
RCREG = 0;
}
}

void fwd1stp(void);
void rev1stp(void);
void startup(void);

//main function
//================================================================
void main(void)
{

//device peripheral configuration
init();
startup();
speed_current = 0;
ei();
RCIE = 1;
RB4 = !RB4;

//infinite loop
while(1)
{
if(data_in > speed_current)
{
fwd1stp();
speed_current++;

}

if(data_in < speed_current)
{
rev1stp();
speed_current–;
}

}
}

void fwd1stp(void)
{
D=1;
DelayMs(dly);
D=0;
B=1;
DelayMs(dly);
B=0;
C=1;
DelayMs(dly);
C=0;
A=1;
DelayMs(dly);
A=0;
}

void rev1stp(void)
{
A=1;
DelayMs(dly);
A=0;
C=1;
DelayMs(dly);
C=0;
B=1;
DelayMs(dly);
B=0;
D=1;
DelayMs(dly);
D=0;
}

void startup(void)
{
for(int c =0; c<120; c++)
{
fwd1stp();
}
for(int c =0; c<120; c++)
{
rev1stp();
}

}

I found a delay of about 2ms between coil transitions worked best however I think I didn’t quite get the wiring of the coils right and the stepper code can certainly use some improvement like finer delay sequences and half-stepping to make it A LOT smoother and increase the resolution… I’ll get to this eventually! but for the moment IT WORKS! here is a quick vid I made of the stepper being controlled by LFS: (I apologize for holding the camera the wrong way and the shitty vid xD in my defense it was about 6AM and I hadn’t had my coffee yet! :P)

Well for now thats that! when I get home this evening I’m going to do some more work on this project and see if I can get it all up and running! feel free to comment and keep a look out for part 2!

~Rob.

Categories
Project Logs

Simple Home Automation

This was originally posted on another blog that I posted on occasionally, but has unfortunately become spam ridden.. thus I will not be linking to it.(Matt sort your blog out :P)ย  I will however take this opportunity to copy it verbatim as my first post on RobThePyro Hacks It ๐Ÿ˜‰

Right time for my first real blog post… And its going to be about a little project I made to control the lights in my room from my PC over the parallel port! I’ve been thinking about this for awhile now and today I finally got round to doing it, quite simple in theory really: connect a suitable relay in series with your lights, connect it you the parallel port and write a simple application to flip a bit on the port thus turning the lights on and off! ๐Ÿ™‚

OK so for starters I scratched around in my box of components and found a 12V DPST, 240V AC 5A relay! perfect for turning the lights on and off. Now firstly the parallel port is only capable of about 2mA @ 5v SOOO usually to switch the relay I’d have used a small transistor but I decided to go for a 4n33 optocoupler (http://pdf1.alldatasheet.com/datasheet-pdf/view/250132/VISAY/4N33.html) instead as this would allow us to not only switch the 12V needed by the relay but also optically separate the port from the rest of the circuit, should anything go wrong! Here is a pic of the relay all wired up with the optocoupler hot glued on the side. there is also a small 470ohm resistor inside the heat-shrink near my thumb to prevent the 4n33 from drawing to much current from the port and damaging it!

Relay

I tried to keep everything well insulated since we are going to be working with mains here!

Next the port: I didn’t have a spare lpt cable lying around so I’m just using my LED test cable atm (It has 8 leds connected to the first 8 bits of the port, I just disconnected the first bit and connected it to the relay for the mean time before I come up with a more permanent solution ๐Ÿ™‚ I’m not going to go into more detail about this here, If you want to read up about it I suggest you check out this GREAT article by Levent Saltuklaroglu @ CodeProject.com

LTP port connecter

-don’t ask about the purple paint on my thumb :P-

Now for Power: the relay needs 12v to activate its coil, hmm where to get that? AH what luck! your PC’s outputs 12v on the molex’s amongst other places SO I re-purposed a molex pass-through to 12V fan adaptor to power my relay, I also made it easily detachable should I want to disconnect it. ๐Ÿ™‚

LTP Wiring

Now for the switch wiring: I connected two decent pieces of wire onto the relays normally closed pole and centre pole (this means the lights will be on by ‘default’ unless the parallel port pin goes high (or 1) and activates the relay turning the lights off)

Dissasembled light switch

-Disassembled the switch-

Obviously for the next step your going to want to get a decent torch ready and turn the power off at the distribution board. I made a point of connecting the LOAD to the centre pin of the relay and not the LIVE wire so that the unused exposed pole of the relay doesn’t get ‘hot’ when the relay switches the lights off and risk a short inside the switch!! Its small things like this that keep these simple mods reasonably safe! ๐Ÿ™‚

Wired light switch

-Attached for testing, sorry for shitty pic.-

AH! and she works! B) Note I left the light dimmer attach as well so I can still manually adjust brightness and turn them off if needed! Now to put the switch back together nicely!

Finished?

I haven’t thought of a nice cable management solution just yet but I am quite happy to have a couple wires lying around for the advantages I get!

The Software: I wrote a super simple C# application for testing that uses the freely available inpout32.dll to control the port, again I’m not going to go into detail here since there is tonnes of info available on Google and if you want the basic info on controlling the port I’m going to send you here again ๐Ÿ˜› So this is what the app looks like atm:

App

And now for a vid of the action! ๐Ÿ™‚

If anyone is interested in the app or the sauce (YUM! :P) post a comment here and I’ll upload it! Once I get some more time I’m going to write a much better app that can turn on/off at certain times, be controlled over my network from another PC or the internet!! and even have a small app on my phone so I can turn the lights on/off from my bed!!! And anything else I can think of!

Some other considerations: for those of you that know a thing or two about electronics, yes I should have put a diode across the relay to prevent feedback spikes from damaging the PSU, but I’m fairly confident that such a small relay wont be a problem -time will tell!- also I have kept this REALLY simple, I am planning alot of future upgrades like controlling other devices, RS-232 or USB control, and possibly even implementing a Triac to dim the lights from the computer!!! add a couple of sensors and you could be looking at ‘SMART’ lighting that would dim to the right level depending on ambient light, whether your in the room or not, etc.

Right well that’s it for now!

Rob~

Discalmer: This project deals with mains that can injure or KILL, so we cannot be responsible to you or your property if you attempt this bla bla etc. so be careful out there kids! ๐Ÿ™‚