Skip to content

Commit 379677d

Browse files
Merge pull request #239 from arduino/benjamindannegard/opta-rs485-tutorial
[PC-970] benjamindannegard/opta-rs485-tutorial
2 parents 5361e88 + 740b942 commit 379677d

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
---
2+
title: 'Getting Started with Modbus RTU and RS485 on Arduino Opta'
3+
description: "Learn how to make use of the Arduino Opta's Modbus RTU RS485 protocol"
4+
difficulty: beginner
5+
tags:
6+
- Getting started
7+
- Modbus
8+
- RS-485
9+
author: 'Benjamin Dannegård'
10+
libraries:
11+
- name: ArduinoRS485
12+
url: https://www.arduino.cc/reference/en/libraries/arduinors485/
13+
software:
14+
- ide-v1
15+
- ide-v2
16+
hardware:
17+
- hardware/05.pro-solutions/solutions-and-kits/opta
18+
---
19+
20+
## Overview
21+
22+
The Arduino Opta® is equipped with the industry standard RS485 interface. Making use of this feature is made easy with the help of the Arduino IDE and the [ArduinoRS485 library](https://www.arduino.cc/reference/en/libraries/arduinors485/). This tutorial will go through the steps to get the Modbus RTU protocol up and running using two Optas connected via RS485 and the Arduino IDE. Going through some important functions in the library and show an example sketch that makes use of the library and RS485 interface.
23+
24+
## Goals
25+
26+
- Learn how to connect two Optas for RS485 communication
27+
- Run a Modbus-RS485 sender sketch on Opta
28+
- Run a Modbus-RS485 receiver sketch on Opta
29+
- Learn how to send values using the Modbus RTU protocol and the RS485 interface
30+
31+
### Required Hardware and Software
32+
33+
- USB-C® cable (either USB-C to USB-A or USB-C to USB-C)
34+
- [Arduino Opta](https://store.arduino.cc/pages/opta)
35+
- Power supply of 12-24V DC, 1A
36+
- [Arduino IDE](https://www.arduino.cc/en/software)
37+
38+
## Instructions
39+
40+
### Setting up With Arduino IDE
41+
42+
First make sure the latest version of the Arduino IDE is installed. Download the IDE from [here](https://www.arduino.cc/en/software) if you need help setting up the Opta with the Arduino IDE, please have a look at our [Getting started with Opta tutorial](/tutorials/opta/getting-started).
43+
44+
To make it easier to use the RS485 protocol with Opta let's make use of a library. The library is called [ArduinoRS485 library](https://www.arduino.cc/reference/en/libraries/arduinors485/), which can be found in the Arduino IDE library manager. Once installed let's take a look at a simple sketch to use for testing out the Modbus RTU protocol.
45+
46+
If you want a more in-depth article that explains the entirety of what a Modbus and RS485 protocol is, take a look at our [Modbus article](https://docs.arduino.cc/learn/communication/modbus).
47+
48+
### Using Modbus RTU on Opta
49+
50+
The sender sketch will run a RS485 connection between your two devices and it will let you send a message over the serial monitor to the receiving device. The receiving device, which will be a Opta in this example, will then take the message, open or close the corresponding relay and turn on or off a LED. If you send the number 0 through the serial monitor, the receiving Opta will open or close relay 1 depending on it's current status, while turning on or off a status LED.
51+
52+
Here are some important functions in the sketch:
53+
54+
- `RS485.begin(9600)`: Initializes the RS485 object communication speed with assigned baudrate, `9600` in this case.
55+
- `RS485.beginTransmission()`: Enables RS485 transmission.
56+
- `RS485.print()`: Writes binary data over RS485. This data is sent as a byte or series of bytes.
57+
- `RS485.endTransmission()`: Disables RS-485 transmission.
58+
59+
Connect the sender and receiver Opta according to the image shown below.
60+
61+
![Connecting RS485 on Opta](assets/opta-modbus-connection.png)
62+
63+
***Modbus RTU communication is supported using Opta's RS485 physical interface. Note that Opta does not have internal terminator resistors so they need to be added if necessary following the Modbus protocol specification.***
64+
65+
### Modbus RTU RS485 Sender Sketch
66+
67+
Let's start by uploading the sender sketch to the device you want to designate as the sender device.
68+
69+
```arduino
70+
#include <ArduinoRS485.h>
71+
72+
int incomingByte = 0; // for incoming serial data
73+
74+
void setup()
75+
{
76+
Serial.begin(115200); // opens serial port
77+
RS485.begin(9600);
78+
}
79+
80+
void loop() {
81+
82+
if (Serial.available() > 0)
83+
{
84+
incomingByte = Serial.read();
85+
RS485.beginTransmission();
86+
RS485.print(incomingByte);
87+
RS485.endTransmission();
88+
delay(1000);
89+
}
90+
}
91+
```
92+
93+
94+
### Modbus RTU RS485 Receiver Sketch
95+
96+
Some important functions in the receiver sketch:
97+
98+
- `RS485.begin(9600)`: Make sure this is set to the same baudrate as the sender device, `9600` in this case.
99+
- `RS485.receive()`: Enables reception through the RS485 connection.
100+
- `RS485.parseInt()`: We use this function to make sure that the correct value is received and read.
101+
102+
Now upload this sketch to the receiver device.
103+
104+
```arduino
105+
#include <ArduinoRS485.h>
106+
int readValue = 0;
107+
bool newState = false;
108+
109+
int relays[] = {D0, D1, D2, D3};
110+
int leds[] = {LED_D0, LED_D1, LED_D2, LED_D3};
111+
112+
void setup()
113+
{
114+
for (int i = 0; i < 4; i++){
115+
pinMode(relays[i], OUTPUT);
116+
pinMode(leds[i], OUTPUT);
117+
}
118+
119+
RS485.begin(9600);
120+
RS485.receive();
121+
122+
Serial.begin(9600);
123+
while (!Serial);
124+
}
125+
126+
void loop(){
127+
while (RS485.available() > 0){
128+
readValue = RS485.parseInt();
129+
RS485.parseInt();
130+
newState = true;
131+
}
132+
133+
if (newState){
134+
changeRelay();
135+
newState = false;
136+
}
137+
}
138+
139+
void changeRelay(){
140+
if (digitalRead(relays[readValue]) == 1){
141+
digitalWrite(relays[readValue], LOW);
142+
digitalWrite(leds[readValue], LOW);
143+
}else{
144+
digitalWrite(relays[readValue], HIGH);
145+
digitalWrite(leds[readValue], HIGH);
146+
}
147+
}
148+
```
149+
150+
### Testing Out the Sketches
151+
152+
Now that both sketches has been uploaded to the devices. Plug in the USB to the sender device, make sure the receiver device is powered and then open the serial monitor in the Arduino IDE. With the serial monitor open send a value between 0-3 in the serial monitor to the sender device. Sending a 0 should open the first relay and turn on the first status LED, from left to right. Sending a 0 again will close the relay and turn off the LED.
153+
154+
The values the device can receive and the result:
155+
156+
- Sending the value 0: Will turn on or off the first relay and status LED.
157+
- Sending the value 1: Will turn on or off the second relay and status LED.
158+
- Sending the value 2: Will turn on or off the third relay and status LED.
159+
- Sending the value 3: Will turn on or off the fourth relay and status LED.
160+
161+
## Conclusion
162+
163+
In this tutorial we went through how to establish a Modbus RTU connection between two Optas. And then how to write sketches using the `ArduinoRS485.h` library to send and receive values between these two devices. Finally the tutorial showed how to take these values sent with RS485 to interact with the Opta.
164+
165+
### Next Steps
166+
167+
Now that you are familiar with the Modbus RTU communication protocol on the Opta, have a look at our [Getting started with Opta tutorial]() to get a better overview of other features on the device.
168+
169+
If you wish to incorporate connectivity in your Opta solutions, have a look at the [Connectivity on Opta tutorial]().
170+
171+
If you are interested in seeing the RS485 protocol being put to work in a real life scenario, have a look at our [Tank levels application note for the Opta]().

0 commit comments

Comments
 (0)