from machine import Pin, ADC, PWM
import utime
# Constants for simulation
VOLTAGE_MAX = 240 # Simulated maximum voltage (in volts)
CURRENT_MAX = 10 # Simulated maximum current (in amperes)
OVERLOAD_THRESHOLD = 500 # Overload threshold in watts
# Pins and components
voltage_sensor = ADC(0) # Voltage sensor (potentiometer on ADC0)
current_sensor = ADC(1) # Current sensor (potentiometer on ADC1)
overload_led = Pin(15, Pin.OUT) # LED for overload indication
normall_led = Pin(13, Pin.OUT) # LED for overload indication
buzzer = PWM(Pin(14)) # Buzzer on GP14 with PWM for tone control
def read_sensor(adc, max_value):
"""Read ADC value and convert to real-world value."""
raw_value = adc.read_u16() # ADC range: 0 to 65535
return (raw_value / 65535) * max_value
def calculate_power(voltage, current):
"""Calculate power (P = V * I)."""
return voltage * current
def play_buzzer():
"""Play a warning tone on the buzzer."""
buzzer.freq(1000) # Set frequency to 1kHz
buzzer.duty_u16(32768) # 50% duty cycle (0-65535)
utime.sleep(0.5) # Buzz for 0.5 seconds
buzzer.duty_u16(0) # Turn off buzzer
while True:
# Read simulated voltage and current values
voltage = read_sensor(voltage_sensor, VOLTAGE_MAX)
current = read_sensor(current_sensor, CURRENT_MAX)
# Calculate power
power = calculate_power(voltage, current)
# Display values (for debugging in the Wokwi console)
print(f"Voltage: {voltage:.2f} V, Current: {current:.2f} A, Power: {power:.2f} W")
# Check for overload condition
if power > OVERLOAD_THRESHOLD:
overload_led.value(1) # Turn on LED
normall_led.value(0)
play_buzzer() # Play buzzer sound
print("Overload Detected")
else:
overload_led.value(0) # Turn off LED
normall_led.value(1)
print("Normal")
utime.sleep(1) # Delay for readability