You went wrong in interpreting the Kelvin-Plank statement of the second law. There is no violation. The statement is (the details of the statement may differ depending on the references)(italics done by me for emphasis):
"It is impossible to devise a cyclically operating heat engine, the effect of which is to absorb energy in the form of heat from a single thermal reservoir and to deliver an equivalent amount of work"
No work is done by the system in the free expansion. In this case no heat is absorbed by the system in the cycle, only rejected by the system to the surroundings as a result of the isothermal compression done by the surroundings needed to return the system to its original state. The system does not deliver work. The surroundings does work on the system. There is no violation.
You can look at this also from the perspective of the surroundings. Since the system is isolated from the surroundings during the free expansion of the system, the surroundings does not partake in that process. When the surroundings does work to compress the gas in the system, it performs work, But this work is not part of a cycle. It is simply a single process, namely, a reversible isothermal process. Net work done during a single process using heat from a single reservoir (as opposed to a sequence of processes comprising a cycle) does not violate the law.
I think there has been a misunderstanding. Summarizing my question is:
"Why free expansion is said to be irreversible although (at least in
my opinion) you can bring back the whole universe to its initial
state?
But the whole universe has not been brought back to its original state.
The system has been brought back to its original state (including original entropy) but the state of the surroundings has now been changed. Heat has been transferred to the surroundings increasing its entropy. Thus there is a positive total entropy change of the universe (system + surroundings) making the entire process irreversible.
The following is in response to your follow up questions:
I'm trying to find another variable of the system or of the enviroment
(except entropy) which has changed at the end of the cycle in order to
show the irreversibility.
If by the term another "variable" you mean another property, the short answer is there is no other property that I am aware of to account for a process being irreversible than the property of entropy. This is the reason the second law and its associated property, entropy, was needed. The first law (conservation of energy) is satisfied by a process even if the process is impossible, as long as energy is conserved. The simplest example is that of natural heat flow.
We know that heat only flows naturally, or spontaneously (without external influence), from a hot body to a cold body. The reverse has never been observed to occur spontaneously, even though the reverse process does not violate the first law of thermodynamics if the heat lost by the cold body equals the heat gained by the hot body.
The example of the free expansion is more subtle. But the idea of spontaneity applies. The gas spontaneously expands from its chamber to the evacuated chamber. We would never see all the gas that expanded into the evacuated chamber spontaneously return to its original chamber. To be more precise, the probability is essentially zero. But there is no violation of the first law.
But the system comes back exactly to its
initial state, I was thinking that if we consider the enviroment made by the cold
reservoir, since it exchanges heat with the system, if we not consider
its thermal capacity infinity then its temperature is increased. Could
be this the variable which generates irreversibility in your opinion?
Good question. But if the temperature of the environment increases, and only heat transfer to the environment is involved, that would mean the internal energy of the environment would also increase. But that would be impossible in this example.
The work done by the environment on the system to return it to its original state exactly equals the heat transferred from the system to the environment (being that compression of the gas is isothermal). That means the change in internal energy of the environment has to be zero. For the entire cycle the change in internal energy of both the system and the environment is zero. This can be so regardless of whether the cycle is reversible or irreversible. The property of entropy is needed to show irreversibility.
Hope this helps.
Best Answer
It does matter if the process is reversible or irreversible because reversible processes give maximum work. It can be verified by a PV diagram of a reversible process. These processes produce maximum work because the value of pressure is infinitesimally greater (in case of compression) than the pressure of the gas in the container.
As we know, $W_{ext}=-P_{ext}\Delta V$, if Pressure was maximum for each infinitesimal change in volume then the work done (corresponding to that change) will also be maximum. This can only happen if the external pressure is infinitesimally greater than the pressure of the gas in the container and compression will take place very slowly.
In other words, the PV diagrams of reversible processes give maximum area under the PV curve and the volume axis.
These processes, ideally speaking, never reach completion and are very slow and obviously, cannot be realized in real life. A reversible process is a ideal process.
Work done in path dependent. This can also be verified by the PV diagram. Take this diagram for example.
There are infinitely many paths that can be taken to move the system from state A to state B and each path will give a different value of work done.
The work done in path A-B is greater than the work done in path A-C-B. (Work done is give by the area under the PV curve and V axis).
The work done by the system in a cyclic transformation is equal to the heat absorbed by the system. Since $\Delta U=0$, if the system work is done by the system $(\Delta V=+ve)$ then the heat has to be absorbed by the system $(q=+ve)$ in order for $\Delta U$ to be $0$. And if work is done on the system then the energy will be released from the system.