Based on the Adafruit guide I decided to make the photocell reading work on WiPy, all for a final goal of making this.
More about the whole project once it's done, but now I just wanted to share the port of this Python code:
Code: Select all
#!/usr/bin/env python
# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!
import RPi.GPIO as GPIO, time, os
DEBUG = 1
GPIO.setmode(GPIO.BCM)
def RCtime (RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(RCpin, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
return reading
while True:
print RCtime(18) # Read RC timing using pin #18
Code: Select all
from machine import Pin
import time
# Based on:
# https://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi/basic-photocell-reading
def RCtime(RCpin):
# print('RCtime()')
reading = 0
data = Pin(RCpin, mode=Pin.IN, pull=Pin.PULL_DOWN)
time.sleep_ms(100)
# print('data.value()', data.value())
while (data.value() == 0):
reading += 1
return reading
def read():
# print('read()')
while True:
print(RCtime('GP0'))
if __name__ == '__main__':
# print('init')
read()
- 1x GL5528 Photoresistor
- 1x 1uF Electrolytic capacitor