14m Potentiometer

SKL BOI

Member
Hello

I am designing a light feedback system where in bright light the output light is off and in dark light the output light is on however the user will be able to set the brightness at any point so for example when its dark and the user has set the intensity the same as a normal dimmer control to half the output light will be half lit. then when it brightens up in the day this light dims until it is no longer required or increased so it comes on again.

the first problem is with the 100k potentiometer. i have it on leg 3 of my 14m (readadc 4). my code is as follows:

Code:
Main:
readadc10 0,b1 `LDR	
readadc 4,b0 'potentiometer
[COLOR="Red"]if b0 <=50 then 
goto Main
Endif
let b0 = 0
readadc 4,b0 'potentiometer[/COLOR]
let b2 = b1-32767
let b3 = b2+b0 / 2
pwmout 2,99,b3
[COLOR="red"]let b0 = 0[/COLOR]
goto main
The parts in red are the parts i have added to try and make the LED turn off when the potentiometer is at its lowest point. At the moment if i turn the circuit on with the potentiometer off the LED is off but as soon as you turn it enough for it to light up it will not turn off again unless the circuit is turned off and on again.

I can't seem to fix this. I have checked the voltage from the potentiometer and when its turn one way I get 0 volts (off) and when the other way I get 4.7 I think or something close enough to 5 anyway. So I know its the programming.

I have just tested it by pwmout 2,99,b0 which is the potentiometer input and it turns off and gos to full brightness.

So can anyone see a better or more accurate way to compare the LDR and Potentiometer and output the required light level because it must be that. Is that right?

Thanks

SKl BOI
 
Last edited:

SKL BOI

Member
It was a 10k to start with and i had more problems it was very steppy as in in the whole turn the output would go from bright to dark 4 or 5 times. I will it again.
 

Attachments

sghioto

Senior Member
SKL BOI,

You need a word variable to store the readadc10 value and any number above 255.
Or better, use only readadc (0 -255) values for the LDR

Steve G.
 
Last edited:

lbenson

Senior Member
Not to your point, but your "let b0 = 0"s are unneeded--whatever value is in b0 is changed with your "readadc 4". Not sure what you want to achieve with "let b2 = b1-32767". The b0 through b13 variables are byte-sized--can contain only values between 0 and 255.

Readadc10 returns a 10-bit number, so it won't fit into the 8 bits of b1. You want to read into a word. This word will have a maximum value of 1023 (10 bits, not the full 16 bits which it could contain), so you will need to scale between 0 and 1023.
 

hippy

Technical Support
Staff member
In general, when comparing two analogue inputs, one usually uses READADC and byte variables or READADC10 and word variables; it usually simplifies things when they are both the same.

The first thing to do when things don't seem right is to look to see what inputs you are getting; either add DEBUG or SERTXD commands to see what is being read in. Trim the code back to just reading inputs and showing the results. Get happy that the inputs are behaving as expected before moving on to comparisons and other processing.

This is also a good time to check that an LDR and pot is working as expected, that LDR readings go up or down with brighter levels, pot readings get bigger when turned clockwise or anti-clockwise. It's much easier to change hardware at the early stages to match what you want to see and what your software will expect.

On the 100K versus 10K pot - I've never noticed much difference between the two. The source impedence issues would be more significant for non-pot sources and perhaps for multi-turn pots. For setting a level to match with any non-linearity etc is unlikely to affect things greatly. The above suggested tests with DEBUG or SERTXD will show if the results look aceptable.
 

SKL BOI

Member
Ok using a word works better. its not stepping at all. thanks.

how would i invert the LDR input variable. and its not reacting very much to the potentiometer or the LDR when change individualy?
 

lbenson

Senior Member
>how would i invert the LDR input variable.

I'm not certain what you are trying to achieve with this inversion. Perhaps a table or several tables would help us here in the peanut gallery: LDR reading versus potentiometer reading verses PWM duty cycle percentage desired.

Then as hippy said, vary the lighting conditions to see what LDR readings you get. Vary the potentiometer settings to see what readings you get there (sertxd the variables into which you read the values). Display with sertxd what you have calculated the duty cycle value to be.
 

PhilDawson8270

New Member
Does it have to be controlled by PIC? This would be quite easy to implement with a regular analogue signal with an LDR, Pot, Few resistors and a transistor.
 

hippy

Technical Support
Staff member
how would i invert the LDR input variable

Alternatively, invert the circuit. The LDR and resistor form a potential divider; switch the two over and where readings would have gone up, they'll now go down and vice-versa.

To actually 'invert' a byte variable "b0 = b0 ^ $FF", for a word variable "w0 = w0 ^ $FFFF", but that may not be what you actually require.
 
Top