LED sensor help

Status
Not open for further replies.

flemmard

New Member
Hi all,
I'm working on LED sensor and trying to interface with PIC microcontroller .
I know that i should use ADC to do my action in PIC.
For now, my LED sensor circuit works, it gives 5V , if not interrupted and 1.5v , if interrupted.My question how do I range the limit for this in PIC digital format?
How do i set the range for interruption and non-interruption in PIC C programming?
Please let me know , if i need to rephrase my question for you guys to understand.

THanks !
 
hi once more!

If the LED sensor output voltage is +1.5V when dark and +5V when lit, it should be possible use a digital port input to detect high,low.


I hope you dont mind me answering your posts all the time.
 
ericgibbs said:
hi once more!

If the LED sensor output voltage is +1.5V when dark and +5V when lit, it should be possible use a digital port input to detect high,low.


I hope you dont mind me answering your posts all the time.

Hi, that's fine but how do i set the range ? how do i initialize for 1.5v and 5v in digital ?
can u please write a piece of code in C for me to understand?
thanks
 
flemmard said:
Hi, that's fine but how do i set the range ? how do i initialize for 1.5v and 5v in digital ?
can u please write a piece of code in C for me to understand?
thanks

hi,
I would feed the output of the sensor into one end of a variable resistor, say 5K, the other end of the variable to 0V. The wiper of the variable [pot] to a PIC port pin. Adjust the pot so that the PIC see's a high or a low [ 1 or 0]
as when the LED is lit and dark.
OK?

EDIT re the C code, I use assembler, but I could give you a assembler sample for reference.
 
Last edited:

Hi, can you please give me the code for asm?


Thanks
 
Hi guys, looking at this thread, I get the feeling that you're both talking at cross-purposes?

Flemmard, I think you're asking about connecting your sensor to an ADC pin of your PIC and then using some code to differentiate between 5V and 1.5V voltage levels on it.
Eric OTOH, I think you're talking specifically about connecting the sensor directly to a digital IO pin of the PIC, and relying upon the thresholds being ok for the PIC?

Eric, isn't it the case that having voltage levels a bit close to the thresholds is a little iffy in terms of power consumption or such? Or is that only when the input pins are left floating? I forget. Presumably with the adjustment described it's probably ok anyhow.

Flemmard, if you use what I think Eric's technique is (digital IO pin), you don't really need special code to tell the PIC to set any ranges, the PIC can't directly alter its own threshold levels for the digital pins. Eric's description with the potentiometer is for adjusting the voltage range of the sensor to fit the PIC, rather than the other way round. I think.

If OTOH you still prefer to use an ADC pin, then after reading the ADC port (which IIRC is a little bit more involved, but I've not got that far in my PIC experiments yet ) you would simply compare the ADC value to some constant that would lie firmly in-between the values corresponding to each voltage, if that makes sense. Eric's digital IO method is probably better for what you're asking for. Less programming
 

hi tomble,

Looking at the dark/light voltage levels from the light sensor +1.5V and +5V it would be easier to just test for a high/low on a digital pin.
As you say the program routine would be easier and slightly faster.

I cant help out with 'C' programming, I'm a assemblersaurus
 

Hi, you are correct.Anyway, i got some info from Eric.
So you mean what Eric said was right?
 
Last edited:
ericgibbs said:
hi once more!

If the LED sensor output voltage is +1.5V when dark and +5V when lit, it should be possible use a digital port input to detect high,low.


I hope you dont mind me answering your posts all the time.

Hi, Thanks.So can i connect LED sensor directly to digital port of PIC without ADC ?
 
May i know how do i do that in PIC C ?
Eh, I'm afraid that though I know C in general, I only program computers with it. I've only programmed PICs in assembler (and not much!), so I can't help you with how to read the ADC either

HOWEVER, re working out whether the value read corresponds to "dark" or "light" in the ADC method, C pseudocode would be a bit like:
Code:
#define ANALOG_THRESHOLD=  something
/* Actual value for threshold should be the ADC reading that
would correspond to about 3.25V or so, or about 65% of the
maxval of the ADC, whatever that is */

level=read_the_ADC_somehow(); /* I dunno */
if (level <ANALOG_THRESHOLD) {
  /* it is dark */
} else {
  /* it is light */
}
Does that make sense? As I say, I can't say what code you'd have to use for reading the ADC. And I don't know what resolution you'd use for the ADC either, so you'd have to choose the ANALOG_THRESHOLD constant to be about 65% of the maximum ADC value at that resolution. The percentage doesn't have to be precise at all, but the maximum value is what matters. 50% would be fine really.

It'd still be far easier codewise to use Eric's version, if you can adjust the sensor's voltage range to suit the PIC.

Hi, Thanks.So can i connect LED sensor directly to digital port of PIC without ADC ?
Maybe not exactly directly, a little bit of extra circuitry like Eric described would probably be needed to make the voltage levels more PIC-friendly.

To calibrate the potentiometer (assuming that's workable) for use on a digital pin, you'd probably want to make a simple PIC program that just runs in a loop, reading the input pin, and writing what it sees on it to some output pin where you temporarily attach an LED just for this calibration task. You can manage that, right? Then adjust it as he described.
 


Many PIC chips have on board comparator modules. Set the Vref above 1.5V but below 3V and work with the Cout.
 
I think the first thing he needs to do really is post the circuit of how he's connecting it?.
 


HI, Thanks a lot ! I got your points .It really helped me ....
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…