Issues With Simple PWM

I am trying to setup PWM in my project but have been facing issues. In order to remove any hidden issues I have reduced my code the following but I am still seeing unwanted effects. When I setup the PWM the LED lights dimly as expected. But as soon as it enters the loop, the LED shuts off and flickers every 5 seconds. Why would the LED turn off when entering the loop?

Code:

void setup()
{
pinMode(9, OUTPUT);
analogWrite(9, 50);
Serial.begin(9600);

Serial.println("Test Break 1");
delay(5000);

}

void loop()
{
Serial.println("Test Break 2");
delay(5000);
analogWrite(9, 50);
}

Outcome:

  1. LED turns on (dim).
  2. Serial Prints "Test Break 1"
  3. 5 Second Pause
  4. LED Shuts Off
    ---- The following repeats ----
  5. Serial Prints "Test Break 2"
  6. 5 Second Pause
  7. LED flickers on briefly

Sorry if this is obvious but I am fairly new to programming with an Arduino. Thank you in advance for your help! :slight_smile:

What LED? Which Arduino? Do you have a current limiting resistor on the LED?

Thank you for the reply!

Mega2560
LED is a 5mm that came with the starter kit. Forward Voltage: 2V; Cont Current: 50mA
Resistor is 220 OHMs so current should be roughly 13mA

Here’s the setup. I removed everything to confirm nothing was affecting the light.

Test the LED by running the Blink example, change the pin to pin 9. Also try changing the PWM value 50 to something bigger like 200.

Check your wiring. Those jumpers are notorious for intermittent failures.

I can confirm that the LED and jumpers work. As i had more complicated code (did a strobe light using Millis). I mean I think I’m just baffled but not being able to get this simple task to work.

I did the change too 200 and only difference is the led is on brighter at the beginning but still flashes after going into the loop.

Decided to test out one more theory to see it it was truly the loop that was shutting down the PWM. I updated the code to the following which results in the LED staying let for 5 seconds and staying off for 5.

void setup()
{
	pinMode(9, OUTPUT);
	analogWrite(9, 200);
	Serial.begin(9600);
	
	Serial.println("Test Break 1");
	delay(10000);
}

void loop()
{
	Serial.println("Test Break 2");
	delay(5000);
	analogWrite(9, 200);
	Serial.println("Test Break 3");
	delay(5000);
}

Got curious and pulled out the scope and found that that it is delivering the correct signal during those 5 seconds.

But nothing after the loop restarts

My Mega exposes the expected behaviour, not what you describe. You must be doing something wrong or must have very damaged hardware.

What blinks at 5 seconds rhythm is the TX diode, during transmission of the "Test Break 2" message. Connected the LED to TX0 (wrong connector)?

I also wondered if perhaps your Mega was damaged... the image is fuzzy around the Mega, so it could be a pin assignment error too.

Is that table you have it sitting on, conductive, e.g. metal? :slight_smile:

Good points. So what I did now was test the code on a second board to test the damage theory. Same results.

Also here is an up-close of the pins being used so we can be on the same page. Only using pin 9 and GND.

Table is wood. Non conductive.

I have moved it to my silicone mat to be sure.

Try a different PWM pin? Although - two Elegoo's and yet it works on another board? Hmmm wait are they both Elegoo Mega's?

They are two Elegoo's. But I think I have found the issue. I think the sketch might have been corrupted somehow. I created a brand new one and copied the code over and now everything is working as expected. Thank you guys for your help. I will need to port everything over to the new sketch.

Eventually it was compiled for a different board?

If your harddisk or filesystem is corrupted, that would be really bad. You might want to check that with a disk scan, and perhaps also with a tool that checks the SMART information and can run the tests of the harddisk itself. Check if there is new firmware for you harddisk and if older firmware has issues.
Perhaps you could re-install the Arduino IDE.
Did you change the clock of the computer ? Perhaps that confused the Arduino compiling/build process.

Sometimes an Arduino board keeps resetting, therefor I often start by outputting a message:

void setup()
{
  Serial.begin( 115200);
  Serial.println( "The sketch has started");

Sometimes I want to check if the upload really worked. Then I use this:

void setup()
{
  Serial.begin( 115200);
  Serial.println( F( "Compilation: " __DATE__ ", " __TIME__));

You are not alone (also Elegoo Mega board): https://forum.arduino.cc/t/my-led-wont-undim/895216

Thank you for the replies. As I think through it I’m suspecting it was due to a library I probably installed then deleted. Probably messed with the internal timers somehow. I’m new to all this but that’s what I could gather from searching the internet. Just a theory.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.