When we learn a new skill or task, our movements are reinforced and shaped. Learning occurs because the neural activity patterns in the movement control–related brain regions that are rewarded are repeated. But how does this reinforcement work? Athalye et al. developed a closed-loop self-stimulation paradigm in which a target motor cortical activity pattern resulted in the optogenetic stimulation of dopaminergic neurons. With training, mice learned to reenter specific neuronal activity patterns, which triggered self-stimulation and shaped their neural activity to be closer to the target pattern.