Hey, so I'm using an ultrasonic sensor to control the speed of my DC motors. Pretty straightforward stuff, except that when I run the code on my Arduino, the ultrasonic sensor throws up garbage values or keeps oscillating between values when the DC motor starts up. I am assuming this is because of the noise emitted by the DC motor. My 3V DC motor also emits a high pitched frequency noise when running below a signal of 125. I am using a 3V DC motor, HC-SR04 ultrasonic sensor, Arduino UNO and I am powering all of it with my Arduino board. How do I get rid of this issue? Code is below
include <NewPing.h>
NewPing sonar(9, 8, 500);
int motor = 3;
void setup() { // put your setup code here, to run once:
pinMode(motor, OUTPUT);
Serial.begin(9600);
while(!Serial);
}
void loop() { // put your main code here, to run repeatedly:
int time_ms = sonar.ping_median(5);
int distance = sonar.convert_cm(time_ms);
Serial.println("The distance is ");
Serial.println(distance);
delay(1500);
}