from machine import Pin, ADC, I2C
from pico_i2c_lcd import I2cLcd
import time
# LED Indikator
led_onboard = Pin("LED", Pin.OUT) # Built-in LED
led_status = Pin(16, Pin.OUT) # LED eksternal
led_warning = Pin(15, Pin.OUT) # LED warning untuk power berlebih
# Setup ADC untuk voltage dan current
voltage_adc = ADC(26) # GP26 untuk voltage
current_adc = ADC(27) # GP27 untuk current
# Konstanta
VOLTAGE_REFERENCE = 3.3
ADC_MAX = 65535
CURRENT_SCALE = 5.0 # Skala maksimum 5A untuk simulasi
POWER_LIMIT = 50.0 # Batas power dalam Watt
# Setup I2C LCD
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16) # 0x27 adalah alamat I2C LCD, 2 baris, 16 kolom
def read_voltage():
adc_value = voltage_adc.read_u16()
voltage = (adc_value / ADC_MAX) * VOLTAGE_REFERENCE * 10 # Skala 0-33V
return voltage
def read_current():
adc_value = current_adc.read_u16()
current = (adc_value / ADC_MAX) * CURRENT_SCALE # Skala 0-5A
return current
def calculate_power(voltage, current):
return voltage * current
def display_normal(voltage, current, power):
lcd.clear()
lcd.putstr(f"V:{voltage:.1f}V I:{current:.2f}A")
lcd.move_to(0, 1)
lcd.putstr(f"Power:{power:.1f}W")
def display_warning(voltage, current, power):
lcd.clear()
lcd.putstr(f"V:{voltage:.1f}V I:{current:.2f}A")
lcd.move_to(0, 1)
lcd.putstr("POWER OVER LIMIT!")
# Main program
print("Program starting...")
led_onboard.on()
warning_state = False
try:
while True:
# Baca voltage dan current
voltage = read_voltage()
current = read_current()
power = calculate_power(voltage, current)
# Cek power limit
if power > POWER_LIMIT:
if not warning_state:
warning_state = True
print("WARNING: Power exceeds 50W limit!")
led_warning.on()
display_warning(voltage, current, power)
# Blink status LED lebih cepat saat warning
led_status.toggle()
time.sleep(0.2)
else:
if warning_state:
warning_state = False
led_warning.off()
display_normal(voltage, current, power)
# Normal blink rate
led_status.toggle()
time.sleep(0.5)
# Print ke serial monitor
print(f"Voltage: {voltage:.1f}V, Current: {current:.2f}A, Power: {power:.1f}W")
except KeyboardInterrupt:
print("Program stopped")
led_onboard.off()
led_status.off()
led_warning.off()
lcd.clear()
except Exception as e:
print(f"Error: {e}")
while True:
led_onboard.toggle()
time.sleep(0.2)