Categories
Experimentation!

Pic 12f683 Blinds Control

Check out what I made today after 4 hours of tinkering…

More to come 😉
~Rob.

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.