The Stirling engine was invented in 1816 by Robert Stirling as a safer alternative to steam engines. It works by using the expansion and compression of a gas or fluid to convert heat into mechanical energy. The engine has two pistons - a power piston and displacer - that move due to pressure changes from heating and cooling the gas. Heat is applied to one side to expand the gas, pushing the piston, then it cools and contracts on the other side. Its advantages include using various heat sources, being environmentally friendly, and operating safely at low pressures. Applications include power generation, cooling, heating, and marine uses.