A magnetic compass wound with a copper wire is connected to one end of dry cell. The other end is connected to one of the two metal pins separated by a small gap in a rubber cap. The second pen is connected to the other end of dry cell to completed the circuit.
The magnetic needle deflects because whenever current pass through a wire it acts as a magnet is called magnetic effect of current.