Low-cost inertial measurement units (IMUs) are commonly used to determine the orientation of objects, such as unmanned aerial vehicles (UAVs) and smartphones. They calculate yaw by measuring Earth’s magnetic field’s horizontal components. However, in the presence of tilt (pitch or roll), a tilt-compensation operation is necessary. This is usually done by projecting measurements onto a horizontal plane. This method has limitations, particularly for large tilt angles and when the IMU is pointing toward the east or west directions. In this paper, we expose the shortcomings of this conventional approach and propose a novel machine learning–based solution employing an artificial neural network (ANN). This method eliminates the need to determine tilt angles and uses accelerometer and magnetometer measurements as its inputs. The dataset for training and testing the ANN was collected based on a 3D nonmagnetic scaled platform, using a low-cost IMU and a Raspberry Pi platform. On one hand, our method outperforms the conventional tilt-compensation technique and other complementary filters (Madgwick and Mahony) in terms of accuracy, as evidenced by the root mean square error (RMSE = 1.95°). However, this superiority comes at the expense of a more complex system that consumes more processing time.