Brain-machine interface (BMI) techniques are developed to implement direct data communication links between the brain and artificial computers. The main objective in using BMI systems is to bypass or compensate possible damages in data processing pathways in the brain or to exchange data between the brain and the peripheral nervous system. BMI systems are usually made in the form of prosthetic devices that record neural activity and transfer this data to a computer which decodes the information and translates results to a sequence of commands, for example, to move a robotic limb. In closed-loop interface paradigms, the system also generates feedback signals to pass sensory information, such as the precision of the generated movement, back to the brain by stimulating corresponding neural circuitries. This article presents a new approach for implementation of closed-loop BMI by combining optogenetic neural stimulation with electrocorticography and fluorescence microscopy. We used a new generation of microfabricated electrocorticography (micro-ECoG) devices in which electrode arrays are embedded within an optically transparent biocompatible substrate that provides optical access to the brain tissue during electrophysiology recording. An optical setup was designed capable of projecting arbitrary patterns of light for optogenetic stimulation and performing fluorescence microscopy through the implant. For realization of a closed-loop system, the feedback can be taken from electrophysiology data or fluorescence imaging. In the closed-loop protocols discussed in this paper, the feedback was taken from the micro-ECoG and the electrophysiology data was continuously transferred to a computer and compared with some predefined spatial-temporal patterns of activity. The computer processes the data and readjusts the duration and distribution of optogenetic stimulating pulses to minimize the difference between the recorded activity and the predefined setpoints. Details of the system design and implementation of typical closed-loop paradigms are discussed in the article.