1

You initialize the serial communication via USB with

Serial.begin(baudrate);

where baudrate is a long specifying the number of symbols you want to send over the connection per second. You can put pretty much any number in there, but not every number makes sense.

One thing that I found odd though is that I can't seem to actually use a different baudrate than 9600. The sketch compiles and uploads just fine, but if I check the actual speed of the serial port via

setserial -ag port

where port is the port of my Teensy 4.0 board (that command is something you can use in the terminal of Linux) or with

Serial.baud();

directly in the sketch it always says 9600. I suspect that this is caused by something inside the code behind Serial.begin() since I can manually set the baudrate via the terminal in Linux using

stty speed baudrate < port > port

where (again) baudrate is the speed I want to use and port is the port of my board (checking it with the first terminal command: it works). But when restarting the program on the board the baudrate reverts to 9600.

Does anyone know why this is and how to fix this?

3

The whole concept of Baud Rate with USB communication is completely meaningless. There is no such thing as "baud rate" over USB.

What there is, and what you are confusing with "baud rate" is a configuration item which the host can send to the device which is a "I would like you to communicate with other devices at this speed" configuration item.

This is intended for use with USB to UART adaptors where it is used to configure the baud rate of a physical UART interface for downstream communication.

When you set the baud rate in your code it is ignored, since it has no meaning.

When you set the baud rate on your computer it sends it as this configuration value, which you can then retrieve, should you have an interest in that value, using the Serial.baud() function.

  • Of course there is a baudrate over USB. There is one for every communication. Do you mean to say that the Arduino IDE cannot change it? – LukasFun Nov 21 at 16:38
  • @LukasFun No, there is no baud rate. There is a data rate, but that is largely unrelated to the actual rate of communication between the host and any one device, since it is a packet-based protocol. – Majenko Nov 21 at 16:39
  • So... how do you control the data rate of the USB connection then, I guess? And why would that rate not matter? – LukasFun Nov 21 at 16:41
  • You don't control the data rate. The data rate is defined by the class of the interface and device. USB 1 ("Full Speed") is 12Mbps. USB 2 ("High Speed") is 480Mbps. USB 3 ("Super Speed") is 5Gbps. The chosen speed is the highest that both the device (Arduino) and host (computer) can work together at. – Majenko Nov 21 at 16:43
  • That data rate is divided amongst all devices on the bus. – Majenko Nov 21 at 16:44

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.