How to Make LEDs Brighter or Dimmer Using PICAXE 40x1

I have an idea for my next project after my color sensor and Mr Basic kit, but I need to control a ton of LEDs using 3 picaxe 40x1s. I need to be able to adjust the amount of current to the LEDs using the out pins of the PICAXE 40x1. I know I can use resistors, but I need the pic to be able to adjust them on the fly. Any ideas? I was looking at PWM but the manual says it is x2 only. I also need to use every out pin on the picaxe 40x1 as well as converting the in/out pins to out. Basically I will be controlling 144 tri color LEDs and select the brightness of R G and B to make a custom color.

the pwm command is only for
the pwm command is only for the x2. for the x1 you should use "pwmout".

Pwmout can only be used on
Pwmout can only be used on the output pin 1 or 2 on the 40x1. I need a solution that will work on all out pins as well as the out/in pins when set for out. That’s where I’m stumped.

a question: do all the LEDs

a question: do all the LEDs in your matrix have to be of the same brightness (= do you need one pwm per led or just one pwm signal for all of them?)

if they can have a common pwm signal then it might be cheap to do.

36 different PWN signals (3
36 different PWN signals (3 for each row (since each row has 3 anodes)) cathodes are shared in columns. This way I can light up any LED using any one of its 3 colors in any strength.

Use a software PWM

I kind of answered this same question in another post, but it was based on assembly language.

https://www.robotshop.com/letsmakerobots/node/6282#comment-20354

The idea remains the same though. You basically have a ‘brightness’ variable and a counter for each LED Thats 432 counters and 432 “brightness” levels. I don’t know anything about Picaxe basic, but if you limit your brightness values to be between 0 and 100 ( think of that as percent ), the algorithm is as follows …

For every column …
{
Turn on the column

For every LED in the column …
{
counter = counter + brightness
if (counter > 99)
{
counter = counter - 100
LED = ON
}
else
{
LED = OFF
}
}

turn off the column
}

Just keep running that loop over and over. I’m not sure how long it will take to process 432 LED’s, but a fast Picaxe should work.

That is just an on and off
That is just an on and off not a dimmer. Think of a dimmer switch in a room. It can be off, on all the way or on anywhere in between. The psudo code above is just on or off. That is easy :wink:

This is a dimmer

Just look at one LED for example.

Brightness = 0
Counter will never be greater than 99, so the LED is always off

Brightness = 100
Counter will always be greater than 99, so the LED is always on

Brightness = 25 LED will be "on" 25% of the time
Counter = 0 OFF
Counter = 25 OFF
Counter = 50 OFF
Counter = 75 OFF
Counter = 100 ON --> Counter Reset to 0
Counter = 25 OFF
Counter = 50 OFF
Counter = 75 OFF
Counter = 100 ON --> Counter Reset to 0
Counter = 25 OFF
Counter = 50 OFF
Counter = 75 OFF
Counter = 100 ON --> Counter Reset to 0

Brightness = 75 LED will be "on" 75% of the time
Counter = 0 OFF
Counter = 75 OFF
Counter = 150 ON --> Counter Reset to 50
Counter = 125 ON --> Counter Reset to 25
Counter = 100 ON --> Counter Reset to 0
Counter = 75 OFF
Counter = 150 ON --> Counter Reset to 50
Counter = 125 ON --> Counter Reset to 25
Counter = 100 ON --> Counter Reset to 0

Brightness = 90 LED will be "on" 90% of the time
Counter = 90 OFF
Counter = 180 ON --> Counter Reset to 80
Counter = 170 ON --> Counter Reset to 70
Counter = 160 ON --> Counter Reset to 60
Counter = 150 ON --> Counter Reset to 50
Counter = 140 ON --> Counter Reset to 40
Counter = 130 ON --> Counter Reset to 30
Counter = 120 ON --> Counter Reset to 20
Counter = 110 ON --> Counter Reset to 10
Counter = 100 ON --> Counter Reset to 0
Counter = 90 OFF
Counter = 180 ON --> Counter Reset to 80
Counter = 170 ON --> Counter Reset to 70
Counter = 160 ON --> Counter Reset to 60
Counter = 150 ON --> Counter Reset to 50

Doesnt this just make the
Doesnt this just make the LED stay on for a longer or shorter amount of time? Not really dimming it. I need them to dim so colors that require say 50% power to red, 75% power to blue and 10% power to green can be made. Maybe Im not getting your code but I still dont see how it dims.

This is what a PWM signal does

If the LED is only on for 25% of the time, it gets 25% power and will be 25% as bright ( more or less ). Even the room light dimmer you mentioned above works the same way, power is only allowed to flow some percentage of the time. The lower the percentage, the dimmer the light. Try my algorithm above with one LED connected to a pin of the Picaxe. Change the “brightness” value and see how the brightness changes.

The thing to remember is this is done in a loop. Each time through the loop the LED is either turned ON or OFF based on the “brightness” value. This loop runs so fast you can’t see the LED turning on and off, so it just appears dimmer or brighter.

It would be difficult to
It would be difficult to control 144 LEDs this way especially since each LED has 3 colors. I may do it this way but it will be fun to manage multiple LEDs. I was hoping for a resistor that could be raised or lowered by using code :slight_smile:

They exist
But you would need 36 of them, each is an 8 pin dip.

Hmmm have a link?
Hmmm have a link?

Funny you should ask

http://para.maxim-ic.com/en/search.mvp?fam=dig_pot&tree=digipots

http://www.analog.com/en/digital-to-analog-converters/digital-potentiometers/products/index.html

http://www.microchip.com/ParamChartSearch/chart.aspx?branchID=11026&mid=11

I think it would be cheaper

I think it would be cheaper to buy led matrix controller (I know there must be something like that.) or LCD. Other possibility is to use some more powerful/real programming language (C on atmega or pic) to create software PWM.

 

Other way is to use digital to analog converter. It is possible to build one using ADC, digital output and capacitor. Or you can buy DA converter.

I think RascalRobot’s idea

I think RascalRobot’s idea is cool, but will it be fast enough for a picaxe to handle? They say it runs at 1000 instructions per second with a 4Mhz resonator, so you might need a high-freq resonator or some other chip like a bare PIC or AVR, not to forget the Propeller with 8 cpus.

Weirdo: are there matrix controllers which are also specialized in delivering PWMed currents?

I think you may be a bit
I think you may be a bit confused regarding programming languages - C is no more ‘powerful’ or ‘real’ than any other language; all programming languages have the same μC resources at the end of the day. As far as the PWM example goes, it happens to be very easy to program a PWM software routine in Assembly (15 lines for a 256-shade 8bit controller).

I may just settle for them
I may just settle for them to glow at full blast and limit the color possbilities for now. I guess it may be time for me to stray from picaxe and run with the big boys.

Does anyone with a 28 or
Does anyone with a 28 or 40x2 know if all the out pins can use pwm? If so I may get a few x2s and make this using those chips. According to the manual you can use pwm on all i/o pins.

I would try software pwm on
I would try software pwm on the 40x1 first, like rascal suggested. If you have them handy theres nothing to lose except maybe a couple of hours of time, and the 40x2 pwm on all pins function is only going to be software as well. You could probably take a peek inside the picaxe pwm library and see how they did it.