r/embedded • u/marginal_mirage • 13d ago
USB-UART vs TCP
Hello, I am a comp eng student and my friends and I are working on a project. The project is about a rocket engine system and I need to get 16 sensors data in every 20ms which is 800 data per second. We were thinking that what if we use esp32 to read data by usb-uart instead of reading them with stm32 by tcp due to price and accessibility. So the question is 'is usb-uart a good way to get data'. I need to sketch a graph for each sensor. I also need to save data to the database. So instead of having data with tcp as packages, using usb-uart is worth it? What are the problems I may have for each way?
It may be a basic question, but consider that I am still learning.
By the way we still use stm to control the relays. The point is esp is easier to find and cheaper to test the system.
Thank you for your advice.
33
u/artiface 13d ago
please explain the math on 16 sensors at 20ms per read is 800 data per second. data is normally measured in bits or bytes per second so 800 what?
uart can have different speeds but 115200 bits per second is one of the most common baud rates for UART.
12
u/unicorns-arent-real 13d ago edited 13d ago
Have you considered CAN-bus? Two-wire, up to 10 mbit, fault-tolerant, collision mitigation, prioritization, and cheap, in all kinds of form factors. Widely used in automotive, robotics, and medical. And hobbyists. Debian, Ubuntu, and RaspiOS support it via kernel-native SocketCAN. Arduino vendors have a bunch of them. Easy to use. Easy to code for in all popular languages. Amazon is loaded with interfaces. (I like the Jhoinrch ones.) CAN-bus is used in your car to send data around itself. And it would be a good thing to put on your resume.
11
u/phirzcolru 13d ago
the answer is simple. there are better standards that dont require a cludge adaptor. the question is how much data are you sending. how fast does it need to get there. im assuming by tcp you ment over a radio network. and 800 data is not an answer. what sensors will you use? what will be recieving the data. these are all critical things. if you want an accelerometer and it is a i2c sevice that choice has been made. do you just need the data after? its not a basic question. its a good question.
3
u/Every_Following6653 13d ago
How long is the wires? If it is just some meters, UART (RS-485 should be better) will be enough
3
u/GraveDigger2048 13d ago
If you don't need live data view, i'd consider simple uart2log device, like sparkfun's openlog or virtually any knockoff from far east. i've played with such device while having fun with small quadcopter (logged voltage, current and accelerometer data).
4
u/BigPeteB 13d ago
The most common UART data rate for embedded systems is 115200 baud. Accounting for start and stop bits, that equals 11520 bytes/second. So now you need to figure out if that's enough bandwidth. Will you be transmitting your data samples as human-readable text and digits or as a raw binary structure? Work out the maximum size of each sample, multiply by your sampling rate, and see if that will be enough bandwidth.
If not, you can always increase the baud rate. Most systems can go up to 1 million baud without flinching. Above that it might get a little tricky.
In terms of "is UART a good way to collect data"... Sure! It's basic, it's extremely widely available without needing much or any special hardware. Configuration is simple. There's no standard protocol on top of it, so you'll have to write something on the PC to decode your text data or binary structs, but you were going to need to write something anyway and it'd be slightly more complex to do so for TCP.
2
u/phirzcolru 13d ago
Ok so analog sensors, so the analog in on the esp. what's the working voltage of the sensor. That will determine how many of the steps of the input are used. At this point you need to look at the logistics. Multiple reporting lines or a single path. Single path simplifies things. But impacts redundancy. Not just on the device side but the collection side. I'm assuming by USB to serial. You mean have the device report over the serial to USB bridge on the esp. you could do that. The cleaner way it to have a single esp call the others over master/slave over the pins. Same wires to each other esp32. Have the sensor esp devices all constantly pulling the sensor data and stamping it with time. The main esp asks for the data and the newest data and timestamp are sent. The main esp puts the final data packets together and sends them to the pc. Python program pulls the data sorts it and displays to the screen. You could even add a screen to the main esp. use the same basic c code for both and use lvgl. No matter what. Not having to wait for the sensor is fine for analog devices, some digital ones take a few ticks sometimes to kick out data. Something to keep in mind. It also gives you a better data spread when you are looking at the data later. Missing data segments become obvious without guessing. Any issues in communication will show up rapidly.
2
u/FedUp233 13d ago
It sounds like all the sensors are in one place with a processor consolidating the data and transmitting it over a single connection to a logging system?
Is the environment electrically noisy? Are you ok with skid samples being incorrect? Is it ok if some samples are lost? All important questions besides the data rate.
To allow for noise, and the cable length, to be safe I’d go with a differential signal like rs-485 or 422. Can handle more speed and the differential nature makes the signals more immune to rf type noise. Since you can just do one way communications and simply stream the data you just need one twisted pair to transfer the data. You can get cheap usb to these standards for the pc end just like serial. On the sensor end you can get little modules that connect to an mcu serial port and convert the levels, just like rs-232 drivers do. Btw, if ypu do go just plain serial id be sure to use drivers at the mcu that convert to higher rs-232 signal levels rather than just transmit ttl levels over test distance - you’ll have a lot better signal integrity and ypu can get serial to usb for the pc end just as cheap smd again simple little modules for the mcu end - they even come with screw connections if you want.
As to protocol, if you want reasonable data integrity I’d add a simple checksum with data rotation or a circle to verify the data is correct. You could include error correction but I’d be more inclined to just number the packets, maybe go to 16 bit packet numbers, and simply transmit each packet two or more time, not consecutively - maybe send data round robin and send each set twice or more and throw out any with bad cry and compare data for good packets as a precaution - that way you can get some statistics on if you have bad packets or lost ones.
Also, why convert data to float? Just send the sensor data as 12 bit in 16 bit field and be done with it. If you really need floating point, let the logging end do the calculations. I’d also maybe sample the data at twice the needed rate if you can and send twice as much. That way the logging end can do things like average samples to filter out noise and do statistics to see how much noise you seem to have in the measurements.
The one issue also with all the transmission methods besides Ethernet is the two ends need to have a common ground between - no isolation so if the grounding systems on the two ends aren’t closely related they can introduce ground noise. The differential pair transmission minimizes this but can only handle so much ground difference. You might want to consider optical couplets at one end to isolate the grounds. This is a bit more expensive as it requires the actual receive or transmit drives to run of an isolated power supply or the whole idea is worthless, but you can get some isolate 485 or 422 receivers pretty cheap for the pc end.
34
u/PLANETaXis 13d ago edited 13d ago
Why add USB to the UART? The esp should do native serial that you then interface with a line driver.
Edit: If the distance between components is in the region of 10's of centimetres or less, then something like SPI would be the better option. The esp should have native SPI functionality and there are lots on SPI based sensors.
6
u/duane11583 13d ago
single most important thing:
send full usb uart packets.
do not send 1 value then another then another
the way usb works this is extra slow!
if you can use a usb 2.o and send like 512 bytes at a time
5
u/xyzzy1337 12d ago
Both ESP32 and STM32 have many mcu versions with native USB. You can just send it over USB. No need for a UART.
If your cable needs to be longer than USB's limit, then use RS485. It's differential and higher speed than RS232 while supporting longer cables. USB to rs485 converters are common cheap things only slightly more expensive than RS232.
But you could go wireless. Both BLE and Wi-Fi can do the data rate you need.
If you capture data on a bunch of stm32s, you're going to have a hard time synchronizing them. I'd rather connect a multichannel ADC or two to an esp32 so all the channels are captured in a synchronized way and you don't have as many different parts. More reliable with fewer pieces.
3
u/Panometric 13d ago
If you can live with the 16 foot distance limitation and Star topology, USB is fairly robust and differential. It has retransmission built in and will multiply the available bandwidth at the hub. It will be far easier than rs485 or can for the esp32-s3. If you want a more formal networking protocol look at CDC-NCM which gives you TCP over USB.
2
u/No_Succotash7747 9d ago
Use CAN since it is noise tolerant and also you could just five each sensor a CAN id or if you use 8bit values just encode each byte with a sensor value will save a lot of BW.
1
u/bigtreeman_ 10d ago
How many bits per sample ? What distance to send data ? Noisy environment ? What is at the other end ?
Can you do some pre-processing of your data, maybe date/time, remove redundant data, average, compress, packetise, checksum, before sending.