In
the 1950s scientists tinkering with semiconductors found
that by introducing small, minutely controlled amounts
of certain impurities called dopants to the
semiconductor matrix, the density of free electrons
could be shepherded and controlled.
The dopants, similar enough in structure and valence to
fit into the matrix, have one electron more or less than
the semiconductor; for example, doping with phosphorus,
which has five valence electrons, produces a (negative)
n-type semiconductor, with an extra electron which can
be dislodged easily. Aluminum, boron, indium, and
gallium have only three valence electrons, and so a
semiconductor doped with them is (positive) p-type, and
has holes" where the missing electrons ought to be.
These holes behave just like electrons, except that they
have an opposite, positive charge. (Holes are
theoretical, but so are electrons, and either or both
may or may not exist, but we know for sure that if one
exists, they both do, because we can't create something
out of nothing in the physical world.) It is important
to understand that, although loosely bonded or extra
carriers exist in a substance, it is still neutral
electrically, because each atom's electrons are matched
one for one by protons in the nucleus.
The fun begins when the two semiconductor types are
intimately joined in a pn-junction, and the carriers are
free to wander. Being of opposite charge, they move
toward each other, and may cross the junction, depleting
the region they came from, and transferring their charge
to their new region. This produces an electric field,
called gradient, which quickly reaches equilibrium with
the force of attraction of excess carriers. This field
becomes a permanent part of the device, a kind of slope
that makes carriers tend to slide across the junction
when they get close.

When light strikes a Photovoltaic cell, atoms are
bombarded with photons, and give up electrons. When an
electron gets lopped off an atom, it leaves behind a
hole, which has an equal and opposite charge. Both the
electron, with its negative charge, and the hole, with
its positive charge, begin a random walk generally down
the gradient. If either carrier wanders across the
junction, the field and the nature of the semiconductor
material discourage it from recrossing.
A proportion of carriers which cross this junction can
be harvested by completing a circuit from a grid on the
cell's surface to a collector on the back plane. In the
cell, the light pumps" electrons out one side of the
cell, through the circuit, and back to the other side,
energizing any electrical devices (like the battery in
the diagram) found along the way.