A magnetometer can sense where the strongest magnetic force is coming from, generally used to detect magnetic north.
This compact sensor uses I2C to communicate and it’s very easy to use. It can be use with either 3 or 5V microcontrollers. Simply connect VCC to +3-5V and ground to ground. Then read data from the I2C clock and data pins.
Download library and connect the SCL pin to your Arduino's I2C clock pin, and SDA pin to your Arduino's I2C data pin and upload test example sketch to read out magnetic field data and heading (i.e. which way is north).
Adafruit Product Page: https://www.adafruit.com/products/1746
Adafruit Learn Link: https://learn.adafruit.com/adafruit-hmc5883l-breakout-triple-axis-magnetometer-compass-sensor/overview
Arduino Library: https://github.com/adafruit/Fritzing-Library