To turn on the white led……!!!

Photo of author

By Jackson Taylor

Home » Electronics Topics » Arm M4 - Tm4C123G-Energia » To turn on the white led……!!!

As an Amazon Associate we earn from qualifying purchases.

Some development boards have a single RGB LED but no separate white one. The board does not offer a white setting, so you make white yourself by switching on all three colors at the same time. This guide shows how to do that on a Tiva C series TM4C123G LaunchPad using Energia, and explains why it works.

Why there is no white LED on the board

The on-board RGB LED is really three small LEDs in one package: red, green and blue. Each one has its own pin. There is no fourth white LED, so a command like “turn on white” does not exist.

Your eye sees white light when red, green and blue are mixed in roughly equal amounts. If all three LEDs are lit together and sit close enough, they blend into something that looks white. That is the whole trick.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

Which pins control which color

On this board the three LEDs are wired to port F:

  • PF1 controls the red LED.
  • PF2 controls the blue LED.
  • PF3 controls the green LED.

Note that blue comes before green in the pin order. It is easy to mix them up when you write the code, so keep the list above handy.

What you need

  • A Tiva C LaunchPad with the on-board RGB LED.
  • A USB cable to power and program the board.
  • Energia installed on your computer, with the correct board selected.

You do not need any extra wiring or resistors, because the LED is already connected on the board.

Code to turn on the white LED

The sketch has two parts. In setup() you set the three pins as outputs. In loop() you set all three pins high so that red, blue and green light up together.

void setup()
{
    pinMode(PF_1, OUTPUT);   //sets PF1 pin as output(RED LED locate here)
    pinMode(PF_2, OUTPUT);   //sets PF2 pin as output(BLUE LED locate here)
    pinMode(PF_3, OUTPUT);   //sets PF3 pin as output(GREEN LED locate here)
}
void loop()
{
    digitalWrite(PF_1, HIGH);    // Sets PF1 in high state(RED LED on)
    digitalWrite(PF_2, HIGH);    // Sets PF2 in high state(BLUE LED on)
    digitalWrite(PF_3, HIGH);    // Sets PF3 in high state(GREEN LED on)
}

One small fix compared with the original comments: the last line sets PF3 high, not PF2. The code itself was already correct.

See also
ULN2003

How the code works

setup()

setup() runs once when the board starts or resets. pinMode(pin, OUTPUT) tells the microcontroller that the pin will drive a signal out, rather than read one in. Without this step the LED will not light properly.

loop()

loop() runs over and over. digitalWrite(pin, HIGH) puts the pin at its high voltage, which turns the matching LED on. Because all three pins are set high, all three colors shine at once and you see white.

Writing the same values again and again does no harm. If you prefer, you could move the three digitalWrite calls into setup() and leave loop() empty, since the state never changes.

Upload and test

  1. Connect the LaunchPad to your computer with the USB cable.
  2. Open Energia and paste in the sketch.
  3. Select the correct board and port in the Tools menu.
  4. Click Upload.
  5. When the upload finishes, look at the RGB LED. It should glow white.

If the light does not look pure white

The result is often a slightly tinted white, for example a bit blue or a bit green. This is normal. The three LEDs inside the package are not equally bright, and the human eye reads each color differently, so equal power does not give a perfectly balanced mix.

You can improve it in a few ways:

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
  • Look at the LED from a slight distance or through a piece of white paper, which helps the colors blend.
  • Use PWM with analogWrite() to lower the brightest color, if the pin supports it on your board. This lets you tune the balance by trial and error.
  • Avoid looking straight at the LED from very close, since the individual colors are more visible there.

If you want to see how PWM changes brightness, the Arduino LED fading project explains the idea on a different board, and the same principle applies here.

See also
ARM M4 - TM4C123G-ENERGIA

Troubleshooting

  • Only one or two colors light up: check that all three pinMode and digitalWrite lines are present and that each pin name is correct.
  • The wrong color appears: you may have swapped PF2 and PF3. Remember that PF2 is blue and PF3 is green.
  • Nothing happens after upload: confirm the right board and port are selected and that the upload finished without errors.
  • The code will not compile: make sure you are using Energia and the pin names are written as PF_1, PF_2 and PF_3.

Where to go next

Once white works, you can mix other colors by turning on only two of the three LEDs. Red and green together give a yellowish color, red and blue give magenta, and blue and green give cyan. Adding a delay and switching between combinations makes a simple color cycle.

If you are new to LED code in general, the Arduino LED blinking project is a good starting point, and you can also see how to control RGB LED lights with Arduino to compare the approach. For more projects on this board, see the guide to the Tiva C series TM4C123G with Energia.

FAQ

Does the board have a built-in white LED?

No. The on-board LED is an RGB LED, so white is made by turning on red, blue and green together.

Which pins do I use for white?

Use PF1 for red, PF2 for blue and PF3 for green. Set all three as outputs and drive them high.

Why does my white look slightly colored?

The red, green and blue LEDs have different brightness levels, so an equal mix is rarely perfectly neutral. Adjusting brightness with PWM can help.

Can I turn the white light off?

Yes. Use digitalWrite(pin, LOW) on PF1, PF2 and PF3 to switch all three LEDs off.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works