0
\$\begingroup\$

Hello Stack Exchange community,

I am currently working on a project involving two Arduino Nano boards connected via RS485 communication using MAX485 chips. I have set up the connections between the Arduinos as per the figure provided. Additionally, I have included the master and slave code for reference.

Connections:

Arduino UNO =>RS485 Module 5V =>VCC; GND =>GND; pin 0 (RX) => RO; pin 8 => RE & DE; pin 1 (TX)=> DI;

Code:

Master Code:

arduino

int Enable_pin = 8;

void setup()
{
  Serial.begin(9600);
  pinMode(Enable_pin, OUTPUT);

  digitalWrite(Enable_pin, HIGH);
}
void loop()
{
  for(uint8_t i =0;i<=99;i++)
  {Serial.println(i);
  delay(1000);}
}

Slave Code:

arduino

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

int Enable_pin = 8;

Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);

void setup()
{
  Serial.begin(9600);
  pinMode(Enable_pin, OUTPUT);
  digitalWrite(Enable_pin, LOW);        //  (LOW to receive value from Master)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(100);
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);

}

void loop()

{
  while (Serial.available())
  {

    display.clearDisplay();
    uint8_t PWM_master = Serial.read();            //Receive INT value from Master throught RS-485
    
 

    display.setCursor(50, 30);
    display.setTextSize(4);
    display.print(PWM_master);

    display.display();
    delay(100);

  }

The expected output of the system is continuous numbers from 0 to 99 displayed on an OLED screen. However, I am encountering an issue where I am receiving garbage values intermittently on the OLED display. Specifically, the garbage values are either '10' or '13'. This is puzzling as my RS485 module wires are short, and the environment is not noisy. Additionally, I am powering the setup using a laptop's USB output.

I am seeking assistance in understanding why these consistent garbage values (10 or 13) are appearing in the output. Could it be related to the code, hardware connections, or power supply? Any insights or suggestions to resolve this issue would be greatly appreciated.

Thank you in advance for your help!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ I just skimmed this. But the hardware level of RS-485 is mostly bullet-proof, from past experience, anyway. So I'm thinking software is the issue and that it is a red-herring to imagine otherwise. You likely can test all this out using a scope (or other observation method) and getting rid of the Nanos, entirely. Just verify the RS-485, itself, directly, end to end. I suspect you will find no problems there. \$\endgroup\$ Commented Oct 1, 2023 at 18:35
  • \$\begingroup\$ You haven't provided details of your OLED display, but a quick google showed this site for what looks to be the unit you are using. In that example display.print() is using a ASCII string, but you seem to be sending raw values from 1..99 to that call. I would expect that you need to translate these numbers to ASCII strings before sending them to the display. \$\endgroup\$
    – Peter M
    Commented Oct 1, 2023 at 18:53
  • \$\begingroup\$ How are you powering the two Arduinos, from same supply, or different supplies? What kind, like two separate ungrounded DC supplies, like two-prong wall warts or USB phone chargers? \$\endgroup\$
    – Justme
    Commented Oct 1, 2023 at 18:56
  • \$\begingroup\$ @Justme RS485 works on differential current flow. It shouldn't matter how the two devices are powered - as long s they are powered within specifications \$\endgroup\$
    – Peter M
    Commented Oct 1, 2023 at 19:06
  • \$\begingroup\$ @PeterM See, that's what people mistakenly think but it is not true. And based on what you say, clearly if it does not work they may be powered out of specification - which is why I asked to confirm if they are or are not. \$\endgroup\$
    – Justme
    Commented Oct 1, 2023 at 19:38

2 Answers 2

1
\$\begingroup\$

You are printing variable as text line, so the 13 and 10 are the Carrige Return and Line Feed character byte values.

So you are sending the data as line of ASCII text string, so for example the value 45 is sent as characters '4', '5', 13, 10.

Which means when you are reading those as bytes you get a stream of bytes 52, 53, 13, 10.

Also the next thing that will go wrong is that you like many others think RS-485 is a two wire interface. Well it is but it needs a common ground between the transceivers. So unless you provide a common ground reference, such as a third ground wire between devices if no other ground reference is available, it will stop working if you use two separate floating mains power supplies.

\$\endgroup\$
0
0
\$\begingroup\$

I changed my master code to following and it fixed the problem.

int Enable_pin = 8;

void setup()
{
  Serial.begin(9600);
  pinMode(Enable_pin, OUTPUT);

  digitalWrite(Enable_pin, HIGH);
}
void loop()
{
  for(uint8_t i =0;i<=99;i++)
  {Serial.print(char(i));
  delay(1000);}
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.