"""
https://docs.wokwi.com/parts/wokwi-photoresistor-sensor
There are two parameters that control the sensitivity of the LDR:
rl10 is the resistance of the LDR at illumination level of 10 lux
Resistance sensitivity (resistance change as function of illumination level change)
in linear approximation expressed as γ (values about 0.8 to 0.95).
The gamma value determines the slope of the log(R) / log(lux) graph
gamma = log(Ra/Rb) / log (a/b) a ,b : lux in points a,b , Ra Rb - resistance of ldr in those points
from table in wokvi web :
lux 0.1, 1, 10, 50, 100, 400, 1000, 10000, 100000
LDR KOhm : 1.25*1e2 250 50 16.2 9.98 3.78 1.99 0.397 0.079
The defaults are RL10 = 50, and GAMMA = 0.7 RL10 - resistance in 10 lux, RL100 - resistance in 100 lux
pow() function returns the value of x to the power of y (xy).
"""
from machine import Pin, ADC
from time import sleep
debug = 1 # לצרכי הדפסה במעקב בתוכנית
###### נתונים של הפוטורזיסטור ###########
my_adc = ADC(Pin(27))
GAMMA = 0.7;
RL10 = 50*1e3
RL50 = 16.2*1e3
RL100 = 9.98*1e3
#gamma = math.log(50*1e3/9.98*1e3)/mat.log(10/100)
resolution = 3.3 / 4096
print('resolution = ',resolution)
lux_table = [ 0.1, 1, 10, 50, 100, 400, 1000, 10000, 100000]
###### פונקציות של הפוטו רזיסטור ####
def photoresistor():
photo_resistor_analog_value = my_adc.read() # קריאת הממיר
voltage = resolution * photo_resistor_analog_value # המתח המחושב שנמדד על הפוטורזיסטור
ldr_resistance=10*1e3*voltage/(3.3-voltage) # ההנגדות המחושבת של הפוטורזיסטור
lux = pow(RL10 * pow(10, GAMMA) / ldr_resistance, (1 / GAMMA)) # עוצמת ההערה המחושבת
print('value read = ', photo_resistor_analog_value)
print ('voltage = ', voltage)
print('resistance = ', ldr_resistance)
print ('illumination = ', lux , 'lux')
print()
return lux
###### ואתחול ההדקים לפלט bargraph נתוני ההדקים של המיקרו המתחברים אל ה ######
bargraph_pins = [15 , 2, 4 , 5, 18, 19, 21, 22, 23, 32]
leds = [Pin(pin, Pin.OUT) for pin in bargraph_pins]
################# barGraph פונקציות השייכות ל #########
def bargraph(luxa):
range=check_range(luxa)
if debug:
print('range = ', range, 'lux = ', luxa)
turn_on(range)
'''
def check_range(luxi):
if luxi<=0.103:
return 1
elif luxi>0.101 and luxi <=1 :
return 2
elif luxi>1 and luxi <=10 :
return 3
elif luxi>10 and luxi <=50 :
return 4
elif luxi>50 and luxi <=100 :
return 5
elif luxi>100 and luxi <=400 :
return 6
elif luxi>400 and luxi <=1000 :
return 7
elif luxi>1000 and luxi <=10000 :
return 8
elif luxi>10000 and luxi <=100000 :
return 9
elif luxi>100000 :
return 10
'''
def check_range(luxi):
for i in range(len(lux_table)-2):
if debug:
print('len(lux_table)-2 = ', len(lux_table)-2 , 'i = ',i)
if luxi >= lux_table[i] and luxi < lux_table[i+1] :
if debug:
print ('len lux_table = ' , len(lux_table), 'luxi = ' , luxi , 'lux_table [' , i,'] = ', lux_table[i])
print ('i = ' ,i)
return(i)
#elif luxi > lux_table[len(lux_table)-1]:
# return i+2
def turn_off():
# bar_graph כיבוי כל הלדים ב
for x in leds:
x.value(0)
def turn_on(how_many):
turn_off()
if debug:
print ('how_many = ', how_many)
for i in range(how_many):
leds[i].value(1)
############## פונקציות השייכות לפוטו רזיסטור ########
def main():
for i in range(len(bargraph_pins)):
turn_on(i)
sleep(0.1)
turn_off()
for i in range(len(bargraph_pins),0,-1):
turn_on(i)
sleep(0.1)
turn_off()
sleep(1)
while True:
my_lux = photoresistor()
print('my_lux = ' , my_lux)
bargraph(my_lux)
sleep(2)
main()