HC-SR04 problems with geared motor running

I was playing around and rebuilt the autonomous car from the Inventor Kit v4 (SIK). It uses the HC-SR04 ultrasonic distance sensor to detect hazards and two of the small geared motors to turn the wheels. The sample code runs the motors at full speed (+/- 255) and it was going pretty fast on my floor so I wanted to slow the speed down a little. I reduced the speed to 200 and then the freaked out and saw hazards everywhere. Doing some debugging I saw that the distance sensed by the HC-SR04 kept dropping down to 6-7 inches when nothing was there. About half of the reading were correct, ~50-60 inches, and the other half way too small. If I set the motor speed b ack to 255 everything worked well. Playing around a bit with the motor speed I could get it down to about 240 before getting false readings. All I’ve done is change the speed in the calls to rightMotor/leftMotor in SIK_Circuit_5C-AutonomousRobot.

So I’m guessing there is some sort of noise the motor is putting out that is interfering with the HC-SR04. Anyone else had this type of problem? Any thoughts on how to contain it?

Thanks,

Bryan

Hi Bryan.

It does sound like a noise issue.

You might try re-routing the HC-SR04’s wires as far away from the motor driver and motor wires as possible to see if that helps. If you have one, a 10 or 100 uF cap close to the power pins on the HC-SR04 might help too.

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