Nuvoton Project: RGB LED and Fading Effect
Introduction
Hi, this is David Tseng of CAVEDU Education. This is the second article of Nuvoton Numaker-PFM-M487. We’ve covered the introduction of this board and finished the hello world MBed OS example in the previous article. If you are not familiar with M487 yet, please refer to All-in-one and Cloud-Supported Maker Solution: Nuvoton.
In this topic, we are going to play with three examples: 1) read the on-board switch status and control the on-board RGB LED. 2) Connect external RGB LED and LED to the m487 board to achieve the same effect of 1. Finally, 3) we’ll make the LED fading to demonstrate a breathing light effect. You will have a basic understanding of how to implement analog output with the mBed PwmOut systax.
Hardware setup
This section will show you how to finish this example step by step with ARM mbed OS, let’s begin!
1. Get the parts ready
Prepare the parts below:
● NuMaker-PFM-M487 dev board, 1
● RGB LED module (common cathode), 1
● Micro USB OTG connect wire, 1
● PC (with Windows OS) with network connection to access mbed OS online IDE, 1
2. Driver setup
Refer to the M487 page or our previous example to setup the driver on your system. If everything is correct, your m487 will be recognized by your OS as a disk named as NuMicro MCU.
3. Where is the switch and the RGB LED on the m487?
The locations of the on-board switches and LEDs are shown as below and they are labeled as LEDR, LEDY and LEDG. In EX01, we will use the SW1 and SW2 to control the LEDs, and in EX02 and EX03, we will connect a RGB LED and another LED to the digital pins.
EX01 Switch / RGB LED / buzzer project
If you had finished the previous LED example, your mbed console should already have the m487. If not, please refer to the previous article to finish the setup process.
Please follow the steps below to finish your first C++ example on the mbed website.
Step1:
Visit the MBED OS website, login with your account.
Step2:
After logged in, click the upper-right [Compiler] icon to enter the online compiler interface. Your NuMaker-PFM-M487 board should be there, click the board to check all the details.
Step3:
In the M487 page, you can find the detailed information of this board and many starter examples from the right-hand side column.
Step4:
If you have not added M487 into your Mbed Compiler, click the [Add to you Mbed Compiler] to add this board into your compiler.
Step5:
Back to your compiler page, you can see the [NuMaker-PFM-M487] icon. Click [New] and select [Numaker GPIO button with LED and buzzer] in the Template dropdown menu. This will import the example into your compiler directly.
Click to open main.cpp.
Numaker GPIO button with LED and buzzer / main.cpp
#include "mbed.h" // ignore non-m487 defition #elif defined(TARGET_NUMAKER_PFM_M487) DigitalOut rgbled_B(LED_YELLOW); // in M487, rgbled_B is yellow, not blue, low-active DigitalOut rgbled_R(LED_RED); // low-active DigitalOut rgbled_G(LED_GREEN); // low-active DigitalOut greenled(D2); // in M487, change the pin to match real hardware, low-active DigitalOut buzzer(D3); // in M487, change the pin to match real hardware, low-active DigitalIn button_SW1(SW2); // in M487, button_SW1 is SW2, press button =0 DigitalIn button_SW2(SW3); // in M487, button_SW2 is SW3, press button =0 #endif // main() runs in its own thread in the OS // (note the calls to Thread::wait below for d elays) int main() { rgbled_B=1; rgbled_R=1; rgbled_G=1; greenled=1; buzzer=1; #ifdef MBED_MAJOR_VERSION printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); #endif while (true) { // press SW1 will turn on greeled and RGBLED=blue if (button_SW1==0) { greenled=0; rgbled_B=0; } else { greenled=1; rgbled_B=1; } // press SW2 will turn on buzzer if (button_SW2==0) buzzer=0; else buzzer=1; } }
In the while loop of main(), press SW1 will turn on greeled (connect to D2 pin) and the rgbled_B and release to turn them off. Pressing SW2 will turn on buzzer and release to turn it off (no sound). However, we will use another topic to demo how to use a passive buzzer to make sound effect, therefore we keep focused on the LEDs here.
Step6:
Click the [Compile] icon to compile this project. After that, the compiled file will be downloaded to your PC as .bin extension. For our LED example, it should be “NuMaker-mbed-GPIO-button-buzzer-rgbled.NUMAKER_PFM_M487.bin” or other file name you’d like to have.
Step7 – play!
Drag the .bin file into the “NuMicro-MCU” disk drive (yeah, it should appear when you connect the dev board to your PC). Press the SW2 button on the board, the on-board RGY LED and the LED connect to the D2 pin will light up, as shown below:
EX02 Connect a physical RGB LED
Not enough with the basic example? Connect an external RGB LED(common cathode) to the m487 digital pins. For the common cathode RGB LED we chose, the longest pin is connected to the m487 GND pin, and other three pins are red, green and blue respectively and I connect them to m487 D4, D5 and D6, as shown below:
And clone the project to a new project named as Numker-mbed-RGBLED and modify the main.cpp as below:
Numaker-mbed-RGBLED / main.cpp
#include "mbed.h" #if defined(TARGET_NUMAKER_PFM_M487) DigitalOut LED_D4(D4); DigitalOut LED_D5(D5); DigitalOut LED_D6(D6); DigitalIn button_SW1(SW2); DigitalIn button_SW2(SW3); #endif // main() runs in its own thread in the OS // (note the calls to Thread::wait below for d elays) int main() { #ifdef MBED_MAJOR_VERSION printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); #endif while (true) { // press SW1 will turn on greeled and RGBLED=blue if (button_SW1==0) { LED_D4=0; LED_D5=0; LED_D6=0; } else { LED_D4=1; LED_D5=1; LED_D6=1; } } }
The logic is just the same as EX01. Compile and drag the .bin into your NuMicro MCU drive. After flashing the code, pressing the switch will light the RGB LED as white light, you can change the 0/1 to generate other color of light, as shown in below video:
EX03 LED fading
The final example is to demo the fading effect by the mBed PwmOut method. For most Arduino pins supporting PWM are D3, D5, D6, D9, D10 and D11. We are using D6 in this example. Therefore in this example, we connect a LED to the m487 D6 pin and clone the project to a new project named as Numker-mbed-LEDfading and modify the main.cpp as below:
Numaker-mbed-LEDfading / main.cpp
#include "mbed.h" int main() { PwmOut led(D6); // Create the LED object float brightness = 0.0; // How bright the LED is float fadeAmount = 0.02; // How many points to fade the LED by while(1) { led.write(brightness);//Write a PWM analog brightness value on LED wait_ms(30); // wait for 30ms to see the dimming effect brightness = brightness + fadeAmount; // Increase the brightness if((int)brightness == 1.0f) // Turn brightness to 0 at the ends of the fade // f is for literal float - Delete the warning brightness = 0; } }
Compile and drag the .bin into your NuMicro MCU drive. After flashing the code, the LED connected to D6 will light as a fading effect, as shown in below video:
Challenge in 5 minutes
1. Modify EX02, to generate different color of light, e.g. red light only.
2. Modify EX03, to adjust the fading effect (hint: fadeAmount and wait_ms() ).
More examples you can try
● AudioPlayback
● DC servo
● AWS IoT
More info
● https://www.nuvoton.com/products/microcontrollers/arm-cortex-m4-mcus/m487-ethernet-series/?tab=4
● https://docs.aws.amazon.com/zh_tw/freertos/latest/userguide/getting-started-nuvoton-m487.html
● 新唐 NuMaker-IoT-M487 使用 Mbed OS – 連接 Microsoft Azure ( 11 )
● Nuvoton Machine Learning Speech Recognition Solution – implement on NuMaker-PFM-M487 platform
● 新唐 NuMaker-IoT-M487 – step by step 讓你了解如何運行 Mbed OS(1)
● 支援多種雲服務的 NuMaker 物聯網平台
Author
Dr. David Tseng, CAVEDU Education, MIT CSAIL Visiting Scientist, NVIDIA Jetson AI ambassador