Core2AWS Light Switch
2022-09-13 | By M5Stack
License: General Public License Arduino
* Thanks for the source code and project information provided by @AJB2K3
Things used in this project
Hardware components
M5Stack Core2 AWS×1
M5Stack Mini 3A Relay Unit×1
Software apps and online services
Control a light with an M5Stack Core2 AWS
In this guide I will show you how to control a bedside lamp using and M5Stack Core2 AWS with a Relay Unit.
WARNING! This project requires working with UK Mains operating voltage of 240V AC ! Do not attempt to follow if you are unsure of working with mains voltages.
Introduction.
This project started out as a request for a tutorial that came to me via Facebook.
I'm looking to program my M5stack for the moment to turn on a lamp via a relay controlled by an on/off slide for example.As a simple project, I found this too hard to turn down as it I was able to just use a Core2 AWS and a Relay Unit.
Connecting the Hardware together.
If using the M5Stack Relay Unit with the Core2 AWS all you need to do is connect the relay unit to the Black Port B on the base of the Core2 AWS however, if you are using a relay module like found it kits with Opto isolators,
You will need to use a Grove to Dupont Header cable.
When connecting to non-M5Stack relay modules like this using the Dupont cables the red wire goes to the relays VCC, the black wire to the relays GND and the yellow wire to the in or sig pin.
Once the hardware is connected, we can start creating code.
GUI Elements
Before we create the code that will control the light switch, we first need to add some GUI elements.
The Core2AWS has a touch screen which means we can control the light by touching graphical elements. Unlike the previous versions of the M5Stack Cores, the Core2 family use LVGL for rendering GUI elements resulting in a cleaner simpler to implement touch screen control method.
My first step was to add an LVGL Label element from the menu on the left to the virtual GUI, then I clicked on the label and changed the settings as show in the screen shot.
Next, I added an LVGL switch element.
By default, the size of the element is small and so I changed the settings as shown.
Next, I added a label and left the setting to default as this will be used to show us the digital state of the switch element.
And that was everything needed for the GUI side of the program.
UIFLOW Code
With the GUI built I could create the code in UIFLow.
The code, while basic, contains three loops. The first loop contains the blocks that set label1 to show the value of the LVGL GUI switch, The next loop monitors the position of the LVGL GUI switch and turns the relay on if the switch is set too true while the last loop check to see if LVGL GUI switch is set to false and then turns the relay off.
I tried to combine the functions into an if/then statement but found the LVGL GUI state wasn't triggering the relay!
The UIFlow Code Shown below is the most basic version I could get to work.
Observation
When used with the M5Stack relay, operations were correct but with the kit relay module I found that the relay would operate in reverse (relay would turn on with the Pin was pulled low.
If this happens to you all you need to do is just swap over the position of the relay blocks as shown.
Code
UIFlow Lamp Control MicroPython
from m5stack import *
from m5stack_ui import *
from uiflow import *
import unit
screen = M5Screen()
screen.clean_screen()
screen.set_screen_bg_color(0xFFFFFF)
relay_0 = unit.get(unit.RELAY, unit.PORTB)
switch0 = M5Switch(x=85, y=126, w=150, h=50, bg_c=0xCCCCCC, color=0x0288FB, parent=None)
label0 = M5Label('Light Switch', x=55, y=25, color=0x000, font=FONT_MONT_34, parent=None)
label1 = M5Label('label1', x=140, y=85, color=0x000, font=FONT_MONT_14, parent=None)
def switch0_on():
# global params
relay_0.on()
pass
switch0.on(switch0_on)
def switch0_off():
# global params
relay_0.off()
pass
switch0.off(switch0_off)
while True:
label1.set_text(str(switch0.get_state()))
wait_ms(2)
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum