"""
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))
lux = 0.0
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
if debug:
print('resolution of our adc = ',resolution, 'v')
sleep(1)
lux_table = [0.1, 1.0, 10, 50, 100, 400, 1000, 10000, 100000, 500000]
#########################################
######### נתונים עבור תצוגת ה בר גרף ########
esp32_pins = [15, 2, 4, 22, 23, 5, 18, 19, 21, 32 ]
leds = [Pin(pin, Pin.OUT) for pin in esp32_pins]
###########################################
def my_photoresistor():
global lux
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)) # עוצמת ההערה המחושבת
if debug:
print('value read = ', photo_resistor_analog_value)
print ('voltage = ', voltage)
print('resistance = ', ldr_resistance)
print ('illumination = ', lux , 'lux')
print()
##########################################
###### בודקים כמה לדים להאיר ב בר גרף ומדליקים את הלדים האלו #########
def my_bargraph():
# global lux,lux_table
if lux< 0.103:
n=1
elif lux >= 0.103 and lux<1:
n=2
elif lux >= 1 and lux<10:
n=3
elif lux >= 10 and lux<50:
n=4
elif lux >= 50 and lux<100:
n=5
elif lux >= 100 and lux<400:
n=6
elif lux >= 400 and lux<1000:
n=7
elif lux >= 1000 and lux<10000:
n=8
elif lux >= 10000 and lux<100000:
n=9
elif lux >= 100000:
n=10
#if debug:
# print ('n = ', n)
turn_on(n)
#######################################
#### מכבים את כל הלדים ######
def turn_off():
for led in leds:
led.value(0)
#### מקבלים כמה לדים להדליק ומדליקים אותן ####
def turn_on(n):
turn_off()
for i in range(n):
leds[i].value(1)
'''
פונקציה הרצה בתחילת הרצת התוכנית ובה בודקים
שחיבורי החומרה והגדרות הפינים נכונים. הפונקציה
מדליקה לד אחת אחרי השנייה ומכבה אותן אחת אחרי השנייה
'''
def check_bargraph():
for i in range(len(esp32_pins)):
turn_on(i)
sleep(0.2)
turn_off()
for i in range(len(esp32_pins),0,-1):
turn_on(i)
sleep(0.2)
turn_off()
sleep(0.5)
######### התוכנית הראשית ############
def main():
check_bargraph()
turn_off()
while True:
my_photoresistor()
my_bargraph()
sleep(1)
main()