Photo Interrupter and Motor electromagnetic interference

Hello Everyone

I am trying to build a rotary encoder with a photo interrupter to measure the distance traveled by a small car by measuring the rotations of the axis wheel. When the motor battery is not connected the system works just fine and it measures the right distance. (even when i push it really quickly with my hand). When i connect the motor to the battery though the distance measurement goes crazy. It just goes randomly up even when the car is not moving at all. The only thing I could think of beaing the cause of my problem is some kind of electromagnetic interference from the DC motor. If that is the case what could be a apossible solution? I should note the motor is in no way connected to the Arduino. It has a separate battery.
Below there is a pic of the system (sorry about the mess but it’s pretty staightforward)

Any help would be appreciated!

It would help if you could draw out the circuit and post it (also, please post the code).
Do you have a link to your motor and its driver circuit?

Do you have a reverse connected diode across the motor to suppress the back emf?
Could be helpful to add 100µF-470µF capacitor across the breadboard power rails.

It just goes randomly up even when the car is not moving at all.

Hmmm, if the car is not moving (and hence the motor not drawing current) then it seems odd that EMI, RFI or some such similar interference is the issue.

If you’re using the same battery to power the motor and your Arduino I could expect noise from the motor to upset the Arduino but not when the car / motor is stationary.

Are you powering the motor using PWM from the Arduino or by some other means (eg, directly from the battery)?

Does the motor suck so much current (even whilst stationary) that it is causing the Arduino to malfunction? I’m guessing you don’t have access to a 'scope as you would already have looked at the state of the power supply going into the Arduino, for noise spikes and the like. Decoupling the power to the Arduino would be a good idea as would a small suppressor capacitor across the motor.

If you power the motor using a temporary, separate battery do you still get issues?

The interference is from the PWM in the motor drivers. You need shielded cable for the encoders for starters,
and the suppressor cap idea is good too. Adding small value caps (1nF to 10nF?) across the encoder outputs
may cut down the interfence a lot too.

Any loose loops of wire near a motor or its cabling will pick up loads of interference. If the wires to the motor
aren’t twisted pair they will be putting out loads of interference. So control those cables - twisted pair for all
high current paths, shielded for all sensor cables. Keep the cable routes separate.

If I read the OP's first post correctly his motor battery is not connected to the arduino etc at all...

So - do you still have problems if the motor isn't operating?

DC brushed motors can put out lots of 'mush' up to RF frequencies, but for an arduino I would have thought that if the circuit impedances are low enough there shouldn't be too much problem.

Try adding some ceramic capacitors across the motor terminals - suggest 100n and 1n in parallel.