Arduino Uno with RS485 TTL module

Hi, i am working on my final year project where i have to do data acquisition of a panel which consists of buttons and knobs via RS485 protocol. There status has to be updated on a GUI in real time.
What i want to do is connect the buttons with the sender arduino and the receiver arduino with the PC. The arduinos are connected via 2x RS485 modules.
Now i need to write code for the sender and the receiver arduino. The requirement doc is attached. Any help will be appreciated.


And what is your query or doubt?

Post the code you have so far for the sender and receiver. If you are using an UNO, then you will need to use one of the software serial libraries to create the serial port for the RS485 comms.

on RS-485 4-wire interface

That sounds like the design is running the RS-485 interface in full duplex mode rather than the half duplex mode we norrmally see here.

For the Sender Arduino

int Enable_pin = 8;

const int BUTTON_PIN_2 = D2;
const int BUTTON_PIN_3 = D3;
const int BUTTON_PIN_4 = D4;

int BUTTON2_VALUE=0
int BUTTON3_VALUE=0
int BUTTON4_VALUE=0

void setup()
{
  Serial.begin(115200);
  pinMode(Enable_pin, OUTPUT);
  pinMode(BUTTON_PIN_2, INPUT);
  pinMode(BUTTON_PIN_3, INPUT);
  pinMode(BUTTON_PIN_4, INPUT);
  delay(10);
  digitalWrite(Enable_pin, HIGH);
}

void loop()
{
  // Read button states
  int BUTTON2_VALUE = digitalRead(BUTTON_PIN_2);
  int BUTTON3_VALUE = digitalRead(BUTTON_PIN_3);
  int BUTTON4_VALUE = digitalRead(BUTTON_PIN_4);

  Serial.println(BUTTON2_VALUE);
  Serial.println(BUTTON3_VALUE);
  Serial.println(BUTTON4_VALUE);

  delay(100);
}

For the receiver Arduino

int Enable_pin = 8;

void setup() {
  Serial.begin(115200);
  pinMode(Enable_pin, OUTPUT);
  digitalWrite(Enable_pin, LOW);
  delay(100);
}

void loop() 
{
  if (Serial.available()) 
  {
    int BUTTON2_VALUE = Serial.read();
    int BUTTON3_VALUE = Serial.read();
    int BUTTON4_VALUE = Serial.read();

    // Check for button state changes
    if (buttonState2 != buttonState2Prev) {
      if (buttonState2 == LOW) {
        Serial.println("Button 2 pressed");
      } else {
        Serial.println("Button 2 released");
      }
      buttonState2Prev = buttonState2;
    }

    if (buttonState3 != buttonState3Prev) {
      if (buttonState3 == LOW) {
        Serial.println("Button 3 pressed");
      } else {
        Serial.println("Button 3 released");
      }
      buttonState3Prev = buttonState3;
    }

        if (buttonState4 != buttonState4Prev) {
      if (buttonState4 == LOW) {
        Serial.println("Button 4 pressed");
      } else {
        Serial.println("Button 4 released");
      }
      buttonState4Prev = buttonState4;
    }
  }
  delay(100);
}

basically i don't know what i am doing. I have taken snippets of codes from here and there. Basically i am just trying to start somewhere. trying to read atleast something over the rs485 module

basically i am duplicating a DAQ application for a simulator. They have the serial cards for the commuication but i can only implement it using the RS485 TTL module so even if i achieve half duplex then no issues

Ok. So the first thing you need to be aware of is that the hardware serial port on the UNO (pins 0 & 1) are connected to the on-board serial-USB interface which is how your UNO prints to the IDE serial monitor.

As you need to develop the code, it will be a huge help to you to be able to use this hardware serial port for debug printouts so you can figure out what your code is doing and why it is doing it. This applies to both the sender and receiver UNOs.

Start with a software serial port (see the standard libraries for examples) on each UNO on say pins 2 & 3. Software serial ports can't go very fast so you are limited to a baud rate of around 19200 for your software serial port on an UNO, but I would recommend you start with 9600, or even 4800 baud.

You can simply connect UNO #1 pin 2 to UNO #2 pin 3, UNO #1 pin 3 to UNO #2 pin 2 and UNO #1 GND to UNO #2 GND.

You can then see if you can write a byte from 1 UNO to the other. Remember - start small and simple.

Maybe even read a character in from the IDE serial monitor and write it out so that the other UNO prints out the received character on its IDE serial monitor.

Have a read of the Serial Input Basics tutorial and make sure that you know how example 3 works.

What is the complete message format for the RCP? Does it have unique start and end characters in its response message like the RTIO message?

Get your RTIO UNO code working so that every few seconds it transmits the first 6 byte message.
Then get your RCP UNO code to detect that message (see serial input basics).
Then get your RCP UNO to respond to that message with a simple blank message of 20 bytes.
Then get your RTIO UNO to read in those 20 bytes and print them to the serial monitor.

Repeat the above for the second 6 byte message and the 8 byte response.

All this testing is done without using any RS-485 hardware.

Once you have it working, you can introduce the RS-485 hardware. The brief says it's a 4 wire interface so you will have 2 RS-485 modules on each UNO, one module in permanent TX and the other in permanent RX.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.