Easy PWM Dimmer

Sili

New Member
Hi all!

i just got my 08M2 on an AXE230 Board.

Id like to use PWM for the first time. Setup is very easy. Mechanical switch at P3, LED at P2.

If the Button is pushed, the Led should start Fading in until 100% brightness are reached, pause for a second or so and then start fading out, returning to the main tab, waiting for an input.

it might be very easy, but i didnt find a tutorial which i understood :(

maybe someone could help me with a snippet of code.

thanks so much!
 

Captain Haddock

Senior Member
This is a very simple led dimmer using a 10K pot at C.1 across 0V & 5V, works fine for me.(Can't remember why I added the +3 at line 5 though)

Code:
#picaxe 08m2
main:
setfreq m8
	readadc C.1,b0;
	let w1 = b0 * 4+3
	pwmout c.2, 255, w1
debug
goto main
 

nick12ab

Senior Member
To create a dimming effect using PWM, you can use a for loop. The for loop needs to contain a pwmduty command that will set the duty cycle of the pwm signal that you create at the beginning of the program by using the pwmout command.

In the for loop, there is no need to have a load of if statements to determine the duty cycle; you use the counter variable that you used for the for loop.

Example code:
Code:
#picaxe 08m2

symbol pushbutton = pinC.3
symbol led = C.2
symbol counter = b4


	pwmout led,63,0
main:
	if pushbutton = 1 then
		for counter = 0 to 254
			pwmduty led,counter
			pause 10
		next
		pause 1000
		for counter = 255 to 1 step -1
			pwmduty led,counter
			pause 10
		next
	end if
	goto main
 

Sili

New Member
Hi Haddock.

Thank you for that example. i didnt use a pot for that.

i tried this one:
Code:
init:                                                                     
            symbol Counter=b0                            

main: high 2
if input3 = 0 then dim
goto main


dim:               
           For Counter = 255 to 0 step -1          
                   PWM 2, Counter, 1
                   
           Next Counter                                        
           For Counter = 0 to 255 step 1                    
                   PWM 2, Counter, 1                      
           Next counter                                        
 
	     
           goto main                                              
           End
i used a mechanical switch (n.c, thats why its set to input3=0)
is is possible to set to brightness to which it dimms down and the speed with that it is dimming to a random amount using the random command?
the target is to get an unstable (smooth) "flicker" of light
 

Paix

Senior Member
Welcome to the forum Sill, do you mean a flickering flame effect, like that of a candle?

Just to clarify for others.
 

Sili

New Member
Welcome to the forum Sill, do you mean a flickering flame effect, like that of a candle?

Just to clarify for others.
Hey Paix,

Something like that yeah. In fact im working on a new Version of a flickering light. For some Halloween Setups i use a easy Setup with a lamp starter for fluorescent lamps within a A/C Cable. With that i get an effect like this:

But what i want is a better circuit (without the starter!) which provides a better effect bad electricity. not a full shut-down of the ligh, more like a loss of voltage on the powerline.
It should look like in horrormovies, when the young girl enters the basement and suddenly the lights start so flicker before it gets dark.. maybe you know what i mean.

i think it could be the effect of an candle just a litte bit slowed down!
 

eclectic

Moderator
As a little experiment,
connect your LED/resistor to input/output 2.

Then run the following program:

Code:
#picaxe 08M2

; see Manual 2 PLAY command

Main:

play 1, 0
sertxd ("1",cr,lf)

pause 1000

play 2 ,0
sertxd ("2",cr,lf)

pause 1000

Play 3,0
sertxd ("3",cr,lf)

pause 1000

Play 4, 0
sertxd ("4",cr,lf)

Pause 1000

goto main
e
 

Sili

New Member
As a little experiment,
connect your LED/resistor to input/output 2.

Then run the following program:

Code:
#picaxe 08M2

; see Manual 2 PLAY command

Main:

play 1, 0
sertxd ("1",cr,lf)

pause 1000

play 2 ,0
sertxd ("2",cr,lf)

pause 1000

Play 3,0
sertxd ("3",cr,lf)

pause 1000

Play 4, 0
sertxd ("4",cr,lf)

Pause 1000

goto main
e
Is it playing Silent Night and Rudolph with an LED instead of a speaker??? :confused:
 

eclectic

Moderator
Alternatively, for a "crackly bulb",
connect a Piezo in parallel to the LED/resistor.

Code:
#picaxe 08M2

pwmout C.2, 63, 63

do
random W0

pwmduty C.2, b0

loop
e
 

Sili

New Member
See post #10, then try both methods.

e
Thats crazy. :) I tried that one without the piezo:

Code:
picaxe 08M2

; see Manual 2 PLAY command

Main:

play 1, 0
sertxd ("1",cr,lf)

pause 1000

play 2 ,0
sertxd ("2",cr,lf)

pause 1000

Play 3,0
sertxd ("3",cr,lf)

pause 1000

Play 4, 0
sertxd ("4",cr,lf)

Pause 1000

goto main
thats very close to what i want, but its still very "hard" (like the fluorescent lamp starters). i miss the smooth change of brightness as achieved with the pwm code
 

nick12ab

Senior Member
i used a mechanical switch (n.c, thats why its set to input3=0)
is is possible to set to brightness to which it dimms down and the speed with that it is dimming to a random amount using the random command?
the target is to get an unstable (smooth) "flicker" of light
Yes you can use the random command so that the light dims down to a random amount or at a random speed. Here is some code that is not written very well and is uncommented but it does produce a random flicker and fade effect.
Code:
[color=Navy]#picaxe [/color][color=Black]08m2[/color]

[color=Blue]symbol [/color][color=Black]pushbutton [/color][color=DarkCyan]= [/color][color=Purple]pinC.3[/color]
[color=Blue]symbol [/color][color=Black]led [/color][color=DarkCyan]= [/color][color=Blue]C.2
symbol [/color][color=Black]random1 [/color][color=DarkCyan]= [/color][color=Purple]w0[/color]
[color=Blue]symbol [/color][color=Black]random1msb [/color][color=DarkCyan]= [/color][color=Purple]b1[/color]
[color=Blue]symbol [/color][color=Black]random2 [/color][color=DarkCyan]= [/color][color=Purple]w1[/color]
[color=Blue]symbol [/color][color=Black]random2msb [/color][color=DarkCyan]= [/color][color=Purple]b3[/color]
[color=Blue]symbol [/color][color=Black]random3 [/color][color=DarkCyan]= [/color][color=Purple]w2[/color]
[color=Blue]symbol [/color][color=Black]random3msb [/color][color=DarkCyan]= [/color][color=Purple]b5[/color]
[color=Blue]symbol [/color][color=Black]counter [/color][color=DarkCyan]= [/color][color=Purple]b6[/color]
[color=Blue]symbol [/color][color=Black]tempvar1 [/color][color=DarkCyan]= [/color][color=Purple]b7[/color]
[color=Blue]symbol [/color][color=Black]tempvar2 [/color][color=DarkCyan]= [/color][color=Purple]b8[/color]
[color=Blue]symbol [/color][color=Black]adc1 [/color][color=DarkCyan]= [/color][color=Blue]C.1
symbol [/color][color=Black]adc2 [/color][color=DarkCyan]= [/color][color=Blue]C.4

      setfreq m16
      pwmout [/color][color=Black]led,[/color][color=Navy]63[/color][color=Black],[/color][color=Navy]0
      [/color][color=Blue]readadc10 [/color][color=Black]adc1,random1              [/color][color=Green]'Seed for random command using ADC
      [/color][color=Blue]readadc10 [/color][color=Black]adc2,random2
      random3 [/color][color=DarkCyan]= [/color][color=Black]random1 [/color][color=DarkCyan]+ [/color][color=Black]random2
main:
      [/color][color=Blue]if [/color][color=Black]pushbutton [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then
            random [/color][color=Black]random1
            [/color][color=Blue]random [/color][color=Black]random2
            [/color][color=Blue]random [/color][color=Black]random3
            tempvar2 [/color][color=DarkCyan]= [/color][color=Black]random3msb [/color][color=DarkCyan]& [/color][color=Navy]7
            [/color][color=Blue]for [/color][color=Black]counter [/color][color=DarkCyan]= [/color][color=Black]random1msb [/color][color=Blue]to [/color][color=Black]random2msb
                  [/color][color=Blue]pwmduty [/color][color=Black]led,counter
                  [/color][color=Blue]pause [/color][color=Black]tempvar2
            [/color][color=Blue]next
            random [/color][color=Black]random1
            [/color][color=Blue]random [/color][color=Black]random2
            [/color][color=Blue]random [/color][color=Black]random3
            tempvar2 [/color][color=DarkCyan]= [/color][color=Black]random3msb [/color][color=DarkCyan]& [/color][color=Navy]7
            [/color][color=Blue]for [/color][color=Black]counter [/color][color=DarkCyan]= [/color][color=Black]random1msb [/color][color=Blue]to [/color][color=Black]random2msb [/color][color=Blue]step [/color][color=DarkCyan]-[/color][color=Navy]1
                  [/color][color=Blue]pwmduty [/color][color=Black]led,counter
                  [/color][color=Blue]pause [/color][color=Black]tempvar2
            [/color][color=Blue]next
      end if
      goto [/color][color=Black]main[/color]
 

eclectic

Moderator
Another, using a parallel Piezo and LED

Code:
#picaxe 08M2

symbol centre = 128
symbol newval = b3

pwmout C.2, 63, centre

do
random W0

If b0 <= centre then
newval = centre - b0
gosub downwards

else newval = centre + b0
gosub upwards

endif

pwmduty C.2, newval

loop

upwards:
for b10 = 128 to newval
pwmduty C.2, b10
next
return

downwards:
for b10 = 128 to newval step -1
pwmduty C.2, b10
next
return
 

Sili

New Member
Yes you can use the random command so that the light dims down to a random amount or at a random speed. Here is some code that is not written very well and is uncommented but it does produce a random flicker and fade effect.
Code:
[color=Navy]#picaxe [/color][color=Black]08m2[/color]

[color=Blue]symbol [/color][color=Black]pushbutton [/color][color=DarkCyan]= [/color][color=Purple]pinC.3[/color]
[color=Blue]symbol [/color][color=Black]led [/color][color=DarkCyan]= [/color][color=Blue]C.2
symbol [/color][color=Black]random1 [/color][color=DarkCyan]= [/color][color=Purple]w0[/color]
[color=Blue]symbol [/color][color=Black]random1msb [/color][color=DarkCyan]= [/color][color=Purple]b1[/color]
[color=Blue]symbol [/color][color=Black]random2 [/color][color=DarkCyan]= [/color][color=Purple]w1[/color]
[color=Blue]symbol [/color][color=Black]random2msb [/color][color=DarkCyan]= [/color][color=Purple]b3[/color]
[color=Blue]symbol [/color][color=Black]random3 [/color][color=DarkCyan]= [/color][color=Purple]w2[/color]
[color=Blue]symbol [/color][color=Black]random3msb [/color][color=DarkCyan]= [/color][color=Purple]b5[/color]
[color=Blue]symbol [/color][color=Black]counter [/color][color=DarkCyan]= [/color][color=Purple]b6[/color]
[color=Blue]symbol [/color][color=Black]tempvar1 [/color][color=DarkCyan]= [/color][color=Purple]b7[/color]
[color=Blue]symbol [/color][color=Black]tempvar2 [/color][color=DarkCyan]= [/color][color=Purple]b8[/color]
[color=Blue]symbol [/color][color=Black]adc1 [/color][color=DarkCyan]= [/color][color=Blue]C.1
symbol [/color][color=Black]adc2 [/color][color=DarkCyan]= [/color][color=Blue]C.4

      setfreq m16
      pwmout [/color][color=Black]led,[/color][color=Navy]63[/color][color=Black],[/color][color=Navy]0
      [/color][color=Blue]readadc10 [/color][color=Black]adc1,random1              [/color][color=Green]'Seed for random command using ADC
      [/color][color=Blue]readadc10 [/color][color=Black]adc2,random2
      random3 [/color][color=DarkCyan]= [/color][color=Black]random1 [/color][color=DarkCyan]+ [/color][color=Black]random2
main:
      [/color][color=Blue]if [/color][color=Black]pushbutton [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then
            random [/color][color=Black]random1
            [/color][color=Blue]random [/color][color=Black]random2
            [/color][color=Blue]random [/color][color=Black]random3
            tempvar2 [/color][color=DarkCyan]= [/color][color=Black]random3msb [/color][color=DarkCyan]& [/color][color=Navy]7
            [/color][color=Blue]for [/color][color=Black]counter [/color][color=DarkCyan]= [/color][color=Black]random1msb [/color][color=Blue]to [/color][color=Black]random2msb
                  [/color][color=Blue]pwmduty [/color][color=Black]led,counter
                  [/color][color=Blue]pause [/color][color=Black]tempvar2
            [/color][color=Blue]next
            random [/color][color=Black]random1
            [/color][color=Blue]random [/color][color=Black]random2
            [/color][color=Blue]random [/color][color=Black]random3
            tempvar2 [/color][color=DarkCyan]= [/color][color=Black]random3msb [/color][color=DarkCyan]& [/color][color=Navy]7
            [/color][color=Blue]for [/color][color=Black]counter [/color][color=DarkCyan]= [/color][color=Black]random1msb [/color][color=Blue]to [/color][color=Black]random2msb [/color][color=Blue]step [/color][color=DarkCyan]-[/color][color=Navy]1
                  [/color][color=Blue]pwmduty [/color][color=Black]led,counter
                  [/color][color=Blue]pause [/color][color=Black]tempvar2
            [/color][color=Blue]next
      end if
      goto [/color][color=Black]main[/color]
ADC imput means that there are Pots involved?
I dont want to give input to the circuit! The values should be generated totaly random without a external input. Or is the code missunderstood by me?
 

nick12ab

Senior Member
ADC imput means that there are Pots involved?
I dont want to give input to the circuit! The values should be generated totaly random without a external input. Or is the code missunderstood by me?
No they are left floating. When left floating the ADC values will be somewhat random so it will prevent the flicker sequence being the same every time you turn it on.
 

Sili

New Member
I tried both of em now.

These are exactly what i needed! Awesome. I think im still deeper into the PWM thing now. i like it :)

Thanks for your help! i appreciate that!
 
Top