@dc42 Thank you for your suggestions.
Indeed, the issue was caused by motor currents inducing some noise in the sensor cable.
I followed the procedure I described above and then simply sent an M18 command to disable the motor, and it worked fine. However, when I sent an M17 command to enable the motors again, the issue reoccurred.
Iβve since changed the wiring, and now everything works perfectly.
Thank you so much for your help!