-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNightlamp.py
53 lines (43 loc) · 1.23 KB
/
Nightlamp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
#############################################################################
# Filename : Nightlamp.py
# Description : Control LED with Photoresistor
# Author : www.freenove.com
# modification: 2019/12/27
########################################################################
import RPi.GPIO as GPIO
import smbus
import time
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
ledPin = 11 # define ledPin
def analogRead(chn):
value = bus.read_byte_data(address,cmd+chn)
return value
def analogWrite(value):
bus.write_byte_data(address,cmd,value)
def setup():
global p
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPin,GPIO.OUT) # set ledPin to OUTPUT mode
GPIO.output(ledPin,GPIO.LOW)
p = GPIO.PWM(ledPin,1000) # set PWM Frequence to 1kHz
p.start(0)
def loop():
while True:
value = analogRead(0) # read the ADC value of channel 0
p.ChangeDutyCycle(value*100/255)
voltage = value / 255.0 * 3.3
print ('ADC Value : %d, Voltage : %.2f'%(value,voltage))
time.sleep(0.01)
def destroy():
bus.close()
GPIO.cleanup()
if __name__ == '__main__': # Program entrance
print ('Program is starting ... ')
setup()
try:
loop()
except KeyboardInterrupt: # Press ctrl-c to end the program.
destroy()