|
| 1 | +--- |
| 2 | +title: 'Getting Started With the Opta' |
| 3 | +description: 'Get started with the Opta and get to know some of its features.' |
| 4 | +difficulty: beginner |
| 5 | +tags: |
| 6 | + - Getting started |
| 7 | + - Relays |
| 8 | + - Analog Input |
| 9 | +author: 'Benjamin Dannegård' |
| 10 | +software: |
| 11 | + - ide-v1 |
| 12 | + - ide-v2 |
| 13 | +hardware: |
| 14 | + - hardware/05.pro-solutions/solutions-and-kits/opta |
| 15 | +--- |
| 16 | + |
| 17 | +## Overview |
| 18 | + |
| 19 | +Opta is a robust micro PLC solution with many engaging features. In this tutorial we will go through the setup of Opta with the Arduino IDE and explain how to use its basic features, showing through examples how to program the LEDs on the device, how to use the programmable button, as well as controlling its inputs and outputs. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +## Goals |
| 24 | + |
| 25 | +- Putting Opta to work with the Arduino IDE |
| 26 | +- Blinking the LEDs on the device |
| 27 | +- Programming the button on the device |
| 28 | +- Testing the inputs and outputs on the device |
| 29 | +- Connecting the device to the Arduino Cloud |
| 30 | + |
| 31 | +### Required Hardware and Software |
| 32 | + |
| 33 | +- USB-C® cable |
| 34 | +- [Arduino Opta](https://store.arduino.cc/pages/opta) |
| 35 | +- [Arduino IDE](https://www.arduino.cc/en/software) |
| 36 | +- Power supply of 12-24V DC, 1A (optional if not running the section related to the relays) |
| 37 | +- Analog inputs (optional, alternatively the section related to analog inputs will work but reading random values) |
| 38 | + |
| 39 | +## Instructions |
| 40 | + |
| 41 | +### Setup With the Arduino IDE |
| 42 | + |
| 43 | +Make sure the latest version of the Arduino IDE is installed. The IDE can be downloaded [here](https://www.arduino.cc/en/software). |
| 44 | +Within the Arduino IDE install the core for the Opta. Go to **Tools > Board > Boards Manager**, in the boards manager section search for **Opta mbed** and install it. |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +Now you are ready to upload sketches to the Opta via the Arduino IDE. |
| 49 | + |
| 50 | +### Trying a Blink Sketch |
| 51 | + |
| 52 | +Once the IDE and the core are installed, let's warm up by uploading a first sketch to your Opta. We will be using a modified version of the classical Arduino blink sketch to put your device to work and test if everything is set properly. |
| 53 | +Let's create a simple blink sketch that will blink the four STATUS LEDs on the Opta, highlighted in the image below. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +All the STATUS LEDs on the device are defined in the core of the PLC. |
| 58 | +Hereafter you can see the correspondence between each of them as identified in the core and their labeling on the front panel of the product: |
| 59 | + |
| 60 | +- `LED_D0`: STATUS 1 |
| 61 | +- `LED_D1`: STATUS 2 |
| 62 | +- `LED_D2`: STATUS 3 |
| 63 | +- `LED_D3`: STATUS 4 |
| 64 | +- `LED_RESET`: LED above the reset button |
| 65 | +- `LED_USER`: LED above the user button (only available on Opta WiFi (AFX00002)) |
| 66 | + |
| 67 | +Select the correct **board** and **port** in the **Tools** section. |
| 68 | +Copy the sketch below into the Arduino IDE sketch editor, then upload it to Opta. |
| 69 | +When the sketch is uploaded you will see the Opta's STATUS LEDs blinking in sequence. |
| 70 | + |
| 71 | +```arduino |
| 72 | +void setup() { |
| 73 | + pinMode(LED_D0, OUTPUT); |
| 74 | + pinMode(LED_D1, OUTPUT); |
| 75 | + pinMode(LED_D2, OUTPUT); |
| 76 | + pinMode(LED_D3, OUTPUT); |
| 77 | +} |
| 78 | +
|
| 79 | +void loop() { |
| 80 | + digitalWrite(LED_D0, HIGH); |
| 81 | + delay(100); |
| 82 | + digitalWrite(LED_D0, LOW); |
| 83 | + delay(100); |
| 84 | + |
| 85 | + digitalWrite(LED_D1, HIGH); |
| 86 | + delay(100); |
| 87 | + digitalWrite(LED_D1, LOW); |
| 88 | + delay(100); |
| 89 | + |
| 90 | + digitalWrite(LED_D2, HIGH); |
| 91 | + delay(100); |
| 92 | + digitalWrite(LED_D2, LOW); |
| 93 | + delay(100); |
| 94 | + |
| 95 | + digitalWrite(LED_D3, HIGH); |
| 96 | + delay(100); |
| 97 | + digitalWrite(LED_D3, LOW); |
| 98 | + delay(500); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Configuring the Programmable Button on the Opta |
| 103 | + |
| 104 | +Opta has a programmable button, shown on the image below and identified as USER. It can be programmed using the Arduino IDE to fit your needs. To show how much simple is to use it, let's create a sketch and program the button as a trigger to modify the status of the STATUS LEDs. |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +The button is defined in the core as `BTN_USER`: 'HIGH' as default (not pressed), and 'LOW' when pressed. The new sketch will turn on one by one the LEDs when the button is pressed, and then start over when all the lights have been turned on. Below you can find the entire sketch, where a simple [Switch (case) Statement](https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/) is used, and an image highlighting where the USER button is located on the device. |
| 109 | + |
| 110 | +```arduino |
| 111 | +int buttonState = 0; |
| 112 | +int counter = 0; |
| 113 | +
|
| 114 | +void setup() { |
| 115 | + // Initialize OPTA LEDs |
| 116 | + pinMode(LED_D0, OUTPUT); |
| 117 | + pinMode(LED_D1, OUTPUT); |
| 118 | + pinMode(LED_D2, OUTPUT); |
| 119 | + pinMode(LED_D3, OUTPUT); |
| 120 | + pinMode(BTN_USER, INPUT); |
| 121 | +} |
| 122 | +
|
| 123 | +// The loop function runs over and over again while the device is on |
| 124 | +void loop() { |
| 125 | + buttonState = digitalRead(BTN_USER); |
| 126 | + if(buttonState == LOW){ |
| 127 | + if(counter < 4){ |
| 128 | + counter++; |
| 129 | + } |
| 130 | + else{ |
| 131 | + counter = 0; |
| 132 | + } |
| 133 | + delay(100); |
| 134 | + } |
| 135 | + changeLights(); |
| 136 | +} |
| 137 | +
|
| 138 | +void changeLights() { |
| 139 | + switch(counter){ |
| 140 | + case 0: |
| 141 | + digitalWrite(LED_D0, LOW); |
| 142 | + digitalWrite(LED_D1, LOW); |
| 143 | + digitalWrite(LED_D2, LOW); |
| 144 | + digitalWrite(LED_D3, LOW); |
| 145 | + break; |
| 146 | + case 1: |
| 147 | + digitalWrite(LED_D0, HIGH); |
| 148 | + break; |
| 149 | + case 2: |
| 150 | + digitalWrite(LED_D1, HIGH); |
| 151 | + break; |
| 152 | + case 3: |
| 153 | + digitalWrite(LED_D2, HIGH); |
| 154 | + break; |
| 155 | + case 4: |
| 156 | + digitalWrite(LED_D3, HIGH); |
| 157 | + break; |
| 158 | + } |
| 159 | + delay(100); |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +Once the sketch is uploaded, you can see that an additional LED is turned on each time you press the button, following the sequence: |
| 164 | + |
| 165 | +| Interaction | Result | |
| 166 | +| ------------ | ---------------------- | |
| 167 | +| First press | LED 1 ON. | |
| 168 | +| Second press | LEDs 1 and 2 ON. | |
| 169 | +| Third press | LEDS 1, 2 and 3 ON. | |
| 170 | +| Fourth press | LEDS 1, 2, 3 and 4 ON. | |
| 171 | +| Fifth press | All leds off and back. | |
| 172 | + |
| 173 | + |
| 174 | +### Using Out Relays |
| 175 | + |
| 176 | +Opta has 4 outputs, consisting of 4 electromechanical relays NO (SPST) with a capacity of 10A at 250V AC (considering a resistive load). They are identified as OUTPUTS and located on the bottom of Opta as shown in the image below. |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +The outputs correspond to pins D0 to D3 as follows: |
| 181 | + |
| 182 | +| Output | Pin | |
| 183 | +| -------- | --- | |
| 184 | +| OUTPUT 1 | D0 | |
| 185 | +| OUTPUT 2 | D1 | |
| 186 | +| OUTPUT 3 | D3 | |
| 187 | +| OUTPUT 4 | D4 | |
| 188 | + |
| 189 | +The OPTA output contacts are "clean" contacts, which means they are contacts that are not alive in a "non-connection" situation. This type of contact can be used in any system and with any type of voltage. To properly function, the outputs must therefore be connected by bringing for example a power cable to one of the terminals and connecting the load to the exit of the other terminal. |
| 190 | + |
| 191 | +This way, when the contact is closed by the logic set in the programming, the power supply signal will cross the contact carring the signal up to the reference load. |
| 192 | + |
| 193 | +The “clean” contact also allows carrying a different power system or type of load for each output contact, being possible to control multiple devices or signals that use different voltage levels. |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +Let's run a simple sketch to test the output relays on Opta: in this sketch all the 4 relays are closing and reopening their contacts and after each relay's cycle a led, will be turned on to provide a visual feedback. |
| 198 | +To activate the relays and run this sketch you need to provide Opta with a voltage from 12 to 24 V DC by connecting it a proper power supply. |
| 199 | + |
| 200 | +Opta has dedicated terminals for power supply located in the upper part of Opta and next to the inputs. They are duplicated to help the user to connect the power supply and any common part to the input terminals but they have the same potential (upon polarity). |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | +***These terminals are polarized, it is therefore mandatory to strictly respect the power supply polarity by connecting the positive connector of the power supply to "+" and the negative to "-".*** |
| 205 | + |
| 206 | +The entire sketch can be found below, copy it into your IDE and upload it to your device. |
| 207 | + |
| 208 | +```arduino |
| 209 | +void setup() { |
| 210 | + // Initialize Relays outputs |
| 211 | + pinMode(D0, OUTPUT); |
| 212 | + pinMode(D1, OUTPUT); |
| 213 | + pinMode(D2, OUTPUT); |
| 214 | + pinMode(D3, OUTPUT); |
| 215 | + |
| 216 | + // Initialize OPTA LEDs |
| 217 | + pinMode(LED_D0, OUTPUT); |
| 218 | + pinMode(LED_D1, OUTPUT); |
| 219 | + pinMode(LED_D2, OUTPUT); |
| 220 | + pinMode(LED_D3, OUTPUT); |
| 221 | +} |
| 222 | +
|
| 223 | +void loop() { |
| 224 | + // Closes and opens the contact of relay 1 and turns on led 1 |
| 225 | + digitalWrite(D0, HIGH); // Sets the relay 1 on |
| 226 | + digitalWrite(LED_D0, HIGH); |
| 227 | + delay(1000); |
| 228 | + digitalWrite(D0, LOW); // Sets the relay 1 off |
| 229 | + digitalWrite(LED_D0, LOW); |
| 230 | + delay(1000); |
| 231 | + |
| 232 | + // Closes and opens the contact of relay 2 and turns on led 2 |
| 233 | + digitalWrite(D1, HIGH); // Sets the relay 2 on |
| 234 | + digitalWrite(LED_D1, HIGH); |
| 235 | + delay(1000); |
| 236 | + digitalWrite(D1, LOW); // Sets the relay 2 off |
| 237 | + digitalWrite(LED_D1, LOW); |
| 238 | + delay(1000); |
| 239 | + |
| 240 | + // Closes and opens the contact of relay 3 and turns on led 3 |
| 241 | + digitalWrite(D2, HIGH); // Sets the relay 3 on |
| 242 | + digitalWrite(LED_D2, HIGH); |
| 243 | + delay(1000); |
| 244 | + digitalWrite(D2, LOW); // Sets the relay 3 off |
| 245 | + digitalWrite(LED_D2, LOW); |
| 246 | + delay(1000); |
| 247 | +
|
| 248 | + // Closes and opens the contact of relay 4 and turns on led 4 |
| 249 | + digitalWrite(D3, HIGH); // Sets the relay 4 on |
| 250 | + digitalWrite(LED_D3, HIGH); |
| 251 | + delay(1000); |
| 252 | + digitalWrite(D3, LOW); // Sets the relay 4 off |
| 253 | + digitalWrite(LED_D3, LOW); |
| 254 | + delay(1000); |
| 255 | +} |
| 256 | +``` |
| 257 | + |
| 258 | +***Important: It is not possible to program the Opta while it is being powered with the power pins. You would need to disconnect the power supply, upload the program and then connect the power again.*** |
| 259 | + |
| 260 | +### Using Opta's Inputs |
| 261 | + |
| 262 | +Opta has 8 input pins that can be programmed to be used as analog or digital. The mapping between the marking on the Opta physical terminals (I1 to I8) and their definition in the core can be found below: |
| 263 | + |
| 264 | +| Physical terminal | Definition in core | |
| 265 | +| ----------------- | ------------------ | |
| 266 | +| I1 | A0 | |
| 267 | +| I2 | A1 | |
| 268 | +| I3 | A2 | |
| 269 | +| I4 | A3 | |
| 270 | +| I5 | A4 | |
| 271 | +| I6 | A5 | |
| 272 | +| I7 | A6 | |
| 273 | +| I8 | A7 | |
| 274 | + |
| 275 | +The 8 inputs pins can be used as digital (having the logical values of LOW or HIGH) or as analog inputs (within a range from 0 to 10V). |
| 276 | +* To use them as digital inputs, add the Arduino command *pinMode(pinName, INPUT);* inside the setup(). |
| 277 | +* To use them as analog inputs, add the command *analogReadResolution();* with the bit resolution that you want to use. |
| 278 | + |
| 279 | + |
| 280 | + |
| 281 | +Now let's try a sketch that will read the analog inputs on the Opta. The inputs can operate in a range between 0 and 10V. |
| 282 | +The maximum voltage managed by the microcontroller is 3V. This maximum voltage is important to calculate the voltage of the input using it in conjunction with the resolution factor of the ADCs. That resolution can be selected inside the program within a range between 12bit (4095) and 16bit (65535). |
| 283 | +To get and display the proper voltage value read by the input, we need to convert the value read by the analogRead function and apply a rescaling factor of 0.3 which is determined by the internal voltage divider. |
| 284 | +The sketch will read the inputs on the analog pins A0, A1 and A2 and then print the result in the serial monitor. |
| 285 | + |
| 286 | +```arduino |
| 287 | +void setup() { |
| 288 | + Serial.begin(9600); |
| 289 | + // 65535 is the max value with 16 bits resolution set by analogReadResolution(16) |
| 290 | + // 4095 is the max value with 12 bits resolution set by analogReadResolution(12) |
| 291 | + analogReadResolution(12); |
| 292 | +} |
| 293 | +
|
| 294 | +void loop() { |
| 295 | + // Read the input on analog input I1 corresponding to A0: |
| 296 | + int sensorValueA0 = analogRead(A0); |
| 297 | + float voltageA0 = sensorValueA0 * (3.0 / 4095.0)/ 0.3; |
| 298 | + |
| 299 | + // Print out the value you read from I1 to the max value for the analog inputs resolution: |
| 300 | + Serial.print("I1 value: "); |
| 301 | + Serial.print(sensorValueA0); |
| 302 | + Serial.print(" corresponding to "); |
| 303 | + Serial.print(voltageA0, 5); // Print the voltage as a float with 5 decimal digits |
| 304 | + Serial.println("Volts"); |
| 305 | + |
| 306 | + // Read the input on analog input I2 corresponding to A1: |
| 307 | + int sensorValueA1 = analogRead(A1); |
| 308 | + float voltageA1 = sensorValueA1 * (3.0 / 4095.0)/0.3; |
| 309 | +
|
| 310 | + Serial.print("I2 value: "); |
| 311 | + Serial.print(sensorValueA1); |
| 312 | + Serial.print(" corresponding to "); |
| 313 | + Serial.print(voltageA1, 5); // Print the voltage as a float with 5 decimal digits |
| 314 | + Serial.println("Volts"); |
| 315 | + |
| 316 | + // Read the input on analog input I3 corresponding to A2: |
| 317 | + int sensorValueA2 = analogRead(A2); |
| 318 | + float voltageA2 = sensorValueA2 * (3.0 / 4095.0)/0.3; |
| 319 | +
|
| 320 | + Serial.print("I3 value: "); |
| 321 | + Serial.print(sensorValueA2); |
| 322 | + Serial.print(" corresponding to "); |
| 323 | + Serial.print(voltageA2, 5); // Print the voltage as a float with 5 decimal digits |
| 324 | + Serial.println("Volts"); |
| 325 | +
|
| 326 | + delay(1000); |
| 327 | +} |
| 328 | +``` |
| 329 | + |
| 330 | +Once you have uploaded the code, open the serial monitor to see the values read in each analog input. Ig you have connected a device with an analog voltage value in I1, I2, and/or I3 you will see the voltage or analog value of each of the signals. In case you did not connect anything to the analog inputs, you will see how the values oscillate between 0V and a very small value because the pins are floating. |
| 331 | + |
| 332 | +You may notice from the output values that when the maximum value of 10V is reached, the corresponding numerical value is not 4095 as the maximum value with 12 bits resolution should be. The reason is that there is a precautional margin taken on the maximum voltage level applicable to the inputs to preserve the integrity of the microcontroller. |
| 333 | + |
| 334 | +### Connecting Opta to the Cloud |
| 335 | + |
| 336 | +It is possible to use the Opta with the Arduino Cloud. To set up the Opta to the cloud go to the [Arduino Cloud](https://cloud.arduino.cc/). For help with how to get started with the cloud, go to our [Getting started with the cloud](https://docs.arduino.cc/arduino-cloud/getting-started/iot-cloud-getting-started) tutorial. We also have a number of other helpful tutorials for [the Arduino cloud](https://docs.arduino.cc/arduino-cloud/). |
| 337 | + |
| 338 | +## Conclusion |
| 339 | + |
| 340 | +This tutorial went through the basics of the Opta device. Now you know how to program the LEDs of the PLC, use the user-programmable button to create additional modes and features, and program the relays and digital and analog inputs. With the additional connection of the Opta to the Arduino Cloud, Opta can be programmed online, create HMI interfaces accessible on any device, and even be updated through an OTA using professional encryption security. |
| 341 | + |
| 342 | +### Next Steps |
| 343 | + |
| 344 | +Now that you know the basics of the Opta it could be a good idea to combine these features with other features on the Opta. For example, if you want to add connectivity to your solution, take a look at the [Getting started with connectivity on the Opta tutorial](/tutorials/opta/getting-started-connectivity). |
0 commit comments