from machine import Pin, I2C, ADC
from ssd1306 import SSD1306_I2C
import sys
import time
import dht
# Initialize DHT22 sensor
dht22 = dht.DHT22(Pin(9))
# Define the resolution of the OLED display
pix_res_x = 128
pix_res_y = 64
# Define ADC pin for light sensor
light_sensor_pin = 34 # Update with the correct pin number for your board
adc = ADC(Pin(light_sensor_pin))
# Function to initialize I2C
def init_i2c(scl_pin, sda_pin):
i2c_dev = I2C(1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=200000)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()]
if not i2c_addr:
print('No I2C Display Found')
sys.exit()
else:
print("I2C Address : {}".format(i2c_addr[0]))
print("I2C Configuration: {}".format(i2c_dev))
return i2c_dev
# Function to display text on OLED
def display_text(oled):
oled.fill(0)
# Read sensor data
dht22.measure()
temperature_C = dht22.temperature()
humidity = dht22.humidity()
# Read light intensity
light_intensity = adc.read() # Read ADC value (0-1023 for a 10-bit ADC)
# Convert temperature to Fahrenheit
temperature_F = 32 + (1.8 * temperature_C)
# Display data on OLED
oled.text('Temp (C): ', 0, 10)
oled.text(str(temperature_C), 60, 10)
oled.text('Humidity: ', 0, 30)
oled.text(str(humidity), 60, 30)
oled.text('Light Intensity: ', 0, 50)
oled.text(str(light_intensity), 60, 50)
oled.show()
time.sleep(2)
# Main function
def main():
i2c_dev = init_i2c(scl_pin=27, sda_pin=26)
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev)
while True:
display_text(oled)
# Run the main function
if __name__ == '__main__':
main()