Python Code 10.1.1 Nightlamp
First observe the project result, and then analyze the code.
1. Use cd command to enter 09.1.1_Nightlamp directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/10.1.1_Nightlamp
2. Use the python command to execute the Python code “Nightlamp.py”.
python Nightlamp.py
After the program is executed, when you cover the photosensitive resistance or make a flashlight toward the
photoresistor, the brightness of LED will be enhanced or weakened. And the terminal window will print out
the current input voltage value of PCF8591 AIN0 pin and the converted digital quantity.
The following is the program code:
im port RPi. GPIO as GPIO
im port smbus
im port time
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
ledPin = 11
def analogRead(chn):
value = bus.read_byte_data(address,cmd+chn)
r eturn value
def analogWrite(value):
bus. write_byte_data(address,cmd,value)
def setup():
g lobal p
GPIO. setmode(GPIO.BOARD)
GPIO. setup(ledPin,GPIO.OUT)
GPIO. output(ledPin,GPIO.LOW)
p = GPIO. PWM(ledPin,1000)
p. start(0)
def loop():
w hile Tr ue: