As the piston moves up the pressure inside the cylinder decreases.
Thus the force exerted by the gas in the cylinder on the piston decreases.
What is needed to allow the piston to move up slowly is to apply a decreasing external force on the piston as the piston moves up such that the external force is slightly less than the force exerted by the gas on the piston.
That decreasing force is achieved using the cam, rack and pinion.
There is a lever with axle of the pinion/cam acting as the fulcrum.
As the rack moves up the cam rotates and reduces the distance between the line of action of the weight and the axle thus reducing the torque produced by the weight and hence the torque exerted by the pinion.
Hence the force on the rack/piston is reduced.
Update
Schematic diagram of apparatus

When the piston has moved a distance $x$ from the bottom of the cylinder the rack has also moved upwards a distance $x$.
At the same time the pinion and the cam have rotated through an angle $\theta$.
$x=a \theta$ where $a$ is the fixed radius of the pinion.
For an adiabatic expansion $PV^\gamma = {\rm constant} = \dfrac F A (Ax)^\gamma \Rightarrow F x^\gamma = \rm constant$
With the symbols as defined in the diagram $F = \dfrac b a mg \Rightarrow b\;\theta^\gamma = \rm constant$ and this is the equation for the shape of the cam.