The limitations of the bulk semiconductors currently used in electronic devices-rigidity, heavy weight and high costs--have recently shifted the research efforts to two-dimensional atomic crystals such as graphene and atomically thin transition-metal dichalcogenides. These materials have the potential to be produced at low cost and in large areas, while maintaining high material quality. These properties, as well as their flexibility, make two-dimensional atomic crystals attractive for applications such as solar cells or display panels. The basic building blocks of optoelectronic devices are p-n junction diodes, but they have not yet been demonstrated in a two-dimensional material. Here, we report a p-n junction diode based on an electrostatically doped tungsten diselenide (WSe2) monolayer. We present applications as a photovoltaic solar cell, a photodiode and a light-emitting diode, and obtain light-power conversion and electroluminescence efficiencies of ∼ 0.5% and ∼ 0.1%, respectively. Given recent advances in the large-scale production of two-dimensional crystals, we expect them to profoundly impact future developments in solar, lighting and display technologies.