Maximize Arduino Serial Output
Solution 1:
You are right that the bottleneck is the serial port (in the sense that you transmit data inefficiently). However, most of your other assumptions are wrong.
What does baud rate stand for
Baud rate is the number of distinct symbol changes per second. Here it would be equivalent for bits per second. The baud rate would include all bits transmitted, not only data (start, stop, parity).
How 10-bit value is transmitted
As you have 8n1 transmission, you cannot send exactly 10 bits of data. It must be a multiple of 8, so 8, 16, 24, etc. 10 bits will be sent in two 8 bit parts, just like they are stored in Arduino memory as an int.
How does println() work
println()
converts numbers to string. You can specify base for this conversion (DEC, BIN, HEX, OCT) - default is DEC, so 1023 will be transmitted as 4 bytes + \n
which is a single byte (ASCII 10) and \r
(ASCII 13) which is also a single byte. Total of 6 bytes. 1 will require 3 bytes - 1 for data and 2 for newline and carriage return.
How to make it faster
- Without changing almost anything - use
println(val, HEX)
- you will need 5 bytes maximum for numbers greater than 255. - Using
Serial.write()
- this function puts raw binary data into serial, so if you want to send a value 10, it will send just one byte of value 10. However, when you want to send 10-bit variable, things get complicated - you need 2 bytes for that, and PC needs to know which is the first part and which is second to stitch it back together. You will need to come up with some form of simple transmission protocol to handle this, so probably some start/stop character. - Do you need 10-bits resolution? If you could get on with 8-bits you could transmit just plain data, without any additional characters. Also, as suggested in the comments, you could use faster ADC clock then.
- Also suggested by KIIV - you could use ADC conversion interrupt that would trigger instantly when the measurement is completed.
If you still go with 10-bit, I would suggest 3-byte frame: 1 start byte, and 2 bytes of data. This way you could theoretically reach ~18 000 frames per second, which is above the maximum analogRead()
frequency.
Solution 2:
One option is to sample x times then average those before sending back to the PC. Or take on the challenge of compressing on the 328p! You might be fine with some RLE if your pressure readings are mostly consistent.
Don't use 10 bits. The ADC may have 10 bits of resolution but the accuracy is +/- 2 LSB and is very dependent on a stable vref. So drop the 2 LSB and use the top 8 bits for fast Arduino-to-PC readings or multi-sample and average out the noise.
It's a noisy, shared, low resolution ADC. So don't leave the other analog pins floating as that contributes to the noise (just ground them). Also run the Arduino in a grounded enclosure if possible since the ADC has a MUX causing it to have longer traces. Switching ADC modes or pins also generates a bit of noise (not that you're doing that here). Battery power is great if you can keep a stable input voltage otherwise AC/DC noise might cause unexpected vref fluctuations.
The main loop on the Arduino runs both serial IO between the 328p and the 8u as well as your analog reads. So timing bottlenecks are a bit more complex.
The UNO serial-to-USB conversion is being handled by the ATMEGA8u2 (another chip on the board) which can communicate at 115.2kbaud (not 450000).
There are buffers on the 8u and the 328p just to add to the complexity. And the two chips run on different clocks. The ATMEGA8u2 runs from the crystal and the ATMEGA328p (primary processor) runs from the resonator (both 16MHz).
Depending on your needs, the Teensy has a slightly more accurate ADC (an external one will give better reads) and a faster CPU, more RAM, etc. and it can max the USB port speed. Plus Paul has great info on some low level techniques to make things very fast.
Post a Comment for "Maximize Arduino Serial Output"