Coil (electromagnet)
A coil is a winding of insulated wire around a core — an electromagnet. It costs $2.50 and behaves like a resistor and an inductor in series: its resistance sets the steady current (5 V ÷ 70 Ω ≈ 70 mA), while its inductance makes that current ramp up at V/L instead of appearing instantly. Switching a coil off is the interesting part: the current cannot stop immediately, so the winding throws a voltage spike back at whatever switched it. That is why every relay module has a diode across its coil, and why a bare coil needs one too.
Pins
| Pin | What it is | Type |
|---|---|---|
| 1 | Winding end 1 | passive |
| 2 | Winding end 2 | passive |
| Setting | Type | Range | Default |
|---|---|---|---|
ohms | number | 1–10000 | 70 |
henries | number | 0.000001–10 | 0.1 |
Coil(70)— Electromagnet coil — $2.50- Pins: 2
- Chain ends:
1 >> coil >> 2 - Aliases:
plus→1,minus→2
Either winding end works in either direction; the current is what makes the magnet, not the polarity. The Winding resistance and Inductance props describe the part you have: the 70 Ω / 100 mH default is a small 5 V relay coil.
Wiring it in circuit.py
A transistor-driven coil with its flyback diode — the same circuit as inside a relay module:
from wiring import *
pi = Pi()
q1 = Transistor("2N2222A")
r1 = Resistor(1000)
coil = Coil() # 70 Ω, 100 mH
d1 = Diode("1N4148") # flyback: cathode to +5 V, anode to the collector
pi.GPIO17 >> r1 >> q1.base
q1.emitter >> pi.GND
pi.V5 >> coil.plus
coil.minus >> q1.collector
d1.cathode >> coil.plus # across the winding, band toward the positive rail
d1.anode >> q1.collectorThe diode is not optional here. Without it, the moment the transistor turns off the coil current has nowhere to go and the collector is thrown to hundreds of volts, far past the 2N2222A's rating; with it, the current circulates through the diode and the collector stays near 5 V. The simulator's flyback golden is exactly this experiment: with the diode the collector clamps around 5.8 V, without it the spike is kilovolts.
Driving it in main.py
from gpiozero import OutputDevice
from time import sleep
magnet = OutputDevice(17) # high = coil energised
while True:
magnet.on()
sleep(0.5) # the current ramps up in about 5 ms (L/R)
magnet.off()
sleep(0.5)Electrical notes
- Current ramps as
i = V/R (1 − e^(−tR/L)): with 70 Ω and 100 mH the time constant is 1.4 ms, so the coil is at full current within about 10 ms. - Switch-off: the winding acts as a current source of
iamps. Through the flyback diode the loop resistance is the winding itself, so the current decays with the same 1.4 ms time constant. - The part is a genuine inductor in the solver: the coil makes its circuit island dynamic, and the engine steps it in time. A board of coils is the heaviest realistic load the simulator carries.
- A bare coil straight off a GPIO would pull 47 mA from a pin rated for 16 — use the transistor, as above.
Simulator vs real hardware
- Real coils have capacitance between turns and radiate; neither is modelled, so the simulator's spike without a diode is a numeric artefact of having no stray capacitance to absorb it — still a good demonstration of why the diode is there.
- A real electromagnet's pull force depends on turns and current; here the current is what you see, and the mechanical pull arrives with the relay composite.