The battery on the left sends a current through the electromagnet on the right. The magnetic field in the electromagnetic moves the magnet.

Turn the crank by moving the mouse across the screen from left to right. As the battery rotates, the electrical current is reversed, reversing the polarity of the electromagnet. The changing polarity of the electromagnetic causes the magnet in the motor to move back and forth.