How can I avoid trying to run motors when only connected to laptop USB?

I'm building one of these cheap 4WD car kits and have 4 motors connected to a L298N motor controller (as detailed here). I have external power connected to the 12V on the board, and then connect the 5V back to the Ground/VCC on the v5.0 sensor shield (the blue plug).

I'm supplying 9V to the 12V input on the motor controller and everything seems to work fine.

However, if I disconnect the external power and then connect to my laptop (in order to upload a new sketch), the sketch runs (because it has power) and I hear whining (from the PWM I think). The motors don't turn, but I don't know if that's because they have no power, or if they're trying to pull power from the laptop USB and it's not enough. If it's the latter, I'd really like to ensure my sketch doesn't try to run motors unless it's on external power (eg. USB not connected).

Is there any way to detect this (without just putting a switch in)? It's an Arduino Uno clone.

(deleted)

I suppose that you could use a couple of resistors as a voltage divider added to your Uno clone to sense the external voltage and provide a signal to an input, but I have not read about somebody actually doing this.

spycatcher2k:
Unplug the shield.

I guess that's one way - though a little impractical once everything is assembled.

I thought there may have been an easy way to detect, but if that's not the case when I'll probably not worry. Once I'm past uploading test sketches, it probably won't just start the motors immediately so it'll be less of an issue. I thought I'd ask in case there was a simple way to tell though :slight_smile:

Does your L298 module have 5V regulator on board? If so, you could use it's output to pull up an input pin, if motor power was not connected, the regulator output would be LOW and you could inhibit motor operation.

outsider:
Does your L298 module have 5V regulator on board? If so, you could use it's output to pull up an input pin, if motor power was not connected, the regulator output would be LOW and you could inhibit motor operation.

It does, but I've connected it directly to the VCC on the sensor shield, so as I understand, it's essentially connected to the Arduino, so it'll have 5v on it even when connected via USB? (I presume this is safe - I haven't see anybody say you must disconnect it all when connecting to USB!)

Is the L298 module powering the sensor shield, or vise versa? Is the voltage regulator jumper in place? Can you post a wiring diagram?

The L298N is powering the sensor shield and the jumper is present. I'm putting 9V across the 12V and GND pins of the L298N and connecting 5V and GND pins back to the VCC/GND of the v5.0 Sensor shield (which is mounted atop the Arduino). When not connected to my laptop, the 9V into the L298N is the only power source.

Slight tangent - I'm curious what happens if I leave the 9V power to the L298N and connect USB. That would presumably put 5V on the output-side of the 5V regulator on the L298N while it's trying to provide it - is that safe?

Maybe use the serial abilities. If serial "connects" motors drive values = 0

I was originally thinking something like that, but isn't it possible I plug it into the USB and it gets power but I don't have the IDE open?

In any case, seems like it's more effort than I thought so I'm not going to worry about it for now (the problem will be short-lived anyway).

I am still curious to know if it's safe to connect to USB while the motor board is also trying to provide 5V though (eg. so I don't have to keep turning off the external power).

I would say yes if there is proper protection against voltage spikes.
Depending on your coding or computer skills you could possibly designate one USB port for this and make that USB have data in its serial in a way that you won't have to have the IDE open.
I would have no idea on how to do this though.