Skip to content

Instantly share code, notes, and snippets.

@bennuttall
Last active August 26, 2018 18:53
Show Gist options
  • Select an option

  • Save bennuttall/9b4da3270b3e576e26eb9be2460d738d to your computer and use it in GitHub Desktop.

Select an option

Save bennuttall/9b4da3270b3e576e26eb9be2460d738d to your computer and use it in GitHub Desktop.
from gpiozero import LightSensor
from time import time, sleep
class NoisyLightSensor(LightSensor):
def _read(self):
# Drain charge from the capacitor
self.pin.function = 'output'
self.pin.state = False
sleep(0.1)
# Time the charging of the capacitor
start = time()
self._charged.clear()
self.pin.function = 'input'
self._charged.wait(self.charge_time_limit)
value = (
1.0 - min(self.charge_time_limit, time() - start) /
self.charge_time_limit
)
print('Charge time: {}'.format(value))
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment