By arguing that the expression 3PV/2 for the kinetic energy of an ideal gas is its internal energy function, we avoid the standard appeal to an equipartition theorem and, by analysing the Carnot cycle for a cylinder of gas enclosed by a piston, we show that the Kelvin temperature of an ideal gas is proportional to its internal energy. We report molecular dynamics experiments with ideal gas particles and show that they can exchange energy with their container. We then construct a dynamical system modelling the motion of the piston and heat transfer to the surroundings when the piston is released into a region of different pressure. For isothermal processes, we show that the system decays to equilibrium through damped oscillations in such a way that the work done by the enclosed gas is equal to the negative of the work done by the external pressure. We then show that simple control strategies applied to the dynamical system can make it resemble a quasi-static process. We then generalise the dynamical system to a two-compartment adiabatic cylinder in which the gases in the two chambers are separated by a movable piston. We show that, if the piston is subjected to infinitesimal kinetic friction, in all cases it relaxes to the stable fixed point predicted by equilibrium thermodynamics.