A magnetometer measures a magnetic field’s intensity and direction. It gives data on the magnetic field in three X, Y, and Z. This helps the drone determine the magnetic north and change its course accordingly. Apart from direction sensing, magnetic sensors can also detect surrounding magnetism and ferro-metals, such as electricity poles, wires, vehicles, other drones, etc., to help avoid an accident.
Reference- Sensors Used in Drones