I am trying to use an LDR to measure ambient light using the code below:
As you can see I tried translating code that uses the GPIO library to PIGPIO
Code: Select all
import RPi.GPIO as GPIO import pigpio import time pin_to_circuit = 18 LDR_PIN=18 pi = pigpio.pi() def rc_time (pin_to_circuit): count = 0 #Output on the pin for capacitor #GPIO.setup(pin_to_circuit, GPIO.OUT) #GPIO.output(pin_to_circuit, GPIO.LOW) pi.set_mode(LDR_PIN, pigpio.OUTPUT) pi.write(LDR_PIN, pigpio.LOW) time.sleep(0.1) #Change the pin back to input #GPIO.setup(pin_to_circuit, GPIO.IN) pi.set_mode(LDR_PIN, pigpio.INPUT) #Count until the pin goes high #while (GPIO.input(pin_to_circuit) == GPIO.LOW): while (pi.read(LDR_PIN) == pigpio.LOW): count += 1 return count #Catch when script is interrupted, cleanup correctly try: # Main loop while True: print (rc_time(pin_to_circuit)) except KeyboardInterrupt: exit
The GPIO code works fine giving counts between 50 and 100.
The PIGPIO code does not work as it gives counts of 1 or 2
Can anyone suggest why this happens?
By the way joan's pot_cap.py code also works fine
Many thanks for your time