Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Arduino Nano and PWM on a RGB-led

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-ARDUINO
View previous topic :: View next topic  
Author Message
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Sun Sep 07, 2014 7:28 pm    Post subject: Arduino Nano and PWM on a RGB-led Reply with quote

No rocket-science but nice to show PWM testprogram for a RGB-led


The RGB-led on a PCB. R G B labeling not correct. But no problem.


Three different colors. In this example an ETT Arduino Nano compatible is used. With an Atmega168.

Code:


' PWM op Arduino Nano atmega168 with RGB led

$regfile = "m168def.dat"                                    ' specify the used micro
$crystal = 16000000                                         ' used crystal frequency

$hwstack = 32
$swstack = 32
$framesize = 32

$baud = 9600

Red_pwm Alias Pwm0a
Green_pwm Alias Pwm1a
Blue_pwm Alias Pwm2a

Config Timer0 = Pwm , Compare_a_pwm = Clear_up , Compare_b_pwm = Clear_down , Prescale = 1
Config Timer1 = Pwm , Pwm = 8 , Compare_a_pwm = Clear_up , Compare_b_pwm = Clear_down , Prescale = 1
Config Timer2 = Pwm , Compare_a_pwm = Clear_up , Compare_b_pwm = Clear_down , Prescale = 1

Config Portb.3 = Output                                     'red
Config Portb.1 = Output                                     'green
Config Portd.6 = Output                                     'blue

Dim X As Byte

Print Chr(27) ; "[2J"                                       'ANSI CLS goto 0/0
Print "Arduino Nano Atmega168 RGB PWM Example"
Print

Red_pwm = 0
Green_pwm = 0
Blue_pwm = 0

Print
Print "test RED"
For X = 1 To 255
Red_pwm = X
Waitms 10
Next X
Red_pwm = 0

Print
Print "test GREEN"
For X = 1 To 255
Green_pwm = X
Waitms 10
Next X
Green_pwm = 0

Print
Print "test BLUE"
For X = 1 To 255
Blue_pwm = X
Waitms 10
Next X
Blue_pwm = 0

Print
Print "All RGB colors together"
For X = 1 To 255
Red_pwm = X
Green_pwm = X
Blue_pwm = X
Waitms 10
Next X

Red_pwm = 0
Green_pwm = 0
Blue_pwm = 0

Print

Do
Input "Give PWM value for RED   (0 - 255) " , X
Red_pwm = X
Print
Input "Give PWM value for GREEN (0 - 255) " , X
Green_pwm = X
Print
Input "Give PWM value for BLUE  (0 - 255) " , X
Blue_pwm = X
Print
Loop

End

 


Three separate timers are used.

And here the images on a mini-scope


PWM 50 and PWM 200 used.

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Mon Sep 08, 2014 3:28 pm    Post subject: Reply with quote

good !
I use it with the lcd display 16*2 from DFrobot , the light is drived by a PWM:

Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portb.1 , Rs = Portb.0
Config Lcd = 16 * 2

Enable Interrupts
Config Pinb.2 = Output ' Timer 1 for PWM
Config Timer1 = Pwm , Pwm = 10 , Prescale = 1 , Compare B Pwm = Clear Up
'the output for PWM1 is PB1 (OC1A) and PB2 (OC1B) here we use OC1B
Pwm1b = 200 'you can change the light by increasing or decreasing it, max 255

many thanks
JP Wink
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-ARDUINO All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum