Arduino works fine via USB but doesn't work with barrel jack

Hi,
I am using an Arduino Uno R3. My circuit has currently a sonar(HC-SR04) and a led. What I am trying to do is check the distance with sonar and if it less than 25cm the led should be on. Otherwise it should be off. So, everything works fine if I connect via USB, but when I connect with barrel jack something weird happens, once the distance is less than 25cm the led turns on but never turns off. Again if I connect via the USB, everything works. I used a Samsung M21 phone adapter to connect via USB. For barrel jack I am using a 9V, 1A adapter, I will add the specification via an image below. The output from the barrel jack is a little over 9.8V. I have checked the voltage regulator for continuity but everything seems fine there. Also I have checked the voltage between vcc and gnd in the sonar which seems to be 5.02V.

I will try to give a little bit more insight into what we are trying to do here. Our plan is to build a vending machine with Arduino. We used a 360 MG996R servo to rotate our spiral and a sonar to detect when the item has fallen. When the sonar detects the item has fallen, it will stop rotating the servo. Everything worked before when I connected via USB, but as we want to connect everything into a chassis, nothing is working via the barrel jack. I also I would like to add, as I said everything works with USB, I connected the servo the 5V of the Arduino, everything was powered via the 5V of the Arduino. Could these have any effect in my current situation?

I also had done some work for payment with the same Arduino in a different circuit. In that circuit I had powered a RC522 module and a I2C lcd via the 5v of the Arduino, that circuit also works fine via USB but haven't tested that one with the barrel jack.

Thanks in advance

The Arduino 5V output cannot be used with motors or servos. That tends to burn out the voltage regulator.

Check the output of the power supply with a multimeter and if that is good, try powering the Arduino with nothing attached to it.

I have checked the output, it is a little more than 9.8V. With nothing attached, I loaded the blink sketch, it seemed to work. Also I checked the voltage regulator for continuity everything seemed fine there. I used the article linked below to check if my regulator is the issue, but according to the article everything seems fine there.

Okay, the blink code work with barrel jack connected, what does it imply?

Checking the voltage regulator

Is there a line in your sketch where it's waiting for an active Serial (serial monitor) connection ?

I don't think so. But I am very new to Arduino. I am also attaching my sketch below.

const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
  //  Serial.begin(9600); // Starting Serial Terminal
  pinMode(2, OUTPUT);
}

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   if(cm <= 25){
    digitalWrite(2, HIGH);
   }
   else{
    digitalWrite(2, LOW);
   }
  //  Serial.print(inches);
  //  Serial.print("in, ");
  //  Serial.print(cm);
  //  Serial.print("cm");
  //  Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}

OK, we've eliminated that as a (not uncommon) possibility.

1 Like

All is good. Don't overload the 5V output -- it is for a couple of low power sensors, LEDs, etc. Your external circuit can draw 50 to 100 mA, maximum, as a good rule of thumb.

Also, ignore the terrible tutorials that suggest it is OK to power a servo from the 5V output. This is what to do instead. A 4xAA battery pack or 5V, 2A phone charger with handle 1 or 2 small servos.

1 Like

Thanks a lot. I have reconstructed the circuit with servo and sonar. I have used the barrel jack to power the Servo and sonar, and used my USB to power the Arduino, Only the Arduino. In this set up everything seems to work so far. Though I haven't been able to give input power via the barrel jack because I only have one.

I need a suggestion here. Should I try to make the barrel jack work? Or should I continue with the project using USB for Arduino and barrel jack for other components. As this is a college project and I have a deadline to meet, it would be better for me. I am asking your suggestion because I also need to connect RFID(RC522), LCD(16X2 I2C) and a GSM(SIM 900a) module. Will my current power supply setup have any consequences in the long term?

Thanks in advance for your time.

Please explain how you did that, especially with a 9V power source. Most servos are rated for 4.8 to 6V maximum (at 1 Ampere per small servo), and sonar modules are usually powered from 5V sources. From a 9V source, people use appropriate voltage regulators.

Typical servo power connections look like this:

1 Like

Yes I have used a power supply module to bring the voltage down to 5V. I am adding a image of the module below.

If you are supplying 9V to the barrel jack, you cannot power a servo from the Arduino 5V pin. The servo will draw current through the Arduino's 5V regulator which is dropping 4V (9 - 5), if the servo current is 300ma, the regulator will be dissipating 4V times 0.3 Amps = 1.2 Watts. It will overheat and shut down or burn out.
Post a drawing of your circuit.

I am sorry I have very little idea about tinker Cad. Still I tried my best. In the drawing it shows a 9V battery but I am not using a 9V battery. Rather I am using a 9V adapter with a breadboard power supply module, that brings down the voltage of the adapter to 5V. I am using this 5V to power the Servo and the Sonar.

You don't show the 9 ~ 5V adapter in your picture.

I can't find the module in tinker Cad. It takes as input the barrel jack and outputs 5V via two pins. I have double checked the voltage using a multimeter.

Sorry, I can't help without knowing how every component is connected to every other component.

When you use the breadboard power supply how is the Arduino getting it's power?
Is there a USB cable from the breadboard power supply USB to the Arbuino USB?

1 Like

I have mentioned the entire setup before. I am using bread board power supply for the sensors and there is a separate usb for Arduino. My Arduino barrell jack doesn't seem to work. So, I am using the USB with Samsung Adapter. So far, I haven't faced any issues.

Okay please let me reiterate, Every component is connected as you are seeing in the picture. Only change is instead of a 9V battery, I am using a breadboard power supply unit with wall adapter, which is allowing me to get 5V out of the 9V adapter. And I am using a completely separate USB adapter to power my Arduino. Everything else is exactly the same. Just instead of connecting the positive of the batter to bread board, I am connecting positive of the power supply. Similarly the negative terminal is also connected.

If you know any online tool whcih allows me to draw a breadboard power supply unit, please let me know. I will use that tool to make everything clear to you.

Thanks in advance for your time.

I'm still confused.
So the configuration that does not work is like this:
You have one 9V adaptor plugged into the breadboard power supply barrel jack and that supplies power to the servo and sonar.
Then you have another 9V adaptor plugged into the Arvuino barrel jack for power for the Arduino.
Is that correct?

A pencil, an eraser and a sheet of paper work great.
If necessary you can take a picture of that and use MS_Paint to add text, etc.

1 Like