You can also use a hall effect IC and a small magnet. The hall effect ic's come in a 3 pin TO-92 package and are easy to interface to your micro.

For example

https://dronebotworkshop.com/stepper-motor-hall-effect/