A pulse width modulation method for controlling oxygen concentration in an anesthetic machine or in a ventilator comprising the following steps: step A with a predetermined time interval as one pulse interval a processing unit divides a breathing cycle into multiple consecutive pulse intervals step B a data calculation unit calculates the average inspiratory flow in a certain time interval on the basis of inspiratory flows in one cycle that are detected by a detecting unit and then calculates the average oxygen flow of this stage on the basis of the average inspiratory flow and step C a control unit selects a solenoid valve and controls the opening and closing times thereof on the basis of the average oxygen flow as calculated in the previous step to implement control of the oxygen flow for each interval. By dividing one breathing cycle into multiple consecutive stages with equally spaced pulse cycles by calculating the oxygen flow for each stage and then by controlling the opening/closing of the solenoid valve to implement control of oxygen flow the method implements precision control of the oxygen concentration during ventilation thus allowing a breathing machine to be provided with increased safeness and stability.