PWM status at serial connection

Hello community,

I have developed a dimmer which practically works as the 13 pin led with PWM, but on pin 4 of the mega2560. It works properly, but for circuitry reasons, it is turned down when writing 255 with analogWrite, and turned on when writing 0 with analogWrite.
The problem is that since the serial connects when the serial windows opens, till the program reaches setup, the lamp will stayed on, which is an undesired behavior. I solved it this way:

setup()
{
pinMode(dimmer, OUTPUT);
analogWrite(dimmer, 255);
}

But here, the lamp will flash.

I was wandering if there's a way of configuring the main() or the init() functions so the 4th pin is never set. I tried with sbi() function, but it won't work. I may be missing something or thinking this all wrong.

Can someone please help me with this?
Thanks!

I would add a pull-up

I already made the PCB so I was wondering if there's a way to resolve this on the software.

Try setting the pin HIGH before configuring it as an output:

setup()
{
  digitalWrite(dimmer, HIGH);
  pinMode(dimmer, OUTPUT);
  analogWrite(dimmer, 255);
}

I think, there is no solution w/o external hardware, at least pull-down resistor needed but hard to say w/o knowing your circuitry.
All arduino, and Mega is no exception, initialized pins as inputs during start-up. Better to change logic to "pin high - > switch on"

pert:
Try setting the pin HIGH before configuring it as an output:

setup()

{
  digitalWrite(dimmer, HIGH);
  pinMode(dimmer, OUTPUT);
  analogWrite(dimmer, 255);
}

I tried that. But it still flashes.

I think there must be a way to tune the main.cpp or the wiring.c file, but I can't figure out which and where.

I can't reproduce the issue. If I understand correctly, you have your LED circuit active LOW. The pins will be in the default INPUT mode before your code in setup() runs, but that won't light the LED (INPUT_PULLUP will light it dimly).

I know there will be some microseconds between pinMode(dimmer, OUTPUT); and analogWrite(dimmer, 255); where the pin will be LOW but that shouldn't be noticeable to the human eye and setting the pin HIGH first should fix that anyway. At what point is the flash happening?

You should be aware that there is still some delay before main.cpp even starts as the bootloader code runs so if you really need to do something from the instant of power on then editing main.cpp won't be much help. If you do want to modify main() then the easiest way to do it is to simply define your own main() function in the sketch. This will override the main() in main.cpp.

pert:
I can't reproduce the issue. If I understand correctly, you have your LED circuit active LOW. The pins will be in the default INPUT mode before your code in setup() runs, but that won't light the LED (INPUT_PULLUP will light it dimly).

I know there will be some microseconds between pinMode(dimmer, OUTPUT); and analogWrite(dimmer, 255); where the pin will be LOW but that shouldn't be noticeable to the human eye and setting the pin HIGH first should fix that anyway. At what point is the flash happening?

You should be aware that there is still some delay before main.cpp even starts as the bootloader code runs so if you really need to do something from the instant of power on then editing main.cpp won't be much help. If you do want to modify main() then the easiest way to do it is to simply define your own main() function in the sketch. This will override the main() in main.cpp.

Thanks Pert! I think you're right, but since the circuit I'm using has a pretty big capacitor, it may be charging in that small amount of time, which may be producing the lamp to flash. I'm reproducing the circuit of AC PWM Dimmer by diy_bloke explained on instructables.

OK. I just have an LED and resistor plugged into my Mega so that could certainly explain the different results.

I would think, the dimmer circuit is not powered from the pin, it only behaves according to PWM on pin. And I assume the dimmer circuit grounds the pin it in INPUT state, the pin is not floating,

The setup() function (or the main()) function are too late to set the pin as OUTPUT and set it HIGH. The bootloader runs before and waits for upload for a moment. You would need a modified booloader, that sets that pin OUTPUT HIGH (no need for pwm there).