1

I have 2 Arduino Nano that will communicate in half duplex mode with transceiver using MAX485. But the master has libraries like Wire.h, uses tone() and also receives UART communication.

I'm a beginner and I'm a little bit confused regarding serial communication. I read when using SoftwareSerial interrupts called by some libraries can mess up SoftwareSerial's timer and it will not interpret incoming bytes correctly.

3 bytes are sent when something changes/max. every 200 ms. So not a lot of data and at 9600 baud. Will it cause any problems when running RS485 on SoftwareSerial + together with DFPlayer? (DFPlayer's TX is disconnected)

Also, when it does become a problem using SoftwareSerial?

New contributor
steve d. is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented 16 hours ago
  • 1
    please add the code that is not functioning correctly Commented 2 hours ago

1 Answer 1

2

TL;DR: The statement that "other libraries (and to extend, any other interrupt-driven software, for example your own) can potentially interfere with SoftwareSerial" is correct.

In your described case you will be fine, if you:

  • give up using tone(), or accept that the sound might be distorted during serial communication.

  • use another (serial) communication method than SoftwareSerial for your own communication, and this must not use timely interrupts.


Analysis of SoftwareSerial

Unfortunately, the documentation does not show enough details, so we need to look into the source code.

The transmission works with delay loops. During the transmission of a byte the interrupts are disabled.

No other interrupt can be serviced during this byte transmission. The duration depends on the baudrate. In your case with 9600 baud, one bit time is 1 / (9600 1/s) = 104µs, giving about 1ms interval without any interrupt service.

When you call listen(), the pin change interrupt is armed. With the falling edge of the start bit, the interrupt handler recv() starts to sample the RX line. The timing is realized with delay loops, again.

Please note that listen() is called during begin()!

During the byte receiving all interrupts are automatically disabled, see chapter 7.7 of the data sheet. Again, the duration depends on the baudrate. In your case this is about 1ms interval without any other interrupt service.

By the way, SoftwareSerial works only half-duplex, as it cannot receive while transmitting. This is perfect for RS485.

SoftwareSerial can listen to only one RX line. Starting to listen (via begin() or listen()) on one interface stops listening on any other interface controlled by SoftwareSerial.

So, we need to look up whether the other software uses interrupts or SoftwareSerial.

Analysis of the tone() function

The Arduino Nano uses a ATmega328 according to its technical details.

The Arduino documentation of tone() tells us nothing about the used timer or interrupts.

But the source code does. Apparently it uses timer 2 on a ATmega328. The tone is generated through the interrupt handler on TIMER2_COMPA. If it is postponed during serial communication, the generated sound might not be what you expect.

Analysis of DFPlayer library

The library "DFPlayerMini" uses SoftwareSerial and according to its documentation uses no interrupts.

Unfortunately the library provides no methods to pause listening to the player. Otherwise you could use that to communicate on another interface controlled by SoftwareSerial.

If you happen to begin() your own SoftwareSerial or start listen()ing after initializing the DFPlayMini library, you will effectively stop it listening of on its SoftwareSerial.

Otherwise, if you initialize the DFPlayMini library after your own communication, your own listening will be stopped. Of course, you could re-enable it, but see above.

Final note

Such research and checking the result is a common and necessary part of software development.

3
  • Re “Analysis of SoftwareSerial”: both transmission and reception are performed with interrupts off. The bit timings are therefore immune to interrupts. Transmission is robust, but reception can be messed up if the start bit arrives while interrupts are globally disabled, e.g. because the CPU is already processing an interrupt. On the other hand, as SoftwareSerial keeps interrupts off for long periods (104 µs is a lot), it imposes huge interrupt latencies to the rest of the system. Commented 11 hours ago
  • @EdgarBonet I need some more time to read the sources and the data sheet concerning interrupt priorities in more depth. Anyway, combining the DFPlayer library with an own instance of SoftwareSerial / using tone() is a not so good idea. Unfortunately, there is urgent work to do. Commented 10 hours ago
  • @EdgarBonet I dove into the source and data sheet, and consequently corrected my answer. Thanks for pointing out my errors! (I'll delete my comments some next day.) Commented 6 hours ago

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.