Create an RGB LED Sequencer using an M5Stack Core 2 and an Arduino Uno
2022-10-26 | By Maker.io Staff
License: See Original Project LEDs / Discrete / Modules Arduino
There are a variety of methods for controlling an RGB LED. Controlling an RGB LED may include using discrete switches, transistors, or digital circuits. These approaches allow the individual red, green, and blue LEDs to turn on or off based on the switching circuit, allowing current flow through each of the optoelectronic emitters. Besides discrete switching approaches, an Arduino Uno can be used to control an RGB LED. Wiring each of the anodes of the RGB LED to specified Arduino Uno digital pins will allow various lighting sequences to be produced.
In this guide, we’ll show you how to get started with some common cathode RGB LEDs, an Arduino Uno, and the M5Stack Core!
Using the M5Stack Core 2
Each RGB LED anode lead will be current-limited using a fixed resistor. The fourth lead - the cathode - of the RGB LED will be wired to the ground.
The M5Stack Core 2 is a small ESP32 microcontroller-based unit that allows the creation of an aesthetically appealing user interface (UI) for small human-machine interfaces (HMI) or human input modules (HIM). With HMIs and HIMs, the user or operator of a device can input data, monitor critical processes, and control various tasks. The M5Stack Core 2 has a touch screen liquid crystal display (LCD) that allows interaction between a human and a machine.
M5Stack Core 2 Controller units
Wiring an M5Stack Core 2 Controller to an Arduino Uno
You can easily wire an M5Stack Core 2 controller to build an Alien Alert RGB LED sequencer using an Arduino Uno and a few discrete electronic components. Check out the link below for a parts list and schematic for this guide:
Wiring an M5Stack Core 2 controller to an Arduino Uno is relatively easy to do. A grove breakout cable is used to connect the two electronic units. To assist in the wiring of the M5Stack Core 2 controller to the Arduino Uno and the RGB LED circuit, see the picture below.
Wiring diagram for the RGB LED Sequencer
The M5Stack Core 2 provides the control signal to turn on and off the sequencing of RGB LED colors via the yellow signal wire shown above.
The circuit above features a pushbutton switch, designated as PB1, to test the RGB LED sequencing feature. Upon start-up, the RGB LED will flash red. Pressing PB1 will stop the red LED flashing and allow the sequencing of the red, green, and blue LEDs to occur. The sequencing between the color LEDs is 500 milliseconds (ms). Further, you may use the circuit schematic diagram shown below to wire the RGB LED sequencer circuit.
RGB LED sequencer circuit schematic diagram
The final step is installing the M5Stack Core 2 and the Arduino Uno software.
A functional RGB LED sequencer prototype
Installing the Application Software
You will need to install and use the M5Stack’s UiFlow blockly code software to develop the UI for the M5Stack Core 2. You can find the free coding software here: https://shop.m5stack.com/pages/download. We’ll develop the UI for the M5Stack Core 2 using the desktop IDE software. M5 Stack provides tutorials and videos on using the software to develop aesthetically appealing UIs for M5Stack Core and Core 2 projects. Consult these tutorials before developing and installing the RGB LED sequencer application blockly code. Additionally, if you do not have the Arduino IDE installed, install this software as well.
The UI for the M5Stack Core 2 controller allows the display of three aliens on the TFT LCD. The aliens are displayed in red, green, and blue colors. Touching the Show Aliens button will display the three aliens, and you may hide the creatures by touching the Hide Aliens button.
The M5Stack Core 2 RGB LED sequencer UI
The three aliens are placed on the TFT screen using the image UI icon. You will need to upload an image file to the UI to display the aliens when you touch the Show Aliens button. The RGB-ON and RGB-OFF labels are placed over two of the programmable touch buttons. Touching the RGB-ON button will sequence the red, green, and blue LEDs. When you touch the second button, the LED sequencing will stop, and the red RGB LED will flash continuously. Use the blockly code shown below to provide UI and control features to the M5Stack Core 2. Using a USB C cable, connect the M5Stack Core 2 to your PC. Then, click the RUN button on the UiFlow taskbar to run the code on the M5Stack Core 2.
UI blockly code
The final step is to flash the red LED to RGB LED sequencer code to the Arduino Uno. With the Arduino IDE, you will use the code shown below to enable and operate the RGB LEDs. The M5Stack Core 2 Controller control signal wire is connected to pin D5 of the Arduino Uno. Touching Button A on the LCD will allow the M5Stack Core 2 controller to operate (sequence) the RGB LEDs using pins D9 - D11 of the Arduino Uno.
// Define code variables
const int redLEDPin = 9;
const int greenLEDPin = 10;
const int blueLEDPin = 11;
const int M5StackPin = 5;
int M5StackPinStatus = 0;
const int blueSensorPin = A0;
// Define INPUT and OUTPUT pins
void setup()
{ pinMode(M5StackPin,INPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin,OUTPUT);
pinMode(blueLEDPin,OUTPUT);
}
//If pushbutton is pressed, sequence the RGB LEDs
void loop()
{
M5StackPinStatus = digitalRead(M5StackPin);
if(M5StackPinStatus == HIGH){
digitalWrite(redLEDPin, HIGH);
delay(500);
digitalWrite(redLEDPin, LOW);
delay(500);
digitalWrite(greenLEDPin,HIGH);
delay(500);
digitalWrite(greenLEDPin,LOW);
delay(500);
digitalWrite(blueLEDPin,HIGH);
delay(500);
digitalWrite(blueLEDPin,LOW);
delay(500);
}
else{ // If pushbutton is not press, flash red RGB LED
digitalWrite(redLEDPin,HIGH);
delay(1000);
digitalWrite(redLEDPin, LOW);
delay(1000);
}
}
The red LED to RGB LED sequencing Arduino Uno code
Upload the code shown above to the Arduino Uno and your RGB sequencer device should now be operational. The red LED will flash, indicating the code has been successfully uploaded to the Arduino Uno. You can see the final design in action here:
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum