1

I want to light an LED in a sketch and then have it fade out. I tried to use analogWrite and have it decrease from a value of 255 to 0 using a for() loop, but I can't seem to get it to work. Using digitalWrite I can turn it on and off but cant get it to fade out. Can anyone help please.

const int ledPin1 = 2;
const int ledPin2 = 3;
const int ledPin3 = 9;
int j = 0;

void setup() {
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
}

void loop() {
    j = 255;
    delay(1000);
    if (j >= 5);
    {
        analogWrite(ledPin3,j);
        delay(10);
        j = j - 5;
    }
    delay(1000);
}
5
  • 6
    Please show what code you have, along with your wiring. There is an example in the IDE that fades an LED. Did you get that to work?
    – Majenko
    Commented Sep 18, 2017 at 10:30
  • Yes I did get that sketch to work. I am using pin 9 Commented Sep 18, 2017 at 11:57
  • 2
    So please show us your code and better define "can't seem to get it to work".
    – Majenko
    Commented Sep 18, 2017 at 11:58
  • What sketch did you get to work?
    – SDsolar
    Commented Sep 18, 2017 at 15:42
  • Yes my complaint is a bit vague. The LED comes on and stays on. It is in my code. I am missing what I doing wrong. Some sort of mental block. Wiring is just a LED and resistor from pin 9 to gnd. Commented Sep 19, 2017 at 1:32

2 Answers 2

2

Not all output pins are able to perform PWM ("AnalogWrite").

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

https://www.arduino.cc/en/Reference/AnalogWrite

Note that the PWM-enabled ports are marked by a "~" on the board.

If you try analogWrite, e.g., on port 4, you'll get your observed switch behaviour.

4
  • I am using pin 9 Commented Sep 18, 2017 at 11:58
  • 1
    Well - if that's not the problem, I'm totally with @Majenko. Post code, wiring and tell us what boards you are using.
    – Tommy
    Commented Sep 18, 2017 at 12:03
  • const int ledPin1 = 2; const int ledPin2 = 3; const int ledPin3 = 9; int j = 0; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); } void loop() { j = 255; delay(1000); if (j >= 5);{ analogWrite(ledPin3,j); delay(10); j = j - 5; } delay(1000); } Commented Sep 19, 2017 at 1:27
  • New at this so my posts may be a mess. I am using an UNO. Commented Sep 19, 2017 at 1:29
2

Problems noted in comments:

void loop() {
    j = 255;             // Resets j EVERYTIME
    delay(1000);
    if (j >= 5);         // Semi-colon here makes this a null statement
    {
        analogWrite(ledPin3,j);  // j is always 255 (see line 1)
        delay(10);
        j = j - 5;       // No effect as j is reset in line 1
    }
    delay(1000);
}

Here's a simple fix:

int j = 255;   // Start at 255

void loop() {
    analogWrite(ledPin3, j);   // Set the brightness
    delay(10);
    j -= 5;
    if (j <= 0) j = 255;       // Reset j?
}
2
  • of course. Thank you very much. Works like a charm. I must be getting old. Commented Sep 19, 2017 at 4:18
  • 1
    How about we double what we are paying you. Commented Sep 19, 2017 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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