Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5932

Automation, sensing and robotics • Re: Simple light level detector using photoresistor?

$
0
0
So I tested by following the youtube video after connecting my photoresistor circuitry to GPIO18 (on pin 12).
I used a 6.8 uF capacitor and a series resistor of 215 ohm for discharge.
Connections:
- My photo resistor from pin 17 (3.3V) using a 215 ohm series resistor to pin 12 (GPIO18)
- Capacitor 6.8uF from photo resistor/resistor junction to pin 6 (GND)

Since I have never ever used Python I may have done something wrong here, but this is what I did:

1) fully updated my PiZero:

Code:

sudo apt updatesudo apt full-upgradesudo apt install python3-pipsudo pip3 install RPi.GPIO
2) Using nano I typed in the code shown in the video into photoResistorTest.py:

Code:

import RPi.GPIO as GPIOimport timeGPIO.setmode(GPIO.BOARD)resistorPin = 18while True:    GPIO.setup(resistorPin, GPIO.OUT)    GPIO.output(resistorPin, GPIO.LOW)    time.sleep(0.1)    GPIO.setup(resistorPin, GPIO.IN)    currentTime = time.time()    diff = 0    while(GPIO.input(resistorPin) == GPIO.LOW):        diff = time.time() - currentTime    print(diff * 1000)    time.sleep(1)
3) Then I tried running the example:

Code:

$ python photoResistorTest.py
But nothing at all happened until I had to press Ctrl-C to break it...

Code:

^CTraceback (most recent call last):  File "/home/pi/projects/photoresistor/photoResistorTest.py", line 18, in <module>    diff = time.time() - currentTimeKeyboardInterrupt
I noted in the code that it ends with a while loop, which seems not to be closed, so that may be a problem (no end for the while...)
But that is what was shown in the video.

Questions:
- resistorPin = 18, should this be the actual pin number (12) instead of the GPIO number (18)?
- How does one properly exit a while loop in Python?

Statistics: Posted by Bosse_B — Tue Dec 03, 2024 8:14 am



Viewing all articles
Browse latest Browse all 5932

Trending Articles