# modules
from machine import I2C, Pin, ADC, freq
from time import sleep
import dht
from pico_i2c_lcd import I2cLcd
# Constants
MAX_SAFE_TEMP = 85.0 # Maximum safe temperature in Celsius
HIGH_CLOCK_SPEED = 133000000 # High clock speed in Hz (133 MHz, typical for Pico)
LOW_CLOCK_SPEED = 60000000 # Low clock speed in Hz (60 MHz, lower speed)
CHECK_INTERVAL = 1 # Time between temperature checks in seconds
# Memory map for sensor readings
memory_map = {
"TMP36_temp": 0,
"DHT22_temp": 0,
"DHT22_humidity": 0,
"LDR_light_intensity": 0
}
# Set up sensors
temp_sensor_adc = ADC(26) # TMP36 temperature sensor connected to ADC0 (GPIO 26)
dht_sensor = dht.DHT22(Pin(1)) # DHT22 for temperature and humidity, connected to GPIO 1
light_sensor_adc = ADC(26) # LDR (Light Sensor) connected to ADC on GPIO 26
# Setup I2C for the LCD
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16) # 2 rows and 16 columns for the LCD
# Function to read the internal temperature from the Pico
def read_cpu_temperature():
sensor = ADC(4) # Internal temperature sensor is connected to ADC 4
raw = sensor.read_u16() # Read raw ADC value (16-bit)
conversion_factor = 3.3 / 65535 # 3.3V reference, 16-bit resolution
voltage = raw * conversion_factor
temp_c = 27 - (voltage - 0.706) / 0.001721 # Formula from Pico datasheet
return temp_c
# Function to read the TMP36 temperature sensor
def read_tmp36_temperature():
raw = temp_sensor_adc.read_u16()
voltage = (raw / 65535.0) * 3.3 # Convert raw reading to voltage
temp_c = (voltage - 0.5) * 100 # Convert voltage to temperature (Celsius)
memory_map["TMP36_temp"] = temp_c # Store reading in memory map
return temp_c
# Function to read the DHT22 sensor for temperature and humidity
def read_dht22():
try:
dht_sensor.measure()
temp_c = dht_sensor.temperature()
humidity = dht_sensor.humidity()
memory_map["DHT22_temp"] = temp_c # Store temperature in memory map
memory_map["DHT22_humidity"] = humidity # Store humidity in memory map
return temp_c, humidity
except OSError as e:
print("Error reading DHT22:", e)
return None, None
# Function to read the LDR light sensor
def read_ldr():
raw = light_sensor_adc.read_u16()
voltage = (raw / 65535.0) * 3.3 # Convert raw reading to voltage
memory_map["LDR_light_intensity"] = voltage # Store reading in memory map
return voltage # Return the voltage (indicating light intensity)
# Function to set the clock speed dynamically
def set_clock_speed(speed_hz):
freq(speed_hz)
# Main loop to monitor CPU temperature, adjust clock speed, and read sensors
def monitor_system():
set_clock_speed(HIGH_CLOCK_SPEED) # Start with the high clock speed
while True:
lcd.clear() # Clear the LCD display
# Read CPU temperature
cpu_temp = read_cpu_temperature()
print(f"CPU Temperature: {cpu_temp:.2f} °C")
lcd.putstr(f"CPU Temp: {cpu_temp:.2f}C\n")
# Check if temperature exceeds the maximum safe limit
if cpu_temp > MAX_SAFE_TEMP:
print("Overheating! Reducing clock speed.")
set_clock_speed(LOW_CLOCK_SPEED) # Reduce clock speed
else:
print("Temperature is normal.")
# Read TMP36 temperature sensor
tmp36_temp = read_tmp36_temperature()
print(f"TMP36 Temp: {tmp36_temp:.2f} °C")
lcd.putstr(f"TMP36: {tmp36_temp:.2f}C")
# Pause for a moment before updating again
sleep(3)
# Read DHT22 sensor for temperature and humidity
dht_temp, humidity = read_dht22()
if dht_temp is not None and humidity is not None:
print(f"DHT22 Temp: {dht_temp:.2f} °C, Humidity: {humidity:.2f} %")
lcd.clear()
lcd.putstr(f"DHT22: {dht_temp:.2f}C\nHumid: {humidity:.2f}%")
sleep(3)
# Read LDR for light intensity
light_intensity = read_ldr()
print(f"Light Intensity: {light_intensity:.2f} V")
lcd.clear()
lcd.putstr(f"Light Int: {light_intensity:.2f}V")
# Wait before next sensor reading
sleep(CHECK_INTERVAL)
print("Good Day Sir, Your Temperature Readings Are:\n"
"Light Intensity (Voltage) : 0.00 V\n"
"Total Memory Usage: 8 bytes\n")
# Simulate new information being pulled in
print("New data incoming...\n")
# Main loop to monitor CPU temperature, adjust clock speed, and read sensors
def monitor_system():
set_clock_speed(HIGH_CLOCK_SPEED) # Start with the high clock speed
while True:
lcd.clear() # Clear the LCD display
# Read CPU temperature
cpu_temp = read_cpu_temperature()
print(f"CPU Temperature: {cpu_temp:.2f} °C\n")
lcd.putstr(f"CPU Temp: {cpu_temp:.2f}C\n")
# Check if temperature exceeds the maximum safe limit
if cpu_temp > MAX_SAFE_TEMP:
print("Overheating! Reducing clock speed.\n")
set_clock_speed(LOW_CLOCK_SPEED) # Reduce clock speed
else:
print("Temperature is normal.\n")
# Read TMP36 temperature sensor
tmp36_temp = read_tmp36_temperature()
print(f"TMP36 Temp: {tmp36_temp:.2f} °C\n")
lcd.putstr(f"TMP36: {tmp36_temp:.2f}C\n")
# Pause for a moment before updating again
sleep(3)
# Read DHT22 sensor for temperature and humidity
dht_temp, humidity = read_dht22()
if dht_temp is not None and humidity is not None:
print(f"DHT22 Temp: {dht_temp:.2f} °C, Humidity: {humidity:.2f} %\n")
lcd.clear()
lcd.putstr(f"DHT22: {dht_temp:.2f}C\nHumid: {humidity:.2f}%")
sleep(3)
# Read LDR for light intensity
light_intensity = read_ldr()
print(f"Light Intensity: {light_intensity:.2f} V\n")
lcd.clear()
lcd.putstr(f"Light Int: {light_intensity:.2f}V\n")
# Wait before next sensor reading
sleep(CHECK_INTERVAL)
# Run the monitoring system
monitor_system()