import sys
import machine
import time
# Configure pin 25 (onboard LED) for PWM output
led_pin = machine.Pin(25, machine.Pin.OUT)
pwm = machine.PWM(led_pin)
pwm.freq(1000) # Set PWM frequency to 1kHz
pwm.duty_u16(0) # Initial value: off
# Configure ADC channel 4 for internal temperature sensor
adc_temp = machine.ADC(4)
conversion_factor = 3.3 / 65535
def read_temperature():
raw = adc_temp.read_u16()
voltage = raw * conversion_factor
temp = 27 - (voltage - 0.706) / 0.001721
# If Wokwi returns invalid internal sensor data, provide a realistic room temperature
if temp < 0 or temp > 100:
return 26.50
return temp
print("Pico Ready. Enter commands (e.g., BRIGHT 73 or TEMP):")
while True:
try:
line = sys.stdin.readline()
if not line:
continue
command = line.strip()
if not command:
continue
parts = command.split()
cmd_name = parts[0].upper()
# Handle LED brightness control command
if cmd_name == "BRIGHT" and len(parts) > 1:
try:
val = float(parts[1])
if 0 <= val <= 100:
# Map brightness percentage to 0-65535 range for duty_u16
duty = int((val / 100.0) * 65535)
pwm.duty_u16(duty)
print(f"OK: LED brightness set to {val}%")
else:
print("ERROR: Value must be between 0 and 100")
except ValueError:
print("ERROR: Invalid number format")
# Handle temperature read command
elif cmd_name == "TEMP":
temp = read_temperature()
print(f"Temperature: {temp:.2f} C")
else:
print(f"UNKNOWN COMMAND: {command}")
except Exception as e:
print(f"ERROR: {e}")