1

I have the following code for a TTL to RS-485 Adapter Module

[...code]
digitalWrite(DE_PIN, HIGH); //directional control for adapter 
Serial2.write(Message, Size);
Serial2.flush();
digitalWrite(DE_PIN, LOW);  // Set To Recieve
Serial2_reading_foo();
[code...]

I've noticed that DE_PIN does not seem to go High fast enough before Serial2.write() executes I confirmed this by adding a small delay(ms) which made the code function perfectly.

digitalWrite(DE_PIN, HIGH);
delay(100);
Serial2.write(Message, Size);
Serial2.flush();
digitalWrite(DE_PIN, LOW);  // Set To Recieve
delay(100);
Serial2_reading_foo();

Unfortunately, there's pretty much universal agreement that you shouldn't ever use delay() in programming. Plus doing any kind of wait function here seems to be insensitive to the real problem. Is there a better way to make sure that DE_PIN==HIGH before the Serial2.write() Function begins?

  • Short delays are fine. Consider using delayMicroseconds() and tuning it for the minimum delay that achieves what you want. – Majenko Aug 21 '17 at 13:01
  • what is the actual time interval you need? 0.1 s is pretty long for circuits. – ratchet freak Aug 21 '17 at 13:14
  • I don't know the true interval. Is there any good way to get it mathematically, or is it just guess and check? – ATE-ENGE Aug 21 '17 at 13:16
  • check the spec sheet of the module – ratchet freak Aug 21 '17 at 13:21
  • 1
    How can you not find the spec sheet? It's the first hit in google for MAX485 - or you go to Maxim's website, look for the MAX485, and click on the datasheet. Anyway, the maximum time you should need is a mere 50ns. Do you have the DE and RE pins tied together? – Majenko Aug 21 '17 at 15:43
1

The MAX485 requires 2V on the DE pin to register a HIGH. It takes no more than an absolute maximum of 50ns for the DE pin to switch the buffers. The Arduino takes 10.4ns for an IO pin to rise to 2V.

That's an absolute maximum of 60.4ns.

One clock cycle of a 16MHz Arduino is 63ns.

That is longer than it takes for the IO pin to tell the MAX485 to switch the buffers.

You can increase that rise time by taking in to account the input capacitance of the MAX485 and the resistance of the connection between the Arduino and the DE pin, but it will still be microscopically small compared to the time between the pin going HIGH and the serial transmission starting.

So there must be something fundamentally wrong with your setup. Two possibilities spring to mind:

  1. You aren't controlling RE, only DE, so RE is floating and is being capacitively linked to DE through the parasitic capacitances of the board and circuitry, making it rise and fall very slowly.
  2. You haven't set the IO pin on the Arduino to OUTPUT, so it switches between floating and HIGH through a very high resistance, causing slow rise to HIGH and maybe never getting to LOW at all.

Or a combination of the two.

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.