I'm trying to turn a laser on and off with an external button. When I press the button, the laser should turn on. When I release the button the laser should turn off. However, I don't want the laser to turn off immediately. I want it to turn off linearly over 500 ms. I'm using PWM to alter the strength of the laser over time. However, will my laser power increase and then decrease in the extremely short time segments due to PWM fluctuation? Will it even be able to execute this reduction of power?

1 Answer 1


I dont know your circuit but... Do you effectively want to use an RC filter to reduce PWM fluxuations so that the laser sees an analog voltage which will translate to a power (presumably)? - that will avoid the pulsing of the laser, but I did believe that the pulsing at full voltage was neccersary in most laser drivers - please post a schematic... Anyway if you want this to be less of an issue if an RC filter is not for you you could increase the PWM frequency (google is your friend) on the arduino...

To have it turn of slowly if your using analog, the RC filter could possibly have large enough capacity to slow the laser turn off. It will also effect rise time so suitable values for R and C will need to be found to suit. Otherwise you could dial down the PWM gradually before hard power off in the arduino software, by initiating a loop which gradually reduces power with time.

Somewhere to start anyway :)

  • This is my code to gradually reduce power over time: void loop() { int stateButton = digitalRead(onButton); if(onButton ==1){ digitalWrite(Laser, HIGH); } else{ for(int i = 255; i>=0; i--){ analogWrite(Laser, i); delay(500/255); } } Would this loop through continuously? Would the power descend always loop through the descent even though I didn't press the power button? } Jun 29, 2017 at 18:14
  • Sorry my coding is not great so without formatting I find it hard to read your code. I would do it differently myself, using an if statement which ran the pwm loop when you have activated its variable. Jun 29, 2017 at 21:17

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.