Go Down

Topic: limiting max PMW with code (Read 1 time) previous topic - next topic


Hi all, I am doing my first real arduino project with my niece, making a RGB color mixing box/projector for her school science fair project.  I am using a duemilanove 168 to drive a 10w smd rgb led using resistors and three TIP122 transistors. Three pots (with rgb painted knobs of course) provide the analog input.  I am desperately trying to learn to code, its one of the few things I haven't been able to learn on my own on the net, it's a little daunting at times.    My problem is this led is way too bright and gets too hot, I don't want it blinding children while melting down in the classroom.  The other problem is I accidently burned out one of the three red led's in the module(9 total) by using too little resistance while testing so red is a little dimmer.  I don't have the time or extra money right now to rebuild so what I want to do is limit the max duty cycle of the pwm of each led output until I find a not so bright cool running temp and all are even.  But I also want the pots to smoothly vary the output from 0%-max(limited) pwm.  I tried to play with the code a little but got some wonky results, I just don't have enough practice to get a good enough understanding on how to modify it although I clearly grasp the concepts.  I have many more arduino projects planed so does anyone have a good site or book they recommend to a programming noob? Thanks in advance for any help, here is the code I'm using:

// Init the Pins used for PWM
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;

// Init the Pins used for 10K pots
const int redPotPin = 0;
const int greenPotPin = 1;
const int bluePotPin = 2;

// Init our Vars
int currentColorValueRed;
int currentColorValueGreen;
int currentColorValueBlue;

void setup()
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

void loop()
// Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
  currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
  currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
  currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );

// Write the color to each pin using PWM and the value gathered above
  analogWrite(redPin, currentColorValueRed);
  analogWrite(bluePin, currentColorValueBlue);
  analogWrite(greenPin, currentColorValueGreen);



Code: [Select]
  currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );

The 255 is the maximum value for the PWM pin. If you want a lower maximum value, it should be pretty easy to see what to change...


Jan 22, 2013, 06:25 pm Last Edit: Jan 22, 2013, 06:29 pm by JimboZA Reason: 1
I'm open to correction but I think you could reduce the max pwm value in the map where 1023 becomes 255, you could make that less say 150.

Also look at constrain

edit.... on second thoughts reducing the 255 wont work because it gets subtracted from 255 again.... in fact have to increase the 0 side to say 100 so the subtracted value is between 255-100 = 150 or 255-255 =0
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0


Jan 22, 2013, 06:30 pm Last Edit: Jan 22, 2013, 06:33 pm by HazardsMind Reason: 1
If the LED is too bright, you could get a resistor and wire it to the pin from the LED going to ground OR get 3 resistors and wire them to the 3 pins for the colors.

Another alternative would be to use another PWM pin and connect the LED common wire to it and adjust the brightness in the software. No resistors required.
My GitHub:


hey thanks all for the replies.  Yeah the first thing I did before I asked was change the 255 down to 150 and it did some strange things, cant remember but it did 0-100% on the first half of the pot sweep and did some weird stuff after turning it further.  I added a heat sink and adjusted a resistor as well as made a nimh pack to mount in the box, it runs a few volts lower than the SLA I was using to test it.  I would like to find out how to do it with code still though, I have many automotive projects planned that this would be useful for.


Yeah the first thing I did before I asked was change the 255 down to 150 and it did some strange things, cant remember but it did 0-100% on the first half of the pot sweep

Sounds like you did not change both 255's to 150 in the statements.


to drive a 10w smd rgb led using resistors and three TIP122 transistors

You can not run such a high power LED with just a resistor to limit the current. The forward volts drop changes with temprature and that is why you burnt out your LED. To drive such high powerd lights you need a constant current supply circuit, one for each of the three colours.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131