I have some sensors attached to Arduino Uno r3 and an ESC. I start the Motor attached to ESC through Arduino 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, which means that my motor doesn't start with USB connected to PC. Now, how can I get the sensor values to a laptop? If I connect the USB to the PC after starting the motor, will this work?
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.
"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.
"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.