Promotic
WikipediaLinkedInYoutubeTwitterFacebook

Communication with Arduino microcontroller

Arduino is an open platform based on ATMega microcontrollers by Atmel company. See http://www.arduino.cc.
 
Arduino microcontroller can utilise many ways of communication with PCs or other microcontrollers.
 

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) and/or Ethernet (Modbus TCP) can be used as communication interface (based on the configuration of the microcontroller itself).

 
Arduino procedure:

- Download "arduino-modbus-slave" library from "http://code.google.com/p/arduino-modbus-slave/". This library also contains example that can be used in order to understand the usage.
- Install development software for Arduino from "http://www.arduino.cc".
- Run the software.
- Set the communication port for communication with PC (in menu "Tools / Port").
- Add the downloaded library (in menu "Project / Include Library / Add .ZIP Library").
- Based on the example in the library create the Arduino project.

 
PROMOTIC procedure:

For this communication in the PROMOTIC system can be used: PmModbusMr - Driver for communication by the Modbus Master protocol.

For easy integration of this driver into the application it is handy to use:

Communication by user defined protocol

For communication of the PROMOTIC application with Arduinoit is also possible to create user defined protocol. It is usualy 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 PROMOTIC application (will be Master).

 
PROMOTIC procedure:

For this communication in the PROMOTIC system can be used: PmChar - Driver for communication by user defined ASCII/BIN protocol.

For easy integration of this driver into the application it is handy to use:

Programming the Arduino

The Arduino can be programmed in languages 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. I.e. after the power supply is connected, the restart button is pressed 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 braces 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 defines it to be either input or output.
- The digitalWrite(p1, p2) function is used to control the output. The p1 parameter is the pin number, the p2 parameter defines whether the current flows into the pin (HIGH = current on, LOW = current off).
- The value = digitalRead(p1) function is used to detect whether the current flows into the input or not. The p1 parameter is the pin number. The function returns 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 description "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 is used to set analog values. This function can be used on selected pins marked as PWM.
- The analogRead() function is used to read analog values on input pins marked A.

Elementary Arduino program for serial link and protocol Modbus

#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 = &regBank; //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();
}

Elementary Arduino program for serial link and user defined protocol

float val;
char received;
void setup()
{
  pinMode(2,INPUT); //Declare DI2
  Serial.begin(9600); //Begin serial communication
}
void loop()
{
  while(Serial.available() > 0)
  {
    received = Serial.read();
    if(received == 'a')
    {
      val = analogRead(0);
      val = val*(5.0/1024.0);
      Serial.println(val);
    }
    else if(received == 'd')
    {
      val = digitalRead(2);
      Serial.println(val);
    }
  }
}
PROMOTIC 8.3.22 SCADA system documentation - MICROSYS, spol. s r.o.

Send page remarkContact responsible person
© MICROSYS, spol. s r. o.Tavičská 845/21 703 00 Ostrava-Vítkovice