Lightning bugs, also known as flireflys, produce light through chemical reactions in little cells called photocytes, using a substance called luciferin.
When the enzyme luciferase is also present, the substances oxidize, creating a lot of energy. A spark of light is caused when the substances settle back down again.
It is still unclear why fireflies have this mechanism. It could be for mating purposes, to serve as a warning, or to attract prey.
The lightning bug is capable of controlling the speed and length of the flashes, and although bug specialists haven’t deciphered the code completely, the changes in pattern probably do change the meaning of the flashes.