Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Use photocell with GPIO

    Scheduled Pinned Locked Moved LoPy
    photocelldigitalgpiolopy
    3 Posts 2 Posters 1.6k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      alex89
      last edited by alex89

      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-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @alex89
        last edited by robert-hh

        @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.

        A 1 Reply Last reply Reply Quote 0
        • A Offline
          alex89 @robert-hh
          last edited by

          @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

          1 Reply Last reply Reply Quote 1

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom