im trying to control a relay using the m5stickc plus2 on g26
This is my code:
from M5 import * # Correct import for M5StickC in UIFlow 2.0
import time
Set GPIO pin for relay control (changed to GPIO 26)
RELAY_PIN = 26 # GPIO 26 for relay control
Initialize the relay pin as an output
relay_pin = M5.Pin(RELAY_PIN, M5.Pin.OUT) # Correct way to initialize the pin
Function to turn on relay
def turn_on_relay():
relay_pin.value(1) # Set relay pin to HIGH (turn on)
Function to turn off relay
def turn_off_relay():
relay_pin.value(0) # Set relay pin to LOW (turn off)
Main loop
while True:
try:
# Check if button A is pressed
if buttonA.isPressed():
turn_on_relay() # Turn on relay when button A is pressed
lcd.fill(0) # Clear the screen
lcd.text(50, 50, "Relay ON", color=lcd.WHITE, font=FONT_Default)
else:
turn_off_relay() # Turn off relay when button A is not pressed
lcd.fill(0) # Clear the screen
lcd.text(50, 50, "Relay OFF", color=lcd.WHITE, font=FONT_Default)
time.sleep(0.1) # Small delay to avoid overwhelming the system
except Exception as e:
lcd.fill(0) # Clear the screen on error
lcd.text(50, 50, f"Error: {e}", color=lcd.WHITE, font=FONT_Default)
time.sleep(2) # Wait for a moment before trying again
this is the error im getting on uiflow2.0
Connected to Serial Port!
MicroPython v1.24.0-dirty on 2024-12-20; M5STACK StickC PLUS2 with ESP32(SPIRAM)
Type "help()" for more information.
E (287120) i2c: i2c driver install error
E (287136) time: The current date/time in Shanghai is: Wed Jan 1 02:35:08 2025
E (287246) wifi:NAN WiFi stop
E (287248) transport_base: poll_read select error 113, errno = Software caused connection abort, fd = 54
E (287249) mqtt_client: Poll read error: 119, aborting connection
[INFO] Syncing resources...
[INFO] WiFi connected!
Traceback (most recent call last):
File "main.py", line 6, in <module>
NameError: name 'M5' isn't defined
MicroPython v1.24.0-dirty on 2024-12-20; M5STACK StickC PLUS2 with ESP32(SPIRAM)
Type "help()" for more information.