from machine import Pin, SPI, I2C
import time
from bh1750 import BH1750
import ssd1306
j = None
SCL = 17
SDA = 16
oled_width = 128
oled_height = 64
# I2C 設定 (依你的板子腳位調整)
i2c = I2C(0, scl=Pin(SCL), sda=Pin(SDA))
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
oled.fill(0)
oled.show()
oled.text('Hello', 0, 0)
oled.show()
time.sleep(1)
# Create BH1750 object
light_sensor = BH1750(bus=i2c, addr=0x23)
try:
# Read lux every 2 seconds
for j in range(1, 200):
# display.write_to_buffer(f"%08d"%j)
# display.display()
lux = light_sensor.luminance(BH1750.CONT_HIRES_1)
print("Luminance: {:.2f} lux".format(lux))
oled.fill(0)
oled.text("Luminance:", 0, 20)
oled.text(" {:.2f} lux".format(lux), 0, 30)
oled.show()
time.sleep_ms(500)
except Exception as e:
# Handle any exceptions during sensor reading
print("An error occurred:", e)