Thursday, March 19, 2009

Altimeter

I got four pressure sensors (two MPX4250 and two MP3H6115A) from freescale sometime back. I tried the two 4250s but they didn’t work for some reason. MP3H6115A is a surface mount device (SMD) which can measure pressure from 15kPa to 115kPa. Since I have previous experience working with SMDs, (SHT11, humidity sensor) I decided to work with this. The output (pressure) is reported as a voltage ranging from 0 to 2.8 v. I used PIC16F88 since it has an internal 10-bit ADC (analog to digital converter) and 4k code space.

Pressure is given by
P = (Vout/Vs+.095)/0.009 kPa

Where Vs, supply voltage = 3v
Vout, Output voltage measured from ADC

Altitude is given by

H = -ln(P/Po)*R*T/g

Three variables have to be measured:
T - average temperature in degrees Kelvin
Po - atmospheric pressure at ‘zero’ level
P - atmospheric pressure at current level

Remaining values are fixed constants:
R – universal gas constant = 286
g – gravitational acceleration = 9.81

Assuming Po=101.325 kPa and T=303.15K (30C)
The device reported H = 52.5m

Accuracy of this value is largely depending on the resolution of the ADC.
10-bit resolution of the ADC is not even near to get a reasonable accuracy of the altitude. One unit change in the ADC measurement will change 10m of height. To obtain 1m sensitivity, I need at least 12-bit ADC. So I ordered 12-bit ADCs from Maxim-IC and waiting until they arrive for the final product.





Tuesday, March 03, 2009

PIC Relay – 230v

This simple PIC circuit can be used to pre-program the on/off time of 230v electrical appliances. The PIC is programmed as a clock and at specified times, it can turn on/off a relay to control the appliance. I found a broken power guard (which protects electrical equipments from over and under voltage) in my old parts ‘yard’ at home. It was good source for the relay, diods,  step down transformer, voltage regulator, the plastic container and even the LEDs. This relay is having good characteristics since these power guards were produced mainly to protect refrigerators. Only drawback in this design is that it doesn’t have a user interface. If you want to change the on/off time, you have to do it using the program. (I was too lazy to add a display unit for the device) It is being tested with our refridegerator and is working fine. I put the fridge to turn off at midnight for about four hours. 








/*
* Project name:
PIC Relay
* Copyright:
Chandana Inc.
* Description:
PIC Relay - 230v
* Test configuration:
MCU: PIC16F88
Oscillator: XT, 04.0000 MHz
SW: mikroC v8.0
* NOTES:
Time might not be accurate.
*/

unsigned int countdown ; // interrupt counter
unsigned char update ; // update output
unsigned char secs, mins, hrs ; // tally variables

unsigned char ticks, correction,mode;
char oldstate_min, oldstate_hr, PowerOK, Powerchange, OutputStatus;

void interrupt()
{
if (--countdown == 0)
{
countdown = 8000; // 8000 x 125us = 1 second
update = 1; // flag to update clock output
}
PIR1.TMR2IF = 0; // clear interrupt flag

}

void Clock24()
{
if (++secs == 60) // check each tally for rollover
{ secs = 0;
if (++mins == 60)
{ mins = 0;
if (++hrs == 24)
hrs = 0 ;
}
}
update = 0;
}

void Initialize()
{

CMCON=7;
secs = 255;
mins = 23;
hrs = 15;
update = 1;
countdown = 8000;

INTCON.GIE = 1; // enable GIE
INTCON.PEIE = 1; // enable PEIE
T2CON = 0; // prescaler=0; postscaler=0; Timer2=off
TMR2 = 0; // clear timer2
PR2 = 124; // TMR2 resets when matching PR2
PIE1.TMR2IE = 1 ; // interrupt enabled
PIR1.TMR2IF = 0 ; // interrupt flag cleared
T2CON.TMR2ON = 1 ; // start Timer0
}

void main() {
Initialize();

PORTA = 0;
TRISA =0b00010;
PORTB = 0;
TRISB = 0;

oldstate_min=0;
oldstate_hr=0;
mode=0;
PowerOK=0;
Powerchange=0;
OutputStatus=1;

do
{
if (update == 1) // advance seconds?
Clock24(); // yes...then output


if (Button(&PORTA,1,1,1))
{
PowerOK=1;
//mode=0;
}

if (PowerOK && Button(&PORTA,1,1,0))
{
PowerOK=0;
PowerChange=1;
mode=1;
}

if (Powerchange && PowerOK) //power restore
{
Powerchange=0;
mode=0; //back to normal
}

if (mode==0)
{

if (OutputStatus==0) //turn off
{PORTA.F0=1;
PORTA.F2=1;
PORTA.F3=0;}
else
{PORTA.F0=0; //Turn ON
PORTA.F2=0;
PORTA.F3=1;}

}
else
{
PORTA.F0=0;
PORTA.F2=0;
PORTA.F3=0;


}



} while(1); // endless loop
}//~