0

I am pairing an LDR with a capacitor to to make a light sensing project by returning the value of the variable COUNT when the capacitor is charged. But when i do this with 2 or more LDRs the values of COUNT are very inconsistent. The values work fine if I connect the LDRs at pins 3 and 5(GPIO1 and GPIO2) but if I connect them anywhere else the values don't make any sense even if I use the exact same LDR and capacitor in identical conditions (you can see it in the output). Why is this happening ? Here is the code and output.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

pr1 = 3
pr2 = 5

def rc_time1(pr1):
    count = 0
    
    GPIO.setup(pr1, GPIO.OUT)
    GPIO.output(pr1, GPIO.LOW)
    time.sleep(0.5)
    GPIO.setup(pr1, GPIO.IN)
    
    while(GPIO.input(pr) == LOW):
        count += 1
    return count

def rc_time2(pr2):
    count = 0
    
    GPIO.setup(pr2, GPIO.OUT)
    GPIO.output(pr2, GPIO.LOW)
    time.sleep(0.5)
    GPIO.setup(pr2, GPIO.IN)
    
    while(GPIO.input(pr2) == GPIO.LOW):
        count += 1
    return count

try:
    while True:
        print("LDR_one: ---- ", rc_time1(pr1))
        print("LDR_two: ---- ", rc_time2(pr2))

except KeyboardInterrupt:
    pass

finally:
    GPIO.cleanup()

output at pins 3 and 5

LDR_one ---- 242 LDR_two ---- 246 LDR_one ---- 241 LDR_two ---- 203 LDR_one ---- 209

output at pins 3 and 8

LDR_one ---- 482 LDR_two ---- 141400 LDR_one ---- 482 LDR_two ---- 142865 LDR_one ---- 480

.....

3
  • Frankly this code makes little sense and I doubt the LDR is having ANY impact on the result. Post circuitry.
    – Milliways
    Feb 21 at 6:26
  • You're right, i changed the code, thanks for the advice. I ditched the logic of counting.
    – Coder
    Feb 21 at 19:05
  • You are right about the LDR as well.
    – Coder
    Feb 21 at 19:18

2 Answers 2

1

Both of pins 3 and 5 have hard wired 1k8 resistors to 3V3.

Either use different pins or ensure the others have the same pulls added to the circuit.

3
  • Thank you for the answer, really appreciate it. About the answer though, I currently am using the 3v3 as voltage line. So when I connect the LDR to pins other than 3 and 5 I just have to make sure the resistance is the same. Have I understood correctly?
    – Coder
    Feb 21 at 5:57
  • Yes. You have to be as sure as possible the resistance-capacitance circuit is electrically the same. The current flowing from the 1k8 resistor to the GPIO on pins 2 and 3 change the circuit. Either add the same value resistor to 3V3 to all other GPIO you want to use or don't use pins 2 and 3.
    – joan
    Feb 21 at 8:29
  • Ok got it :). Thanks a ton !! I will try it.
    – Coder
    Feb 21 at 9:12
0

Using a capacitor is an established technique for making a rough and ready ADC but relying on loop count is inaccurate and will not give reproducible results.

You should instead time how long it takes to change.

4
  • I should check how long it takes for the COUNT to change by using time() method ? Thanks for the response, appreciate it.
    – Coder
    Feb 21 at 9:16
  • I tried using time() method as well. The times were inconsistent too depending on the pins and the number LDRs I connected.
    – Coder
    Feb 21 at 9:24
  • @Coder If using pins 3,5 you are measuring the resistance of the 1.8kΩ pullup - these will totally swamp LDR as they are 2 orders of magnitude less. If you disconnected the LDR you would see the same result.
    – Milliways
    Feb 21 at 10:00
  • After numerous attempts I've gotten it to work. I'm using the time.time() function in a way figure out how long it's taking for the capacitor to charge. But the returned values became consistent once i added a 1k resistor to each LDR. Thanks a lot, all your advice has helped a ton.
    – Coder
    Feb 21 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.