RGB Led Control


Hardware Setup:

Get a breadboard ready:)

  1. Determine appropriate resistance values
  2. Attach to any free gpio pins on your raft
  3. Use 3 different color leds ;)
circuit diagram

Software Setup

Turning LED on & off with gpio pin

  1. define pin led is attached to:
    #define BLUE 0
  2. in main, setup gpio as an output:
    gpio_init(BLUE); 
    gpio_set_dir(BLUE, true); //output
  3. in while loop, turn on/off led:
    gpio_put(BLUE, true); //on
    gpio_put(BLUE, false); //off

Driving LED with PWM

Set up LED struct & functions:

Create library file here: rcc-pico/dev/pico/include/led_pwm.h

  1. Have this at top of file:
  2. 
    #include "hardware/pwm.h"
    #include "pico/stdlib.h"
    #include <math.h>
    #include <stdlib.h>
    #include "rcc_pins.h"
    
    using namespace std;
    

    Reference: rcc-pico/dev/pico/include/pwm_helper.h

  3. Create new struct RGBLED of 3 Servo structs, like Motor
  4. Create LEDInit() function, like MotorInit()
  5. Create LEDOn() function, like MotorOn()
  6. Create LEDPower() function, like MotorPower()
  7. hint: use freq around 500 when calling LEDPower()

Test function

Build State Machine

3 led's
tricolor led
Same timing on 3 LED's and Tricolor:)