Drag on the paper to turn the wheel by hand.
The wheel has r teeth and rolls on a ring with R teeth. All the teeth have the same pitch, so the radius of each gear is in proportion to its tooth count. At each step one tooth of the ring meets one tooth of the wheel: the wheel cannot slip.
When the wheel center goes once round, R teeth of the ring pass the wheel. That is R/r turns of the wheel against the ring, and one turn less against the paper. The pen sits at distance d from the wheel center, so it adds a small circle that turns at that rate.
The pen comes back to its start when a whole number of laps brings a whole number of wheel turns. The smallest such number of laps is L = r/g, with g the greatest common divisor of the two tooth counts. In that time the pen makes n = R/g petals. Tooth counts with a small common divisor give dense lace. A large common divisor gives a few bold loops.
Outside the wheel, the wheel turns the other way, faster, and the pen draws an epitrochoid.