books
books books
books
Control Engineering
Control Engineering with Fuzzy Logic • Josef Bernhardt
Control Engineering
with Fuzzy Logic with Fuzzy Logic
Practical Applications and Projects Practical Applications and Projects
with Arduino, ESP32, and RP2040
Immerse yourself in the fascinating world of control engineering with Josef Bernhardt discovered his
with Arduino, ESP32, and RP2040
Arduino and ESP32! This book offers you a practical introduction to classic passion for electronics at a young
age. He took his first steps in
and modern control methods, including PID controllers, fuzzy logic, and
programming in the 1980s with the
sliding-mode controllers. Apple II and the IBM PC. With over
30 years of experience in electronics
In the first part, you will learn the basics of the popular Arduino controllers, development at the University of
such as the Arduino UNO and the ESP32, as well as the integration of Regensburg, Josef Bernhardt is
an expert in his field. There, he
sensors for temperature and pH measurement (NTC, PT100, PT1000, and
worked in both electronics and
pH sensor). You will learn how to use these sensors in various projects software development. Through
and how to visualize data on a Nextion TFT display. his involvement in his own SMD
production, he successfully realizes
The course continues with an introduction to actuators such as MOSFET electronics projects for customers.
His passion for electronics and
switches, H-bridges, and solid-state relays, which are used to control
programming, as well as his constant
motors and actuators. You will learn to analyze and model controlled interest in new technologies, have
systems, including PT1 and PT2 control. accompanied him since his youth
and have characterized his entire life.
The book focuses on the implementation of fuzzy and PID controllers for
controlling temperature and DC motors. Both the Arduino UNO and the
ESP32 are used. The sliding-mode controller is also introduced.
In the second-to-last chapter, you will explore the basics of neural networks
and learn how machine learning can be used on an Arduino.
In the last chapter, there is a practical example of a fuzzy controller for
feeding electricity into the household grid.
This book is the perfect choice for engineers, students, and electronics
engineers who want to expand their projects with innovative control
techniques. Elektor International Media
www.elektor.com
Josef Bernhardt
SKU21xxx_COV_Control Engineering with Fuzzy Logic_170x240.indd All Pages 21/01/2025 17:03
Control Engineering with
Fuzzy Logic
Practical Applications and Projects
with Arduino, ESP32, and RP2040
Josef Bernhardt
Control Engineering with Fuzzy Logic - UK.indd 3 23-01-2025 14:27
● This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V.
PO Box 11, NL-6114-ZG Susteren, The Netherlands
Phone: +31 46 4389444
● All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or
storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this
publication, without the written permission of the copyright holder except in accordance with the provisions of the
Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency
Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to
reproduce any part of the publication should be addressed to the publishers.
● Declaration
The author and publisher have made every effort to ensure the accuracy of the information contained in this book.
They do not assume, or hereby disclaim, any liability to any party for any loss or damage caused by errors or
omissions in this book, whether such errors or omissions result from negligence, accident, or any other cause.
● ISBN 978-3-89576-660-2 Print
ISBN 978-3-89576-661-9 eBook
● © Copyright 2025 Elektor International Media
www.elektor.com
Editor: Ferdinand te Walvaart
Prepress Production: D-Vision, Julian van den Berg
Printers: Ipskamp, Enschede, The Netherlands
Elektor is the world's leading source of essential technical information and electronics products for pro engineers,
electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers
high-quality content - via a variety of media channels (including magazines, video, digital media, and social media) in
several languages - relating to electronics design and DIY electronics. www.elektormagazine.com
●4
Control Engineering with Fuzzy Logic - UK.indd 4 23-01-2025 14:27
Contents
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Chapter 1 • Arduino Hardware for Control Engineering . . . . . . . . . . . . . . . . . . . . . 10
1.1 Arduino UNO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2 Arduino NANO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3 Espressif ESP32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4 Raspberry RP2040 controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5 ESP32 Fuzzy PID controller board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.6 ESP32 Modbus RTU controller board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.7 Programming the Arduino boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.8 The Nextion graphic display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.9 Test software for the ESP32 board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.10 Program example with the ESP32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.11 Arduino ESP32 controller 12 bit A/D converter test . . . . . . . . . . . . . . . . . . . . . . 34
1.12 CodeBlocks development environment for Ansi C . . . . . . . . . . . . . . . . . . . . . . . 40
1.13 Serial Data Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
1.14 Serial Port Data Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
1.15 The Espressif ESP32 as a WebSocket server . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Chapter 2 • Sensors for Control Engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.1 NTC temperature sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.2 RTD temperature sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.3 Linear Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.4 Thermocouples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.5 Applications of thermocouples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2.6 Sensors with integrated electronics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
2.7 pH sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.8 Rotary encoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
2.9 Force measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
2.10 Linear regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
2.11 Spline interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
2.12 Polynomial regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
●5
Control Engineering with Fuzzy Logic - UK.indd 5 23-01-2025 14:27
Control Engineering with Fuzzy Logic
Chapter 3 • Actuators - MOSFET and SSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.1 MOSFET BSP76 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.2 MOSFET circuit with optocoupler for 10 A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.3 H-bridge L298 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.4 H-bridge with BTS7960 from Infineon up to a maximum of 43 A . . . . . . . . . . . . . 89
3.5 Thyristor actuator for AC voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Chapter 4 • Control, regulation and control systems . . . . . . . . . . . . . . . . . . . . . . . 92
4.1 Formula symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
4.2 Difference between control and control with feedback according to IEC 60050-351 . . 92
4.3 Examples control technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
4.4 Control of a real heating system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
4.5 Control of a DC motor with PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Chapter 5 • On-Off and PID controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
5.1 Two-point controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
5.2 PID controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
5.3 Parameter determination using Ziegler-Nichols . . . . . . . . . . . . . . . . . . . . . . . . . 126
5.4 PID parameter determination with the relay method . . . . . . . . . . . . . . . . . . . . . 127
5.5 Arduino PID controller with PT2 simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
5.6 Arduino PID controller with real controlled system (ALU block) . . . . . . . . . . . . . . 135
5.7 Arduino PID controller for standing pendulum . . . . . . . . . . . . . . . . . . . . . . . . . 146
Chapter 6 • Fuzzy Logic basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
6.1 Fuzzy Logic history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
6.2 Fuzzy controller structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
6.3 Fuzzification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
6.4 Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
6.5 Defuzzification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
6.6 Fuzzy controller example for a fan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
6.7 Control of an inverted pendulum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
6.8 Fuzzy sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
6.9 Simulation of a function with fuzzy logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
6.10 Application of a fuzzy function for a temperature profile . . . . . . . . . . . . . . . . . 183
●6
Control Engineering with Fuzzy Logic - UK.indd 6 23-01-2025 14:27
Contents
Chapter 7 • Fuzzy Logic temperature controller . . . . . . . . . . . . . . . . . . . . . . . . . . 186
7.1 Fuzzy controller with PT2 simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
7.2 Fuzzy controller with the ESP32 and real heating . . . . . . . . . . . . . . . . . . . . . . . 193
7.3 Fuzzy examples with Gauss, Bell and Sigmoid . . . . . . . . . . . . . . . . . . . . . . . . . 206
Chapter 8 • Fuzzy logic temperature controller with two inputs . . . . . . . . . . . . . 207
8.1 Temperature controller with two inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
8.2 Fuzzy control example for a heating control system . . . . . . . . . . . . . . . . . . . . . 211
Chapter 9 • Fuzzy controller examples with a DC motor . . . . . . . . . . . . . . . . . . . 220
9.1 DC motor basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
9.2 Control of the motor with PWM (speed control) . . . . . . . . . . . . . . . . . . . . . . . . 222
9.3 Motor control with PWM and speed measurement . . . . . . . . . . . . . . . . . . . . . . . 223
9.4 Motor speed control with fuzzy logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
9.5 Motor position control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Chapter 10 • Sliding mode controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
10.1 General description of the sliding mode controller . . . . . . . . . . . . . . . . . . . . . . 250
10.4 Optimization of the parameters 𝑐1 and 𝑐2 with fuzzy logic . . . . . . . . . . . . . . . . 268
10.3 Sliding mode controller example with real system . . . . . . . . . . . . . . . . . . . . . 256
Chapter 11 • Neural networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
11.1 History of neural networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
11.2 Biological neuron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
11.3 Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
11.4 Activation functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
11.5 The perceptron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
11.6 Feedforward networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
11.7 Training the neural network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
11.8 ANN example XOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
11.9 Fuzzy Gauss controller with neural network . . . . . . . . . . . . . . . . . . . . . . . . . . 287
11.10 Color sensor evaluation with neural network . . . . . . . . . . . . . . . . . . . . . . . . 294
11.11 Training a neural network with two hidden layers . . . . . . . . . . . . . . . . . . . . . 307
Chapter 12 • Fuzzy controller for feeding electricity into the domestic grid . . . . 320
12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
12.2 Shelly 3EM Energy Meter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
●7
Control Engineering with Fuzzy Logic - UK.indd 7 23-01-2025 14:27
Control Engineering with Fuzzy Logic
12.3 Measurements with the Shelly Plus 1PM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
12.4 Charge controller - Victron Smart Solar 100 I 20 . . . . . . . . . . . . . . . . . . . . . . 326
12.5 Windows software SharpDevelop_Shelly3emWinform . . . . . . . . . . . . . . . . . . . 329
12.6 Soyo Micro Inverter for feeding into the grid . . . . . . . . . . . . . . . . . . . . . . . . . 329
12.7 ESP32 PID Fuzzy Control Board for closed-loop control . . . . . . . . . . . . . . . . . . 331
12.8 Fuzzy controller function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Appendix A • Circuits layouts and parts list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Appendix B • Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Appenix C • Weblinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
●8
Control Engineering with Fuzzy Logic - UK.indd 8 23-01-2025 14:27