from machine import Pin, ADC
import time
# Pin setup for LEDs
RED_LED_PIN = 17 # Red LED
YELLOW_LED_PIN = 16 # Yellow LED
GREEN_LED_PIN = 15 # Green LED
# Pin setup for the temperature sensor
TEMP_SENSOR_PIN = 26 # Analog temperature sensor input pin (GPIO 26)
# Setup the pins for LEDs
red_led = Pin(RED_LED_PIN, Pin.OUT)
yellow_led = Pin(YELLOW_LED_PIN, Pin.OUT)
green_led = Pin(GREEN_LED_PIN, Pin.OUT)
# Setup the temperature sensor (using ADC)
temp_sensor = ADC(Pin(TEMP_SENSOR_PIN)) # Correct ADC setup
# Function to convert the analog value to temperature (for LM35)
def read_temperature():
# Read analog value (0-65535) and convert it to voltage
adc_value = temp_sensor.read_u16() # Use read_u16() for Pico
voltage = adc_value * (3.3 / 65535) # Convert ADC value to voltage (3.3V reference)
temperature_c = voltage * 100 # LM35 gives 10mV per °C, so multiply by 100
return adc_value, temperature_c # Return both the ADC value and the temperature for debugging
def main():
while True:
# Read the current temperature and the raw ADC value
adc_value, temperature = read_temperature()
# Debug: print the raw ADC value and the temperature to monitor the values
print(f"ADC Value: {adc_value}, Temperature: {temperature}°C")
# Turn off all LEDs initially
red_led.value(0)
yellow_led.value(0)
green_led.value(0)
# Check temperature and control LEDs
if temperature > 27:
print("Temperature above 27°C - Red LED ON")
red_led.value(1) # Turn on the Red LED
elif 21 <= temperature <= 27:
print("Temperature between 21°C and 27°C - Yellow LED ON")
yellow_led.value(1) # Turn on the Yellow LED
elif temperature < 21:
print("Temperature below 21°C - Green LED ON")
green_led.value(1) # Turn on the Green LED
else:
print("Temperature out of expected range!") # This should never occur
time.sleep(1) # Delay for 1 second
if __name__ == "__main__":
main()
Loading
pi-pico-w
pi-pico-w