ATtiny13 vs PLC, or how to get 14 I / O from an 8-foot controller


    Before proceeding, I would like to warn a zealous reader.

    1. What I describe cannot be done for many reasons, these reasons will be joyfully indicated to you in the comments, and I in no way urge you to do so. And in no case do I claim that the device described below can replace a real PLC. Everything described was done only in order to prove to myself that this is technically possible and does not apply to real equipment.
    2. If you feel sick only from mentioning the word "Arduino", you better not read. I performed all the actions with the controller in the Arduino IDE, it’s easier for me. But nothing prevents to do the same without using it.

    For a long time I wanted to write an article about my experiments with ATtiny13, but I was primarily frightened by the attitude to the arduinoists on the site. Moreover, I have been working with controllers for only a few years (thanks again to Arduino, oh, where have you been before!). Comments on the article Three eyes hang on the post ... showed that the topic is still interesting to a sufficient number of users. By the way, the title of the article was supposed to be “PLC on ATtiny13”, but in the same comments, they politely explained to me on the table that the described device could not be called “PLC”. Well there is no way.

    What do we need from the PLC?

    It began with the fact that I wanted to make a functional analogue of the PLCs that are used in my enterprise, taking into account how they are used. Most of the control circuits we have relay with all the ensuing shortcomings. PLCs of various types are also used in places, starting from the 90s. Basically, they perform the logical task of the control circuit, starting from the state of limit switches and proximity sensors.

    No complex interfaces, no analog signals. On Off. All. For the most part, even the load for the PLC outputs is provided by the same relays that they were so eager to get rid of. (The funny thing is that almost everywhere PLC relay outputs are used, which switch external relays, which switch electromagnets of hydraulic distributors ... Sometimes one more link from the starter is added.) So, what do I need from the PLC:

    1. Discrete inputs for a signal level of 24 volts.
    2. Discrete outputs with a voltage of 24 volts.
    3. Testing the internal algorithm according to a given scheme, which can be changed if necessary (mainly during debugging).
    4. Speed ​​of about 50 ms, no longer needed to replace relay circuits.

    And that’s it. No interfaces. No interruptions. No screens. Ah, yes, the screens ... It will not hurt to display the status of the inputs and outputs; here, LEDs haven’t come up with more reliable LEDs yet. Well, do not forget that everything is done out of sports interest, respectively, the budget falls on the shoulders of the developer. By the way, a very good incentive.

    If you do not take into account the signal level of 24 volts, then absolutely any microcontroller with the number of I / O (inputs / outputs) no less than the required one can easily cope with the task.

    We deal with the levels

    Initially, I wanted to use optocouplers at the inputs. Anyone who is more or less versed in electronics, especially industrial, knows that there are not many optocouplers. The comments will tell you in detail why it is impossible without optocouplers. But I weighed the pros and cons, and decided to refuse. So some may not read further, and immediately proceed to write a comment.
    So, we have an input signal of 24 volts, or 0 volts in the absence of a signal. This will allow the use of a non-contact inductive position sensor or a conventional contact limit switch. From 24 volts you need to get the voltage of the logical unit safe for the controller. The good old voltage divider copes with this as well as possible.


    The resistors are calculated taking into account that for any deviations of the input voltage by plus or minus 20% of the nominal voltage at the input of the microcontroller, there should be more than the switching voltage (usually this is half the voltage of the controller), but not more than the controller voltage, we consider it equal to 5 V. Suitable for any controller in the AVR family. In addition, the estimated current through the divider is about 7 mA, which reduces the likelihood of interference, and at the same time does not heavily load the sensor output and the power source. A 4.7 V zener diode serves to protect against surge surges at the same interference. There is still a bounce of contacts, we can fight with it programmatically or not at all, it's just a mockup. Now I would still have to indicate the status of the input. And here you can apply lifehack, killing two birds with one stone:


    The LED on the circuit performs two functions - it works like a zener diode, limiting the voltage to 2.8 ... 2.9 volts (only for blue and white LEDs!), Well, it also shines. I must warn you that in some cases the voltage on the LED is not enough to switch the controller input! When using several controller inputs, we thus simplify installation and save several zener diodes. By smoothly increasing the input voltage, you can notice that the moment of ignition of the LED and the moment of switching the input do not coincide a bit. But this does not bother us very much, since there should not be intermediate levels at the input, only 0 V or 24 V. Naturally, by this moment you should have at least the minimum test firmware for one input and one output in the controller.

    Now the output signal from the controller also needs to be adapted for 24 V, and taking into account that the common wire, which is also a minus, is common to all output relays. Here you need at least 2 transistors and 2 resistors:


    In fact, one more resistor would be needed, and a protective diode parallel to the relay, but this is not about that. At first I used this circuit. But at one point, I drew attention to the optocouplers, which I refused at the beginning.
    The one who bothers us will help us

    We look at the parameters of the optocouplers by datasheets: the cheapest and most affordable PC817C by the output allows you to switch U o: 35 V: I o: 50 mA. Those relays that are connected to the PLC at our place (at the enterprise) take up to 45 mA. Without a stock, but quite suitable. Most small-sized relays at 24 V pull at all up to 15 ... 20 mA. In the end, you can buy a more expensive optocoupler, if you really need it, there both current up to 80 mA, and voltage up to 60 V. We are suitable for the layout and 817. So that you do not learn from your mistakes, I warn you that for maximum output current through the optocoupler LED, you need to pass more than 7 mA.

    Well, again, recall the indication. To display the status of the output, we add a LED in series with the optocoupler's LED, we still have more than 3 volts falling idle there. And it's time to draw a protective diode before you burned the optocoupler. By the way, I could not find a method for choosing a protective diode, depending on the parameters of the relay. On the one hand, it must be pulsed, on the other, high-voltage. In addition, it can be placed parallel to the transistor, which is better? Practice shows that a sufficiently low-power diode IN4148 in this case, but I would like a theoretical justification.


    A 220 ohm resistor is indicated with the calculation again on a blue or white LED, if you put red, the current in the circuit from 3.5 mA will increase to 9 mA, this must be taken into account.

    A little bit about programming

    Well, there are so many letters, but I still haven’t got to the bottom of the title, nor to ATtiny13. To program my controller, I used the FLProg project , a visual programming environment that brings the controller programming as close as possible to PLC programming in LAD and FBD. The circuit created in this environment, which is also a project, is compiled into an Arduino sketch, which I already upload from the Arduino environment to ATtiny13 or another controller of the AVR family. For example, such a simple circuit


    compiles into code like this:

    bool _k1 = 0;
    void setup()
    pinMode(1, INPUT);
    pinMode(2, INPUT);
    pinMode(3, OUTPUT);
    void loop()
    _k1 = (((((!(digitalRead (1)))) &&((digitalRead (2))))) || ((((!(digitalRead (1)))) &&(_k1))));
    digitalWrite (3, _k1);

    Try to write the same thing manually - I missed one bracket somewhere, and all in vain. Many thanks to Sergey Glushenko, creator of the FLProg project, his program has significantly influenced my “creativity” over the past three years. Arduinschikov everything should be clear and so, and the rest I do not know how to explain. The only pin in the diagram and in the code is the controller pins in the Arduino environment, their numbering differs from the numbering of the legs. How to fill all this into ATtiny13 is described in more detail here: Updated Attiny13 or Attiny13a programming guide using the Arduino IDEYes, another trifle: FLProg still does not support ATtiny13 (it seems to be added in the near future), so we pretend that we are working with another controller, for example ATmega168. Just FLProg will offer to use pins that ATtiny13 does not have, but you can not take them. And in Arduino IDE you need to set the correct controller type, otherwise nothing will work.

    Well, the most important thing

    What do we have in the end? We have ATtiny13, which has 5 legs available for use, which we can designate as inputs or outputs. If you really want to, you can use 6 legs, but it is fraught, we read here, for example: Few conclusions? We use RESET .

    Well, get 6, still not enough. How to get more?

    And like this:


    You may notice that just the input binding and the output binding are connected to the same pin. Now let's move on to the software part. Moreover, the software part will be even without code, in order to avoid criticism. Just an algorithm.

    I already mentioned that a circuit speed of about 50 ms is enough for me. This means that once every 50 ms it is enough to quickly interrogate the status of the inputs, then designate them as outputs, and apply the required level to these outputs. After that we assign outputs to inputs, and repeat the process.

    During the polling at the input divider, we have 0 volts or 2.8 V, the same voltage is applied to the input of the microcontroller, in any case this is not enough for the current to flow through the output circuit. In order for the current to flow through the output circuit, a voltage of at least 1.1 V (drop on the optocoupler's LED) plus 2.9 V is required, totaling at least 4 volts. Accordingly, there is no current at this moment, the transistor is closed.

    Further, the entrance became the exit. If the output is in a logical zero state, again no current flows through the output circuit, the transistor is closed. If the output is in the unit state, then the voltage on it is close to the power supply, that is, about 5 V, respectively, a current flows through the output chain of the optocoupler-resistor-LED, which opens the optocoupler transistor and turns on the relay. Part of the current at the same time flows through 3 kOhm resistors (which is horizontal in the diagram) and 680 ohms. But falling on the resulting divider is not enough to light the input circuit LED. If at this time the input is high, the input LED is on, and a small current from the controller pin is added to the current of the input circuit.

    What do we have in the end? With a high level of relay output, 50 ms is turned on, then less than 1 ms is turned off, and 50 ms is turned on again. Of course, in 1 millisecond the relay does not have time to turn off, it will disappear only if the output is found most of the time in the zero state. The optocoupler transistor does not really like this situation, the diode is to help it.

    The circuit is tested in work, it functions not only in theory. Here are asked a few more details for reliability, the diagram absolutely necessary minimum.

    But we get only 6 + 6 = 12 inputs / outputs, where to get 2 more?

    scheme again

    Well, do you understand the logic? An analog input is already needed here, we have them) with as many as 4 pieces, however, one is again on the reset leg, let us leave it for a rainy day. At the pin of the microcontroller in the input state polling mode, the voltage is 0 volts if both inputs have a low level and 2.8 volts if both inputs have a high level (we have already passed this above). But if on one of them 24, on the other 0, or vice versa, we get 2 more intermediate values ​​- 0.93 V or 1.87 V. It remains only to compare the obtained value to meet certain limits, and assign the state of the inputs to variables in the program. In the Arduino IDE, all this is done quite slowly and resource-intensively, but even so, I have almost half of ATtiny's memory left, and a satisfactory speed. You can write everything in Assembler, and it will be even cooler.

    So, as you can see, 6 outputs and 10 inputs (!) Can be obtained from ATtiny13 even without register chips (I also had such an experience). Of course, there is little practical benefit from this; debugging such a device is very inconvenient. Although it is possible to debug on a controller with a large number of legs, the ready-made circuit can be "uploaded" to ATtiny. Well, it’s also more economically feasible to do the same on ATmega8, and without dancing with tambourines. But the pleasure is not that :-)

    And if you do not have enough pairs of legs of the microcontroller to change an existing device, you can use one of the proposed options.

    The PLC on the KDPV is not related to this publication, I just did not give photos of the real PLCs used at my enterprise, so as not to accidentally offend the manufacturer. But the amount of I / O on it corresponds to the task. By the way, these numbers (8/6) emerged from the specific task on which I wanted to test my non-PLC so as not to talk about "spherical horses in a vacuum."

    Well, if they don’t bother me after the first publication, I may write about my more practical experience of the “PLCbuilding”, with photos and solitaire . Waiting for feedback.

    PS If you look at the diagrams in the article and think: “What does this remind me of?”


    Also popular now: