For billions of years photosynthetic microbes have evolved under the variable exposure to sunlight in diverse ecosystems and microhabitats all over our planet. Their abilities to dynamically respond to alterations of the luminous intensity, including phototaxis, surface association and diurnal cell cycles, are pivotal for survival, far more than any other environmental cues. If these strategies fail in the absence of light, the microbes can still sustain essential metabolic functionalities and motility by switching their energy production from photosynthesis to oxygen respiration. For suspensions of motile microbes above a critical density we demonstrate that this switch reversibly controls collective microbial aggregation. Aerobic respiration dominates over photosynthesis in conditions of low light, which causes the microbial motility to sensitively depend on the local availability of oxygen. For dense microbial populations in self-generated oxygen gradients, microfluidic experiments and continuum theory based on a reaction-diffusion mechanism show that oxygen-regulated motility enables the collective emergence of highly localized regions of high and low cell densities.