NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Help with PWM

February 18, 2011
by thatguy
thatguy's Avatar

Hey guys, using PWM I would like to be able to make an LED go from off to fully on, climbing up to the max, at the moment it's maximum brightness is not as bright compared to putting an LED straight into vcc and ground...

here is my code, help!! and thanks in advance !

int main() {

DDRB |= (1<<PB1)|(1<<PB2);
TCCR1A = (1<<COM1A1)|(1<<COM1B1)|(1<<1);
TCCR1B = 1;

//OCR1BH = 0; //OCR1BL = 0;

//OCR1AH = 0;

//OCR1AL = 680; OCR1AH = 0;

while(1) {

    // OCR1AL = 300;
     while(OCR1AH != 2)
           OCR1BH = (OCR1BL - 1);       
          //OCR1BL = (OCR1BL - 1);
          OCR1AH = (OCR1AL - 1);
     OCR1AH = 0;
     OCR1BH = 0;

     while(OCR1AH != 250)
                  //OCR1BL = 20;  
         OCR1BH = (OCR1BH + 1);
         OCR1AH = (OCR1AH + 1);

    // OCR1AL = 50000;

} return 0;


February 20, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi thatguy,

When you put an LED directly across Vcc and GND you are drawing an awful lot of current, in fact you are being limited by the power supply and not the circuit. This is likely to result in either your LED burning out or your battery draining so its not a good idea to do it for too long. Usually when hooking up LEDs you want to use a current limiting resistor so that you can dictate exactly how much current goes through the LED (and how bright it is). When driving LEDs from the MCU there are a couple of reasons you don't really need this current limiting resistor, there is an explanation here.

So basically, it is no surprise the LED is not as bright when you are running it off the MCU, and that brightness is probably much closer to where you want to be running the LED.


Post a Reply

Please log in to post a reply.

Did you know that you can connect to certain car computers via the OBD-II port with a microcontroller? Learn more...