Help On Project. Using the LDR on the DEV Board PIC16F88

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Jody
Posts: 2
Joined: Tue Jun 12, 2007 11:25 pm
Contact:

Help On Project. Using the LDR on the DEV Board PIC16F88

Post by Jody »

I need to write a program to use the LDR On the Dev board to turn on 4 LED's. Dark-4 LEDs and the lighter it gets the less LEDs get turned on.

The LEDs act as valves.

HELP ASAP thanks.
Jody
PM me or email andrevs@sbsa.com

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello

All you need is an ADC sampling routine to sample AN0 which is connected to the LDR. This routine is fairly common so you should be able to pick up an example in the language you are using fairly easily. Also the datasheet for the PICmicro will point you in the right direction when writing the routine.

Then it is simply a case of taking the analogue value and creating setpoints to switch the LEDs on or off.

for example.

Code: Select all

if (ADC_val > setpoint1)
{
       //Switch On LED
}
else
{
       //Switch Off LED
}

Jody
Posts: 2
Joined: Tue Jun 12, 2007 11:25 pm
Contact:

LDR

Post by Jody »

Thanx alot

But im new to this type of progamming, and i dont know how to even start!! So if u can, i need mor detailed help..

Will be Appreciated
Jody

Legoman
Posts: 2
Joined: Sat Dec 15, 2007 3:45 pm
Location: UK
Contact:

Post by Legoman »

Which Pic are you using?
I did something similar. It took the voltage from the LDR run that through an ADC, scaled the value to MIDI range(0-127), then displayed the value on the LED's.

I can send you a copy of the code if you'd like?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Jody

To start you would need an ADC to read the voltage of the LDR.

This is done by adding an ADC component to your program and attaching it to AN0 via the Component connection window.

Then using hardware macros call the ADC sample function and then read back the ADC result using the read as byte function. Place the returned result into a byte variable.

Then use a decision icon to see if the sample is over a certain threshold.

Eg. VAR1 > 100

Then if the sample held in Var1 is over 100 then the yes branch will be run. Otherwise the no branch will be run.

Post Reply