Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

A magnetometer is a sensor that measures magnetic field strength and direction, allowing a robot to estimate its heading like a digital compass. In robotics, this helps a mobile robot, drone, or rover know which way it is facing even when wheel slip or drift affects other sensors. The sensor usually reports magnetic field components along three axes, which can be combined to find the direction of magnetic north.

This information is important for navigation, mapping, and stable autonomous motion.

Understanding Robotics: Magnetometer (Digital Compass)

A compass reading is only useful when the sensor is level or when its tilt is known. The Earth’s magnetic field points downward into the ground in many places, not simply across the surface. When a robot pitches forward or rolls sideways, part of this downward field moves into the sensor directions used for heading.

The result can be a false turn even if the robot has not rotated around its vertical axis. Robots handle this with tilt compensation.

They use an accelerometer to estimate which way gravity points, then mathematically level the magnetic field measurement before calculating heading. This works best when the robot is not accelerating hard, since an accelerometer then senses motion as well as gravity.

Nearby materials can change the reading far more than students often expect. A battery, motor, steel screw, speaker, wire carrying current, or metal chassis can bend the local magnetic field. A fixed magnetic object creates a steady shift in the measurements.

This is called hard iron error. Magnetic material in the robot frame can change the field differently in different directions. This is called soft iron error.

During calibration, a robot is rotated through many orientations while measurements are recorded. An ideal set of readings forms a round pattern around the center. Offsets move that pattern away from the center.

Stretching makes it oval. Calibration recenters, rescales, and sometimes rotates the data to make the field estimate more reliable.

A magnetometer does not normally control a robot by itself. It is one part of a sensor fusion system. A gyroscope measures turning very quickly, but small errors build up over time.

The compass provides a slower long-term reference that can pull the estimated direction back toward the correct value. GPS can give direction of travel when a vehicle is moving, yet it cannot reliably tell the direction a stationary robot is facing. Wheel encoders estimate turns from wheel motion, but they fail when wheels slip.

Combining these sensors gives better results than trusting any single sensor. Software often reduces the influence of the compass when its field strength looks unusual, because that may indicate nearby interference.

Students should test a compass sensor in the place where the robot will operate, not only on a clear desk. Rotate the robot slowly and compare its reported direction with a known reference. Keep motors running during tests because their magnetic effect may appear only under power.

Record headings at several positions and notice whether the error changes near walls, tables, or metal objects. Remember that magnetic north is not exactly geographic north. The difference depends on location and changes gradually over time, so navigation systems may need a local correction.

Heading also wraps around at north. A change from three hundred fifty degrees to ten degrees is a small turn, not a jump of three hundred forty degrees. Handling this wraparound correctly prevents sudden steering errors.

Key Facts

  • A 3-axis magnetometer measures magnetic field components Bx, By, and Bz.
  • Horizontal heading can be estimated by heading = atan2(By, Bx), then converted to degrees.
  • Magnetic field magnitude is B = sqrt(Bx^2 + By^2 + Bz^2).
  • Declination correction converts magnetic heading to true heading: true heading = magnetic heading + declination.
  • Hard iron distortion adds a constant offset to measured field values, such as Bmeasured = Btrue + Boffset.
  • Soft iron distortion stretches or compresses the measured field pattern and often requires scale and rotation calibration.

Vocabulary

Magnetometer
A sensor that measures the strength and direction of a magnetic field along one or more axes.
Heading
The direction a robot is facing, usually measured as an angle from north in degrees.
Magnetic declination
The angle difference between magnetic north and true geographic north at a location.
Hard iron distortion
A constant magnetic offset caused by nearby permanent magnets or DC currents in the robot.
Soft iron distortion
A distortion caused by materials that bend or concentrate magnetic field lines, changing the measured shape and scale of the field.

Common Mistakes to Avoid

  • Using atan(By divided by Bx) instead of atan2(By, Bx) is wrong because it can place the heading in the wrong quadrant.
  • Ignoring magnetic declination is wrong because magnetic north and true north are usually not the same direction.
  • Calibrating the sensor near motors, batteries, or steel tools is wrong because those objects can distort the measured field and produce a bad offset.
  • Assuming a magnetometer works perfectly during acceleration or tilt is wrong because the horizontal components must often be tilt compensated using an accelerometer or IMU.

Practice Questions

  1. 1 A robot measures Bx = 30 microtesla and By = 40 microtesla on a flat surface. Find the magnetic heading using heading = atan2(By, Bx) in degrees.
  2. 2 A magnetometer reads Bx = 18 microtesla, By = 24 microtesla, and Bz = 40 microtesla. Calculate the total magnetic field magnitude B = sqrt(Bx^2 + By^2 + Bz^2).
  3. 3 A robot gives stable compass readings on a wooden table but becomes inaccurate when its motors turn on. Explain whether this is more likely hard iron distortion, soft iron distortion, or electrical interference, and describe one calibration or design fix.