Use photocell with GPIO



  • Hello,

    I am trying to use a photocell with the lopy using a 1uF capacitor and digital PIN.
    I am trying to do the same as this : https://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi/basic-photocell-reading

    But it is not working, I am sure I am doing something wrong. I used PIN10, here is my code:

    from machine import Pin
    import time
    import machine
    
    def rc_time ():
        count = 0
        p_out = Pin('P10', mode=Pin.OUT)
        p_out.value(0)
        time.sleep(0.01)
        p_in = Pin('P10', mode=Pin.IN)
        while (p_in() == 0):
            count += 1
            if (count == 30000):
                break
        return count
    
    # I always get 30000...
    try:
        # Main loop
        while True:
            val = rc_time()
            print(val)
            # time.sleep(0.1)
    except KeyboardInterrupt:
        pass
    


  • @robert-hh said in Use photocell with GPIO:

    me could be longer, and must not be not shorter, since it discharges the capacitor.
    The sample code talks about a loop time of 1 ms. WiPy is faster. The loop time should be around 15 µs. Are you sure that you connected the photocell between 3.3v and P10? If you never have a '1' reading at P10 as input, then the wiring is wrong.
    I mad a quick crosscheck with G9 = P22, and sleep(0.1), and it worked.
    Nevertheless, you could do better using the analog ports of the esp32 with a resistor and the photocell.

    Hello,
    I just tried again and it is working now. Thanks



  • @alex89 Comparing the codes, the sample code uses sleep(0.1), while you code has sleep(0.01). The sleep time could be longer, and must not be not shorter, since it discharges the capacitor.
    The sample code talks about a loop time of 1 ms. WiPy is faster. The loop time should be around 15 µs. Are you sure that you connected the photocell between 3.3v and P10? If you never have a '1' reading at P10 as input, then the wiring is wrong.
    I mad a quick crosscheck with G9 = P22, and sleep(0.1), and it worked.
    Nevertheless, you could do better using the analog ports of the esp32 with a resistor and the photocell.



Pycom on Twitter