I'm just starting off with using the Arduino and was learning about PWM
Below is what my circuit looks like.

enter image description here

Below is the very simple code.

const int pinNine = 9;    // LED connected to digital pin 9

void setup() {
  // initialize serial communications at 9600 bps:
  // set the digital pins as outputs

void loop() {      
  analogWrite(pinNine, 0); 

I was expecting my LED to not be lighted as its a 0% duty cycle. I even change the pinNine outputValue to (64, 127, etc) but for some reason the LED is always lighted. Any ideas?

  • Loks correct, alas. Try another pin to see if it changes. Try digital write on / off to see that port behaves as expected. Bad port pih possible. What pin is serial on? Jan 30, 2015 at 9:14
  • 1
    Check that the ground (-) rail on the breadboard is definitely going to ground on the Arduino. If it's going to +5v instead, and if your LED is the wrong way round, you could end up with it being lit all the time. (Pin 9 would basically be acting as ground.) Jan 30, 2015 at 10:17
  • See my updated answer. Your code runs as expected on a Nano. Problem is probably hardware related. Jan 30, 2015 at 15:45
  • Where does that red wire go to? i.e. Is the other side of the led connected to 5V by any chance? Try running the Fade example sketch (File=>Examples=>01.Basic=>Fade).
    – Gerben
    Jan 30, 2015 at 15:47

4 Answers 4


Added at top:

Your code runs correctly on a $US3 Arduino Nano :-)

It seems extremely likely that you have a hardware problem of some sort.

If you have a voltmeter you can try these easy checks.
I carried these out to be sure that I got what was expected and the results are as you would expect them to me. Use a meter set to DC Volts - typical common meter circuitry averages the PWM well.

  1. Remove LED etc connection to pin 9.
    Measure the voltage from the 5V pin to ground. If powered from USB this will usually be slightly below 5V.

  2. Run your code with the analogWrite set to 128 ie

    analogWrite(pinNine, 128);

  3. Measure the voltage on pin 9 relative to ground. It should be close tohalf the voltage on the 5V pin in 1. above. This is because the analogWrite value is controlled by PWM with values from 0 to 255. 128/255 ~= 50% so output is 50% of Vref = 5V by default. in a 5V system.

  4. Set analogWrite to

     analogWrite(pinNine, 25); 
  5. The voltage on pin9 should be about 10% of the voltage on the 5V pin.

IF you get approximately the voltages shown in 3. and 5. your code is working OK and your hardware is OK. If not then 'summat aglae'.

Try another analog pin.

Check that the red wire to the LED goes to UNO ground.

The voltmeter is a surer way of checking that the LED is as you can see actual numeric values that result.


It always helps greatly if you show PIN IDs on photos so people can be confident re what you have done.

If you view the whole UNO-R2-R3 connector with numbering it is obvious that the important pinouts are the same. If however you view part of the connector with no numbering/ pin names shown, then on the R3 there are additional pins on the ARef end of the connector which make comparisons'more difficult).

You APPEAR to have an Arduino UNO R3
It would help if you gave actual model and weblink in future "just in case it matters". .

You appear to have connected to pin 9 (as you intended to) Pin 9 has PWM capability, as required. It seems that your code should work.


enter image description here

IF the red wire connects to ground the circuit seems electrically correct.
Does it?


Try digital write on / off on pin 9 to see if LED changes and that the port behaves as expected. A bad port pin is possible.

What pin is serial on?

Try another pin with PWM capability to see if it changes.

ARDUINO UNO R3 from UNO home page

The connector pinouts vary widely between models of UNO.

enter image description here

  • Did they change pinout for UNO between versions? I'm pretty sure he wired it correctly. Edit: You can see from the reset button on his picture that it is Rev.3. Jan 30, 2015 at 10:30
  • 1
    @Allan Look at the page referenced to see the differences. | I imagine that a dyed in the wool Arduino aficionado can tell not only the version but which corner of the vineyard it was grown in and how long it was left in the cask to mature. BUT when asking question and YOU have the information to hand it makes sense all round to tell as much of what you know that is relevant so question answerers can avoid duplicating the 'research'. Jan 30, 2015 at 11:31
  • I agree completely that it should be noted when possible. OP probably didn't realise this would come in handy (I don't even own an UNO..:) I don't see anywhere in your link that the pinout has changed. They all feature the same chip, having PWM on 3,5,6,9,10,11. My old Duemillanove is the same :) Jan 30, 2015 at 12:33
  • @AllanNørgaard - loose terminology on my part - I meant the connector pinout or functionality. In his photo you cannot see the module's labelling - which can be seen on the photos I have added. He has connected it correctly and on my $US3 UNO the code runs as expected. He has some sort of hardware issue. I'm about to update my answer. Jan 30, 2015 at 13:24
  • @Russell McMahon . Thank you. I will remember to show PIN IDs and actual model next time i post. Maybe a 'top' view of the circuit is most ideal.
    – noi.m
    Jan 31, 2015 at 4:59

