We know that magnets will interact. The opposite ends will attract while their like ends repel. When current flows through a wire, it creates an electromagnetic field. If we wrap the wire into a coil, it creates a larger, stronger electromagnetic field with a north and south pole.
We reverse the polarity by changing the direction of current. We can turn the magnetic field of the coil on and off by simply allowing or stopping the flow of current. But the magnetic field of a permanent magnet is always on. If we place a magnet centrally and allow it to rotate freely, we can use other magnets to rotate this. We can also place electromagnets around this and control the rotation by controlling the current flowing through each coil.
The more coils we use, the more precise the rotation. A simple way to control the coils is with an H-bridge. If we imagine the motor has two coils and each coil is connected to four switches. If we close switches one and four coil one polarises attracting the rotor, then we close switches five and eight, causing the rotor to turn because of the attraction and repulsion of the magnetic fields. Then we close switches two and three.
The polarity has now reversed and the rotor turns. Then we close, which is six and seven to rotate the rotor again. Then we close switches one and four to complete the rotation, the switch sequence continues, causing the rotor to turn. Reversing the sequence reverses the direction of rotation. Changing the frequency of switching controls the speed. The switches are electronic instead of manual, allowing us to programme them to turn on and off with precise timing.