All projects
01Live

RGB Light Show

Millions of colors from one on-board LED via software PWM.

GPIOPWM
01

What you learn

GPIO output, system clock setup, and brightness control via software PWM. No external parts.

02

Parts you need

03

Code

main.c
1#include <stdint.h>
2#include "inc/hw_memmap.h"
3#include "driverlib/sysctl.h"
4#include "driverlib/gpio.h"
5 
6#define LED_ALL (GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3)
7#define PWM_STEPS 256
8 
9static void showColor(uint8_t r, uint8_t g, uint8_t b, uint32_t periods) {
10 volatile uint32_t p, i;
11 for (p = 0; p < periods; p++)
12 for (i = 0; i < PWM_STEPS; i++) {
13 uint8_t out = 0;
14 if (i < r) out |= GPIO_PIN_1;
15 if (i < g) out |= GPIO_PIN_3;
16 if (i < b) out |= GPIO_PIN_2;
17 GPIOPinWrite(GPIO_PORTF_BASE, LED_ALL, out);
18 SysCtlDelay(2);
19 }
20}
21 
22// Color wheel: 0..255 -> a rainbow color
23static void wheel(uint8_t pos, uint8_t *r, uint8_t *g, uint8_t *b) {
24 if (pos < 85) { *r = 255 - pos * 3; *g = pos * 3; *b = 0; }
25 else if (pos < 170) { pos -= 85; *r = 0; *g = 255 - pos * 3; *b = pos * 3; }
26 else { pos -= 170; *r = pos * 3; *g = 0; *b = 255 - pos * 3; }
27}
28 
29int main(void) {
30 SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL |
31 SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
32 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
33 while (!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF)) {}
34 GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, LED_ALL);
35 
36 uint8_t r, g, b; uint16_t pos;
37 while (1)
38 for (pos = 0; pos < 256; pos++) {
39 wheel((uint8_t)pos, &r, &g, &b);
40 showColor(r, g, b, 3); // smooth transition
41 }
42}
04

Output

The simulation below runs in your browser — it mimics the real board.

wheel
0/255
RGB
255,0,0