The physics
A current loop acts like a small bar magnet. Its moment points at right angles to the loop. Turns, current and area set its size.
μ = N I A
A field turns a moment toward itself. θ is the angle between μ and B.
τ = μ B sin θ U = −μ B cos θ
U is least at 0°, so the magnet rests there. At 180° it balances, but a nudge flips it back.
A slow turn takes work equal to the rise in U. A magnet nudged from rest swings to and fro.
W = μ B (cos θ₁ − cos θ₂) T = 2π √(I / μ B)
A solenoid with a current is a bar magnet too. Its magnetisation is N I / L.