Breathing rhythm is regulated by neurons in the brain stem. Day and night these neurons receive information about the amounts of oxygen and carbon dioxide in the blood.
If there is a shortage of oxygen and an excess of carbon dioxide, the neurons send signals to the breathing muscles to make them contract. The lungs inflate to allow more air to enter. The reverse is also possible.