company. See
microcontroller can utilise many ways of the communication with PCs or other microcontrollers.
The Modbus protocol
For communication of the PROMOTIC application with the
Arduino, the
Modbus protocol seems to be the best choice.
Both serial link (
MODBUS RTU) or Ethernet (
MODBUS TCP) can be used as communication interface (based on the configuration of the microcontroller itself).
Procedure for Arduino:
- Launch the software.
- Set the communication port for communication with PC (in the menu "Tools / Port").
- Add the downloaded library (in the menu "Project / Include Library / Add .ZIP Library").
- Based on the example in the library create the Arduino project.
Procedure for PROMOTIC:
For this communication in the PROMOTIC system can be used:
Communication by user defined protocol
For communication of the PROMOTIC application with Arduinoit is also possible to create user defined protocol. It is usually a very simple protocol e.g. of the following type:
- The PROMOTIC application sends text "ai2" (requests value of analog input 2)
- Arduino answers with text "24.3"
Such protocol can be programmed into Arduino (will be
Slave) and also into the PROMOTIC application (will be
Master).
Procedure for PROMOTIC:
For this communication in the PROMOTIC system can be used:
Programming the Arduino
The
Arduino can be programmed in language
C and
C++. Although deeper knowledge of these languages is not required.
One of the main sections of each program is the
void setup() {} function. Write the code between braces to be executed
only once at program start. It means that either after the power supply is connected, after pressing the
"restart" button or the code is uploaded to the
Arduino.
The second important function for running the program is the function
void loop() {}. Write the code between brackets to be
executed repeatedly in a loop until the power supply is disconnected.
Digital inputs and outputs:
The Arduino can be functionally extended. In order to do that it contains inputs and outputs called "pins". These pins can be used to connect additional circuits, chips, relays, memories etc. There are simple functions available in the Arduino to be used with these pins.
- Function pinMode(p1, p2) can be called in order to define whether the pin would be considered as input or output. The p1 parameter is the pin number, the p2 parameter specifies whether it is either input or output.
- The digitalWrite(p1, p2) function serves to control the output. The p1 parameter is the pin number, the p2 parameter specifies whether the current flows into the pin (HIGH = current on, LOW = current off).
- The value = digitalRead(p1) function serves for reading whether the current flows into the input or not. The p1 parameter is the pin number. The function returns the value: HIGH = current on, LOW = current off.
Analog inputs and outputs:
Programming the analog input and output values is more complex than the digital values. The name "Analog" is not precise in this case because these are in fact not analog values. If it is necessary to use real analog value in the range for example 0-5V, then using an external D/A converter would be necessary. This is caused by the fact that
PWM signal is being generated on selected pins - this is considered to be a kind of analog signal "imitation". In reality the values between 0 and 5V are alternated very quickly.
- The value = analogWrite() function serves for setting analog value. This function can be used on selected pins marked as PWM.
- The analogRead() function is used for reading analog values on input pins marked A.
Elementary Arduino program for serial link and Modbus protocol
#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>
modbusDevice regBank; // Setup the brewtrollers register bank. All of the data accumulated will be stored here
modbusSlave slave; // Create the modbus slave protocol handler
void setup()
{
pinMode(2, INPUT); // Declare DI2
pinMode(13, OUTPUT); // Declare LED as output
regBank.setId(1); // Assign the MODBUS device ID
regBank.add(10002); // Add Digital Input register
regBank.add(13); // Add Digital Output register
regBank.add(30001); // Add Analog Input register
slave._device = ®Bank; // Assign the modbus device object to the protocol handler
slave.setBaud(9600); // Initialize the serial port for coms at 9600 baud
}
void loop()
{
byte DI2 = digitalRead(2); // Digital Input
if (DI2 >= 1) regBank.set(10002,1);
if (DI2 <= 0) regBank.set(10002,0);
int DO13 = regBank.get(13); // Digital output
if (DO13 <= 0 && digitalRead(13) == HIGH) digitalWrite(13,LOW);
if (DO13 >= 1 && digitalRead(13) == LOW) digitalWrite(13,HIGH);
float valueAI0 = analogRead(0); // Analog Input
regBank.set(30001, (word)valueAI0);
slave.run();
}