Table Of ContentA Hands-On Course in
Sensors Using the Arduino
and Raspberry Pi
Series in Sensors
Series Editors: Barry Jones and Haiying Huang
Other recent books in the series:
Resistive, Capacitive, Inductive, and Magnetic Sensor Technologies
Winncy Y. Du
Semiconductor X-Ray Detectors
B. G. Lowe and R. A. Sareen
Portable Biosensing of Food Toxicants and Environmental Pollutants
Edited by Dimitrios P. Nikolelis, Theodoros Varzakas, Arzum Erdem,
and Georgia-Paraskevi Nikoleli
Optochemical Nanosensors
Edited by Andrea Cusano, Francisco J. Arregui, Michele Giordano,
and Antonello Cutolo
Electrical Impedance: Principles, Measurement, and Applications
Luca Callegaro
Biosensors and Molecular Technologies for Cancer Diagnostics
Keith E. Herold and Avraham Rasooly
Compound Semiconductor Radiation Detectors
Alan Owens
Metal Oxide Nanostructures as Gas Sensing Devices
G. Eranna
Nanosensors: Physical, Chemical, and Biological
Vinod Kumar Khanna
Handbook of Magnetic Measurements
S. Tumanski
Structural Sensing, Health Monitoring, and Performance Evaluation
D. Huston
Chromatic Monitoring of Complex Conditions
Edited by G. R. Jones, A. G. Deakin, and J. W. Spencer
Principles of Electrical Measurement
S. Tumanski
Novel Sensors and Sensing
Roger G. Jackson
Hall Effect Devices, Second Edition
R. S. Popovic
A Hands-On Course in
Sensors Using the Arduino
and Raspberry Pi
Volker Ziemann
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2018 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Printed on acid-free paper
Version Date: 20180129
International Standard Book Number-13: 978-0-8153-9360-3 (Paperback)
International Standard Book Number-13: 978-0-8153-9359-7 (Hardback)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been
made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity
of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright
holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this
form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may
rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized
in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying,
microfilming, and recording, or in any information storage or retrieval system, without written permission from the
publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://
www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923,
978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For
organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.
Library of Congress Cataloging-in-Publication Data
Names: Ziemann, Volker (Associate professor of physics), author.
Title: A hands-on course in sensors using the Arduino and Raspberry Pi /
Volker Ziemann.
Other titles: Sensors series.
Description: Boca Raton, FL : CRC Press, Taylor & Francis Group, [2018] |
Series: Series in sensors
Identifiers: LCCN 2017045669| ISBN 9780815393597 (hardback ; alk. paper) |
ISBN 9780815393603 (pbk. ; alk. paper) | ISBN 9781351188319 (e-book) |
ISBN 9781351188296 (e-book) | ISBN 9781351188302 (ebook) | ISBN
9781351188289 (ebook)
Subjects: LCSH: Detectors. | Raspberry Pi (Computer) | Arduino (Programmable
controller) | Microcontrollers.
Classification: LCC TK7872.D48 Z54 2018 | DDC 681/.20285464--dc23
LC record available at https://lccn.loc.gov/2017045669
Visit the eResource: https://www.crcpress.com/9780815393603
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
Contents
Preface xv
Acknowledgments xvii
hapter
C 1(cid:4) Introduction 1
hapter
C 2(cid:4) Sensors 5
2.1 ANALOGSENSORS 5
2.1.1 Resistance-based sensors 5
2.1.2 Voltage-based sensors 10
2.1.3 Current-based sensors 14
2.2 SIGNALCONDITIONING 16
2.2.1 Voltage divider 16
2.2.2 Amplifiers 17
2.2.3 Filters 21
2.2.4 Analog-to-digital conversion 23
2.2.5 Supply voltage 26
2.3 DIGITALSENSORS 28
2.3.1 Buttons and switches 29
2.3.2 On/off devices 30
2.3.3 I2C devices 32
2.3.4 SPI devices 35
2.3.5 RS-232 devices 36
2.3.6 Other sensors 36
hapter
C 3(cid:4) Actuators 39
3.1 SWITCHES 39
3.1.1 Light-emitting diodes and optocouplers 39
3.1.2 Large currents 41
3.2 MOTORS 43
3.2.1 DC motors 44
3.2.2 Servomotors and model-servos 46
3.2.3 Stepper motors 47
v
vi (cid:4) Contents
3.3 ANALOGVOLTAGES 51
3.4 OTHERACTUATORS 52
hapter
C 4(cid:4) Microcontroller: Arduino 55
4.1 HARDWARE 55
4.1.1 Arduino UNO 55
4.1.2 ESP8266 and NodeMCU 56
4.2 GETTINGSTARTED 57
4.3 HELLOWORLD,BLINK 58
4.4 INTERFACINGSENSORS 60
4.4.1 Button 60
4.4.2 Analog input 61
4.4.3 I2C 65
4.4.4 SPI 77
4.4.5 Other protocols 80
4.5 INTERFACINGACTUATORS 85
4.5.1 Switching devices 85
4.5.2 DC motors 87
4.5.3 Servos 91
4.5.4 Stepper motors 92
4.5.5 Analog voltages 101
4.5.6 Human attention actuators 102
4.6 COMMUNICATIONTOHOST 103
4.6.1 RS-232 and USB 103
4.6.2 Bluetooth 104
4.6.3 WiFi 105
4.6.4 Other communication 110
hapter
C 5(cid:4) Host Computer: Raspberry Pi 113
5.1 HARDWARE 113
5.2 GETTINGSTARTED 113
5.3 INSTALLINGANDUSINGNEWSOFTWARE 117
5.4 RASPIASAROUTER 121
5.5 COMMUNICATIONWITHTHEARDUINO 123
5.5.1 Arduino IDE 123
5.5.2 From the command line 124
5.5.3 Python 125
5.5.4 Octave 129
5.6 DATASTORAGE 132
5.6.1 Flatfile 132
5.6.2 MySQL 134
Contents (cid:4) vii
5.6.3 RRDtool 138
5.7 ONLINEPRESENTATION 141
hapter
C 6(cid:4) Control System: EPICS 147
6.1 INSTALLATION 147
6.2 COMMUNICATINGWITHEPICS 149
6.3 ASYNANDSTREAMLIBRARIES 150
6.4 WRITINGANIOC 151
6.5 STARTINGTHEIOCATBOOTTIME 154
hapter
C 7(cid:4) Messaging System: MQTT 157
7.1 BROKER 158
7.2 NODEMCUCLIENTS 159
7.3 GATEWAYTOEPICS 161
hapter
C 8(cid:4) Example: Weather Station with Distributed Sensors 167
hapter
C 9(cid:4) Example: Geophones 177
hapter
C 10(cid:4) Example: Monitor for the Color of Water 185
hapter
C 11(cid:4) Example: Capacitance Measurement 191
hapter
C 12(cid:4) Example: Profile of a Laser Beam 197
hapter
C 13(cid:4) Example: Fire-Seeking Robot 205
hapter
C 14(cid:4) Presenting and Writing 223
14.1 PREPARINGAPRESENTATION 223
14.2 PREPARINGAREPORT 225
14.3 PRESENTINGDATA 226
14.4 GOODENGLISH 227
14.5 POSTSCRIPTUM 228
ppendix
A A(cid:4) Basic Circuit Theory 229
ppendix
A B(cid:4) Least-Squares Fit and Error Propagation 231
Bibliography 235
Index 237
List of Figures
1.1 The outline of the book. 2
2.1 Imageofalight-dependentresistor(LDR)ontheleftandhowtoconnectit
inaplainvoltage-dividerconfiguration(center)andinaWheatstonebridge
(right). 5
2.2 Theband-levelschemeandtheschematicsforthecircuitwiththeLDRina
voltage divider. The upper graph illustrates dark conditions and the lower
graph shows conditions where the LDR is exposed to light. Note that the
vertical axis by convention shows the energy of electrons. This causes the
positive pole of the battery to have the most negative energy. See the text
for a discussion. 6
2.3 NTC resistors are doped semiconductors that have donor levels just below
theconductionband.Increasingthetemperatureincreasesthekineticenergy
of the electrons that allows them to occasionally jump into the conduction
band, where they contribute to the conductivity of the material. 8
2.4 A linear potentiometer (left) and a circuit illustrating the electric connec-
tions (right). 9
2.5 A joystick is shown on the left and a fluid-level resistive sensor on the right. 10
2.6 A strain gauge. 10
2.7 Schematic of an MQ-x gas detector (left) and a sensor mounted on a small
breakout board (right). 11
2.8 Image of an LM35 temperature sensor (left) and how to connect it (right). 11
2.9 On the left we show a schematic of a thermocouple on the top and a ther-
mopile on the bottom. On the right we show an image of an MLX90614
contact-free thermometer. 12
2.10 Schematic of a Hall sensor (left) and the A1324 sensor (right). 12
2.11 The operational principle of an ADXL accelerometer. 13
2.12 An SM-24 geophone with a diameter of about 30mm. 14
2.13 Energy-band diagram (left) and circuit (right) of a reverse-biased pin diode
in photoconductive mode. 14
2.14 Image of a BPW34 pin diode on the left and two phototransistors on the
right; an IR-sensitive BPX38 and an SFH3310, sensitive in the visible part
of the spectrum. 15
2.15 On the left we have a voltage divider to reduce the input voltage of 0-5V
to0-3.3V.Therightcircuitshowstheuseofclampingdiodestoprotectthe
input of the microcontroller to lie between ground and 5V. 16
ix