Flat side of LED is -. This means that your white wire is on - side and your red wire on + side. Therefore your power rail connection is reversed... and therefore your LED is ON when you send 0 on PWM. By the way, you will set the LED OFF by writing 255 on PWM.

Solution: connect power rails correctly and everything will be back to normal.

  • Maybe. H did say "... I even change the pinNine outputValue to (64, 127, etc) ...". So that depends on the range of values of "etc". At 5V you'd expect ~~~~ I=V/R = (5-3)/220 =~ 9 mA and at 2.5V you'd expect close to 0 mA. As he said he's tried PWM level 128 at ~= 2.5V you'd expect the LED to change very substantially. |"Flat side"? - I need to upgrade my (old) eyes :-). Jan 31, 2015 at 0:54
  • Yes, I saw what he wrote. But it may be difficult to see the intensity change between 50% and 100% as our eyes are not linear, mostly for a bright light dot. He didn't write that he tried 255. Concerning the flat side, this is what I (old eyes too) see on the left of the LED on the picture (not very distinct I must admit).
    – MAC
    Jan 31, 2015 at 8:48
  • You might be right. No certainty until he answer questions. I too asked about polarity of feed. | BUT it seems likely he'd notice at full and half voltages, from 0 to 128 the voltage halves but the LED meeds a minimum Vf to operate so brightness will be way down. |An InGaAlP green LED may gave Vf in the 1.7V - low 2.xV range and an InGaN green Led may be 2.7V up. Even at 1.7V and 5V drive I~~~= (5-1.7)/220 =~ 15 mA and at 2.5V ~~~= 4 mA. That's a very low voltage green LED and for higher Vfs the ratio is even larger. When looking for changes anything over 2:1 is extremely noticeable. Jan 31, 2015 at 10:21
  • @RussellMcMahon. As he is not filtering the output, he will never generate 2.5 V, so the average current should be linear with the code. What is not linear is our perception of brightness. But according to his answer below, the issue was more fundamental and now things are solved.
    – MAC
    Jan 31, 2015 at 12:18
  • yes/maybe/no/ ... :-). I build LED lights - that does not make me automatically right :-) but I am experienced therewith. . Arduino PWM is (I read) 490 Hz so yes, minimal filtering with stray capacitance. | Eye and LED responses combined are close enough to linear with these levels of current. At 2:1 most people can distinguish between two sources viewed one at a time. When viewed simultaneously a difference of 1.5:1 is typically visible. For adjacent wall washing > 20% difference can be seen by average user. Feb 1, 2015 at 13:19

I have a few things you could try:

  1. Remove pinMode(pinNine, OUTPUT), this is only used for digitalWrite(), the PWM controller might be affected by this.
  2. Did you select the correct Arduino Chip in the IDE? Arduino maps pin 9 to a hardware pin (PB1 for UNO), which is not consistent between different boards
  3. Try inserting a delay(100) after analogWrite(...). I'm not sure if the analogWrite call restarts the PWM cycle, resulting in very fast switching that might be seen as 'always on'

Other than that, your sketch seems to be correct. I assume the black wire in the breadboard is wired to Ground, correct?


Ok, it seems my understanding of the PWM was wrong. I tried the circuit and code from here (http://arduino.cc/en/Tutorial/Fade) and it worked as expected (fade in and out).

Actually when i wrote my original code i was expecting the LED to blink. So, as i got closer to 255 i was expecting it to spend more time being 'lit', as compared to 0 where it should spend less time being 'lit'.

I even change the pinNine outputValue to (64, 127, etc) but for some reason the LED is always lighted.

It being lighted makes sense. What i missed out was it being dimmer when the value was lower.

Thanks for all your help..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.