Silicon dominates the electronics industry, but its poor optical properties mean that III–V compound semiconductors are preferred for photonics applications. Photoluminescence at visible wavelengths was observed from porous Si at room temperature in 1990, but the origin of these photons (do they arise from highly localized defect states or quantum confinement effects?) has been the subject of intense debate ever since. Attention has subsequently shifted from porous Si to Si nanocrystals, but the same fundamental question about the origin of the photoluminescence has remained. Here we show, based on measurements in high magnetic fields, that defects are the dominant source of light from Si nanocrystals. Moreover, we show that it is possible to control the origin of the photoluminescence in a single sample: passivation with hydrogen removes the defects, resulting in photoluminescence from quantum-confined states, but subsequent ultraviolet illumination reintroduces the defects, making them the origin of the light again.