RS485 to PC using MAX485 issues

Hello,

I need some help. I need to connect my arduino to PC using rs485. I figured I could use the MAX485 IC from Maxim. However, I am not able to communicate with the arduino through serial monitor. I will be powering the arduino board with an external PS and does not need to use the USB-port. Therefore I choose to connect the RX and TX from the MAX485 directly to Pin 0 and 1 respectively. As far as I have understood, I do not need to use extra libraries since I am going to use Pin 0 and 1 as RX-TX, and therefore I do not have to do any changes to the code I wrote for serial communication with USB?

On the computer side I am using a simple USB-2-rs485 converter.

I have connected my arduino to the MAX485 chip using this exact circuit attached, which I found online:
http://www.extremadura-web.es/Blog/2013/04/19/arduino-uno-modbus-max485/

I know I should terminate the ends, but right now I am working with a 3 meter long cable just for testing…

Is it even feasible to get this working with the MAX485 and a usb-2-rs485 converter?

I am also attaching my code, which works fine when I use it through USB…

PS: This is my first post, so go easy on me :slight_smile:

#include <TimerOne.h>

unsigned char channel_1 = 4;  // Output to Opto Triac pin, channel 1

unsigned char CH1;
unsigned char CHANNEL_SELECT;
unsigned char i=0;
unsigned char clock_tick; // variable for Timer1
unsigned int delay_time = 50;

unsigned char low = 75;
unsigned char high = 5;
unsigned char off = 95;
int ByteReceived;


void setup() {
  
pinMode(channel_1, OUTPUT);// Set AC Load pin as output
attachInterrupt(1, zero_crosss_int, RISING);
Timer1.initialize(100); // set a timer of length 100 microseconds;
Timer1.attachInterrupt( timerIsr ); // attach the service routine here

{
  Serial.begin(9600);  
  Serial.println("--- Start Serial Monitor SEND_RCVE ---");
    Serial.println(" Type in Box above, . ");
  Serial.println("(Decimal)(Hex)(Character)");  
  Serial.println(); 
}

}

void timerIsr()
{
clock_tick++;

if (CH1==clock_tick)
{
digitalWrite(channel_1, HIGH); // triac firing
delayMicroseconds(5); // triac On propogation delay 
digitalWrite(channel_1, LOW); // triac Off
}
}



void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Every zerocrossing interrupt: For 50Hz (1/2 Cycle) => 10ms ; For 60Hz (1/2 Cycle) => 8.33ms
// 10ms=10000us , 8.33ms=8330us

clock_tick=0;
}



void loop() {

if (Serial.available() > 0)
  {
    ByteReceived = Serial.read();
    Serial.print(ByteReceived);   
    Serial.print("        ");      
    Serial.print(ByteReceived, HEX);
    Serial.print("       ");     
    Serial.print(char(ByteReceived));
    
    if(ByteReceived == '0') // Single Quote! This is a character.
    {
      i = 85; 
      CH1 = i;
      Serial.print(" 10 % ");
    }

.... 


    Serial.println();    // End the line

  // END Serial Available
  }

}

You cannot use Serial (Rx/Tx) for both USB and another serial device at the same time. Serial.read() will receive whatever signal arrives at Rx, and Serial.print() will transmit to whatever is connected to Tx. In so far your code doesn’t make much sense to me.

No, I am not using Serial at the same time. USB is not connected when I try to get it to answer with rs485. I uploaded the code using USB, removed the USB and only use rs485 connected through max485 to rx/tx pins.. But I can't seem to communicate.

(deleted)

(deleted)

Terminated both ends at the same time. No difference…

(deleted)

If you try a loopback test on arduino side (disconnect wire for pin 0 and 1 and short it together) does it echo in serial monitor?

Cheers, Ale.

I was not writing pin 2 to low, however when I did, it made no difference. I also tried some other code I found online where the pin was written to low, did not work then either…

I tried what you told me ilguargua, no echo in serial monitor, but is it even possible to do a loopback test on a rs485 in that way? Either it transmits or it recieves right? Not both at the same time.

(deleted)

Yes, I have swapping them.. I also tried swapping out the MAX485 chip (I have 4 available), and checked all connections for connectivity with a multimeter. I've also tried running the system with the MODBUS library, but I could not make that work either. Really frustrating. Especially since I thought it would be almost plug and play.

(deleted)

OK, so I wrote the short sketch, and tested it. Nothing.

However, I decided to check all my connections, and came across something weird. When powered with external PS I only get 3,8V across the 5V output on the Arduino Uno. However, when I use USB it's fine (4,9V). So I measured the external PS incase that was faulty, it was not (5,1V). Could this be my problem? And why is it working fine with USB power but not with an external one? Is something on the board faulty?

The USB port connects the Gnd lines of both ends. If that fixes your problem, check your Gnd lines. Did you connect the second module to the PC Gnd?

Sometimes you have to now when to give up. After diving into checking the transistors for faults I got sick of it, walked over to the local store and bought me a new board. 4 minutes after coming home communication was established... At least I now know I didnt fuck up. Thanks for all the help guys.

HI,
Which board?

Tom... :slight_smile:

(deleted)