Photostimulation is a noninvasive way to control biological events with excellent spatial and temporal resolution. New methods are desired to photo-regulate endogenous proteins expressed in their native environment. Here, we present an approach to optically control the function of a neuronal protein directly in neurons using a genetically encoded unnatural amino acid (Uaa). By using an orthogonal tRNA/aminoacyl-tRNA synthetase pair to suppress the amber codon, a photo-reactive Uaa 4,5-dimethoxy-2-nitrobenzyl-cysteine (Cmn) is site-specifically incorporated in the pore of a neuronal protein Kir2.1, an inwardly rectifying potassium channel. The bulky Cmn physically blocks the channel pore, rendering Kir2.1 non-conducting. Light illumination instantaneously converts Cmn into a smaller natural amino acid Cys, activating Kir2.1 channel function. We express these photo-inducible inwardly rectifying potassium (PIRK) channels in rat hippocampal primary neurons, and demonstrate that light-activation of PIRK ceases the neuronal firing due to the outflux of K(+) current through the activated Kir2.1 channels. Using in utero electroporation, we also express PIRK in the embryonic mouse neocortex in vivo, showing the light-activation of PIRK in neocortical neurons. Genetically encoding Uaa imposes no restrictions on target protein type or cellular location, and a family of photoreactive Uaas is available for modulating different natural amino acid residues. This technique thus has the potential to be generally applied to many neuronal proteins to achieve optical regulation of different processes in brains. The current protocol presents an accessible procedure for intricate Uaa incorporation in neurons in vitro and in vivo to achieve photo control of neuronal protein activity on the molecular level.