Can Arduino carry data over USB? - Page 1
Forum section: electronics / projects

author: Ben321
author_rank: Frequent Contributor
timestamp: November 07, 2019, 09:30:59 pm
content: From what I've ready about it, the Arduino USB port seems to just be used to update your program, so you write the program on the PC and then upload it over USB to the Arduino. I'm wondering something though. Can that same port be used to transfer data to an Arduino running a program? Let's say I want to use an Arduino as interface between an external sensor, and my PC. The data pins on the Arduino can obviously collect digital data from the external sensor's analog-to-digital converter, and then I want to send that data from the Arduino over USB to my PC, which will be running software that will collect that data and log it (save the data to a file on the harddrive). Is this possible? Or do I need another board (shield board) that adds another USB port to the Arduino, rather than using its main USB port?

author: ejeffrey
author_rank: Super Contributor
timestamp: November 07, 2019, 09:39:25 pm
content: Yes. The USB port acts as a regular serial port.

author: rstofer
author_rank: Super Contributor
timestamp: November 08, 2019, 12:19:44 am
content: Tools->Serial Monitor is the console device in the IDE that uses the USB COM port of the Arduino.How you format the data is up to you. It is best if the data can be displayed as plain text on the Serial Monitor but it isn't required.

author: NiHaoMike
author_rank: Super Contributor
timestamp: November 08, 2019, 02:18:14 am
content: Quote from: rstofer on November 08, 2019, 12:19:44 amThat's actually a very inefficient way to pack the data. It works if the data rate is low, but it's worth learning some better encoding schemes.https://en.wikipedia.org/wiki/Binary-to-text_encodingFor a beginner, I suggest encoding the data as hex. It's straightforward to understand and leaves a lot of symbols left over for out of band communication. For example, you can use a newline character to denote the end of packet.

author: Renate
author_rank: Super Contributor
timestamp: November 08, 2019, 04:38:11 pm
content: Aw, if you're using an AVR8 to connect a sensor over USB there are better ways than using a serial over USB.It would be better to use an AVR8 with a USB interface, like a ATMega32u4 (Leonardo in Arduino-speak, I think).Using serial over USB gets into problems of framing/waiting for the 0x0a (newline or whatever).Use the USB HID sensor protocol.Ok, you don't have to get too serious about the descriptors, just make send things in regular HID reports.Neat, clean, you never get half a report, like with serial over USB.The one exception to all this is if you want/had to use a serial optoisolator, in which case serial out of the AVR8 to isolator to FTDI would work.You could also use a USB optoisolator too.

author: iMo
author_rank: Super Contributor
timestamp: November 08, 2019, 04:57:58 pm
content: Even an atmega8 can transfer data via USB..

author: alsetalokin4017
author_rank: Super Contributor
timestamp: November 08, 2019, 05:47:05 pm
content: https://playground.arduino.cc/Interfacing/Processing/

author: Ben321
author_rank: Frequent Contributor
timestamp: November 12, 2019, 08:34:21 am
content: Why not just send binary data over serial? Serial ports support 8-bit bytes, and rates up to a couple megabits-per-second (at least when using a modern USB to serial converter). Of course, I don't know if Arduino supports 8-bit bytes, or if it is limited to 7-bit bytes (text mode only). And I'm not sure what its data rate limit is.

author: Ben321
author_rank: Frequent Contributor
timestamp: November 12, 2019, 08:36:36 am
content: Quote from: Renate on November 08, 2019, 04:38:11 pmProgramming USB is hard, unless you are actually programming for a serial port, and the USB just functions as a USB-to-serial converter. I think I'll stick with serial programming, if that's what it comes down to. What you can do in a few lines of code with serial (including serial over USB), takes at least a hundred lines of code to do in pure USB.

author: NiHaoMike
author_rank: Super Contributor
timestamp: November 12, 2019, 01:29:58 pm
content: Quote from: Ben321 on November 12, 2019, 08:34:21 amFraming - knowing when one packet ends and the next begins - can be a problem if you don't have an "out of band" value to use as the frame delimiter. The link I posted earlier describes ways of encoding that leaves room for out of band framing.

author: Ben321
author_rank: Frequent Contributor
timestamp: January 03, 2020, 06:01:27 am
content: Quote from: NiHaoMike on November 12, 2019, 01:29:58 pmOne thing that is guarantied with a serial port (whether native serial on a mother board, or serial-over-USB) is that every byte sent gets received, unless the physical cable gets disconnected. As for making sure the entire set of bytes in your data set is recognized properly is to prepend your raw data with a 4 byte header. That header is an unsigned 32bit integer that tells the number of bytes following the header will be your data packet. Then encapsulate everything within those simple packets, everything from control signals to data transfer. Basically when using serial, you need to create your own protocol, but fortunately that is simple. You just need to write the software at each end of the connection and program it to use your protocol.

author: Psi
author_rank: Super Contributor
timestamp: January 03, 2020, 07:07:36 am
content: Quote from: imo on November 08, 2019, 04:57:58 pmTechnically a ATTiny85 can transfer data over Ethernet

author: mikerj
author_rank: Super Contributor
timestamp: January 03, 2020, 12:48:24 pm
content: Quote from: NiHaoMike on November 12, 2019, 01:29:58 pmFixed length frames makes framing very easy, though can introduce other problems.

author: ejeffrey
author_rank: Super Contributor
timestamp: January 03, 2020, 06:08:46 pm
content: Quote from: Ben321 on January 03, 2020, 06:01:27 amThat assumption is a good way to create an unreliable product. Unless you have an out-of-band way to reset the device into a known state (such as by togging GPIO on the USB->serial adapter like the arduino) your microcontroller doesn't actually know when the "cable is connected." Also, even after connection, if the device sends data before the host application opens the port the OS will drop the data. Its really best to use explicit frame synchronization always. Then if your host and device ever get out of sync they can resynchronize after a single message. There are plenty of simple ways to do this depending on what data you need to transmit and what overhead is acceptable.

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 04, 2020, 07:12:08 am
content: i've done that binary data transfer in packets (64 bytes per packet for most efficient/speed transfer iirc, nearly 8KB of total data per capture) but sadly the code is in asm and you need to work closely with FTDI API on PC client side to do handshaking before any data is to be transferred reliablyhttp://soasystem.com/eng/amla/index.htm... provided your arduino board is using FTDI chip, not the CH341 (not sure and never tried on that china serial IC). if you transfer in HEX format, you'll have double (or more depending on what HEX format you use) the size to transfer compared to raw binary. you better just get on with standard serial communication library provided by the arduino if you only need/want to send ASCII (8 bits per byte compared to 16 bits per byte/data/character in HEX format). fwiw...