Electrical microstimulation can establish causal links between the activity of groups of neurons and perceptual and cognitive functions. However, the number and identities of neurons microstimulated, as well as the number of action potentials evoked, are difficult to ascertain. To address these issues we introduced the light-gated algal channel channelrhodopsin-2 (ChR2) specifically into a small fraction of layer 2/3 neurons of the mouse primary somatosensory cortex. ChR2 photostimulation in vivo reliably generated stimulus-locked action potentials at frequencies up to 50 Hz. Here we show that naive mice readily learned to detect brief trains of action potentials (five light pulses, 1 ms, 20 Hz). After training, mice could detect a photostimulus firing a single action potential in approximately 300 neurons. Even fewer neurons (approximately 60) were required for longer stimuli (five action potentials, 250 ms). Our results show that perceptual decisions and learning can be driven by extremely brief epochs of cortical activity in a sparse subset of supragranular cortical pyramidal neurons.