What should be a simple dim LED display

Hello,
First I am new to the forum, new to Arduino, and new to any sort of coding. So forgive me if this ends up being a very simple/obvious answer.

I am currently attempting to have 2 LEDs lit, each with a different brightness. I am using an Arduino Mega2560. Code as follows:

void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
analogWrite(13, 15);
pinMode(12, OUTPUT);
analogWrite(12, 100);
}

void loop() {
  // put your main code here, to run repeatedly:
}

This results in the LED connected to pin 13 blinking (long, short, short) over a period approx. 1s. and LED connected to 12 not on at all. If I change the code to:

void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
analogWrite(13, 100);
pinMode(12, OUTPUT);
analogWrite(12, 100);
}

void loop() {
  // put your main code here, to run repeatedly:
}

Both LEDs will turn on and be at the correct dimness.

Any ideas why I cant seem to have the two LEDs display different intensities?

Thank you very much for any input.

The code in void setup() runs once.

You might find a path to follow here https://www.arduino.cc/en/Tutorial/PWM and here https://create.arduino.cc/projecthub/muhammad-aqib/arduino-pwm-tutorial-ae9d71?f=1

  1. remove the pinMode calls. You don't need them when using analogWrite
  2. this wasn't a real test because you changed two things at once - the call order and the values

Did you test that.
I can't get it to work no matter what I do.
Only if I write 0 or 255 or the same value to the second PWM pin.
The attached sketch flashes the build-in LED.
Leo..

void setup() {
  analogWrite(13, 15);
  analogWrite(12, 100);
}

void loop() {
}

Wawa:
Did you test that.
I can't get it to work no matter what I do.
Only if I write 0 or 255 or the same value to the second PWM pin.
The attached sketch flashes the build-in LED.
Leo..

void setup() {

analogWrite(13, 15);
  analogWrite(12, 100);
}

void loop() {
}

Are you using a Mega2560?

GoForSmoke:
Are you using a Mega2560?

Of course.

Perhaps the opamp on pin 13 changes things?

bwp723:
This results in the LED connected to pin 13 blinking (long, short, short) over a period approx. 1s. and LED connected to 12 not on at all. If I change the code to:

Sounds like LEDS have no current limit resistors and the load current is causing the Mega to reset..reset..reset.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Tom... :slight_smile:

Two things :

  1. Leds are polarity sensitive , they will only work connected the correct way around.
  2. You need series resistors between the led and the analog output, 270-330ohms. The other side of the leds should both go to the gnd/0v connection.

Ok three then...

3.you should be using the pwm pins 12&13 and not the analog pins.

Did you guys actually try this?

TomGeorge:
Sounds like LEDS have no current limit resistors and the load current is causing the Mega to reset..reset..reset.

I tested it without LEDs. Just the build-in LED.

hammy:
you should be using the pwm pins 12&13 and not the analog pins.

Of course I tested it on the PWM pins.

Pin13 LED (build-in) goes dim if you have pin12 on value 0/255/same.
Pin13 LED blinks when you write other values to pin12.
Leo..

Has to be wired up wrong !!

hammy:
Has to be wired up wrong !!

The only 'wire' I have is the USB lead.

I would guess that there is a bug in the PWM implementation. Pin13 can be connected to either OC0A or OC1C (probably the latter), whereas Pin12 is connected to OC1B.

Unanticipated behaviors occur when an output compare register is changed at the wrong time. I don't have a 2560 to test, but try removing the analogWrite() to pin 12 and see it that changes the behavior.

Wawa:
The only 'wire' I have is the USB lead.

Does your desk have a head size dent in it yet?

This is weird. Mega2560 schematic is open, but knockoffs (got one) may have manufacturing shortcuts.