A GPS module and an IMU can both feed an ESP32 with numbers that describe motion, but they observe different physical quantities. Treating them as interchangeable sensors creates bad measurements quickly.
A GPS receiver estimates geographic position from satellite signals. Two position fixes can be converted into an approximate distance across Earth’s surface. An IMU such as the MPU-6050 measures acceleration and angular rate along its axes. Those measurements can be used to estimate tilt, rotation, and short-term motion.
That boundary matters in projects that need both location and balance. GPS answers where the device is on Earth. An IMU describes how the device is moving or oriented locally.
GPS does not directly measure the gap between two modules
A typical GPS module connected to an ESP32 reports values such as latitude, longitude, altitude, time, speed, and fix quality. The exact fields depend on the receiver and protocol.
If two devices have GPS receivers, each device can obtain a coordinate:
Device A -> -7.257500, 112.752100
Device B -> -7.258000, 112.753000The ESP32 can calculate the geographic distance after it has both coordinate pairs. GPS itself is not acting like an ultrasonic sensor or LiDAR unit pointed from A toward B.
For relatively short surface distances, the Haversine formula is a common calculation:
a = sin²(Δφ / 2) +
cos(φ1) × cos(φ2) × sin²(Δλ / 2)
c = 2 × atan2(√a, √(1-a))
d = R × cHere, φ is latitude in radians, λ is longitude in radians, and R is Earth’s mean radius. The result d is the great-circle distance between the two coordinates under the spherical-Earth approximation.
The calculation can be numerically precise while the result is still physically inaccurate. The limiting factor is usually the position fixes supplied to it.
Position error becomes distance error
Consumer GNSS positions move even when the receiver is stationary. Satellite geometry, blocked signals, atmospheric effects, multipath reflections, antenna quality, and receiver design all affect the fix.
GPS.gov gives about 4.9 metres as a typical smartphone GPS accuracy radius under open sky and notes that performance worsens around buildings, bridges, and trees. A standalone embedded receiver has its own antenna and receiver characteristics, so that smartphone figure should not be treated as a specification for every ESP32 GPS module.
This has an important consequence. Suppose two stationary receivers are physically two metres apart. If each reported coordinate is moving by several metres, subtracting those coordinates does not produce a reliable two-metre ranging system.
For separation on the scale of tens or hundreds of metres, ordinary GNSS may be useful depending on the required error budget. For precise short-range separation, another ranging technology is usually a better fit. RTK GNSS is a separate class of solution that uses correction information and suitable receivers to reach much higher positioning accuracy.
GPS is receive-only from the device’s perspective
A normal GPS receiver does not transmit a message to a GPS satellite to request its position. GPS satellites broadcast one-way navigation signals containing timing and orbital information, and the receiver uses those signals to solve its position.
That also means two ESP32 GPS nodes do not automatically know each other’s coordinates.
A system with two moving devices needs a separate communication path if device A must know the distance to device B:
GPS A -> ESP32 A -- communication --> ESP32 B
^
GPS B -------------------------------|The communication link might be Wi-Fi, ESP-NOW, LoRa, cellular, or another radio appropriate to the range and data requirements. GPS supplies position; the radio exchanges the position data.
An IMU measures local motion instead
Balance is a different measurement problem.
The MPU-6050, for example, contains a three-axis accelerometer and a three-axis gyroscope. Its accelerometer measures specific force along X, Y, and Z, while its gyroscope measures angular rate around those axes.
A stationary accelerometer can use the direction of gravity as a reference for tilt. In a simple case, roll and pitch can be estimated from acceleration components:
roll = atan2(ay, az)
pitch = atan2(-ax, sqrt(ay² + az²))These expressions are useful when gravity dominates the accelerometer reading. During rapid linear acceleration, vibration, or impact, the accelerometer contains motion-related acceleration as well, so treating every sample as pure gravity gives a distorted tilt estimate.
The gyroscope handles fast rotational motion better because it directly reports angular rate:
angle_new = angle_old + angular_rate × ΔtBut integrating angular rate also integrates bias error. A small gyro offset accumulates into angle drift over time.
Balance normally needs sensor fusion
The accelerometer and gyroscope have complementary weaknesses. Accelerometer-derived tilt has an absolute gravity reference but becomes noisy or misleading during dynamic motion. Gyroscope integration responds smoothly to rotation but drifts.
A simple complementary filter combines them:
angle =
α × (previous_angle + gyro_rate × dt)
+ (1 - α) × accelerometer_angleThe gyro dominates short-term changes while the accelerometer slowly pulls the estimate back toward the gravity reference.
A Kalman filter or a more complete attitude estimator can model the system in greater detail, but a more complicated filter cannot repair a badly mounted sensor, severe vibration, incorrect axis mapping, timing errors, or uncalibrated bias. Mechanical placement and sampling discipline remain part of the measurement system.
Yaw has a different reference problem
An accelerometer provides a gravity reference for roll and pitch when conditions are suitable. Gravity does not provide a compass heading.
A six-axis IMU containing only an accelerometer and gyroscope therefore cannot obtain an absolute yaw reference from gravity. Gyroscope integration can track yaw changes for a while, but bias causes the estimate to drift.
Applications that need heading relative to magnetic north commonly add a magnetometer, producing the sensor set often called a nine-axis IMU. Magnetometers introduce their own calibration and magnetic-interference problems, especially near motors, steel structures, high-current wiring, and magnets.
GPS can sometimes provide course over ground while a device is moving, but that is not the same measurement as the physical heading of a stationary device.
GPS and IMU can complement each other
A useful embedded design assigns each sensor a job instead of expecting one sensor to solve every motion problem:
GNSS:
global position
ground speed
course while moving
long-term geographic reference
IMU:
acceleration
angular rate
tilt
fast local motion
ESP32:
sampling
filtering
state estimation
communication
application logicConsider a small outdoor robot. GNSS can indicate that the robot is 35 metres from a waypoint. The IMU can indicate that the chassis is tilted 12 degrees or rotating rapidly. Neither measurement replaces the other.
The same separation applies to a wearable or tracked object. GNSS can report where it moved outdoors; an IMU can detect orientation changes, impacts, or motion patterns between slower position updates.
Choose the sensor from the quantity being measured
The word “distance” can hide several different requirements. Geographic distance between two outdoor coordinates is a positioning problem. Direct distance to a nearby object is a ranging problem. Wheel travel is an odometry problem. Tilt and balance are orientation problems.
That distinction determines the sensor:
| Measurement | Typical sensor |
|---|---|
| Geographic position | GNSS/GPS |
| Distance between geographic coordinates | GNSS positions + calculation |
| Short direct range to an object | ToF, ultrasonic, LiDAR, or UWB depending on requirements |
| Roll and pitch | Accelerometer + gyroscope |
| Fast rotation | Gyroscope |
| Absolute magnetic heading | Magnetometer combined with IMU |
| High-precision outdoor position | RTK-capable GNSS system |
An ESP32 is useful because it can combine these data streams, not because it changes what the sensors physically observe.
Keep the measurement boundaries explicit
GPS, communication radios, and IMUs solve three separate parts of an embedded system. GPS receives satellite navigation signals and produces a position estimate. A communication radio moves data between devices. An IMU measures local inertial motion.
Once those boundaries are explicit, the architecture becomes easier to reason about. Calculate geographic separation from coordinates only when the GNSS error budget is acceptable. Use an IMU for balance and orientation, with filtering appropriate to the motion. Add a ranging sensor when the requirement is actual short-range separation rather than location.
The ESP32 can connect all of them, but measurement quality still comes from choosing a sensor whose physical observable matches the quantity the application needs.