from machine import Pin, SoftI2C, ADC
import machine
import ssd1306
import time
PIN2 = Pin(2, Pin.OUT)
PIN4 = Pin(4, Pin.OUT)
# Initialize ADC channels
adc1 = ADC(36) # Analog-to-digital converter for first sensor
adc2 = ADC(39) # Analog-to-digital converter for second sensor
# Create an OLED display object
pinsI2C = SoftI2C(scl=Pin(22), sda=Pin(21)) # Use SoftI2C for compatibility
oled = ssd1306.SSD1306_I2C(128, 64, pinsI2C)
# Define UV severity thresholds (adjust as needed)
good_threshold = 0.075 # uW/cm^2
ok_threshold = 0.15 # uW/cm^2
bad_threshold = 0.3 # uW/cm^2
very_bad_threshold = 0.6 # uW/cm^2
dangerous_threshold = 0.8 # uW/cm^2 # Ensure distinct from "bad" level
sensor_offset = 100 # Replace with actual value from sensor datasheet
sensor_sensitivity = 0.1875
# Define the conversion factor based on your setup:
# - If both modules share the same 3.3V power supply: conversion_factor = 0.1875
# - If the GUVA-S12SD has a different power supply voltage, adjust the factor accordingly
conversion_factor = 0.1875 # Example assumption for 3.3V power supply
print("Initialized modules.")
while True:
# Read raw analog values from both sensors
uv1_raw = adc1.read()
uv2_raw = adc2.read()
# Calculate UV intensity from each sensor
uv1_intensity = uv1_raw / 4095
uv2_intensity = uv2_raw / 4095
# Calculate the difference between the two sensors
uv_diff = uv1_intensity - uv2_intensity
# Convert to mW/cm^2 if needed: uv_diff_mW_cm2 = uv_diff * 100
# Format and display values on the OLED screen
oled.fill(0) # Clear the screen
oled.text(f"OUT:{uv1_intensity:.2f}", 0, 0)
oled.text(f"IN :{uv2_intensity:.2f}", 0, 10)
oled.text(f"Dif:{uv_diff:.2f}", 0, 20)
oled.text("uv", 100, 0)
oled.text("uv", 100, 10)
oled.text("uv", 100, 20)
# Determine and display UV severity level
if uv1_intensity <= good_threshold:
severity = "GOOD"
PIN2.on()
PIN4.off()
suggestions = "You can go out"
elif uv1_intensity <= ok_threshold:
PIN2.on()
PIN4.off()
severity = "OK"
suggestions = "You can go out"
elif uv1_intensity <= bad_threshold:
PIN2.off()
PIN4.on()
severity = "BAD"
suggestions = "stay at home"
elif uv1_intensity <= very_bad_threshold:
PIN2.off()
PIN4.on()
severity = "VERY.BAD"
suggestions = "stay at home"
else:
PIN2.off()
PIN4.on()
severity = "DANGER"
suggestions = "stay at home"
oled.text(f"outside:{severity}", 0, 30)
oled.text(f"{suggestions}", 0, 50)
if uv2_intensity <= good_threshold:
severity_home = 'good'
else:
severity_home = 'bad'
oled.text(f"Inside:{severity_home}", 0, 40)
oled.show()
time.sleep_ms(500) # Adjust delay as needed