In present and future society, all individuals must be able to face the problems, risks, advantages and opportunities that will arrive with new paradigms in the labour market, social relations and technology. To reach this goal, a quality and inclusive education together with a proper and complete formation in technology (communications, robotics, programming, computational thinking (CT), etc.) must be imparted at all educational levels. Moreover, all individuals should have the same opportunities to develop their skills and knowledge, as stated in Goal 4 of the Sustainable Development Goals, Sustainable Education. Following this trend, in the present work, a practical experience about how to teach CT using robotics is developed, showing the results and evaluation of the lessons on robotics taught to students in their 4th year of compulsory secondary education, and where the students showed a high degree of apathy and demotivation. The teaching unit was based on an action research approach that includes a careful selection of pedagogical techniques and instruments to attract and keep the attention and interest of the students. In addition to the robotics lessons, a previous computational thinking training with Blockly Games was carried out, which contributed to noticeably increase the students motivation and to introduce them to the programming of robots. Moreover, gamification was used to motivate and evaluate the individual knowledge, and the students were required to present the work performed through a final project. The individual needs of the students were fulfilled with a daily monitoring. The results show that the pedagogical techniques, instruments and evaluation were adequate to increase the motivation of the students and to obtain a significant learning, showing how the teaching of CT may attract students that have lost interest and motivation, while providing them with abilities that will be essential for the learning throughout life.