papous
Posts: 72
Joined: Fri Jan 05, 2018 5:50 am

GPIO to PIGPIO "translation"

Wed Nov 14, 2018 4:48 pm

While Joan's pot_cap.py module for reading an ldr is magic, it is difficult for me as an amateur to comprehend. So I looked for an alternative and found rhe code below which works and I can understand it.

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
#define the pin that goes to the circuit
pin_to_circuit = 27

def rc_time (pin_to_circuit):
    count = 0  
    #Output on the pin for 
    GPIO.setup(pin_to_circuit, GPIO.OUT)
    GPIO.output(pin_to_circuit, GPIO.LOW)
    time.sleep(0.1)
    #Change the pin back to input
    GPIO.setup(pin_to_circuit, GPIO.IN)
    #Count until the pin goes high
    while (GPIO.input(pin_to_circuit) == GPIO.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:
    pass
finally:
    GPIO.cleanup()
I then tried to "translate" this using the pigpio lib and this is what came out:

Code: Select all

import sys
import time
import pigpio
LDR_PIN=27
pi = pigpio.pi() # Connect to Pi.

def RCtime (LDR_PIN):
	count = 0 
	# Discharge capacitor 
	pi.set_mode(LDR_PIN, pigpio.OUTPUT) 
	pi.write(LDR_PIN, pigpio.LOW)
	time.sleep(0.001) 
	pi.set_mode(LDR_PIN, pigpio.INPUT) 
	# Count loops until voltage across  capacitor reads high on GPIO 
	while (pi.read(LDR_PIN) == pigpio.LOW): 
		count += 1 
		return count 

#Catch when script is interrupted, cleanup correctly
try:
    # Main loop
    while True:
        print (RCtime(LDR_PIN))
        time.sleep(1)
except KeyboardInterrupt:
    pass
finally:
    pi.stop()
    quit
But this does not work.
What am I doing wrong?
Thank you

User avatar
bensimmo
Posts: 4121
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GPIO to PIGPIO "translation"

Wed Nov 14, 2018 7:41 pm

As an alternative
This would make it even easier, probably the same LDR wiring too.
ignore the LED for now
Image

Code: Select all

from gpiozero import LightSensor

sensor = LightSensor(18)

while True:
    sensor.wait_for_light()
    print("It's light! :)")
    sensor.wait_for_dark()
    print("It's dark :(")
https://gpiozero.readthedocs.io/en/late ... ght-sensor

and then this to switch from RPi.gpio to pigpio https://gpiozero.readthedocs.io/en/late ... _gpio.html (it seems more complicated than it actually is, i remember it being a bit easier to comprehend before they moved to factories)

User avatar
bensimmo
Posts: 4121
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GPIO to PIGPIO "translation"

Wed Nov 14, 2018 7:46 pm

just realised you're measuring the time constant, will get back

What doesn't work (it's been a while since I use pigpio directly), error messages?
pigpiod is running (sudo pigpiod) or enabled in the Raspberry Preferences/raspi-config ?

have you tried increasing the discharge time from 1ms to the same as the other program?
try
pi.write(LDR_PIN, 0) to set low

papous
Posts: 72
Joined: Fri Jan 05, 2018 5:50 am

Re: GPIO to PIGPIO "translation"

Thu Nov 15, 2018 6:48 am

Thank you bensimmo for your answer.
The first bit of code using RPi.GPIO prints values according to light conditions (dark/light)
The second, using PIGPIO prints 1 and does not change to light intensity.
There are no errors on running the code

papous
Posts: 72
Joined: Fri Jan 05, 2018 5:50 am

Re: GPIO to PIGPIO "translation" (!SOLVED)

Thu Nov 15, 2018 7:02 am

stupid mistake. wrong indent on return count
It works now
Thank you

User avatar
bensimmo
Posts: 4121
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GPIO to PIGPIO "translation"

Thu Nov 15, 2018 9:39 am

A yes, it returns as soon as it is in the while.
Though it looked the same as the previous version, there was a slight difference in alignment, I guess it didn't mind that and ran properly.

Been there, done that, still doing that ;-)

Return to “Python”