Ultrasonic sound sensor only works with usb a/b

Hi All,

I just finisched my first project. I created a light for my little son that get’s triggered by a ultrasonic sounds sensor - HC-SR04. The board that I am using is a arduino uno.

It is working wheb plugged into my laptop with a usb a/b cable. So I soldered all components into an old Paw Patrol toy and connected the arduino with the round power cable connector. Now the lights are constantly lighting up instead of being triggered by the sensor. Did I miss something? Why does it only work when connected to my laptop with usb a/b?

#define trigPin 13

#define echoPin 12

int tijd = 1000;
int red = 9;
int green = 10;
int blue = 11;

void setup()

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);


void loop()

{ long duration, distance;

digitalWrite(trigPin, LOW);


digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance < 200){

  analogWrite(blue, random(255));
  analogWrite(red, random(255));
  analogWrite(green, random(255));


else {

  analogWrite(blue, LOW);
  analogWrite(red, LOW);
  analogWrite(green, LOW);




When it's all soldered together does it still work if you plug the USB in?

What exactly are you connecting to the round power connector? A battery? What sort of battery?

It might be worth trying a different pin for trigpin. Pin 13 has the builtin LED connected to it and that can sometimes cause problems.


Yes, if I connect it with my laptop using the usb a/b it still works. ~~When I connect it with a mobile charger via usb a/b it still works. ~~ When I connect it with my laptop via the round connector or via a 5v charger, it doesnt get triggered anymore.

Correction: it only works when its connected to the laptop via usb a/b. So it needs the laptop to function properly.

I changed the trigpin, but it doesnt make a difference.

The round power in connector feeds a regulator and so needs at least 6.5-7V to work. If you are connecting 5V then that needs to go to the 5V pin or via the USB connector.

If by "mobile charger" you mean a 5V power bank then many of them will not work properly with devices like an Arduino that take very little power (they think the battery they're charging is full and so switch off).