When a current flows through a wire, a magnetic field is produced around the wire. The effect can be shown by placing a plotting compass near a wire carrying a current. The needle deflects when the current is switched on and goes back when it is switched off. Around a straight wire the field lines are circles centred on the wire, and reversing the current reverses the direction of the field. The field is stronger when the current is larger and weaker as the distance from the wire increases.
Shaping the wire into a coil called a solenoid increases the strength of the field, because the fields from the separate turns add together. Inside the solenoid the field is strong and uniform. Outside, the field has a similar shape to the field of a bar magnet.
Putting an iron core inside the solenoid makes the field much stronger, because the iron becomes magnetised. A solenoid with an iron core is called an electromagnet. Its strength can be increased by raising the current, adding more turns of wire or using an iron core. Unlike a permanent magnet, an electromagnet can be switched off, which is why it is used in devices such as electric bells and relays.