Project updated to beta 6.0.1 (released on December 20th, 2021)
The intention of this project is to show you how to drive RGB LEDs using Meadow.Foundation by simply instantiating RgbLed
or RgbPwmLed
objects and use their intuitive APIs that will simplify your hardware solution at the software level.
RGB (Red, Green, Blue) LED lights are diodes that have four legs - one for each of the colors mentioned and one for a common cathode (ground) or common anode (vcc), which is also the longest one.
If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LED project to properly set up your development environment.
Working with RgbLedIn this first section, we'll work with an RGB LED driver included in Meadow.Foundation specified as RgbLed
using three digital input ports. Since this driver uses only digital output ports, we can only set individual pins to high or low, meaning that you can only combine them to make seven different colors.
Assemble the circuit
To wire a common cathode RGB LED, connect it like the diagram below, and use 220 ohm resistors to connect the Red, Green and Blue pins with the D02
, D03
and D04
pins on the Meadow board. It should look like this:
Create a project
In Visual Studio 2019, create a new Meadow Application project, and in the MeadowApp class, copy the following code:
public class MeadowApp : App<F7Micro, MeadowApp>
{
RgbLed rgbLed;
public MeadowApp()
{
Initialize();
TestRgbLed();
}
void Initialize()
{
var onboardLed = new RgbPwmLed(device: Device,
redPwmPin: Device.Pins.OnboardLedRed,
greenPwmPin: Device.Pins.OnboardLedGreen,
bluePwmPin: Device.Pins.OnboardLedBlue);
onboardLed.StartPulse(Color.Red);
rgbLed = new RgbLed(
Device.CreateDigitalOutputPort(Device.Pins.D02),
Device.CreateDigitalOutputPort(Device.Pins.D03),
Device.CreateDigitalOutputPort(Device.Pins.D04)
);
onboardLed.StartPulse(Color.Green);
}
void TestRgbLed()
{
Console.WriteLine("TestRgbLed...");
while (true)
{
Console.WriteLine("Going through each color on each RGB LED...");
for (int i = 0; i < (int)RgbLed.Colors.count; i++)
{
rgbLed.SetColor((RgbLed.Colors)i);
Thread.Sleep(500);
}
}
}
}
In the Initialize
method, we initialize an RgbLed
object, passing three IDigitalOutputPort
on pins D02
, D03
and D04
by creating them on Meadow doing Device.CreateDigitalOutputPort(IPin pin));
Once rgbLed
has been initialized from the constructor it calls TestRgbLed()
method, where the app goes inside an infinite while loop in which enters a For loop that iterates through each value of the RgbLed.Colors enum that corresponds to eight possible colors: Red, Green, Blue, Yellow, Magenta, Cyan, White (all pins set to high) and Black (all pins set to low). Each color is set for 500ms (half a second).
Run the project
Click the run button in Visual Studio to see your RGB LED goes through all the possible colors for half a second! It should be similar to the following gif:
In the previous section we can see that working with digital output ports, we can make the RGB LED set each pin to High or Low, resulting in a very limited number of colors. What could we do to control the intensity of each pin so we can further expand the variety of colors or make a pulsing animation. For that we'll use RgbPwmLed
, another RGB LED driver that is controlled with pulse-with-modulation capable ports.
Important Note: Meadow only supports PWM ports from pins D02
to D13
.
Pulse With Modulation is a way of controlling voltage digitally to emulate an analog signal. Meadow boards generate this signal as a square wave. The two key parameters under the control of the developer are the frequency and duty cycle. You can read more here.
Assemble the circuit
For this section of the project, you no longer need the resistors to connect the RGB LED. Make the same connections with jumper or solid core wires.
Create a project
In our existing project (or feel free to create a new project), copy the following code:
public class MeadowApp : App<F7Micro, MeadowApp>
{
RgbPwmLed rgbPwmLed;
public MeadowApp()
{
Initialize();
TestRgbLed();
}
void Initialize()
{
var onboardLed = new RgbPwmLed(device: Device,
redPwmPin: Device.Pins.OnboardLedRed,
greenPwmPin: Device.Pins.OnboardLedGreen,
bluePwmPin: Device.Pins.OnboardLedBlue);
onboardLed.StartPulse(Color.Red);
rgbPwmLed = new RgbPwmLed(
Device.CreatePwmPort(Device.Pins.D02),
Device.CreatePwmPort(Device.Pins.D03),
Device.CreatePwmPort(Device.Pins.D04)
);
onboardLed.StartPulse(Color.Green);
}
void TestRgbLed()
{
Console.WriteLine("TestRgbLed...");
while (true)
{
for (int i = 0; i < 360; i++)
{
var hue = ((double)i / 360F);
rgbPwmLed.SetColor(Color.FromHsba(((double)i / 360F), 1, 1));
Console.WriteLine($"Hue {hue}");
Thread.Sleep(25);
}
}
}
}
In the Initialize
method, we initialize an RgbPwmLed
object, passing three PWM on Pins D02
(red), D03
(green) and D04
(blue) calling the Device.CreatePwmPort(IPin pin);
method.
Once rgbPwmLed
has been initialized, from the constructor it calls TestRgbPwmLed()
method, where the app goes inside an infinite while loop in which enters a For
loop, where a local double hue
is declared and assigned the result of dividing i/360. After that, we call the SetColor(Color.FromHsba(hue, 1, 1))
method on the rgbPwmLed
object, and finally do a Thread.Sleep(25)
to see all the colors at a moderate speed.
Run the project
Click the run button in Visual Studio to see your RGB LED transition to different colors! It should be similar to the following gif:
This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.
- It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
- The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.
Comments