Optogenetic techniques have been developed to allow control over the activity of selected cells within a highly heterogeneous tissue, using a combination of genetic engineering and light. Optogenetics employs natural and engineered photoreceptors, mostly of microbial origin, to be genetically introduced into the cells of interest. As a result, cells that are naturally light-insensitive can be made photosensitive and addressable by illumination and precisely controllable in time and space. The selectivity of expression and subcellular targeting in the host is enabled by applying control elements such as promoters, enhancers and specific targeting sequences to the employed photoreceptor-encoding DNA. This powerful approach allows precise characterization and manipulation of cellular functions and has motivated the development of advanced optical methods for patterned photostimulation. Optogenetics has revolutionized neuroscience during the past 15 years and is primed to have a similar impact in other fields, including cardiology, cell biology and plant sciences. In this Primer, we describe the principles of optogenetics, review the most commonly used optogenetic tools, illumination approaches and scientific applications and discuss the possibilities and limitations associated with optogenetic manipulations across a wide variety of optical techniques, cells, circuits and organisms.