Magnetic field sensors are important in a variety of applications, including transport and medical devices. However, existing solid-state approaches for the detection of three-dimensional magnetic fields require multiple sensors, making the set-ups bulky. Here, we show that a single spin–orbit torque device composed of a Ta/CoFeB/MgO heterostructure can detect a vector magnetic field. In-plane and out-of-plane field components lead to the displacement of domain walls in the CoFeB layer, modulating the associated anomalous Hall effect resistance. Modulation of the anomalous Hall effect resistance varies linearly with the x, y and z components of a vector magnetic field. Our compact three-dimensional magnetic field sensor exhibits good linearity within a certain range (3.2%, 2.7% and 4.3% for the x, y and z directions, respectively) and high sensitivity (205, 282 and 1,845 V A−1 T−1 for the x, y and z directions, respectively). The sensor also exhibits low 1/f noise. Changes in the anomalous Hall resistance of a single device comprising a Ta/CoFeB/MgO heterostructure can be used to probe three-dimensional magnetic fields with high sensitivity and good linearity.