Code stops working after unplugging USB cable; troubles uploading

(I'm having issues uploading and with the code not working once I unplug the USB cable).

Here's the basic description of my project: I have a MEGA hooked up to a MEGA sensor shield v2.0; that is connected to an L298N Dual Motor controller module--> two PWM motors. I also have the motor encoders and 3 HC-SR04 distance sensors hooked up to the sensor shield. All of this + a chassis makes up a robot (two wheeled). There are two 12V batteries connected to the motor module, and the Arduino is powered by a reverse-polarity-protector going from the module to A12 on the sensor shield.

ISSUE: I'm trying to make the robot autonomously navigate a room using the motors and sensor input. I had written code that worked, but when I try it now (a few months after writing) the robot just spins in circles.It is supposed to start by going forward and then reacting to the sensors; continue going forwards unless something is in the way, basically.

I've deduced that it can't be that the sensors are malfunctioning constantly, becuase the code works just fine UNTIL I unplug the arduino from the computer.

WHAT I NEED HELP WITH (please): any suggestions as to why the code might be working fine until I unplug the USB cable? I'm also having a (presumably related) connectivity issue where the Arduino program (like on the computer) just fails to recognize the port half the time, so I have trouble uploading.

I'm assuming this is either an issue with the board or sensor shield? (I mean, it was working near perfectly before... /sigh).

Thanks in advance.
brightorange

I'm trying to make arobot autonomously navigate a room using the motors and sensor input. I had written code that worked, but when I try it now (a few months after writing) the robot just spins in circles.It is supposed to start by going forward and then reacting to the sensors; continue going forwards unless something is in the way, basically.

I've deduced that it can't be that the sensors are malfunctioning constantly, because the code works just fine UNTIL I unplug the arduino from the computer.

Any suggestions as to why the code might be working fine until I unplug the USB cable? I'm also having a (presumably related) connectivity issue where the Arduino program (like on the computer) just fails to recognize the port half the time, so I have trouble uploading.

So I read in some post that it might be an issue with the Arduino using the CPU when plugged in, but there was no solution? It seems like that might be the problem here, and I'm assuming this is either an issue with the board or sensor shield? (I mean, it was working near perfectly before... /sigh).

BASIC LAYOUT OF PROJECT: I have a MEGA hooked up to a MEGA sensor shield v2.0; that is connected to an L298N Dual Motor controller module--> two PWM motors. I also have the motor encoders and 3 HC-SR04 distance sensors hooked up to the sensor shield. All of this + a chassis makes up a robot (two wheeled). There are two 12V batteries connected to the motor module, and the Arduino is powered by a reverse-polarity-protector going from the module to A12 on the sensor shield.

Thanks in advance.
brightorange

This type of problem is usually a power issue. When you have the Arduino board plugged in to the USB cable, you are able to draw 500 mA from the 5V line. When you disconnect the USB cable and power the Arduino from the barrel jack or Vin pins, the current the voltage regulator can provide is much less, perhaps only ~100 mA with a 12 V supply.

1 Like

Thanks so much for responding!

Hmm that makes sense. Do you see a way I might be able to draw the necessary current without the USB cable? I’m not really sure how else to power it—since I think the straight 12V would be enough to fry the Arduino, I figured running it through all that kind of gets it under control. (Lmk if I’m not making sense).

Also, is the “current voltage regulator” you mention a part of the board/sensor shield?

And, if you’ve got time, another weird thing has cropped up: I can get sensor and encoder readings when I’m using just the MEGA itself, but I get absolutely nothing when I use the same exact pins through the sensor shield. Do you think this is related to the power issue?

Thanks again!

@brightorange, do not cross-post. Threads merged.

brightorange:
Do you see a way I might be able to draw the necessary current without the USB cable? I’m not really sure how else to power it—since I think the straight 12V would be enough to fry the Arduino, I figured running it through all that kind of gets it under control. (Lmk if I’m not making sense).

The voltage regulator on the Arduino Mega is sufficient to power the Arduino Mega itself. It's only when you start powering other things things with significant current draw off the 5V pins of the Mega that you'll exceed the limits of the voltage regulator.

brightorange:
Also, is the “current voltage regulator” you mention a part of the board/sensor shield?

There is a voltage regulator on the Arduino Mega connected to the barrel jack and the VIN pin. I'm not familiar with your sensor shield so I can't tell you what is going on there. If you want help with that, you'll need to give us enough information to work with.

brightorange:
And, if you’ve got time, another weird thing has cropped up: I can get sensor and encoder readings when I’m using just the MEGA itself, but I get absolutely nothing when I use the same exact pins through the sensor shield. Do you think this is related to the power issue?

I'd need to know more about the sensor shield and how you have everything wired to answer that question.

@coding badly ah, sorry. I couldn't figure out how to delete the first one that I had definitely posted in the wrong section. (how do you do that?)

Thanks.

@pert thanks again for responding

(not totally sure how to do the reply section by section, but I'll just try to put things in the same order)

So what I've done is hooked up two 12V batteries to a motor module, from which I take a reverse-polarity protector and plug that into pin A12 on the sensor shield. The things drawing current off of the board would include 2 sets of encoders and 3 ultrasonic (HC-SR04) distance sensors.

So with those same 12V batteries: the wires coming off were to short/thick to hook up properly, so I soldered on new wires that are slightly thinner on the red end and even thinner on the black/GND end. I'm starting to think this might be the problem/reason things aren't working?


I'm using https://www.amazon.com/SainSmart-Sensor-Arduino-MEGA1280-MEGA2560/dp/B007PABRNM (or similar, since it was passed on to me).

--
I actually made a (sort of messy) schematic diagram! I'll attach the png of it below. Please let me know if anything's unclear.

--
Update: so I was having the issue where everything was working just plugged into the Arduino MEGA itself, but not on the sensor shield. I've learned that there is a tiny jumper that goes on the sensor shield that does this:

"Jumper on: +5 comes from the Arduino. Should be limited to about 300 ma
Jumper off: +5 comes from the blue terminal strip and an external supply."
(http://www.yourduino.com/sunshop/index.php?l=product_detail&p=195)

It's when I unknowingly/accidentally had/took the jumper off that none of the sensors were working (makes sense because no power to anything on the board). Now that I have it back on, I'm back to the original problem where my robot just spins in circles the second it's disconnected from the USB cable.

I decided to test whether the robot was just like weirding-out because of lack of power, so I connected an XBEE to it to display the results of the navigating-code in real time. It continuously displays 'L' for 'left'. (I'll include this section of the code below).

byte rx_byte = 0; // stores received byte

void adjust(){
Serial.println(bm.dist()); //so middle sensor
Serial.println(bl.dist()); //so left sensor
Serial.println(br.dist()); //so right sensor

int tooclose = 20;
int justright = 27;
int goaway = 10;

//since one of the front sensors is broken, I'm going to code this with the back

if(bm.dist() > tooclose && bl.dist() > goaway && br.dist() > goaway)
{
 Serial.println("FRONT");
 rx_byte = 'F';
 setSpeeds(70);
}
else if (bm.dist() < tooclose)
{
 if(bl.dist() < br.dist() && bl.dist() < tooclose)
 {
 Serial.println("RIGHT");
  rx_byte = 'R';
 turn(-90);
 delay(250);
 }
 else if(br.dist() < tooclose)
 {
 Serial.println("LEFT");
  rx_byte = 'L';
 turn(90);
 delay(250);
 }
 else if(bl.dist() < tooclose && br.dist() < tooclose)
 {
 Serial.println("BACK");
  rx_byte = 'B';
 setSpeeds(-70);
 delay(400);
 }
}
}
void setup() {
 Serial.begin(9600);
 Serial.println("Starting up...");
  Serial3.begin(9600); // serial port 3
 //digitalWrite(40, LOW); //interruptPin
  //setSpeeds(100);
}
void loop() {
 adjust();
 if (Serial3.available()) {
 Serial3.write(rx_byte);
 }
}

So I guess my question is how you might suggest adjusting the power so I have enough current for the robot to work?

Thanks again for your time.

Hello,

Double update! I did some more testing, and I think I've figured out the root problem.

I'm using XBEEs as a pseudo serial monitor for debugging what's going on after I uplug the USB cable, since my code starts malfunctioning all of a sudden. (Now, it's just running backwards and displaying 'B').

I have a quick sketch written up to test the encoders, and when I ran that without the USB cable, it worked fine. Deduction: so the pins on the sensor shield are still receiving power?

So then I thought to test just the sensors individually, with this loop:

byte rx_byte = 0; // stores received byte

void adjust(){
Serial.println(bm.dist()); //so middle sensor
Serial.println(bl.dist()); //so left sensor
Serial.println(br.dist()); //so right sensor

int tooclose = 10;
int justright = 20;
int goaway = 5;

if(bm.dist() < tooclose)
{rx_byte = 'Y';
}else if (bm.dist()> tooclose)
{rx_byte = 'N';
}
}
void setup() {
 Serial.begin(9600);
 Serial.println("Starting up...");
  Serial3.begin(9600); // serial port 3
 //digitalWrite(40, LOW); //interruptPin
  //setSpeeds(100);
}
void loop() {
 adjust();
 if (Serial3.available()) {
 Serial3.write(rx_byte);
 }
}

Lo and behold I'm getting the appropriate 'Y' and 'N's when the USB cable is plugged in, but am not receiving anything when it's just running off the batteries.

Oh, and I know I mentioned that I had extended the original 12V battery wires with thinner ones, but I reverted to the original (what a struggle lol they're so short) and it didn't really make a difference. :frowning:

So now the question is why the encoders would work and not the sensors?

Thanks.

brightorange's picture:


Why does is VCC on the motor board connected to A12 on the Mega?

What is the "Reverse polarity protector"? A diode?

Are all the grounds connected when running off battery?

You didn't show how the XBee is powered.

@pert
I'm going to be totally honest--this setup was sort-of handed down to me, and they had plugged the VCC into A12 on the MEGA, so I did too. It'd worked fine until now/just recently.

Here's the Reverse Polarity Protector I was talking about: Turnigy 5A 26V SBEC (SBEC-26V) It's supposed to provide a constant 5A, and I have it set to a constant 5V as well (it can do 6V instead).

Yes, all the grounds are connected--as in, all the sensors, the XBee, the battery grounds, etc..

Yikes, you're right. The XBee is plugged into the 3.3V pin on the MEGA and I've also connected the GND.

Also--I mentioned above that I wasn't receiving anything on the other side of the XBee once I unplugged the USB cable, but now I am... except all the sensor values are registering as exactly zero. This is happening even after I switched the sensor-pins to where I had connected the encoders, just to make sure I'm not using some fried pins.

Thanks.

brightorange:
I'm going to be totally honest--this setup was sort-of handed down to me, and they had plugged the VCC into A12 on the MEGA, so I did too. It'd worked fine until now/just recently.

Is that the only power supply to the Mega? If so, that is very bad news and it may have damaged the Mega. It is possible to back power a microcontroller through the IO pins, but that's not something you would ever do intentionally.

brightorange:
Here's the Reverse Polarity Protector I was talking about: Turnigy 5A 26V SBEC (SBEC-26V) It's supposed to provide a constant 5A, and I have it set to a constant 5V as well (it can do 6V instead).

That's a voltage step-down converter. It happens to have reverse polarity protection but it's not at all accurate to call it a "Reverse Polarity Protector". It's primary job is to reduce voltage down to 5 V in a relatively efficient manner (compared to a linear voltage regulator).

Hi @pert thanks for sticking with me!

Yeah, that’s the only power supply to the MEGA. Rip, okay gotcha. Thanks for letting me know. :confused:

So the Voltage-Step-Down-Converter is a solid way to power the MEGA? In trying to figure this out, what sort of confused me is that it looked like the MEGA was better-powered (for lack of better words) when I connected the converter (+) line to wither 5V or A12 instead of Vin. Let me explain:

In the midst of all this, I decided to try out the simple Blink test-sketch. When I plugged a 9V battery into the Arduino’s GND and Vin pins, BLINK worked. But when I plugged the Reverse Polarity Protector into the Arduino’s GND and Vin pins, the Arduino received power yet the code did not work. So I plugged the Reverse Polarity Protector into the Arduino’s GND and 5V pins–it worked! (I was then even more confused).

Thanks again!

brightorange:
So the Voltage-Step-Down-Converter is a solid way to power the MEGA?

Yes, as long as you are supplying the converter with at least 8 V. It's not clear to me how the VS and VCC pins of your motor board are related to each other.

brightorange:
what sort of confused me is that it looked like the MEGA was better-powered (for lack of better words) when I connected the converter (+) line to wither 5V or A12 instead of Vin.

There is a voltage regulator on Vin. You need to supply at least 7 V input to Vin to get the necessary 5 V output from the voltage regulator. Since the voltage step down module is only supplying 5 V, you can't connect it to Vin. If it's supplying 5 V, you should connect it to the Mega's 5 V pin, which bypasses the voltage regulator and powers the board directly. Just make sure you don't have the module set to 6 V and make sure you don't hook it up backwards, because there is no protection circuitry on the Mega's 5 V pin.

pert:
There is a voltage regulator on Vin. You need to supply at least 7 V input to Vin to get the necessary 5 V output from the voltage regulator. Since the voltage step down module is only supplying 5 V, you can't connect it to Vin. If it's supplying 5 V, you should connect it to the Mega's 5 V pin, which bypasses the voltage regulator and powers the board directly. Just make sure you don't have the module set to 6 V and make sure you don't hook it up backwards, because there is no protection circuitry on the Mega's 5 V pin.

That makes so much sense, thank you. I've been seeing the pieces all over the forum, but it's so much easier to understand all laid out. Playing around with the voltage step down module, it's hard to say that I didn't do either (rip).

pert:
It's not clear to me how the VS and VCC pins of your motor board are related to each other.

So I've attached a picture below that's a sort-of schematic of the motor module. You'll see that there's a +5 pin (near the one that accepts +7 to +35V, which is where I'm feeding in 12V) and that's where I am connecting the positive terminal of the voltage regulator. I'm connecting the negative terminal directly to the 12V battery's/the common GND.

Thanks again, @pert.



brightorange:
So I've attached a picture below that's a sort-of schematic of the motor module. You'll see that there's a +5 pin (near the one that accepts +7 to +35V, which is where I'm feeding in 12V) and that's where I am connecting the positive terminal of the voltage regulator.

That step-down voltage module requires at least 8 V input. It won't be able to output 5 V to the Mega if you're only supplying it with 5 V. You should connect the step down directly to the 12 V of the battery

Wow... upset at myself for not catching that. (I know I've said it already, but... it worked before, so I didn't even think about this part...)

Thank you so much for all of your help, @pert. I can't even tell you how much I appreciate it.

You're welcome. I'm glad if I was able to be of assistance.
Per