eng_moh
Posts: 13
Joined: Wed Feb 08, 2017 6:43 am

Photo resistance value

Sun Mar 05, 2017 12:37 pm

I use this code to determined photo resistance value

#***********************************************
#!/usr/bin/python
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

def RCtime (RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(0.1)

GPIO.setup(RCpin, GPIO.IN)
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
return reading

while True:
print RCtime(11)
#***********************************************
But my outputs are

reading (ohm) true value(ohm)
200 2000
900 12000

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Photo resistance value

Sun Mar 05, 2017 1:45 pm

There's a couple of issues with that code. Firstly if your program gets interrupted by something else, then the count will be suspended for a while and the result will be low. It would be far better to remember the time at the beginning of the loop and the end of the loop and subtract the two.

Secondly, there is nothing in that code that calibrates one count to one ohm. The sped of the loop will differ depending on which model of Pi you are using and maybe whether it is idle or heavily loaded. The voltage at which the GPIO switches from reading how to reading high is not well defined, Having said that, the result should be roughly proportional to the RC constant and therefore to the resistance. You've only given us two data points, but it seems likely that there isn't a proportional relationship in those numbers. So something else must be wrong.

You haven't told us exactly what circuit you are using. I would guess that you have a capacitor to ground and the LDR to 3.3V, with the junction connected to the Pi either with or without a resistor between the resistor and the Pi. I would guess there is no such resistor, because if there was I doubt you could drain the capacitor just by setting the pin low when the LDR was 200 ohms. So the junction is connected directly to the Pi. It might be that the capacitor is also being drained through the Pi. Do you have a high pull-up enabled on that port? It might be best to make sure set no pull-ups explicitly. There will always be some current flowing to the Pi even without pull-ups enabled and, if the LDR is sufficiently high resistance, it will make a difference. But I doubt it would be enough to limit your measurement to 1k, as seems to be happening.

eng_moh
Posts: 13
Joined: Wed Feb 08, 2017 6:43 am

Re: Photo resistance value

Tue Mar 21, 2017 12:34 pm

thank you
but do you have anther method to measure resistance

Return to “Beginners”