In this article, we will make two Arduino control each other by turning off /on their built in LED using the RS-485 module
You will need the following items to carry out this project
Hardware
Software
- Latest Arduino IDE, get it here
Introducing the RS-485 module
The RS-485 module is a device that allows sending and receiving of data up to 1.2 km at a rate of 20 Mbps. It can support up to 32 devices. It runs off 5 volts meaning your Arduino can supply power to it.
IMPLEMENTATION
STEP 1: CIRCUIT CONNECTION
Below is the circuit diagram of the project.
RS-485 Arduino Connection
STEP 2: UPLOAD THE CODE
1. Start your Arduino IDE and copy the following code into your Arduino IDE
#include <SoftwareSerial.h> /*-----( Declare Constants and Pin Numbers )-----*/ #define SSerialRX 10 //Serial Receive pin #define SSerialTX 11 //Serial Transmit pin #define SSerialTxControl 3 //RS485 Direction control #define RS485Transmit HIGH #define RS485Receive LOW #define Pin13LED 13 /*-----( Declare objects )-----*/ SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX /*-----( Declare Variables )-----*/ int byteReceived; int byteSend; void setup() /****** SETUP: RUNS ONCE ******/ { // Start the built-in serial port, probably to Serial Monitor Serial.begin(9600); Serial.println("RS-485 EXAMPLE"); Serial.println("Use Serial Monitor, type Y/N to turn off/ON LED and press ENTER"); pinMode(Pin13LED, OUTPUT); pinMode(SSerialTxControl, OUTPUT); digitalWrite(Pin13LED, LOW); digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver // Start the software serial port, to another device RS485Serial.begin(9600); // set the data rate }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { if (Serial.available()) { byteReceived = Serial.read(); digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit RS485Serial.write(byteReceived); // Send byte to Remote Arduino delay(10); digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit } if (RS485Serial.available()) //Look for data from other Arduino { byteReceived = RS485Serial.read(); // Read received byte if (byteReceived == 'Y' || byteReceived == 'y') { digitalWrite(Pin13LED, HIGH); } else if (byteReceived == 'N' || byteReceived == 'n') { digitalWrite(Pin13LED, LOW); } Serial.write(byteReceived); // Show on Serial Monitor delay(10); digitalWrite(Pin13LED, LOW); // Show activity } }
2. Click the upload button
(Note: Upload code to both Arduino)
3. Open your serial monitor and type Y or N to turn on/off the built in LED
Feel free to write any comments, thank you.
Wanna do more?
Build a Carbon Monoxide sensor with LCD display. Get the parts
Thanks for sharing this piece of information.
Thanks for the great post on your blog, it really gives me an insight on this topic.~,*”*