Example Code for Arduino-Automatic Transmission Mode

Last revision 2025/12/10

This guide offers an example code for implementing automatic transmission mode on Arduino using RS485, covering hardware setup, software preparation, and a wiring diagram to toggle an LED via serial commands.

Hardware Preparation

Software Preparation

Wiring Diagram

DFR0259-Wiring Diagram

RS485 Shield Pin name USB to RS422/RS485 Cable Pin name
GND GND
A A
B B

Note: Downloading code, run / program mode switch must hit the OFF position, otherwise it will lead to the download fails. When downloaded,it is complete to ON, and the shield to normal use.

Sample Code

/*
# This sample codes is for testing the RS485 shiled(automatic transmission mode).
# Editor : YouYou
# Date   : 2013.9.16
# Ver    : 0.1
# Product: RS485 shield
# SKU    : DFR0259
*/
int led = 13;
void setup()
{
  Serial.begin(9600);
  pinMode(led,OUTPUT);
}

/**
* @brief main loop function: continuously detect serial instructions and perform corresponding operations.
* @details 1. Check whether there is instruction data transmitted by RS485 link in the serial receiving buffer.
* 2. Read the command character, only respond to the uppercase character' V', flip the LED and return the confirmation command.
*/

void loop()
{
  int temp;
//Detect whether there is available data in the serial port receiving buffer (RS485 received instruction)
  if(Serial.available())
  {
    temp=Serial.read();
	//Determine whether it is a trigger instruction: uppercase character' v' (case-sensitive)
     if(temp=='V'){
	 //Flip LED status: 1-digitalRead(led) can switch from ON to OFF/OFF to ON.
      digitalWrite(led,1-digitalRead(led));
    Serial.println("OK");//The serial port returns the "OK" string (with line breaks) as a confirmation response of successful instruction execution.
     }
  }
}

Result

Send characters "V" via the serial monitor of Arduino IDE, you can see the LED status change, and reply "OK".

Was this article helpful?

TOP