Shop OBEX P1 Docs P2 Docs Learn Events
Controlling RGB Leds with PWM in Spin — Parallax Forums

Controlling RGB Leds with PWM in Spin

Hi,

I have some 4 pin, RGB leds that i want to be able to select any colour.
I have never used the PMW features of the propeller chip.

So can anyone tell me how this is done?

I need to control about 4 RGB leds in my current project.
I have 12 spare pins on the propeller 1, so i wish to control the leds directly ( rather than using a dedicated external controller chip)

Can this be done with just 1 extra COG and SPIN code only?

Thanks

Comments

  • If you had considered RGB with a controller then maybe this: https://forums.parallax.com/discussion/175189/cogless-1-wire-pixel-ws2811-ws2812-type-driver

    For direct-drive LEDs there's probably some PWM motor driving code on GitHub that could be a reasonable starting point.

  • Thanks,

    But I have used external controllers for LEDs before, I need to control directly from Propeller if possible.

    I'm surprised no one has done his before in 20 years?

  • I'm surprised no one has done his before in 20 years?

    There are several examples of PWM code for P1 in the Object Exchange repository on GitHub...

    Download the entire ZIP, git clone, or just peruse this repository and copy the code you need:
    https://github.com/parallaxinc/propeller

    Inside the repository:

    propeller/libraries/community/p1/All/led_pwm/
    propeller/libraries/community/p1/All/24-channel LED driver with 8 bit per channel PWM/
    propeller/libraries/community/p1/All/Dedicated pwm generator/
    and, there are more...
    
  • @DavidM said:
    Can this be done with just 1 extra COG and SPIN code only?

    Short answer is no, not easily.

    You get two counters per cog so you get two PWM channels which you can control using Spin in each cog. The example which first comes to mind of controlling two counters from Spin is my H-bridge demo code. There are likely better examples but I remember my own projects more than others I've seen.

    If you're willing to use one cog to generate the PWM from PASM you could use a Spin cog to set the various PWM values for each pin. I believe the examples provided by @dgately take this approach.

    I'll take a look to see if I have any other examples to add to the list provided by @dgately . I'll update this thread if/when I find some of these additional examples.

  • @"Duane Degn" said:

    If you're willing to use one cog to generate the PWM from PASM you could use a Spin cog to set the various PWM values for each pin. I believe the examples provided by @dgately take this approach.

    Hi Duane,

    I think this is the approach, one cog for generating the PWM, the other for controlling the values.
    In my current project i have spare cogs and lots of spare pins.
    I shall take a look at the PWM code in the libraries ( which i have installed )

    Thanks

  • @dgately said:

    I'm surprised no one has done his before in 20 years?

    There are several examples of PWM code for P1 in the Object Exchange repository on GitHub...

    Download the entire ZIP, git clone, or just peruse this repository and copy the code you need:
    https://github.com/parallaxinc/propeller

    Inside the repository:

    propeller/libraries/community/p1/All/led_pwm/
    propeller/libraries/community/p1/All/24-channel LED driver with 8 bit per channel PWM/
    propeller/libraries/community/p1/All/Dedicated pwm generator/
    and, there are more...
    

    Thanks Dgately, I will take a look

  • @dgately said:

    propeller/libraries/community/p1/All/led_pwm/

    I experimented with this one, I had to do some modifications to the spin code, so I could control each led on its own rather than using the demo functions.

    It works PERFECTLY!
    No need for any other IC's!

    Thanks for your help!

  • DavidMDavidM Posts: 626
    edited 2023-02-06 06:53

    OK,

    I Thought I had it working, I discovered that the 32 led array is referenced in reverse order.

    Attached is my cleaned up demo of the 32 PWM LED Driver. ( I started from the demo that was part of the "LED PWM" library)

    It uses only one COG when the driver starts. You need to keep the driver on and running to see any leds , so If you called STOP, all leds with be off and free up the COG.

    It Should run on any propeller 1 board with at least 9 LEDS as per my demo.
    But you can change the starting led pin number and the no of consecutive pins with leds following that.
    I have set this up so i can input values used by DMX lighting systems, so the brightness values are 0-255, but the driver only uses a resolution of 64 values. no big deal.

    Need to call the LED's by number, so your first LED is 1 ( not zero or not the pin number) and so on.
    So if you are using this for an RGB led, then Led 1, 2& 3 are your R, G & B Values of the first LED.

    There is a FADE? function in the Driver, but i did not utilise that in my demo, I just needed simple control of LED Brightness.

    Thanks

Sign In or Register to comment.