Promotic
WikipediaLinkedInYoutubeTwitterFacebook

Communication with Arduino microcontroller

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

Modbus protocol

In order to establish communicate between the PROMOTIC application and the Arduino microcontroller, 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 "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 "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 Modbus Master protocol.

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

Communication by user defined protocol

 
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 e.g. 0-5V, 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.
© MICROSYS, spol. s r. o.Tavičská 845/21 703 00 Ostrava-Vítkovice