Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
π Ready to explore and create with us? Click [here] and join today!
2.2.1 Photoresistorο
Introductionο
Photoresistor is a commonly used component of ambient light intensity in life. It helps the controller to recognize day and night and realize light control functions such as night lamp. This project is very similar to potentiometer, and you might think it changing the voltage to sensing light.
Required Componentsο
In this project, we need the following components.
Itβs definitely convenient to buy a whole kit, hereβs the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Schematic Diagramο
Experimental Proceduresο
Step 1: Build the circuit.
Step 2: Go to the folder of the code.
cd ~/raphael-kit/python/
Step 3: Run the executable file.
sudo python3 2.2.1_Photoresistor.py
When the code is running, the brightness of the LED will change according to the light intensity sensed by the photoresistor.
Code
Note
You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like raphael-kit/python
. After modifying the code, you can run it directly to see the effect.
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import ADC0834
import time
LedPin = 22
def setup():
global led_val
# Set the GPIO modes to BCM Numbering
GPIO.setmode(GPIO.BCM)
# Set all LedPin's mode to output and initial level to High(3.3v)
GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.HIGH)
ADC0834.setup()
# Set led as pwm channel and frequece to 2KHz
led_val = GPIO.PWM(LedPin, 2000)
# Set all begin with value 0
led_val.start(0)
def destroy():
# Stop all pwm channel
led_val.stop()
# Release resource
GPIO.cleanup()
def loop():
while True:
analogVal = ADC0834.getResult()
print ('analog value = %d' % analogVal)
led_val.ChangeDutyCycle(analogVal*100/255)
time.sleep(0.2)
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the program destroy() will be executed.
destroy()
Code Explanation
def loop():
while True:
analogVal = ADC0834.getResult()
print ('analog value = %d' % analogVal)
led_val.ChangeDutyCycle(analogVal*100/255)
time.sleep(0.2)
Read the analog value of CH0 of ADC0834. By default, the function
getResult()
is used to read the value of CH0, so if you want to read
other channels, please input 1, 2, or 3 into () of the function
getResult()
. Next, what you need is to print the value via the print
function. Because the changing element is the duty cycle of LedPin
, the
computational formula, analogVal*100/255
is needed to convert analogVal
into percentage. Finally, ChangeDutyCycle()
is called to write the
percentage into LedPin
.