0% found this document useful (0 votes)
318 views7 pages

Contents - PID-based Practical Digital Control

This document provides an overview and summary of the book "PID-based Practical Digital Control With Raspberry Pi and Arduino Uno" by Dogan Ibrahim. The book discusses using the Raspberry Pi 4 and Arduino Uno for PID-based automatic control applications. It covers topics such as open-loop and closed-loop control systems, sensors, PID controllers, and several practical control projects implementing PID control with the Raspberry Pi and Arduino. The full code for all discussed projects is available for free download.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
318 views7 pages

Contents - PID-based Practical Digital Control

This document provides an overview and summary of the book "PID-based Practical Digital Control With Raspberry Pi and Arduino Uno" by Dogan Ibrahim. The book discusses using the Raspberry Pi 4 and Arduino Uno for PID-based automatic control applications. It covers topics such as open-loop and closed-loop control systems, sensors, PID controllers, and several practical control projects implementing PID control with the Raspberry Pi and Arduino. The full code for all discussed projects is available for free download.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

books books

PID-based Practical
PID-based Practical

PID-based Practical Digital Control • Dogan Ibrahim


Digital Control
With Raspberry Pi and Arduino Uno
The Arduino Uno is an open-source microcontroller development system
encompassing hardware, an Integrated Development Environment (IDE),
and a vast number of libraries. It is supported by an enormous community
Digital Control
of programmers, electronic engineers, enthusiasts, and academics. The
libraries in particular really smooth Arduino programming and reduce
programming time. What’s more, the libraries greatly facilitate testing
With Raspberry Pi and Arduino Uno
your programs since most come fully tested and working. Prof Dogan Ibrahim has a BSc,
Hons. degree in Electronic
Engineering, an MSc degree in
The Raspberry Pi 4 can be used in many applications such as audio and Automatic Control Engineering,
video media devices. It also works in industrial controllers, robotics, games,
#define PhaseA 2 // Phase A
and a PhD degree in Digital Signal
and in many domestic and commercial applications. The Raspberry Pi 4 Processing. #define PhaseB 3 // Phase B
also offers Wi-Fi and Bluetooth capability which makes it great for remote
and Internet-based control and monitoring applications. Dogan has worked in many industrial #define PWM 5 // PWM pin
organizations before he returned
to academic life. He is the author #define DIR 4 // DIR pin
This book is about using both the Raspberry Pi 4 and the Arduino Uno of over 70 technical books and has
in PID-based automatic control applications. The book starts with basic published over 200 technical articles float sk, Kp, Ti, Td, pk, T, pk_1, ek, ek_1, a, b, yk, qk, wk, uk;
theory of the control systems and feedback control. Working and tested on electronics, microprocessors,
volatile unsigned long Count = 0; // Encoder count
projects are given for controlling real-life systems using PID controllers. The microcontrollers, and related fields.
open-loop step time response, tuning the PID parameters, and the closed- float conv = 150.0 / 90.0;
The full program listings of all the
loop time response of the developed systems are discussed together with projects discussed in the book //
the block diagrams, circuit diagrams, PID controller algorithms, and the may be downloaded free of charge
full program listings for both the Raspberry Pi and the Arduino Uno. The from the Elektor Store website, // Configure PWM pin and Initialize variables
projects given in the book aim to teach the theory and applications of PID www.elektor.com (search for:
controllers and can be modified easily as desired for other applications. book title). //
The projects given for the Raspberry Pi 4 should work with all other void setup()
models of Raspberry Pi family.
{
The book covers the following topics: TCCR2B = TCCR2B & B11111000 | B00000011;
> Open-loop and closed-loop > ON-OFF temperature control with
control systems Raspberry Pi and Arduino Uno pinMode(DIR, OUTPUT); // Direction is outpu
> Analog and digital sensors > PID-based temperature digitalWrite(DIR, LOW);
> Transfer functions and control with Raspberry Pi and
continuous-time systems Arduino Uno pinMode(PWM, OUTPUT); // PWM is output
> First-order and second-order > PID-based DC motor control with Elektor International Media BV
pinMode(PhaseB, INPUT); // Phase B is input
system time responses Raspberry Pi and Arduino Uno www.elektor.com
> Discrete-time digital systems > PID-based water level control pinMode(PhaseA, INPUT); // Phase A is input
> Continuous-time PID controllers with Raspberry Pi and Arduino
> Discrete-time PID controllers > PID-based LED-LDR brightness
attachInterrupt(digitalPinToInterrupt(PhaseB), EncoderISR, RIS
control with Raspberry Pi and attachInterrupt(digitalPinToInterrupt(PhaseA), EncoderISR2, R
Arduino Uno
Serial.begin(19200); Dogan Ibrahim
PID-based Practical
Digital Control
With Raspberry Pi and Arduino Uno

Dogan Ibrahim

PID-based Practical Digital Control With Raspberry Pi and Arduino Uno 220802 UK.indd 3 15-08-2022 15:32
● 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 used their best efforts in ensuring the correctness of the information contained in
this book. They do not assume, and 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.
All the programs given in the book are Copyright of the Author and Elektor International Media. These programs
may only be used for educational purposes. Written permission from the Author or Elektor must be obtained before
any of these programs can be used for commercial purposes.

● British Library Cataloguing in Publication Data


A catalogue record for this book is available from the British Library

● ISBN 978-3-89576-519-3 Print


ISBN 978-3-89576-520-9 eBook

● © Copyright 2022: Elektor International Media B.V.


Editor: Jan Buiting
Prepress Production: D-Vision, Julian van den Berg

Elektor is part of EIM, 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

PID-based Practical Digital Control With Raspberry Pi and Arduino Uno 220802 UK.indd 4 15-08-2022 15:32
Contents

Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Chapter 1 • Control Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.1 Open-loop and closed-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.2 Microcontroller in the loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

1.3 Control system design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Chapter 2 • Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.1 Sensors in Computer Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.2 Temperature Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.2.1 Analog Temperature Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.2 Digital Temperature Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2.3 Position Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.4 Velocity and Acceleration Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

2.5 Force Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

2.6 Pressure sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

2.7 Liquid Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

2.8 Flow Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Chapter 3 • Transfer Functions and Time Response . . . . . . . . . . . . . . . . . . . . . . . . 37

3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

3.2 First-order Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

3.2.1 Time Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

3.3 Second-order Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

3.3.1 Time Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

3.4 Time Delay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

3.5 Transfer Function of a Closed-loop System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Chapter 4 • Discrete Time (Digital) Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

4.2 The Sampling Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

4.3 The Z-Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

4.3.1 Unit step function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

4.3.2 Unit ramp function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

●5

PID-based Practical Digital Control With Raspberry Pi and Arduino Uno 220802 UK.indd 5 15-08-2022 15:32
PID-based Practical Digital Control

4.3.3 Tables of z-Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

4.4 The z-Transform of a function expressed as a Laplace Transform . . . . . . . . . . . . . 58

4.5 Inverse z-Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

4.6 Pulse transfer function and manipulation of block diagrams . . . . . . . . . . . . . . . . . 61

4.6.1 Open-loop systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

4.7 Open-loop time response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

4.8 Closed-loop system time response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

Chapter 5 • The PID Controller in Continuous-Time Systems . . . . . . . . . . . . . . . . . 69

5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

5.2 Proportional-only Controller with a First-Order System . . . . . . . . . . . . . . . . . . . . 70

5.3 Integral-Only Controller with a First-order System . . . . . . . . . . . . . . . . . . . . . . . 71

5.4 Derivative-only Controller with a First-order System . . . . . . . . . . . . . . . . . . . . . . 72

5.5 Proportional + Integral Controller with a First-order System . . . . . . . . . . . . . . . . 73

5.6 Proportional + Integral + Derivative controller with a First-order System . . . . . . . 74

5.7 Effects of Changing the PID Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

5.8 Tuning a PID Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

5.8.1 Open-loop Ziegler and Nichols Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

5.8.2 Open-loop Cohen-Coon PID Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

5.8.3 Closed-loop Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

5.8.4 Practical PID Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

5.9 The Auto-tuning PID Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

5.10 Increasing and Decreasing PID Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

5.11 Saturation and Integral Wind-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

5.12 Derivative Kick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

5.13 Using the PID Loop Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

Chapter 6 • The Digital PID Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

6.2 Digital PID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

6.3 Choosing a Sampling Time, T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

6.4 Microcontroller Implementation of the PID Algorithm . . . . . . . . . . . . . . . . . . . . . 90

Chapter 7 • On-Off Temperature Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

●6

PID-based Practical Digital Control With Raspberry Pi and Arduino Uno 220802 UK.indd 6 15-08-2022 15:32
Contents

7.2 Temperature Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

7.3 Project 1: ON-OFF Temperature Control with Arduino Uno . . . . . . . . . . . . . . . . . . 93

7.4 Project 2: ON-OFF Temperature Control with Hysteresis and Arduino Uno . . . . . . . 98

7.5 Project 3: ON-OFF Temperature Control with Button Control – Arduino Uno . . . . . 100

7.6 Project 4: ON-OFF Temperature Control with Rotary Encoder and Arduino Uno . . 103

7.7 Project 5: ON-OFF Temperature Control with Raspberry Pi 4 . . . . . . . . . . . . . . . 108

Chapter 8 • PID Temperature Control with the Raspberry Pi . . . . . . . . . . . . . . . . 118

8.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

8.2 Project 1 - Reading the temperature of a thermistor . . . . . . . . . . . . . . . . . . . . . 118

8.3 Project 2: Open-loop Step-input Time Response . . . . . . . . . . . . . . . . . . . . . . . . 122

8.4 Project 3: PI Temperature Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

8.5 Project 4: PID Temperature Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

8.6 Using the PID Loop Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

Chapter 9 • PID Temperature Control with the Arduino Uno . . . . . . . . . . . . . . . . 143

9.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

9.2 Project 1: Reading the Temperature of a Thermistor . . . . . . . . . . . . . . . . . . . . . 143

9.3 Project 2: PID Temperature Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

9.4 Project 3: PID Temperature Control with Arduino Uno and Timer Interrupts . . . . . 151

9.5 Project 4: PID Temperature Control using the Arduino Uno PID Library . . . . . . . . 154

Chapter 10 • DC Motor Control with Arduino and Raspberry Pi . . . . . . . . . . . . . . 159

10.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

10.2 Types of Electric Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

10.3 Brushed DC Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

10.3.1 Permanent-magnet BDC Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

10.3.2 Series-wound BDC Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

10.3.3 Shunt-wound BDC Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

10.3.4 Compound-wound BDC Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

10.3.5 Separately-excited BDC Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

10.3.6 Servo Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

10.3.7 Stepper Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

10.4 Brushless DC Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

10.5 Motor Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

●7

PID-based Practical Digital Control With Raspberry Pi and Arduino Uno 220802 UK.indd 7 15-08-2022 15:32
PID-based Practical Digital Control

10.6 Transfer Function of a Brushed DC Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

10.7 The DC Motor Used in the Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

10.8 Project 1: Motor Speed and Direction Control Using an H-Bridge Integrated Circuit . 169

10.9 Project 2: Displaying the Motor Speed with Arduino Uno . . . . . . . . . . . . . . . . . 176

10.10 Project 3: Displaying Motor Speed on LCD with Arduino Uno . . . . . . . . . . . . . 180

10.11 Project 4: Displaying Motor Speed with Raspberry Pi . . . . . . . . . . . . . . . . . . . 183

10.12 Project 5: Displaying Motor Speed on LCD with Raspberry Pi . . . . . . . . . . . . . 185

10.13 Project 6: Identification of the DC Motor with Raspberry Pi . . . . . . . . . . . . . . 187

10.14 Project 7: PID Motor Speed control with Raspberry Pi . . . . . . . . . . . . . . . . . . 190

10.15 Project 8: PID Motor Speed Control with Arduino Uno . . . . . . . . . . . . . . . . . . 194

Chapter 11 • Water Level Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

11.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

11.2 Ultrasonic Transmitter-Receiver Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

11.3 Project 1: Measuring Distance using the HC-SR04 Ultrasonic Module with
Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

11.4 Project 2: Measuring Distance using the HC-SR04 Ultrasonic Module with
Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

11.5 Project 3: Step Input Response of the System with Raspberry Pi . . . . . . . . . . . 205

11.6 Project 4: PID-based Water Level Control with Raspberry Pi . . . . . . . . . . . . . . . 208

11.7 Project 5: PID-based Water Level Control with Arduino Uno . . . . . . . . . . . . . . . 214

Chapter 12 • PID-based LED Brightness Control . . . . . . . . . . . . . . . . . . . . . . . . . 218

12.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

12.2 Project 1: Step Time Response of LED Brightness Control using the Raspberry Pi 218

12.3 Project 2: PID-Based LED Brightness Control using the Raspberry Pi . . . . . . . . . 222

12.4 Project 3: PID-based LED Brightness Control using the Arduino Uno . . . . . . . . . 227

12.5 Project 4: PID-based LED Brightness Control using the . . . . . . . . . . . . . . . . . . 230

Arduino Uno Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

●8

PID-based Practical Digital Control With Raspberry Pi and Arduino Uno 220802 UK.indd 8 15-08-2022 15:32

You might also like