Here, an ‘all optical’ combination of techniques was used to investigate how spatiotemporal brain activity patterns drive behaviour. Optical readout from a genetically encoded calcium indicator obtained in real time from mouse barrel cortex was used to infer population activity; these data were used to reconfigure the rate and pattern of barrel-cortex photostimulation rapidly enough for ongoing patterns of sensory-evoked activity in individual neurons to be rapidly enhanced in the awake mouse in real time.