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