0
$\begingroup$

I have some sensors attached to arduino uno r3 and an esc. I start the Motor attached to esc through ardiuno with no usb connected to laptop. It starts correctly. There is a must that i will have to start the arduino from non usb supply so that esc is correctly started, means that my motor doesnt start with usb connected to pc. Now how can i get the sensor values to laptop. If i connect the usb to pc after starting the motor, will this work.

$\endgroup$
  • 1
    $\begingroup$ Be advised that directly connecting USB to an Arduino that is directly controlling a motor raises the chance of frying your computer's USB port due to ground-terminal currents or back-currents. If possible, isolate one of these connections with optoisolators (easier) and try to keep power sources separate (harder). $\endgroup$ – Ronan Paixão Oct 3 '15 at 20:12
2
$\begingroup$

What exactly is the question? I'm not sure, so I'll describe one system I have, and hope you find your answer somewhere in this description.

I connect my Arduino USB cable to my laptop all the time, both before and after applying main motor power.

power

"How do I start the Arduino before inserting the USB cable?"

Many people find it easier to plug a AC-to-DC adapter into the wall, which sends 7 V to 12 V to a barrel plug, and then they plug that barrel plug into the barrel jack of the Arduino. On my system, I connect power to an Arduino shield, and that shield sends that power into the Vin pin of the Arduino board. See "Arduino Tutorial: Power up!"; "Powering an Arduino"; "If I have my Arduino connected to a 9V DC supply and then connect the Arduino to a computer with a USB cable, what happens exactly?"

As soon as power is connected to the Arduino, the Arduino starts running the setup() function.

reset

"What happens when the Arduino is running off external 12 VDC power, and I later plug in the USB cable? What do I do about that reset?"

On my (unmodified) Arduino, like most Arduinos, plugging in the USB cable resets my Arduino.

There are 2 ways to deal with this:

  • Accept that it's going to reset occasionally. Design the rest of the system to tolerate resets. Or,
  • Add some hardware to prevent that reset.

I prefer the "tolerate resets" approach, because that also makes the system more robust against the inevitable power glitches. People who design systems that can't tolerate resets usually end up with a system that can't tolerate power glitches either, so they have to put in some sort of battery backup system.

Every time my Arduino resets or powers up, it forgets where it was before and starts my program from the beginning -- the setup() function.

My program does exactly the same thing after it powers up, whether or not the USB cable is plugged into it.

My main loop() prints some debug information out its USB port using the Serial library. Usually nothing is plugged into that Arduino's USB port, so that information is simply lost, but sometimes I plug in a USB cable to my laptop, and that information shows up in the serial monitor window.

My main loop() uses the Servo library to repeatedly tell each servo where to go based on the current values in an array. (Those values are slowly incremented or decremented elsewhere in my program based on other sensors).

Because my setup() function initializes that array to all zeros, when I first power up my system, all my servos reset and suddenly snap back to the zero position (centered). Then they start slowly wandering around.

Because my setup() function initializes that array to all zeros, when I plug in my USB cable after the system is already powered on, all all my servos reset (again) and (again) suddenly snap back to the zero position (centered). Then they start moving normally, slowly wandering around, and I start getting data on my laptop serial monitor.

For my particular system, this sudden motion is a harmless quirk.

How do I prevent this reset?

If you don't want your Arduino to reset every time you plug in the USB cable, you might want to look at "Disabling Auto Reset On Serial Connection"; "Why does starting the serial monitor restart the sketch?"; "Preventing reset on serial monitor connect?"; etc.

How do I make a system that tolerates reset?

Many Arduino systems have no long-term state. They do exactly the same thing every time through the main loop, whether it's the first time through the loop when powered on, or it's been running for months. A person can design such a system to tolerate reset relatively easily.

If you want to collect data while the USB cable is unplugged, then later send that data through the USB port, you might consider storing that data on a SD or MMC card using the Arduino SD library, or perhaps storing that data in EEPROM using the Arduino EEPROM library.

If you want to send commands to the Arduino while the USB cable is plugged in, and you want the Arduino to continue to act on those commands even after the USB cable is plugged back in a second time, you might consider storing the result of those commands in a SD or MMC card or in the Arduino EEPROM using those same libraries. Then both immediately after storing, and also in the setup() routine that runs every time the Arduino resets or is re-connected to power, call a function that reads that non-volatile memory and does the appropriate things.

That stored data is part of the "long-term state", so we put it in non-volatile memory. The normal volatile SRAM memory is zeroed out every time the Arduino powers up or is reset.

$\endgroup$
  • $\begingroup$ I actually want to use laptop to send data to arduino to control the servos. but I will have to use external power to start arduino. $\endgroup$ – Bilal Ayub Sep 29 '15 at 17:19
  • $\begingroup$ Yes, you can send data from the laptop to the Arduino, even if you plug in the USB cable after you power up the Arduino. Did you know you are more likely to get a good answer on this site if you use question mark symbols? $\endgroup$ – David Cary Sep 30 '15 at 2:24
  • $\begingroup$ but the arduino restarts if i plug in the usb cable $\endgroup$ – Bilal Ayub Sep 30 '15 at 8:22
  • $\begingroup$ Yes, I already said that "plugging in the USB cable resets my Arduino.". That's not a problem in my application, but other people have found that it is a problem in their application, and found ways to prevent that reset -- ways that I've already linked to in this answer. What exactly is your question? Asking good questions is surprisingly difficult, and I hope you and I both get better at asking good questions. $\endgroup$ – David Cary Oct 1 '15 at 2:11
  • $\begingroup$ I want to start the arduino before inserting the usb (and using it). Tell me the method for this. that is my question. $\endgroup$ – Bilal Ayub Oct 1 '15 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.