Hi, I have prepared a code and Circuit which was working fine on simulator, and giving different output voltages as per mentioned in code, but when made physical circuit with IRF 530 MOFET, it is giving constant 11.9VDC from drain as given additional by additional voltage source 12VDC.
PWM can lower the average voltage to "simulate" analog and control the speed of a motor, dim an incandescent light, or make an LED appear dim.
It can also be low-pass filtered to get true-analog but that doesn't work very well with a high current load and you can sometimes end-up frying a MOSFET.
Please find attached drawing. I have not attached load to drain, currently i am just doing measurement by Voltmeter connected in between drain and Positive terminal of Battery.
You want about a 10K resistor Gate to Source and you want about a 220 Ohm resistor in your gate circuit (between Gate and PWM. You also as mentioned earlier need to be using a Logic Level MOSFET. What MOSFET are you using? The IDF 530 as drawn is not a logic level MOSFET. Read the data sheet.
It appears you are trying to make a D/A converter with an IRF530 and PWM. that would work at the low currents you apparently are trying to use. The MOSFET will not be close to being fully enhanced so not much current will be available. The duty cycle will control the voltage but you also need some capacitance to maintain the voltage when the MOSFET is off. Simply add a capacitor across your volt meter. What you are actually adding is a low pass filter. The circuit may sort of work as shown but it is dependent on the voltmeters capacitance and response. Put a scope across it and see what you really have.