Thanks for the suggestion Chris. I was thinking the motors were producing an ultrasonic noise that would give the bad readings, similar to the warning about air conditioners in the Inventor Kit guide. But you seemed to be thinking it was electrical noise. You were right. I tested by using a second Arduino board and putting the distance sensor on that so I could move the motors around and have them electrically isolated. I had really consistent distances when the motors were off or were well away from the set up. Small fluctuations (>1 inch variability at 13 inches away) when the motors were slightly above the object being measured and back to very small variations when the motors were next to the distance sensor (closest to the autonomous car arrangement).
For example, readings with the motor above the object:
13.18
13.05
13.18
13.02
13.18
13.07
13.36
12.86
12.91
12.74
13.48
12.74
11.80
13.13
I then moved the distance sensor to the same breadboard as the motor controller but kept it connected to the second Arduino so still electrically isolated but the exact layout as for the car. I was able to get consistent distance readings.
Once I connected back to the same Arduino as the motors I started getting the big drops. Instead of just random noise I would get something like this:
Motor: 197 Distance: 11.22
Motor: 198 Distance: 11.61
Motor: 197 Distance: 11.34
Motor: 198 Distance: 11.59
Motor: 197 Distance: 11.24
Motor: 197 Distance: 4.14
Motor: 197 Distance: 3.48
Motor: 197 Distance: 3.94
Motor: 197 Distance: 4.59
Motor: 198 Distance: 5.34
Motor: 198 Distance: 11.32
Motor: 198 Distance: 11.20
Motor: 198 Distance: 11.21
Motor: 197 Distance: 11.10
Motor: 197 Distance: 11.05
Motor: 197 Distance: 11.22
Motor: 197 Distance: 11.11
Motor: 198 Distance: 11.43
Motor: 197 Distance: 11.14
Motor: 198 Distance: 11.24
Motor: 197 Distance: 10.91
Motor: 197 Distance: 4.92
Motor: 197 Distance: 3.78
Motor: 197 Distance: 4.11
Motor: 198 Distance: 11.38
Motor: 197 Distance: 11.12
Motor: 198 Distance: 11.50
Motor: 198 Distance: 11.09
[Measurements every 0,5 sec. Motor is the PWM signal I was sending to the motors. I don’t get this affect when the PWM is close to 255.]
I put a 10 uF capacitor in as you suggested (in parallel connecting VIN to GRD) and that didn’t change anything. Putting the 100 uF capacitor in actually made some of the drops go to zero. Hopefully I was putting them in the circuit properly.
To solve the car problem I added a proximity sensor to verify something is close and that works adequately so now this is just an unexpected issue I’m curious about.
Bryan