import time
from machine import Pin, I2C, ADC
import math
import ssd1306
temp_adc= ADC(27)
light_adc = ADC(26)
sensor_temp = ADC(4)
GAMMA = 0.7
RL10 = 33
BETA = 3950
K = 273.15
deg25 = K + 25
conversion_factor = 3.3 / (65535)
# OLED I2C
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
while True:
analogValue = light_adc.read_u16()
voltage = analogValue / 65536. * 3.3
resistance = 2000 * voltage / (1 - voltage / 3.3)
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA))
analogValue = temp_adc.read_u16()
celsius = 1 / (math.log(1 / (65535 / analogValue - 1)) / BETA + 1.0 / deg25) - K
analogValue = sensor_temp.read_u16() * conversion_factor
temperature = 27 - (analogValue - 0.706)/0.001721
oled.fill(0)
output_string = f"Light = {lux}lux"
oled.text(output_string, 0, 15, 1)
print(output_string)
output_string = f"NTC T = %.2fC"%celsius
oled.text(output_string, 0, 30, 1)
print(output_string)
output_string = f"Chip T = %.2fC"%temperature
oled.text(output_string, 0, 50, 1)
print(output_string)
oled.show()
time.sleep(1)
Loading
pi-pico-w
pi-pico-w