This is an old revision of the document!


Attaching 1-Wire devices

In this tutorial, we are going to demonstrate, how to connect 1-Wire temperature sensor and create very simple temperature regulator.

  • UniPi controller running Mervis OS
  • Basic project in Mervis IDE in Full mode with
    • attached controller
    • created ModbusTCP channel with the UniPi controller device
    • defined Executable project with one program
    • setted Autogen on the Unipi controller device
    • defined task running a program from Executable project
    • successfully executed Build

In this tutorial, we will use Unipi Neuron L503.

The 1-Wire temperature sensor available in our eshop is equipped with RJ45 connector, which can be directly connected to UniPi Neuron units.

FIXME obrazek pripojeni cidla k Neuronu

FIXME pripojovani k Axonu

Let's assume your Mervis IDE workspace looks like this:

Right click on the PLC in the Solution tab of the Left panel. Click on the Add channel option.

New channel called channel will appear under the PLC. It is hightlighted, so in the Properties panel, you can see its properties. First, change the name of the channel to something more descriptive, in this example 1wire.

Second parameter we need to change is the protocol. The 1-Wire protocol is “hidden” in the PlatformIO option of the Protocol dropdown menu.

And select the OneWire link protocol from the corresponding dropdown menu.

1-Wire bus is a serial bus, which can interconnect many sensors. Each sensor has its unique HW address, through which the master of the bus addresses the slave and reads data from. Therefor we need to tell UniPi controller to look for a certain devices on the bus. Luckily, we can detect all the sensors on the bus and then select the ones we are interested in.

To detect the sensors, right click on the 1wire channel name under the PLC in the Solution tab of the Left panel. In the context menu, click on the Detect One Wire Sensors.

To run the detection, the controller has to be in the Commissioning mode. The Mervis IDE will warn you and will set the mode for the detection and after detection change the mode of the controller back. Just confirm the warning by clicking on Next >.

If you follow this tutorial precisely, the Mervis IDE will show an error that the Selected channel does not exist in the PLC.

When you define new channel, the Mervis IDE will not be able to work with it, until you Deploy the solution to the PLC

Deploy the solution and run the detection again. The dialog Detect OneWire Sensors will appear and should contain a sensor you just connected. If you have more sensors, you should see all of them. To import the sensor into the Solution, tick the Import checkbox and click on Next >

The importing of the sensor will proceed and finish with restoring the PLC's mode.

Since we added new device, we need to Set autogen to have the temperature variables available in the program.

The Mervis IDE has a feature called Debugging, which allows to connect to the running UniPi controller, read its state and even alter the variables. To start the debugging, click on the Start Debugging button on the Ribbon.

To view the variables and their state in debug mode, click on the Variable Browser tab on the Main window. You will be presented with all the variables available in the controller. You can use searching by Name. Our temperature sensor has been autonamed “1W-Thermometer” (check the device in the Left panel under the 1wire channel), so enter this name into the Name searchbox. The variable containing the actual temperature is called 1W-Thermometer_Temperature and its current value in PLC is 29.875°C.

We have connected the temperature sensor to our controller and verified, that we can read current temperature. Now we can use it in very simple temperature regulator.

We will be monitoring temperature and if the temperature will be under 30°C, we will turn the RO2.1 ON and when the temperature will be greater or equal to 30°C, we will turn the RO2.1 OFF. The RO2.1 will represent a heating element.

We could “hardwire” the temperature “30°C” into the program, but we would be able to change it other than reprogramming the PLC. Most likely, we would like to set the threshold (or desired) temperature by some knob, buttons or even HMI device. So it is better to define it as another variable and set the threshold by changing the variable's value. Later we can wire this variable to some other HW input.

Our program therefor will need two inputs: measured_temperature and threshold_temperature. In programming, each variable has to have some type. It can be integer, real number, boolean, character, string of characters or even more complicated type. Temperature by its nature is real number and so will be our variables measure_temperature and threshold_temperature.

We also need one output: heating_on, which will be boolean, because the heating will be only ON or OFF represented by the boolean values TRUE and FALSE. The naming heating_on will help us to identify, that it contains a state of the heating. And we can easilly say, that heating_on = true, then the heating is ON.

Let's start creating our program. Programming has to be done when the Debugging is stopped, so stop it by clicking on the Stop Debugging on the Ribbon, if you have it still running. Then double click on the name of the program in the Left panel to display it in the Main window.

If you remember, in the Variable browser, the name of temperature variable was 1W-Thermometer_Temperature. The name is autogenerated, and the first part is from the name of the sensor. That's not very descriptive, so we will change the name of the sensor to office, because it is measuring temperature in our office. Click on the name of the sensor in the Left panel. In the Properties panel, you can see property Name. Change it's value to office and either click on the V or submit by ENTER key.

That will create variable named office_Temperature. You can check it in the Variable Browser. Now we can place the temperature onto the program canvas to Input area. Right click on the Input area and click on Global VariablesInsert Existing Variable. The Insert Existing Variable dialog will appear and you can search for the office_Temperature variable and confirm the adding by clicking on OK

Now we need to add second input, the threshold_Temperature. This variable doesn't exist yet, so we cannot add it from existing variables as we did with office_Temperature. Right click on the Input area where you want to place the input and click on Global VariablesCreate New Variable. In the Create New Variable dialog, enter the name threshold_Temperature, set the type to real and set the default value to 30. And click on OK.

The last thing with setting inputs and outputs is to define the output, the RO2.1. Right click on the Output area and click on Global variablesInsert Existing Variable. In the list of variables look for RO and select the Neuron_L503_RO_2.01_w. If you don't have unit with relay outputs, you can use DO instead, the logic is the same.

And if everything went well, you should have similar program canvas

Now we can fill the Logic area with some “high level” logic. We need to compare the office_Temperature against threshold_Temperature. We will need to place some Function block to achieve this. Switch the Left panel into FUPLA Box Explorer. You can see a list of categories, which have subcategories, which have subcategories,… Since we don't know, how the desired FB is named, we can only guess where it can be. Since we want to compare two analogue values, it could be in the Basic Analogue Boxes, so open the tree in FUPLA Box Explorer. We want to compare two variables, so it can be in Mathematics category - open it. And you can see Compare Functions at the bottom of the category.