Capacitor Charging with Arduino PWM Output

I have created a simple timer circuit. The input pulse (of 10 millisecond) to the timer is 5V which activates a mosfet which in turn switches ON a 24V Relay (Connected across diode D1 on the right hand side) for approx 2 seconds and then the capacitor discharges. This 5V pulse from a normal GPIO of arduino works fine. But when i connect it to a PWM output, the capacitor discharges in less than 1 second. Increasing the pulse time also doesnt have any impact on the discharge time of the capacitor. Can anyone advise what coulde be the issue. Circuit diagram and both codes are given below. My issue is that in my project i have used up all my GPIOs except the PWM ones, so i have to use the PWM outputs only if possible. I think it could be because the PWM output is charging and discharging the Capacitor at a very fast rate. So is there any way to program a PWM output to increase its duty cycle?

GPIO Code:

digitalWrite(Relay, HIGH); 
  delay(10);             
  digitalWrite(Relay, LOW);
  delay(2000);

PWM Code:

analogWrite(9,255);
delay(10);
analogWrite(9,0);
delay(2000);

First, why? You have a processor, why mess with finicky hardware to do simple timing?

Second, what processor board are you using? There is no need to use PWM on PEM-capable output pins, just use digitalWrite on them as usual, having made them pinMode OUTPUT.

HTh

a7

I cannot find any functional difference between both code versions. Your PWM solution is not using PWM, it only produces a single pulse.

Perhaps you have killed Q1 already by a missing current limiting resistor and negative gate voltage.

After all I also don't understand why you don't replace the external delay circuit by software.

I am using an arduino compatible industrial board from Comfile (FA-DUINO-32TA (Industrial ARDUINO) - COMFILE Technology). The device will be used for a critical industrial application. I am using a software watchdog timer but in order to provide additional safety (redundancy) and to comply to EN60204 standards, i am putting a hardware watchdog timer which will trip the emergency stop relays as well as activate an interrupt to reset the board in case the code hangs for some reason. I have used up all the digital outputs for other functions so now i have only PWM outputs left. I can measure 5V on the PWM output with the code above. If i connect a 5V relay to this output, it switches on fine, but my timer circuit is not charging enough to hold the charge for 2 seconds. Any suggestions would be welcome.

Can you please advise what you meant by this statement. Do you think i need any additional resistors in the circuit. As i said, when the same circuit is connected to a standard GPIO set as output 5V, i get correct results, but when i use PWM output, it dont get the required discharge time of the capacitor.

I tried with the following code as well, but the result is same. no change.

digitalWrite(9,HIGH);
delay(10);
digitalWrite(9,LOW);
delay(2000);

Yes.

Then you should consult an electronics expert for an industrial grade circuit.

Thanks for the explanation about a hardware additional mechanism.

I still do not understand the idea of a PWM only output.

And if you’ve set the pin mode, the sequence with analog write at maximum and minimum values should be equivalent.

Have you looked at the signal with an oscilloscope done one way or the other? They should be identical…

a7