0% found this document useful (0 votes)
635 views193 pages

Internet of Things With 8051 and ESP8266

Uploaded by

funkrocknow3826
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)
635 views193 pages

Internet of Things With 8051 and ESP8266

Uploaded by

funkrocknow3826
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/ 193

Internet of Things with

8051 and ESP8266


Internet of Things with
8051 and ESP8266

Anita Gehlot, Rajesh Singh, Praveen Kumar Malik,


Lovi Raj Gupta, Bhupendra Singh
First edition published 2021 by
CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742

and by
CRC Press
2 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN

© 2021 Taylor & Francis Group, LLC

CRC Press is an imprint of Taylor & Francis Group, LLC

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 uti-
lized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopy-
ing, 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, access www.copyright.com or contact the
Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are not
available on CCC please contact [email protected]

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for iden-
tification and explanation without intent to infringe.

ISBN: 978-0-367-53478-3 (hbk)


ISBN: 978-1-003-08213-2 (ebk)

Typeset in Times LT Std


by KnowledgeWorks Global Ltd.
Contents
Preface...............................................................................................................................................xi
Author’s Biographies...................................................................................................................... xiii

SECTION A Basics of 8051 Microcontroller and IoT

Chapter 1 Introduction to the IoT..................................................................................................3


1.1 Introduction to IoT.............................................................................................3
1.1.1 IoT in Society .......................................................................................4
1.2 IoT Components................................................................................................. 4
1.2.1 Sensor ...................................................................................................4
1.2.2 Push Button ..........................................................................................5
1.2.3 Pressure Sensor ....................................................................................5
1.2.4 Ultrasonic Sensor .................................................................................5
1.2.5 Infrared Sensor.....................................................................................5
1.2.6 Proximity and Touch Sensor ................................................................5
1.2.7 Laser Sensor .........................................................................................5
1.2.8 Bump Sensor ........................................................................................5
1.2.9 Metal Sensor.........................................................................................5
1.2.10 The Fire Sensor ....................................................................................5
1.2.11 The Passive Infrared (PIR) Sensor.......................................................6
1.2.12 Potentiometer........................................................................................6
1.2.13 Alcohol Sensor .....................................................................................6
1.2.14 Temperature Sensor..............................................................................6
1.2.15 Gas Sensor............................................................................................6
1.2.16 GPS.......................................................................................................6
1.2.17 Gyroscope.............................................................................................6
1.2.18 Thermal Camera...................................................................................6
1.2.19 Humidity Sensor...................................................................................6
1.2.20 Magnetometer.......................................................................................6
1.2.21 Compass Sensor ...................................................................................7
1.2.22 Accelerometer.......................................................................................7
1.2.23 Load Cell ..............................................................................................7
1.2.24 Fingerprint Sensor................................................................................7
1.2.25 Inertial Measurement Unit ...................................................................7
1.2.26 Current and Voltage Sensor..................................................................7
1.2.27 Vibration Sensor...................................................................................7
1.2.28 RFID.....................................................................................................7
1.3 Actuators............................................................................................................7
1.3.1 Rotational Actuators............................................................................. 8
1.3.2 AC Motor..............................................................................................8
1.3.3 DC Motor .............................................................................................8
1.3.4 Geared DC Motor.................................................................................8
1.3.5 Servo Motor..........................................................................................8
1.3.6 Industrial Servo Motor ......................................................................... 8

v
vi Contents

1.3.7 Stepper Motor.......................................................................................8


1.3.8 Linear Actuator ....................................................................................8
1.3.9 DC Linear Actuator..............................................................................9
1.3.10 Solenoid ................................................................................................9
1.3.11 Muscle Wire .........................................................................................9
1.3.12 Pneumatic and Hydraulic Actuators ....................................................9
1.4 Cloud Computing Model ...................................................................................9
1.4.1 Software as a Service (SaaS)................................................................9
1.4.2 Platform as a Service (PaaS) .............................................................. 10
1.4.3 Infrastructure as a Service (IaaS) ...................................................... 11
1.5 Zigbee .............................................................................................................. 12
1.6 BLE.................................................................................................................. 12
1.7 6LoWPAN ....................................................................................................... 13
1.7.1 Functions ............................................................................................ 13

Chapter 2 Meet 8051 and Keil Compiler—A Software Development Environment.................. 15


2.1 Introduction to 8051 ........................................................................................ 15
2.2 Keil Compiler—A Software Development Environment................................ 15
2.2.1 uVision3 IDE ...................................................................................... 15
2.2.2 uVision Debugger ............................................................................... 17
2.2.3 Execution............................................................................................ 17

Chapter 3 Introduction to NuttyFi Board and Its Programming ................................................ 19


3.1 Introduction to NuttyFi Board ......................................................................... 19
3.1.1 Pin Description................................................................................... 19
3.1.2 Specifcation of NuttyFi ..................................................................... 19
3.2 Programming Steps to NuttyFi/Esp8266 ........................................................20
3.2.1 Installing NuttyFi in to Arduino IDE.................................................20

Chapter 4 Introduction to Customized Board with 8051 Microcontroller and


NuttyFi/ESP8266 .......................................................................................................25
4.1 Introduction to Customized Board with 8051 Microcontroller
and NuttyFi/ESP8266......................................................................................25
4.1.1 ADC 0804 (ADC or Analog-to-Digital Conversion) .........................25
4.1.2 MUX 74HC4051 ................................................................................25
4.1.3 NuttyFi................................................................................................ 27
4.1.4 Reset Circuit ....................................................................................... 27
4.1.5 Crystal Circuit ....................................................................................28
4.1.6 LCD Circuitry .................................................................................... 29

SECTION B Interfacing of 8051 Microcontroller


and NuttyFi/ESP8266 with I/O devices

Chapter 5 Interfacing of 8051 and NuttyFi/ESP8266 with LED................................................ 33


5.1 Circuit Diagram............................................................................................... 33
5.2 Program ........................................................................................................... 36
Contents vii

Chapter 6 Interfacing of 8051 and NuttyFi/ESP8266 with Seven Segment Display.................. 39


6.1 Seven Segment Display ................................................................................... 39
6.2 Circuit Diagram............................................................................................... 39
6.3 Program ........................................................................................................... 43

Chapter 7 Interfacing of 8051 and NuttyFi/ESP8266 with LCD................................................ 45


7.1 Introduction of Liquid Crystal Display ........................................................... 45
7.2 Circuit Diagram............................................................................................... 45
7.3 Program ........................................................................................................... 48

Chapter 8 Interfacing of 8051 and NuttyFi/ESP8266 with Analog Sensor ................................ 53


8.1 Potentiometer................................................................................................... 53
8.1.1 Circuit Diagram.................................................................................. 53
8.1.2 Program.............................................................................................. 56
8.2 Temperature Sensor ......................................................................................... 58
8.2.1 Circuit Diagram.................................................................................. 58
8.2.2 Program.............................................................................................. 61
8.3 Light Dependent Resistor ................................................................................ 63
8.3.1 Circuit Diagram..................................................................................64
8.3.2 Program.............................................................................................. 65
8.4 Flex Sensor ...................................................................................................... 68
8.4.1 Circuit Diagram.................................................................................. 68
8.4.2 Program.............................................................................................. 71
8.5 Gas Sensor ....................................................................................................... 72
8.5.1 Circuit Diagram.................................................................................. 73
8.5.2 Program.............................................................................................. 75

Chapter 9 Interfacing of 8051 and NuttyFi/ESP8266 with Digital Sensor................................. 79


9.1 Push Button ..................................................................................................... 79
9.1.1 Circuit Diagram.................................................................................. 79
9.1.2 Program.............................................................................................. 81
9.2 PIR Sensor ....................................................................................................... 83
9.2.1 Circuit Diagram.................................................................................. 83
9.2.2 Program..............................................................................................84
9.3 Flame/Fire Sensor ........................................................................................... 87
9.3.1 Circuit Diagram.................................................................................. 87
9.3.2 Program.............................................................................................. 89

SECTION C Interfacing of 8051 Microcontroller and


NuttyFi/ESP8266 with Special Devices

Chapter 10 Interfacing of 8051 and NuttyFi/ESP8266 with UART Based Devices .................... 93
10.1 Ultrasonic Sensor ............................................................................................ 93
10.1.1 Circuit Diagram.................................................................................. 93
10.1.2 Program.............................................................................................. 93
viii Contents

10.2 DHT11 .............................................................................................................96


10.2.1 Circuit Diagram..................................................................................96
10.2.2 Program..............................................................................................97

Chapter 11 Interfacing of 8051 with I2C Based Devices ........................................................... 101


11.1 DHT11 ........................................................................................................... 101
11.1.1 Circuit Diagram................................................................................ 101
11.1.2 Program............................................................................................ 102
11.2 Ultrasonic Sensor .......................................................................................... 104
11.2.1 Circuit Diagram................................................................................ 104
11.2.2 Program............................................................................................ 106
11.3 EEPROM ....................................................................................................... 107
11.3.1 Circuit Diagram................................................................................ 107
11.3.2 Program............................................................................................ 108
11.4 DS1307 .......................................................................................................... 110
11.4.1 Circuit Diagram................................................................................ 110
11.4.2 Program............................................................................................ 112

Chapter 12 Interfacing of 8051 with SPI Based Devices........................................................... 115


12.1 Introduction ................................................................................................... 115
12.1.1 SPI Lines .......................................................................................... 115
12.2 Circuit Diagram............................................................................................. 115
12.3 Program ......................................................................................................... 117

Chapter 13 Interfacing of 8051 with One Wire Interface-based Devices .................................. 119
13.1 DS1820........................................................................................................... 119
13.1.1 Circuit Diagram................................................................................ 119
13.1.2 Program............................................................................................ 120
13.2 8051 To 8051 Communication....................................................................... 123
13.2.1 Circuit Diagram................................................................................ 123
13.2.2 Program............................................................................................ 125

Chapter 14 Interfacing of 8051 with Bluetooth .......................................................................... 129


14.1 Introduction ................................................................................................... 129
14.2 Circuit Diagram............................................................................................. 129
14.3 Program ......................................................................................................... 130

SECTION D Case Study Based on Data Logger to C loud Server

Chapter 15 Health Monitoring System for Solar Panel with Cayenne APP ............................... 135
15.1 Introduction ................................................................................................... 135
15.2 Circuit Diagram............................................................................................. 135
15.3 Program ......................................................................................................... 136
15.3.1 Program for 8051 Microcontroller ................................................... 137
15.3.2 Program for NuttyFi ......................................................................... 140
15.4 Cloud Server .................................................................................................. 142
Contents ix

Chapter 16 Smart Irrigation System in Agricultural Field with Blynk APP.............................. 145
16.1 Introduction ................................................................................................... 145
16.2 Circuit Diagram............................................................................................. 145
16.3 Program ......................................................................................................... 147
16.3.1 Program for 8051.............................................................................. 147
16.3.2 Program for NuttyFi ......................................................................... 150
16.4 Cloud Server .................................................................................................. 152
16.4.1 Steps to Design Blynk App .............................................................. 152

Chapter 17 Environmental Parameters Monitoring System with Virtuino APP........................ 155


17.1 Introduction ................................................................................................... 155
17.2 Circuit Diagram............................................................................................. 155
17.3 Program ......................................................................................................... 157
17.3.1 Program for 8051.............................................................................. 157
17.3.2 Program for NuttyFi ......................................................................... 160
17.4 Cloud Server .................................................................................................. 162

Chapter 18 Weather Station with ThingSpeak Server ................................................................ 163


18.1 Introduction ................................................................................................... 163
18.1.1 Wind Speed Sensor .......................................................................... 163
18.1.2 Calibration........................................................................................ 163
18.2 Circuit Diagram............................................................................................. 163
18.3 Program ......................................................................................................... 165
18.3.1 Program for 8051.............................................................................. 165
18.3.2 Program for NuttyFi ......................................................................... 168
18.4 Cloud Server .................................................................................................. 171
18.4.1 Steps to Create a Channel ................................................................ 171
Index.............................................................................................................................................. 173
Preface
This book will provide a better understanding of 8051 and its interfacing for Internet of Things (IoT)
applications. IoT or Internet-enabled communications were simply explained by Jacob Morgan who
stated that broadband Internet is widely available and more devices come with Wi-Fi capabilities
and sensors with them.
The book includes the basics of IoT, interfacing of 8051, and NuttyFi with I/O devices in different
communication modes. The book comprises of four sections and total eighteen chapters.
Authors are thankful to the publisher for the support and encouragement to write this book.

Dr. Anita Gehlot, Lovely Professional University, India


Dr. Rajesh Singh, Lovely Professional University, India
Dr. Praveen Kumar Malik, Lovely Professional University, India
Dr. Lovi Raj Gupta, Lovely Professional University, India
Bhupendra Singh, Schematics Microelectronics, India

xi
Authors’ Biographies
Dr. Anita Gehlot i s currently associated with Lovely Professional University
as Associate Professor with more than 12 years of experience in academ-
ics. Her area of expertise includes embedded systems, wireless sensor net-
works, and IoT. She has been honored as keynote speakers and session chair
to national/international conferences, faculty development programs, and
workshops. She has 132 patents in her account. She has published more
than 70 research papers in referred journals/conferences and 24 books in
the area of Embedded Systems and IoT with reputed publishers like CRC/
Taylor & Francis, Narosa, GBS, IRP, NIPA, River Publishers, Bentham
Science, and RI Publication. She is editor to a special issue published by
AISC book series, Springer, in 2018, and IGI Global in 2019. She has been awarded with “certificate
of appreciation” from University of Petroleum and Energy Studies for exemplary work. Under her
mentorship, students’ team got “InSc Award 2019” under students projects program. She has been
awarded with “Gandhian Young Technological Innovation (GYTI) Award”, as Mentor to “On Board
Diagnostic Data Analysis System-OBDAS”, Appreciated under “Cutting Edge Innovation” during
Festival of Innovation and Entrepreneurship at Rashtrapati Bhavan, India, in 2018.

Dr. Rajesh Singh  is currently associated with Lovely Professional


University as Professor with more than 16 years of experience in academ-
ics. He has been awarded as gold medalist in M.Tech from RGPV, Bhopal
(MP), India, and his B.E. (Hons) from Dr. B.R. Ambedkar University, Agra
(UP), India. His area of expertise includes embedded systems, robotics,
wireless sensor networks, and IoT. He has been honored as keynote speak-
ers and session chair to national/international conferences, faculty devel-
opment programs, and workshops. He has 152 patents in his account. He
has published more than 100 research papers in referred journals/conferences, and 24 books in the
area of Embedded Systems and IoT with reputed publishers like CRC/Taylor & Francis, Narosa,
GBS, IRP, NIPA, River Publishers, Bentham Science, and RI Publication. He is editor to a special
issue published by AISC book series, Springer, in 2017 & 2018, and IGI Global in 2019. Under his
mentorship, students have participated in national/international competitions including “Innovative
Design Challenge Competition” by Texas and DST and Laureate award of excellence in robot-
ics engineering, Madrid, Spain, in 2014 & 2015. His team has been the winner of “Smart India
Hackathon-2019” hardware version conducted by MHRD, Government of India, for the problem
statement of Mahindra & Mahindra. Under his mentorship, students’ team got “InSc Award 2019”
under students’ projects program. He has been awarded with “Gandhian Young Technological
Innovation (GYTI) Award”, as Mentor to “On Board Diagnostic Data Analysis System-OBDAS”,
Appreciated under “Cutting Edge Innovation” during Festival of Innovation and Entrepreneurship
at Rashtrapati Bhavan, India, in 2018. He has been honored with “Certificate of Excellence” from
3rd faculty branding awards-15, Organized by EET CRS research wing for excellence in profes-
sional education and industry, for the category “Award for Excellence in Research”, 2015, and young
investigator award at the International Conference on Science and Information in 2012.

xiii
xiv Authors’ Biographies

Dr. Praveen Kumar Malik is working as Professor in Deptt. of Electronics


and Communication, Lovely Professional University, Punjab, India. He has
more than 15 papers in different national/international journals. His major
area of interest includes Antenna Design, Wireless Communication, and
Embedded Systems.

Dr. Lovi Raj G  upta is the Executive Dean, Faculty of Technology &
Sciences, Lovely Professional University, Punjab, India. He is a leading
light in the field of Technical and Higher Education in the country. His
research focused approach and an insightful innovative intervention of
technology in education has won him much accolades and laurels. He holds
a PhD in Bioinformatics. He did his M.Tech in Computer Aided Design
& Interactive Graphics from IIT, Kanpur (UP), and B.E. (Hons) from
MITS, Gwalior (MP). Having flair for endless learning, has done more
than 15 certifications and specializations online on IoT, Augmented Reality and Gamification, from
University of California at Irvine, Yonsei University, South Korea, and Wharton School, University
of Pennsylvania. His research interests are in the areas of Robotics, Mechatronics, Bioinformatics,
IoT, and Gamification.
In 2001, he was appointed as Assistant Controller (Technology), Ministry of IT, Govt. of India,
by the Honorable President of India in the Office of the Controller of Certifying Authorities (CCA).
In 2013, he was accorded the role in the National Advisory Board for What Can I Give Mission—
Kalam Foundation of Dr. APJ Abdul Kalam. In 2011, he received the MIT Technology Review
Grand Challenge Award followed by the coveted Infosys InfyMakers Award in the year 2016.

Bhupendra Singh is the Managing Director of Schematics Microelectronics


and provides Product design and R&D support to industries and universities.
He has completed BCA, PGDCA, M.Sc. (CS), M.Tech, and has more than
11 years of experience in the field of Computer Networking and Embedded
Systems. He has published ten books in the area of Embedded Systems and
IoT with reputed publishers like CRC/Taylor & Francis, Narosa, GBS, IRP,
NIPA, and RI Publication.
Section A
Basics of 8051 Microcontroller and IoT
1 Introduction to the IoT

This chapter discusses the introduction to the Internet of Things (IoT). The chapter also describes
the features of IoT and its components.

1.1 INTRODUCTION TO IOT


The meaning of the “Internet” is “The large systems of connected computers around the world that
allow people to share information and communicate with each other”. “Things” is “used to refer in
an approximate way to objects”.
The required data can be collected by the IoT enabled device from the existing wide variety of
technologies and it can send the data, as and when required, to the identifed device. In the pres-
ent market, smart ACs and heaters act according to the requirements of a Wi-Fi system’s user. Old
mobile phones, TVs, and house dustbins are becoming smarter. According to one statistical analy-
sis, IoT enabled devices will reach up to 31 billion in number by 2020. IPv6 is a version of Internet
protocol, which provides identifcation for computers on the network and routes the traffc over the
Internet. IPv6 has 128-bit Hex numbers.

Features: The basic architecture of the IoT comprises sensors, actuators, and their enabling
machine language. Artifcial Intelligence, alongside connectivity, and active engagement,
can be used by small devices.
Artifcial Intelligence: Artifcial Intelligence is mathematically developed man-made
machine intelligence. It was created in order to use the natural environment so that it can
achieve a target. The IoT enabled Artifcial Intelligence to develop a smart algorithm in
order to collect data and communicate with other connected devices through their net-
works. For example, in the smart bin system of a production line, if the material moves
over, then data will be transferred to an ERP system. This will then be followed by an order
received by supplier from ERP, which will refll the intelligent smart bin.
Connectivity: Connectivity is a major issue in most places. Previously, industries had con-
nectivity. At present, XBee, RFID, RX/TX 433MHz, and Wi-Fi are the devices used to
provide network connectivity in order to realize the IoT’s applications.
Sensors: Devices, in the form of sensors/transducers, are required to detect physical param-
eters; these then communicate its data to the destination through an embedded system.
Active Engagement: The IoT is an active engagement with technology that makes a paradigm
shift over today’s passive engagement, which exists in service and product management.
Small Devices: The IoT is a small device that enables and ensures greater precision, scal-
ability, and versatility.

Advantages:
1. In today’s scenarios, the IoT is a part of the personal, as well as the business, life of an
individual.
2. It improves customer engagement with product service.
3. It is optimized in a way to allow the use of technology.
4. It is easy to collect data collection.

3
4 Basics of 8051 Microcontroller and IoT

Disadvantages: Although IoT addresses so many meritorious things, but it has some chal-
lenges as well:
1. Security: Today everyone is aware of the term “cyber security”. This is because most
individuals communicate with each other through virtual networks and this puts them
at risk of hackers.
2. Complexity: When systems and processes become simpler and more user-friendly, the
complexity of developing them increases.
3. Compliance: Any service or technology in business needs to be complying with
regulations.

The IoT is the integration of sensation, communication, and analytical capabilities created within
conventional technologies. The IoT promises to help the automotive industry by directly managing
their existing assets in different places. This allows information from the supply chain and after
sales services, as well as dealers and customers, to be collated in order to help them to understand
and access the data/information as and when required.

1.1.1 IOT IN SOCIETY


Society is becoming smarter by using the IoT ecosystem in day-to-day activities; this has enriched the
lives of human beings. Its application began with the smart dustbin and has progressed to operating the
garage door. In the near future, the IoT could become a multi-trillion dollar industry. CISCO revealed
that the use of IoT enabled devices may increase to 50 billion devices used by 7.6 billion people.

1.2 IOT COMPONENTS


The system used by the IoT comprises of many functional blocks to facilitate various utilities such
as, sensing, identifcation, actuation, communication, and management.
Figure 1.1 shows the various components of IoT devices such as the connectivity, processor,
audio/video interfaces, input-output interfaces, storage interface, memory interface, and graphics.

1.2.1 SENSOR
The sensor is a device that collects information from the environment and converts it into the elec-
trical signals. The type of a sensor depends on the application as well as its availability in the mar-
ket. These can be classifed in two types: analog and digital. The digital sensors are also available

FIGURE 1.1 IoT device components.


Introduction to the IoT 5

as inter IC communication (I2C), two-wire interface (TWI), serial peripheral interface (SPI), three-
wire interface, or one wire interface.

1.2.2 PUSH BUTTON


A push button is used to detect the physical contact; for example, bumpers on a robot can be
equipped with the push buttons. When the button is closed (pressed), it makes a connection between
two terminals and can be used for sending a signal to do a specifc task.

1.2.3 PRESSURE SENSOR


A pressure sensor produces an output proportional to the force that is being applied to it.

1.2.4 ULTRASONIC SENSOR


An ultrasonic sensor is an acoustics sensor that is capable of measuring the time between, when a
signal is sent and when its echo is received. It can be used to measure the distance between an object
and an obstacle. The ultrasonic sensor is an analog sensor with a range of 10–400 cm.

1.2.5 INFRARED SENSOR


An infrared (IR) sensor is capable of detecting an obstacle in the line follower application. The IR
sensor can be used for the collision detection. The module comprises of an IR transmitter and IR
receiver pair, a comparator IC-358, and a potentiometer.

1.2.6 PROXIMITY AND TOUCH SENSOR


This is a digital sensor. The principle of this sensor is to detect a change in capacitance, when a fn-
ger is near the pad. The sensor has two types of output. The frst output is called a proximity output,
which is activated when a fnger is around 2cm above the pad. The other output is a touch output,
which is activated when a fnger is either on the pad or around 1mm above.

1.2.7 LASER SENSOR


A laser sensor can be used in long distance application, which needs a high degree of accuracy.

1.2.8 BUMP SENSOR


This is a digital sensor and can be used for the obstacle detection. It has an onboard performance
limit switch and an anti-debouncing circuit.

1.2.9 METAL SENSOR


This is a digital sensor that gives an active low output when it detects metal. It detects metal objects
from up to 7cm away.

1.2.10 THE FIRE SENSOR


As the name suggests, this is used for detecting fre. The module detects the fre up to a range of
1–2 meters.
6 Basics of 8051 Microcontroller and IoT

1.2.11 THE PASSIVE INFRARED (PIR) SENSOR


This is a digital sensor that is designed to detect the presence of people. It can be operated on +5V
to +9V DC.

1.2.12 POTENTIOMETER
A potentiometer is a simple knob which provides a variable resistance.

1.2.13 ALCOHOL SENSOR


This is used to detect an alcohol on breath or in air. It is highly sensitive with a fast response time.
Its operating temperature range is -10–70°C.

1.2.14 TEMPERATURE SENSOR


An LM35 is a temperature sensor, whose output voltage is linearly proportional to Celsius
(Centigrade). An LM35 is designed to operate for a temperature range of -55–150°C.

1.2.15 GAS SENSOR


This is used to detect gas for liquefed petroleum gas (LPG), isobutane, propane, and liquefed natu-
ral gas (LNG) combustible gases. The detecting range is 100–100,00PPM.

1.2.16 GPS
The Global Positioning System (GPS) is a global navigation satellite system. It uses a constellation
of between 24 and 32 “Medium Earth Orbit” satellites in order to determine location, speed, direc-
tion, and time. GPS can be used for navigation by measuring latitude, longitude, and the altitude of
the system.

1.2.17 GYROSCOPE
A gyroscope measures the rate of an angular acceleration and provides a corresponding signal (ana-
log voltage, serial communication, I2C, and so on).

1.2.18 THERMAL CAMERA


This is used to create a 2-D image of objects. The temperature of an object can also be determined
by using this device.

1.2.19 HUMIDITY SENSOR


The humidity sensor detects the percentage of water in the air and can be used with the temperature
sensor.

1.2.20 MAGNETOMETER
The magnetic sensors or magnetometers can be used to detect the magnets and magnetic felds. This
can be used to fnd out the position of magnets.
Introduction to the IoT 7

1.2.21 COMPASS SENSOR


This is used to determine the earth’s magnetic feld in order to fnd out its orientation with respect
to the magnetic poles.

1.2.22 ACCELEROMETER
An accelerometer measures the linear acceleration. It can measure the static (earth gravity) or
dynamic acceleration in three axes.

1.2.23 LOAD CELL


This is a transducer that converts a force into an electrical signal. The output electrical signal is a
few millivolts and requires amplifcation.

1.2.24 FINGERPRINT SENSOR


This is used to read fngerprints and store the data in its fash memory. The sensor can perform three
functions: Add (Enroll), Empty Database, and Search. It operates on +5V DC. For every fngerprint
it generates a 32-bit data string.

1.2.25 INERTIAL MEASUREMENT UNIT


An inertial measurement unit is a combination of a multi-axis accelerometer and a multi-axis gyro-
scope and, sometimes a multi-axis magnetometer, to provide more accuracy.

1.2.26 CURRENT AND VOLTAGE SENSOR


A current and voltage sensor measures the current and/or voltage of an electric circuit. This can be
useful for gauging how much longer a robot can be operated (i.e., it can measure the voltage from
the battery).

1.2.27 VIBRATION SENSOR


A vibration sensor can be used to detect the vibration of an object by using piezoelectric or other
technologies.

1.2.28 RFID
A RFID module can be interfaced with a microcontroller in UART mode or with an RS232 con-
verter to a PC. A RFID reader module works with 125 KHz RFID tags. It operates on +5V DC
power supply and it has a range of 10cm.

1.3 ACTUATORS
The actuator can be defned as a device, which converts electrical energy into motion. The different
variety of actuators can produce either rotational or linear motions. A DC motor is an example of an
actuator. The selection of the right actuator for a robot requires an understanding of which actuator
is available and suitable; this can be worked out using a bit of math and physics.
8 Basics of 8051 Microcontroller and IoT

1.3.1 ROTATIONAL ACTUATORS


The rotational actuator transforms the electrical energy into a rotating motion. The properties of a
rotational actuator are—the torque (the force an actuator produces at a given distance [unit is N•m])
and its rotational speed (unit in rpm).

1.3.2 AC MOTOR
An AC motor is not used in the robotics. AC motors are useful for industrial purposes with a stationary
arrangement, where very high torque is required; the motors are connected to the mains/wall outlet.

1.3.3 DC MOTOR
The DC motors are available in various shapes and sizes. The most common shape is cylindrical. A
DC motor can operate in both clockwise and anti-clockwise direction by changing the polarity of
the battery. These motors can be purchased from the market; they differ in their operating voltage
(in volt) and speed (rpm).

1.3.4 GEARED DC MOTOR


A geared DC motor is a combination of a gearbox and the DC motor, which can decrease the speed
and increase the torque of a motor. The types of gears are, the spur gear (most commonly used), the
planetary gear(more complex, but it allows the higher gear down to be operated in a more confned
space and with higher effciency), and the worm gear (this allows a very high gear ratio in a single
stage, and also prevents the output shaft from moving, if the motor is not powered).

1.3.5 SERVO MOTOR


The servo motor is a type of the actuator, which rotates to an angular position and can be used in
the remote-controlled vehicles for steering or controlling the fight. It has three wires, GND, Vcc,
and a control input pulse.

1.3.6 INDUSTRIAL SERVO MOTOR


The control of an industrial servo motor is different from a hobby servo motor. It is used in large
machines. An industrial servo motor is made up of an AC motor and, sometimes, a three-phase
motor. It is designed by placing a gear down and an encoder, which provides the feedback about its
angular position and speed. These motors are rarely used for the mobile robots due to their heavy
weight, size, cost, and the complexity. These are more useful in the stationary robots.

1.3.7 STEPPER MOTOR


A stepper motor rotates in the steps with a minimum step angle of 1.8 degree. The number of
degrees the shaft rotates with each step (step size) varies based on several factors. There are two
types of stepper motor: unipolar and bipolar.

1.3.8 LINEAR ACTUATOR


A linear actuator is used where a linear motion (motion along one straight line) is required. It has
three main mechanical characteristics:

1. The minimum and maximum distance that the rod can move, in mm or inches
2. The force in kg or lbs
3. The speed in m/s or inches
Introduction to the IoT 9

1.3.9 DC LINEAR ACTUATOR


A DC linear actuator is made of a DC motor connected to a lead screw. The forced motion of lead
screw either toward or away from the motor, converts the rotating motion into a linear motion. The
DC linear actuators provide linear position feedback by connecting with a linear potentiometer.
These are available in a wide variety of sizes, strokes, and forces.

1.3.10 SOLENOID
A solenoid is designed with a coil wound around a core. When the coil is energized, the core is
pushed away from the magnetic feld and this result in a single directional motion. The multiple
coils, or some other mechanical arrangements, are required to provide a motion in two directions.
This type of the actuator can be used in valves or latching systems where no position feedback is
required.

1.3.11 MUSCLE WIRE


The muscle wire is a special type of wire that can contract when an electric current is moving
through it; once the current is gone, it returns to its original length.

1.3.12 PNEUMATIC AND HYDRAULIC ACTUATORS


In order to produce a linear motion, the pneumatic and hydraulic actuators use air or a liquid such
as water or oil. These types of actuators can have a very long stroke, a high force, and a high speed.

1.4 CLOUD COMPUTING MODEL


Connection and accessibility are crucial in the modern business world, and cloud computing allows
the business to work wherever you like, or whenever you like. However, cloud computing is not
quite as simple as it may seem. Inside of the world of cloud computing, there are three major service
models. By comparing the three different models, you will be better able to determine which cloud
computing service model is right for your business.

Cloud Computing Service Models


The three types of cloud models are Software-as-a-Service, Platform-as-a-Service, and Infrastructure-
as-a-Service. Their basic functions can be summarized in the phrases “Host”, “Build”, and “Consume”.
Each offers a different level of fexibility and control over the product that your business is “buying”.
Each also varies in its relationship to your existing IT infrastructure. Because of the wide variances
between the three, it is important to determine which model will suit your business needs the best.

1.4.1 SOFTWARE AS A SERVICE (SAAS)


The SaaS model allows your business to quickly access cloud-based web applications without com-
mitting to installing new infrastructure. The applications run on the vendor’s cloud, which they, of
course, control and maintain. The applications are available for use with a paid licensed subscrip-
tion, or for free with limited access. SaaS does not require any installations or downloads in your
existing infrastructure, which in turn eliminates the need to install, maintain, and update applica-
tions on each of your computers.
10 Basics of 8051 Microcontroller and IoT

Advantages of SaaS
Affordable
On-premise hardware is not required for this model, which keeps the costs associated low. Small-
scale businesses might fnd this cloud platform particularly appealing.

Accessible Everywhere
Cloud-based applications are accessible everywhere that there is internet access. As such, compa-
nies that require frequent collaboration fnd SaaS platforms useful as their employees can easily
access the programs that they need.

Ready-to-Use
With SaaS, the programs you need are already fully developed and ready to use. The set-up time for
SaaS programs is greatly decreased from the other two types of cloud-based platforms.

Disadvantages of SaaS
Lack of Control
With SaaS, the vendor has control over the programs that your company is using. If you do not feel
comfortable releasing the control of your critical business applications to another party, perhaps
SaaS is not the best option for your business.

Slower Speeds
Relying upon internet access to function, SaaS applications tend to be slower than client/server
applications. However, these programs are still typically quick, though not instantaneous.

Variable Functions and Features


In many cases, SaaS cloud-based applications have less functionality and features than their client/
server counterparts. This disadvantage, however, may be void if your business only needs the fea-
tures offered in the SaaS version to function.

1.4.2 PLATFORM AS A SERVICE (PAAS)


With this model, a third-party vendor provides your business with a platform upon which your busi-
ness can develop and run applications.
Because the vendor is hosting the cloud infrastructure, which supports the platform, PaaS elim-
inates your need to install in-house hardware or software. Your business would not manage or
control the underlying cloud infrastructure, but you would maintain control over the deployed appli-
cations (unlike with SaaS).

Advantages of PaaS
Rapid Time-to-Market
PaaS simplifes application management by eliminating the need to maintain and control the under-
lying infrastructure. As a result, applications can be developed and deployed faster.

Cost-Effective Development
A cloud-based platform provides your business with a base upon which to build your applications,
as opposed to building from nothing, thus dramatically reducing the costs associated.

Scalability
Cloud-based platforms offer reusable code, which, of course, makes it easier to develop and deploy
applications, but also offers increased scalability.
Introduction to the IoT 11

Disadvantages of PaaS
Vendor Lock-In
It is diffcult to migrate many of the services provided by one PaaS product to a competing product,
thus making it hard to switch PaaS vendors. Downtime and additional expenses are likely to occur
when switching from one PaaS provider to another.

Security and Compliance


In the PaaS model, the vendor will store most, or even all, of the application’s data. As such, it is
imperative to assess the security measures of the provider. This, though, often proves diffcult as the
vendor may be storing their databases via a third party, thus leaving you uninformed of the safety
of your data.

Lack of Compatibility
It is possible that your current infrastructure may not be compatible with a cloud platform. If some
elements cannot be cloud-enabled, you may have to switch from your current apps and programs to
cloud-compatible counterparts in order to fully integrate. Alternately, you may need to leave these
elements out of the cloud, and within your current infrastructure.

1.4.3 INFRASTRUCTURE AS A SERVICE (IAAS)


IaaS, as the most fexible of the cloud models, allows your business to have complete, scalable con-
trol over the management and customization of your infrastructure.
In the IaaS model, the cloud provider hosts your infrastructure components that would tradition-
ally be present in an onsite data center (such as servers, storage, and networking hardware). Your
business, however, would maintain control over operating systems, storage, deployed applications,
and possibly limited control of select networking components (e.g., host frewalls).

Advantages of IaaS
Eliminates Capital Expenses
Employing a cloud-based infrastructure eliminates the capital expense of deploying in-house hard-
ware and software. Additionally, IaaS typically is offered as a pay-as-you-go model, with charges
based either in time, or in the amount of virtual machine space that was used.

Supports Flexibility
IaaS is useful in supporting workloads that are temporary, may change unexpectedly, or are experi-
mental. Like all workloads, these loads need infrastructure to support them; however, it is expensive
to commit to additional permanent in-house infrastructure for a temporary need. Cloud-based infra-
structure answers the need for fexibility.

Simple Deployment
It is much easier for your cloud provider to deploy your servers, processing, storage, and network-
ing in the IaaS model, than it is for you to deploy these elements in-house, with no previous base to
build. As a result, your uptime will increase as your systems will be available for use more rapidly.

Disadvantages of IaaS
Insight
Because your entire infrastructure is maintained and controlled by your IaaS provider, it is rare that
you will be provided with the details of its confguration and performance. In turn, this can make
systems management and monitoring more diffcult for your company.
12 Basics of 8051 Microcontroller and IoT

Variability of Resilience
The availability and performance of the workload is highly dependent upon the provider. If the IaaS
providers experience internal or external downtime, your workloads will also be affected.

Costly
IaaS models are typically much more costly than PaaS and SaaS models because they offer much
more support to your business than the other two cloud models. However, they can still be cost-
effective based on their utility to your business.

1.5 ZIGBEE
Zigbee is an IEEE 802.15.4-based specifcation for a suite of high-level communication protocols
used to create personal area networks with small, low-power digital radios, such as for home auto-
mation, medical device data collection, and other low-power low-bandwidth needs, designed for
small scale projects, which need wireless connection. Hence, Zigbee is a low-power, low data rate,
and close proximity (i.e., personal area) wireless ad hoc network.
The technology defned by the Zigbee specifcation is intended to be simpler and less expensive
than other wireless personal area networks (WPANs), such as Bluetooth or more general wireless
networking such as Wi-Fi. Applications include wireless light switches, home energy monitors,
traffc management systems, and other consumer and industrial equipment that require short-range
low-rate wireless data transfer.
Its low-power consumption limits transmission distances to 10–100 meters line-of-sight, depend-
ing on power output and environmental characteristics. Zigbee devices can transmit data over long
distances by passing data through a mesh network of intermediate devices to reach more distant ones.
Zigbee is typically used in low-data rate applications that require long battery life and secure network-
ing (Zigbee networks are secured by 128-bit symmetric encryption keys.) Zigbee has a defned rate of
250 kbit/s, best suited for intermittent data transmissions from a sensor or input device.
Zigbee is a low-cost, low-power, wireless mesh network standard targeted at battery-powered
devices in wireless control and monitoring applications. Zigbee delivers low-latency communica-
tion. Zigbee chips are typically integrated with radios and with microcontrollers. Zigbee operates in
the industrial, scientifc, and medical (ISM) radio bands: 2.4 GHz in most jurisdictions worldwide;
though some devices also use 784 MHz in China, 868 MHz in Europe, and 915 MHz in the US and
Australia; however, even those regions and countries still use 2.4 GHz for most commercial Zigbee
devices for home use. Data rates vary from 20 kbit/s (868 MHz band) to 250 kbit/s (2.4 GHz band).
Zigbee builds on the physical layer and media access control defned in IEEE standard 802.15.4
for low-rate WPANs. The specifcation includes four additional key components: network layer,
application layer, Zigbee Device Objects (ZDOs), and manufacturer-defned application objects.
ZDOs are responsible for some tasks, including keeping track of device roles, managing requests to
join a network, as well as device discovery and security.
The Zigbee network layer natively supports both star and tree networks, and generic mesh net-
working. Every network must have one coordinator device. Within star networks, the coordinator
must be the central node. Both trees and meshes allow the use of Zigbee routers to extend commu-
nication at the network level. Another defning feature of Zigbee is facilities for carrying out secure
communications, protecting establishment and transport of cryptographic keys, ciphering frames,
and controlling device. It builds on the basic security framework defned in IEEE 802.15.4.

1.6 BLE
Bluetooth Low Energy (Bluetooth LE, colloquially BLE, formerly marketed as Bluetooth Smart[1])
is a WPAN technology designed and marketed by the Bluetooth Special Interest Group (Bluetooth
SIG) aimed at novel applications in the healthcare, ftness, beacons, security, and home entertainment
Introduction to the IoT 13

industries. It is independent of Bluetooth BR/EDR and has no compatibility, but BR/EDR and LE
can coexist. The original specifcation was developed by Nokia in 2006 under the name Wibree,
which was integrated into Bluetooth 4.0 in December 2009 as BLE.
Compared to Classic Bluetooth, BLE is intended to provide considerably reduced power con-
sumption and cost while maintaining a similar communication range. Mobile operating systems
including iOS, Android, Windows Phone, and BlackBerry, as well as macOS, Linux, Windows 8,
and Windows 10, natively support BLE.
In 2011, the Bluetooth SIG announced the Bluetooth Smart logo so as to clarify compatibility
between the new low energy devices and other Bluetooth devices.

1.7 6LOWPAN
6LoWPAN is an acronym of IPv6 over Low-Power WPANs. 6LoWPAN is the name of a concluded
working group in the Internet area of the IETF.
The 6LoWPAN concept originated from the idea that “the Internet Protocol could and should be
applied even to the smallest devices”, and that low-power devices with limited processing capabili-
ties should be able to participate in the IoT.
The 6LoWPAN group has defned encapsulation and header compression mechanisms that allow
IPv6 packets to be sent and received over IEEE 802.15.4 based networks. IPv4 and IPv6 are the
work horses for data delivery for local-area networks, metropolitan area networks, and wide-area
networks such as the Internet. Likewise, IEEE 802.15.4 devices provide sensing communication-
ability in the wireless domain. The inherent natures of the two networks though, are different.
The base specifcation developed by the 6LoWPAN IETF group is RFC 4944 (updated by RFC
6282 with header compression and by RFC 6775 with neighbor discovery optimizations). The prob-
lem statement document is RFC 4919. IPv6 over BLE is defned in RFC 7668.
As with all link-layer mappings of IP, RFC4944 provides a number of functions. Beyond the usual
differences between L2 and L3 networks, mapping from the IPv6 network to the IEEE 802.15.4 net-
work poses additional design challenges (see RFC 4919 for an overview).

1.7.1 FUNCTIONS
Adapting the Packet Sizes of the Two Networks
IPv6 requires the maximum transmission unit (MTU) to be at least 1280 octets. In contrast, IEEE
802.15.4’s standard packet size is 127 octets. A maximum frame overhead of 25 octets spares 102
octets at the media access control layer. An optional but highly recommended security feature at the
link layer poses an additional overhead. For example, 21 octets are consumed for AES-CCM-128
leaving only 81 octets for upper layers.

Address Resolution
IPv6 nodes are assigned 128 bit IP addresses in a hierarchical manner, through an arbitrary length
network prefx. IEEE 802.15.4 devices may use either of IEEE 64 bit extended addresses or, after an
association event, 16 bit addresses that are unique within a PAN. There is also a PAN-ID for a group
of physically collocated IEEE 802.15.4 devices.

Differing Device Designs


IEEE 802.15.4 devices are intentionally constrained in form factor to reduce costs (allowing for
large-scale network of many devices), reduce power consumption (allowing battery powered
devices), and allow fexibility of installation (e.g., small devices for body-worn networks). On the
other hand, wired nodes in the IP domain are not constrained in this way; they can be larger and
make use of mains power supplies.
14 Basics of 8051 Microcontroller and IoT

Differing Focus on Parameter Optimization


IPv6 nodes are geared toward attaining high speeds. Algorithms and protocols implemented at the
higher layers such as TCP kernel of the TCP/IP are optimized to handle typical network problems
such as congestion. In IEEE 802.15.4-compliant devices, energy conservation and code-size opti-
mization remain at the top of the agenda.

Adaptation Layer for Interoperability and Packet Formats


An adaptation mechanism to allow interoperability between IPv6 domain and the IEEE 802.15.4
can best be viewed as a layer problem. Identifying the functionality of this layer and defning newer
packet formats, if needed, is an enticing research area. RFC 4944 proposes an adaptation layer to
allow the transmission of IPv6 datagrams over IEEE 802.15.4 networks.

Addressing Management Mechanisms


The management of addresses for devices that communicate across the two dissimilar domains of
IPv6 and IEEE 802.15.4 is cumbersome, if not exhaustingly complex.

Routing Considerations and Protocols for Mesh Topologies in 6LoWPAN


Routing per se is a two-phased problem that is being considered for low-power IP networking:
Mesh routing in the personal area network (PAN) space.

The Routability of Packets between the IPv6 Domain and the PAN Domain
Several routing protocols have been proposed by the 6LoWPAN community such as LOAD,
DYMO-LOW, and HI-LOW. However, only two routing protocols are currently legitimate for large-
scale deployments: LOADng standardized by the ITU under the recommendation ITU-T G.9903
and RPL standardized by the IETF ROLL working group.

Device and Service Discovery


Since IP-enabled devices may require the formation of ad hoc networks, the current state of neigh-
boring devices and the services hosted by such devices will need to be known. IPv6 neighbor dis-
covery extension is an internet draft proposed as a contribution in this area.

Security
IEEE 802.15.4 nodes can operate in either secure mode or non-secure mode. Two security modes
are defned in the specifcation in order to achieve different security objectives: Access Control List
(ACL) and Secure mode.
2 Meet 8051 and Keil
Compiler—A Software
Development Environment
This chapter discusses the basics of microcontroller 8051, with the help of block diagram and pin
description of 8051. It also introduces Keil compiler to program the 8051.

2.1 INTRODUCTION TO 8051


8051 microcontroller was designed by Intel in 1981. It is an 8-bit microcontroller. It is built with
40 pins, 4kb of ROM storage and 128 bytes of RAM storage, two 16-bit timers. It consists of four
parallel 8-bit ports, which are programmable as well as addressable as per the requirement. An
on-chip crystal oscillator is integrated in the microcontroller having crystal frequency of 12MHz or
11.0592MHz. In the following diagram, the system bus connects all the support devices to the CPU.
The system bus consists of an 8-bit data bus, a 16-bit address bus, and bus control signals. All other
devices like program memory, ports, data memory, serial interface, interrupt control, timers, and the
CPU are all interfaced together through the system bus. Figure 2.1 shows the block diagram of 8051.
The pin diagram of 8051 microcontroller is shown in Figure 2.2.

2.2 KEIL COMPILER—A SOFTWARE DEVELOPMENT ENVIRONMENT


Keil Software makes C compilers, macro assemblers, real-time kernels, debuggers, simulators, inte-
grated environments, and evaluation boards for the 8051, 251, ARM, and XC16x/Cl6x/ST10 micro-
controller families. Keil doesn’t make a compiler for every different microcontroller architecture. It
concentrates on just a few chip families, which can support different architecture very well. That’s
why the most of Keil’s customers will agree that they have the best development tools for ARM,
XC1 6x/C1 6x/ST1 0, 251, and 8051 microcontroller families. Since they have fewer product lines
to support, Keil can dedicate more time to better supporting each tool chain.

2.2.1 UVISION3 IDE


uVision3, the new IDE from Keil Software, combines project management, make facilities, source
code editing, program debugging, and complete simulation in one powerful environment, pVi-
sion3 helps to get programs working faster than ever while providing an easy-to-use development
platform. The editor and debugger are integrated into a single application and provide a seamless
embedded project development environment.
uVision3 provides unique features like the following.
The Device Database, which automatically sets the assembler, compiler, and linker options for
the chip you select. This prevents you from wasting your time confguring the tools and helps you
get started writing code faster.
A robust Project Manager who lets you to create several different confgurations of your tar-
get from a single project fle. Only the Keil pVision3 IDE allows you to create an output fle for
simulating, an output fle for debugging with an emulator, and an output fle for programming an
EPROM—all from the same project fle.

15
16 Basics of 8051 Microcontroller and IoT

External
Interrupts
On-Chip
INT0 INT1
ROM
(for

Counter Inputs
Interrupt Program On-Chip ETC
Control RAM T1
code) Timer 0
Timer 1
T0

CPU

Bus Serial
OSC 4 I/O Ports
Control Port

30 PF 30 PF
TXD RXD
Address/Data
4 to 30 MHz

FIGURE 2.1 Block diagram of 8015 microcontroller.

P1.0 1 40 v˜
P1.1 2 39 P0.0 (AD0)
P1.2 3 38 P0.1 (AD1)
P1.3 4 8051 37 P0.2 (AD2)
P1.4 5 36 P0.3 (AD3)
P1.5 6 35 P0.4 (AD4)
P1.6 7 34 P0.5 (AD5)
P1.7 8 33 P0.6 (AD6)
RST 9 32 P0.7 (AD7)
(RXD) P3.0 10 31 EA/VPP
(TXD) P3.1 11 30 ALE/PROG
(INT0) P3.2 12 29 PSEN
(INT1) P3.3 13 28 P2.7 (A15)
(T0) P3.4 14 27 P2.6 (A14)
(T1) P3.5 15 26 P2.5 (A13)
(WR) P3.6 16 25 P2.4 (A12)
(RD) P3.7 17 24 P2.3 (A11)
XTAL2 18 23 P2.2 (A10)
XTAL1 19 22 P2.1 (A9)
GND 20 21 P2.0 (A8)

FIGURE 2.2 8051 pin diagram.


Meet 8051 and Keil Compiler 17

FIGURE 2.3 View of Keil compiler.

It has an integrated make facility with automatic dependency generation. You don’t have to fg-
ure out which header fles and include fles are used by which source fles. The Keil compilers and
assemblers do that automatically.
As your project compiles, errors and warnings appear in an output window. You may make cor-
rections to the fles in your project while pVision3 continues to compile in the background. Line
numbers associated with each error or warning are automatically resynchronized when you make
changes to the source. Figure 2.3 shows the Keil compiler view.

2.2.2 UVISION DEBUGGER


The uVision Debugger from Keil supports simulation using only your PC or laptop, and debugging
using your target system and a debugger interface. uVision includes traditional features like simple
and complex breakpoints, watch windows, and execution control as well as sophisticated features
like trace capture, execution profler, code coverage, and logic analyzer. The uVision Debugger
provides a number of ways to display variables and program objects. Source Code Windows display
your high-level language and assembly program source code. The Disassembly Window shows
mixed high-level language and assembly code. The Registers Tab of the Project Workspace shows
system registers. The Symbol Window hierarchy displays program symbols in your application.
The Output Window displays the output of various debugger commands. The Memory Window
displays up to four regions of code or data memory. The Watch Window displays local variables,
user-defned watch expression lists, and the call stack.

2.2.3 EXECUTION
uVision offers many ways by which you can control and manipulate program execution.

Reset—It is possible to debug reset conditions using the uVision simulator.


Run/Stop—Buttons and Commands make starting and stopping program execution easy.
Single-Stepping—uVision supports various methods of single-stepping through your target
program.
18 Basics of 8051 Microcontroller and IoT

Execution Trace—Execution trace information for each executed instruction is stored by


uVision.
Breakpoints—Both simple and complex breakpoints are supported by the uVision Debugger.

Advanced analysis tools are available to help you test and debug your embedded applications.
Code Coverage helps you determine how much of your program has been tested. The Performance
Analyzer shows how functions and code blocks in your program perform. The Execution Profler
shows execution counts and time for each line of code or instruction. The Logic Analyzer shows
how various signals and variables in your program change over time.
Simulation capabilities make it possible to test the target system without target hardware.
Instruction Simulation simulates the exact effects and timing of each IVICU instruction. Interrupt
Simulation simulates the cause and effect of a system or peripheral interrupt. Peripheral Simulation
simulates the effects of on-chip peripherals including special function registers. Debugger Functions
allow you to expand the command scope of the debugger and create and respond to stimuli. Toolbox
Buttons are a convenient way for you to connect debugger function buttons on the user-interface.
3 Introduction to NuttyFi
Board and Its Programming
Nuttify is an Internet of Things platform with on board ESP122 8266 series. This chapter discusses
the NuttyFi board, its specifcation, and the programming steps for the same.

3.1 INTRODUCTION TO NUTTYFI BOARD


NUTTYFI is an Internet of Things (IoT) hardware platform based on ESP12e 8266 series that
enables user to build IoT products, Research Analysis Systems, Automation, and Projects.
Using NUTTIFY, users can monitor, manage, control, and search devices from any part of the
world. It can also interface with any IoT web server, icloud, Local or IoT mobile platform or applica-
tion easily. A number of services like open source web servers and mobile apps are available on the
internet that are freeware to use.
In order to make easier for developers, NUTTYFI appears to be a complete and best solution for
IoT Products & Projects. NUTTYFI IoT hardware has such ability that the user can upload their
code as per their requirements. Using NUTTYFI, you will be capable to control your application,
devices, actuators, mechanism as well as collect data from IoT devices securely. Figure 3.1 shows
the front view of NuttyFi, while Figure 3.2 shows the back view of NuttyFi with pins.

3.1.1 PIN DESCRIPTION


1. 8 digital pins: From D0 to D7
2. 1 Analog pin-A0
3. Vinput: From 5V to 21V
4. 3.3V output pin
5. UART Pins to Flash program to NUTTYFI Cloud Device using FTDI UART Bridge.

3.1.2 SPECIFICATION OF NUTTYFI


• Voltage: Input from 5V to 21V DC
• Wi-Fi Direct (P2P), soft-AP
• Current consumption: 10uA∼170mA
• Flash memory attachable: 16MB max. (512K normal)
• Integrated TCP/IP protocol stack
• Processor: Tensilica L106, 32-bit
• Processor speed: 80∼160MHz
• RAM: 32K + 80K
• GPIOs: 17 (multiplexed with other functions)
• Analog to Digital: 1 input with 1024 step resolution
• +19.5dBm output power in 802.11b mode
• 802.11 support: b/g/n
• Maximum concurrent TCP connections: 5.

19
20 Basics of 8051 Microcontroller and IoT

FIGURE 3.1 Front view of NuttyFi board.

3.2 PROGRAMMING STEPS TO NUTTYFI/ESP8266


3.2.1 INSTALLING NUTTYFI IN TO ARDUINO IDE
The most basic way to use the ESP8266 module is to use serial commands, as the chip is basically
a Wi-Fi/Serial transceiver.
Follow the steps:
1. Open the Arduino IDE. Arduino IDE version must be 1.6.4 or greater.
2. Note that your Laptop/Computer must be connected with Internet to install NUTTYFI in
Arduino IDE.
3. Click on File and then Click on Preferences, as shown in Figure 3.3.
4. Preference window will appear, copy and paste link to Additional Boards Manager,
Figure 3.4. URL is as follows:
http://arduino.esp8266.com/stable/package_esp8266com_index.json

FIGURE 3.2 Back view of NuttyFi with pins.


Introduction to NuttyFi Board 21

FIGURE 3.3 Select “Preferences”.

FIGURE 3.4 URL to the additional boards manager.


22 Basics of 8051 Microcontroller and IoT

FIGURE 3.5 Select boards manager.

5.
Click on “OK” button.
6.
Now, Click on Tools on Arduino IDE, then Boards> Boards Manager, Figure 3.5.
7.
Now, list of all boards appears in boards manager as shown in Figure 3.6.
8.
Scroll to bottom of the list of boards manager, you will see the “esp8266 by esp8266
community version”, install it, Figure 3.7.
Once installation completed, close and reopen Arduino IDE for ESP8266 library.
9. Now, go to Tools > Board > ESP8266 Modules and you can see many option for ESP8266.
For NuttyFi, it is recommended to select “NodeMCU 1.0 (ESP-12E Module)”, as shown in
Figure 3.8.
10. Now NUTTYFI Wi-Fi Board is ready to use.
11. Next, select the port. If you can’t recognize your port, go to the Control Panel > System >
Device Manager > Port and update your USB driver.
Introduction to NuttyFi Board 23

FIGURE 3.6 List of boards.

FIGURE 3.7 Installing ESP8266.


24 Basics of 8051 Microcontroller and IoT

FIGURE 3.8 Select “NodeMCU1.0 (ESP-12E Module)”.


4 Introduction to
Customized Board with
8051 Microcontroller
and NuttyFi/ESP8266
This chapter discusses the development of a customized board with 8051 and NuttyFi for Internet of
things (IoT) applications. The brief description of each component is discussed.

4.1 INTRODUCTION TO CUSTOMIZED BOARD WITH


8051 MICROCONTROLLER AND NUTTYFI/ESP8266
The customized board comprises of 89S51 microcontroller, reset circuitry, crystal circuitry with
11.0592MHz frequency crystal, ADC 0804, MUX 74HC4051, and NuttyFi board/ESP8266. Figure 4.1
shows the schematics of customized board.
The description of the components is as follows:

4.1.1 ADC 0804 (ADC OR ANALOG-TO-DIGITAL CONVERSION)


ADC is used to convert analog signals to digital data. ADC 0804 is a chip designed to convert ana-
log signal into 8-bit digital data. This chip is one of the popular series of ADC, Figure 4.2.
This chip is specially designed for getting digital data for processing units from analog sources.
It is an 8-bit conversion unit, with a measuring voltage of maximum value 5V. It can sense minimum
4.8mV.
The input analog signal has a limit to its value. This limit is determined by reference value and
chip supply voltage. The measuring voltage cannot be greater than reference voltage and chip supply
voltage. If the limit is crossed, Vin > Vref, the chip gets faulted permanently.
Pin 9 is Vref/2 that means say we want to measure an analog parameter with a maximum value
of 5V. If we need Vref as 5V for that we need to provide a voltage of 2.5V (5V/2) at the Pin 9.
For 2.5V a voltage divider is used, with same value of resistor at both ends they share voltage
equally. Each resistor holds a drop of 2.5V with a supply voltage of 5V. The drop from the later
resistor is taken as a Vref.
The chip works on RC (Resistor Capacitor) oscillator clock. The important point is the capacitor
can be changed to a lower value for higher rate of ADC conversion. However, with speed there will
be a decrease in accuracy.
So, if the application requires higher accuracy choose the capacitor with higher value. For higher
speed, choose lower value capacitor. On 5V ref., if an analog voltage of 2.3V is given for ADC con-
version we will have 2.3*(1024/5) = 471. This will be the digital output of ADC 0804.

4.1.2 MUX 74HC4051


The 74HC4051, 74HCT4051 is a single-pole octal-throw analog switch (SP8T) suitable for use in
analog or digital 8:1 multiplexer/de-multiplexer applications. The switch features three digital select

25
26 Basics of 8051 Microcontroller and IoT

FIGURE 4.1 Schematics of customized with 8051 and NuttyFi board.

CS 1 20 VCC (OR VREF)

RD 2 19 CLK R

WR 3 18 DB0 (LSB)

CLK IN 4 17 DB1

INTR 5 16 DB2

VIN(+) 6 15 DB3

VIN(–) 7 14 DB4

A GND 8 13 DB5

VREF/2 9 12 DB6

D GND 10 11 DB7 (MSB)

FIGURE 4.2 View of 0804.


Introduction to Customized Board 27

4051

X4 1 16 VCC

X6 2 15 X2

COMMON 3 14 X1

X7 4 13 X0

X5 5 12 X3

INHIBIT 6 11 A

VEE 7 10 B

VSS 8 9 C

FIGURE 4.3 View of 4051.

inputs (A, B, and C), eight independent inputs/outputs (Xn), a common input/output (Z), and a digi-
tal enable input (E). When E is HIGH, the switches are turned off. Inputs include clamp diodes. This
enables the use of current limiting resistors to interface inputs to voltages in excess of Vcc.
Pins 9, 10, and 11 are the three digital inputs that select which way the switch will be active.
Depending on the value of these three bits, the common pin will be connected to any one of the vari-
ous X pins. A is the least-signifcant bit, and C is the most-signifcant. So when A=B=C=0, common
is connected to X0 and when A=B=1 and C=0, common is connected to X3.
The power supply for this chip is a little bit complicated, because it aims to support both negative
and positive (analog) voltages through the switch. In “normal” use, you’d connect Vcc to +5V, VSS
to -5V, and VEE to 0V, or the signal ground. What’s important here is that the Vcc to VSS voltages
span the input range, and VEE to Vcc is in the relevant range for our logic signals. Since we’re pass-
ing single-sided (0V to 5V or 9V) square waves through the feedback path, and using the same volt-
age range for our logic signaling, we can connect both VSS and VEE to ground. Figure 4.3 shows
the view of 4051.

4.1.3 NUTTYFI
NUTTYFI is an IoT hardware platform based on ESP8266 12e series that enables user to build IoT
products, research analysis systems, automation, and projects. Using NUTTIFY, users can monitor,
manage, control, and search devices from any part of the world. It can also interface with any IoT
web servers, icloud, Local or IoT mobile platform, or application easily. Number of services open
source web servers and mobile apps are available in the Internet that is freeware to use.
It has 8 digital pins, (as shown in Figure 4.4) 1 Analog pin-A0, Vinput—From 5V to 21V, 3.3V
output pin, UART Pins to fash program to NUTTYFI Cloud Device using FTDI UART Bridge.

4.1.4 RESET CIRCUIT


RESET is an active High input when RESET is set to High, 8051 goes back to the power on state.
The 8051 is reset by holding the RST high for at least two machine cycles and then returning it low,
as shown in Figure 4.5.
28 Basics of 8051 Microcontroller and IoT

FIGURE 4.4 NuttyFi.

There are two methods of reset circuit:

1. Power On Reset
Initially charging of capacitor makes RST High.
When capacitor charges fully it blocks DC.
2. Manual Reset
Closing the switch momentarily will make RST High.

4.1.5 CRYSTAL CIRCUIT


A crystal oscillator is an electronic oscillator circuit, which is used for the mechanical resonance
of a vibrating crystal of piezoelectric material. It will create an electrical signal with a given fre-
quency. This frequency is commonly used to keep track of time for example: wrist watches are used
in digital integrated circuits to provide a stable clock signal and also used to stabilize frequencies
for radio transmitters and receivers. Quartz crystal is mainly used in radio-frequency (RF) oscil-
lators. Quartz crystal is the most common type of piezoelectric resonator; in oscillator circuits we

FIGURE 4.5 Reset circuit.


Introduction to Customized Board 29

FIGURE 4.6 Crystal circuit.

are using them so it became known as crystal oscillators. Crystal oscillators must be designed to
provide a load capacitance, as shown in Figure 4.6.

4.1.6 LCD CIRCUITRY


“Liquid Crystal Display” (LCD) could be a sort of show technology that produces use of liquid
crystals that open or shut by an electrical current. These liquid crystals square measure the idea
for alphanumeric display technology. The liquid crystals square measure manufactured from com-
plicated molecules. Similar to water, they alter their state from solid to liquid, reckoning on the
temperature to that they’re exposed. Once in an exceedingly liquid state, the molecules move around
however square measure possible to create a line in an exceedingly bound direction, permitting
them to replicate light-weight. Crystals square measure organized in an exceedingly matrix with
teams of three crystals of the colors red, inexperienced, and blue, forming a section referred to as a
picture element. LCD has the distinct advantage of getting low-power consumption than the semi-
conductor diode, as shown in Figure 4.7.

FIGURE 4.7 LCD interfacing with 8051.


30 Basics of 8051 Microcontroller and IoT

The connections of LCD with 8051 are as follow:

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the 8051 board and also connect to other LCD to power
up.
3. For programming, user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
4. Pins 1,16 of LCD are connected to GND of Power Supply.
5. Pins 2,15 of LCD are connected to +Vcc of Power Supply.
6. Two Fixed pins of POT are connected to +5V and GND of LCD and Variable lag of POT
is connected to pin 3 of LCD.
7. RS, RW, and E pins of LCD are connected to pins P30, P31, and P32 of the 8051 board,
respectively.
8. D4, D5, D6, and D7 pins of LCD are connected to pins P34, P35, P36, and P37 of the 8051
board, respectively.
Section B
Interfacing of 8051 Microcontroller and
NuttyFi/ESP8266 with I/O devices
5 Interfacing of 8051 and
NuttyFi/ESP8266 with LED
Light Emitting Diode (LED) is semiconductor source of illumination that emits light once current
start fowing through it. Once an acceptable current is applied to the leads, negatrons area unit able
to combine with electron holes and emits energy within the photons. The color of emitted energy is
decided by the band gap of the semiconductor.
Most of the LEDs are made of a spread of inorganic materials of semiconductor. Table 5.1 shows
the colors with different wavelength and material.
The V-I characteristic of a LED is analogous to a diode, which implies that a tiny low amend-
ment in voltage will cause an outsized amendment in current. When applied voltage across the
LED exceeds the forward fall by a low quantity, the present rating is also exceeded by an outsized
quantity, which may damage the LED. The standard resolution is to use constant-current power
to stay the LED below the most current rating. Figure 5.1 shows the LED circuit for limiting the
current.
Figure 5.2 shows the block diagram of the system for LED interfacing. The system comprises
of a +12V power supply, a +12V to +5V converter, a NuttyFi/8051 microcontroller, a resistor 330
Ohm, and the LEDs. The objective of the system is to blink the LEDs using ESP8266/NuttyFi/8051
microcontroller. Table 5.2 shows the list of components used to design the system.

5.1 CIRCUIT DIAGRAM


Description of interfacing of LED with ESP 8266
The interfacing of the devices is as follows:

1. Connect output of +12V power supply to input of +12V to +5V convertor.


2. Connect output of +5V power supply to +5V of the NuttyFi board.
3. Connect anode terminals of LEDs through 330 Ohm resistors to D1, D2 pins of the NuttyFi,
respectively.
4. Cathode terminals of LEDs are connected to the ground.
5. Connect FTDI programmer to FTDI connector of the NuttyFi board.

Figure 5.3 shows the circuit diagram of the system.


Description of interfacing of 8051 with LED with 8051
The interfacing of the devices is as follows:

1. Connect output of +12V power supply to input of +12V to +5V convertor.


2. Connect output of +5V power supply to +5V of the 8051 board.
3. Connect anode terminals of LEDs through 330 Ohm resistors to P2.0, P2.1 pins of the
8051, respectively.
4. Cathode terminals of LEDs are connected to the ground.

Figure 5.4 shows the circuit diagram of the system.

33
34 Interfacing with I/O devices

TABLE 5.1
LED Color with Respect to Semiconductor Material
Color Semiconductor Material Wavelength [nm] Voltage Drop [ΔV]
Infrared Gallium arsenide (GaAs) λ > 760 ΔV < 1.63
Aluminum gallium arsenide (AlGaAs)
Red Aluminum gallium arsenide (AlGaAs) 610 < λ < 760 1.63 < ΔV < 2.03
Gallium arsenide phosphide (GaAsP) phosphide (AlGaInP)
Orange Gallium arsenide phosphide (GaAsP) 590 < λ < 610 2.03 < ΔV < 2.10
Aluminum gallium indium phosphide (AlGaInP)
Yellow Gallium arsenide phosphide (GaAsP) 570 < λ < 590 2.10 < ΔV < 2.18
Aluminum gallium indium phosphide (AlGaInP)
Green Aluminum gallium indium phosphide (AlGaInP) 500 < λ < 570 1.9[93] < ΔV < 4.0
Aluminum gallium phosphide (AlGaP)
Indium gallium nitride (InGaN)
Blue Zinc selenide (ZnSe) 450 < λ < 500 2.48 < ΔV < 3.7
Indium gallium nitride (InGaN)
Synthetic sapphire, Silicon carbide (SiC)
Violet Indium gallium nitride (InGaN) 400 < λ < 450 2.76 < ΔV < 4.0
Ultraviolet Indium gallium nitride (InGaN) (385–400 nm) Λ < 400 3 < ΔV < 4.1
Diamond (235 nm)
Boron nitride (215 nm)
Aluminum gallium nitride (AlGaN)
Purple Dual blue/red LEDs, blue with red phosphor, or white with Multiple types 2.48 < ΔV < 3.7
purple plastic
White Cool/Pure White: Blue/UV diode with yellow phosphor Broad spectrum 2.8 < ΔV < 4.2
Warm White: Blue diode with orange phosphor

I
Anode

Cathode

+
V R

FIGURE 5.1 LED circuit for current limiting.


Interfacing with LED 35

FIGURE 5.2 Block diagram of the interfacing of LED with ESP8266 and 8051.

TABLE 5.2
List of the Components Used to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface LED with ESP8266


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 Output- +5V/1A
3 NuttyFi 01 Analog pin-1
Digital pin-10
FTDI connector for programmer
4 LED breakout board with 330 Ohm 01 RED color LED as indicator
resistor

Components Used to Interface LED with 8051


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 330 Ohm resistor 03 0.25 Watt
4 LED 03 RED
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit

R2
D1
10k
R1
D2
10k

D1 D2
GND GND LED-GREEN LED-RED
+5V
Vinput 3.3V
3.3V
Vcc NuttyFl-Cloud Device

D4
D7 U1
D3
D2 D6 1
VI
7805VO 3
+5V
D1 D5
GND

+12V
Rx D0 330
A0 A0
2

Tx C1
1000uF D1
LED-RED
GND

GND +12V to +5V converter


Rx

GND
-
Tx
-

D1
+12V Power Supply
+50V
~ 1N4007
1000uF LED- yellow RESISTOR
2

AC 330E
GND

7812
1 3
VI VO
9-0-9 REGULATOR 7812

FIGURE 5.3 Circuit diagram for the interfacing of LED to NuttyFi.


36 Interfacing with I/O devices

5.2 PROGRAM
// Program to interface two LEDs with ESP8266/NuttyFi
int LED1_pin=D1; // assign variable to LED1
int LED2_pin=D2; // assign variable to LED1
void setup()
{
pinMode(LED1_ pin, OUTPUT); // Assign pin1 to output pin
pinMode(LED2_pin, OUTPUT); // Assign pin2 to output pin
}
void loop()
{
digitalWrite(LED1_pin, HIGH); // make D1 pin HIGH
digitalWrite(LED2_pin, HIGH); // make D2 pin HIGH
delay(1000); // wait for 1 sec
digitalWrite(LED1_pin, HIGH); // make D1 pin HIGH
digitalWrite(LED2_pin, LOW); // make D2 pin LOW
delay(1000); // wait for 1 sec
digitalWrite(LED1_pin, LOW); // make D1 pin LOW
digitalWrite(LED2_pin, HIGH); // make D2 pin HIGH
delay(1000); // wait for 1 sec
digitalWrite(LED1_pin, LOW); // make D1 pin LOW
digitalWrite(LED2_pin, LOW); // // make D2 pin LOW
delay(1000); // wait for 1 sec
}

// Program to interface two LEDs with 8051


#include <REG52.h> // initializing of header file
void delay(); // declare of delay function

void main()//function: main Objective: to glow LED


{
while(1)
{
P20=1; //Switch OFF the LED connected at P20
delay(); //Random delay for some amount of time
P20=0; //Switch ON the LED connected at P20
delay();//Random delay for some amount of time

P21=1; //Switch OFF the LED connected at P21


delay();//Random delay for some amount of time
P21=0; //Switch ON the LED connected at P21
delay();//Random delay for some amount of time
}
}

//Function: delay Objective: it provides delay to glow LED


void delay()
{
int i;
for(i=0;i<=100;i++);
}
Interfacing with LED 37

+5V
C2 C3
30pF 30pF GND
X1
U1
19 39
XTAL1 P0.0/AD0
38
R1 P0.1/AD1
330R 37
11.0592MHz 18 P0.2/AD2
36
XTAL2 P0.3/AD3
35
U2 P0.4/AD4
34
P0.5/AD5
3

R7 P0.6/AD6 33
D6 9 32
VO

LED-RED RST P0.7/AD7


220
+5V Power Supply

21
P2.0/A8

AT89C51
GND
2 22
GND C4 P2.1/A9
23
+ 29 P2.2/A10
24
PSEN P2.3/A11
VI

+5V 30 25
31 ALE P2.4/A12
26 R3 R2
C1 10µF EA P2.5/A13 330R 330R
P2.6/A14 27
7805
1

P2.7/A15 28
GND R5
1 10 D3
10k
2 P1.0 P3.0/RXD
11 D2
1000u 3 P1.1 P3.1/TXD
12
P1.2 P3.2/INT0 LED LED
D1 4
P1.3 P3.3/INT1
13
IN4007 5 14
6
P1.4 P3.4/T0
15
7
P1.5 P3.5/T1
P1.6 16
+12V

GND

GND 8
P3.6/WR
17 GND GND
P1.7 P3.7/RD

FIGURE 5.4 Circuit diagram for the interfacing of LED to 8051.


6 Interfacing of 8051
and NuttyFi/ESP8266
with Seven Segment Display
This chapter discusses the basic of seven segment display and its features. The interfacing of seven seg-
ment display with 8051 and NuttyFi is also discussed with the help of circuit diagram and programming.

6.1 SEVEN SEGMENT DISPLAY


A seven-segment display is an electronic device, which can be used to display decimal numeric
values. Functional numerical displays (FND) can be employed in digital clocks, basic calculators,
electronic meters, and different electronic devices that show numerical data.
Figure 6.1 shows the seven segment display. Every light-emitting diode has two connecting pins, one
known as the “Anode” and other as the “Cathode”. Seven segment displays can be categorized in two
types—common cathode and common anode. Common anode has all the anode of the seven segments
connected directly to power and the common cathode has all the cathode are connected to the ground.
1. Common cathode seven segment—In this type, cathode of the diodes are connected to
logic “0” or ground. The individual segment measures a “HIGH” signal through a current
limiting device to make it forward bias the individual anode terminal. Figure 6.2 shows the
common cathode seven segment display.
2. Common anode seven segment—In this type, anode of the diodes are connected to logic
“1”. The individual segments measures a logic “LOW” signal through a current limiting
device to make it forward bias the individual anode terminal. Figure 6.3 shows the com-
mon anode seven segment display.
Relying upon the digit to be displayed, the required set of LEDs is forward biased. As an example, to
display the numerical digit zero, make LEDs placed at a, b, c, d, e, and f forward biased. The varied
digits from zero through nine are showed employing a seven-segment display as shown, Figure 6.4.
To understand the working of seven segment display a system is designed. Figure 6.5 shows the
block diagram of the system. The system comprises of a +12V power supply, a +12V to +5V con-
verter, a NuttyFi/8051, a resistor 330 Ohm, and a seven-segment display. The objective of the project
is to display the number on seven segment display using NuttyFi/8051.
Table 6.1 shows the list of the components used to design the system.

6.2 CIRCUIT DIAGRAM


Description of the interfacing of seven segment display with ESP8266/NuttyFi
The interfacing of the seven segment display is as follows:

1. Connect output of +12V power supply to input of +12V to +5V convertor.


2. Connect output of +5V power supply to +5V of the NuttyFi/ESP8266.
3. Connect input pins of seven segment (a–g and GND) to D1–D7 and GND pins of the
NuttyFi respectively.
4. Connect FTDI programmer to FTDI connector of the NuttyFi board.

39
40 Interfacing with I/O devices

FIGURE 6.1 Seven segment display.

FIGURE 6.2 Common cathode seven segment display.

FIGURE 6.3 Common anode seven segment display.


Interfacing Seven Segment Display 41

FIGURE 6.4 Seven segment display for displaying numbers (0–9).

+12V Power
Supply

+12V to +5V NuttyFi 7 Seg


To DC jack 330E
converter 8051uC Display

FIGURE 6.5 Block diagram of the system.

TABLE 6.1
Components List
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface ESP8266 with LED


1 +12V power supply 1 Output +12V/1A
2 +12V to +5V converter 1 Output +5V/1A
3 NuttyFi 1 Analog pin-1
Digital pin-10
FTDI connector for programmer
4 Seven segment breakout board with 330 Ohm 1 Common anode RED color segments
resistor

Components Used to Interface 8051 uC with LED


1 +12V power supply 01 Output +12V/1A
2 +12V to 5V convert2r 01 To regulate the input voltage to +5V/1A
3 330 Ohm resistor 01 0.25 Watt
4 LED 03 RED
5 8051 development board 01 Atmel Make including crystal oscillator
and reset circuit
6 Seven segment breakout with 330 Ohm resistor 1 Common anode RED color segments
42 Interfacing with I/O devices

FIGURE 6.6 Circuit diagram for the interfacing of seven segment display with NuttyFi.

Figure 6.6 shows the circuit diagram of the system.


Description of interfacing of seven segment display with 8051
The interfacing of the seven segment display is as follows:

1. Connect output of +12V power supply to input of +12V to +5V converter.


2. Connect output of +5V power supply to +5V of 8051.
3. Connect input pins of seven segment (a–g and GND) to P3.0, P3.1, P3.2, P3.3, P3.4, P3.5,
P3.6, P3.7, and GND pins of 8051 respectively.

Figure 6.7 shows the circuit diagram of the system.


+5V

C2 C3
30pF 30pF GND
X1
U1
19 39
XTAL1 P0.0/AD0
R1 P0.1/AD1 38
330R P0.2/AD2 37
11.0592MHz 18 36
XTAL2 P0.3/AD3
35
U2 P0.4/AD4
P0.5/AD5 34
3

R7 P0.6/AD6 33
D6 9 32
RST P0.7/AD7
VO

LED-RED
220 21
+5V

P2.0/A8
AT89C51
GND

2 22
GND C4 P2.1/A9
23
+ 29 P2.2/A10
24
30 PSEN P2.3/A11
25
VI

31 ALE P2.4/A12
26
10µF EA P2.5/A13
C1 R5 P2.6/A14 27
1

7805 P2.7/A15 28
+++++
– ––––

GND 10k 1 10
2
P1.0 P3.0/RXD
11
P1.1 P3.1/TXD
12
+5V Power Supply

3
1000u
4
P1.2 P3.2/INT0
13
D1 5
P1.3 P3.3/INT1
14
IN4007 P1.4 P3.4/T0
15
6 P1.5 P3.5/T1
7 P1.6 P3.6/WR 16
8 P1.7 17
P3.7/RD
B1
12V

FIGURE 6.7 Circuit diagram for the interfacing of seven segment display with 8051.
Interfacing Seven Segment Display 43

6.3 PROGRAM
//Program to interface common anode seven segment with ESP8266

int A_pin=D1; // assign variable to D1 pin


int B_pin=D2; // assign variable to D2 pin
int C_pin=D3; // assign variable to D3 pin
int D_pin=D4; // assign variable to D4 pin
int E_pin=D5; // assign variable to D5 pin
int F_pin=D6; // assign variable to D6 pin
int G_pin=D7; // assign variable to D7 pin
void setup()
{
pinMode(A_pin, OUTPUT); // set D1 pin direction to output
pinMode(B_pin, OUTPUT); // set D2 pin direction to output
pinMode(C_pin, OUTPUT); // set D3 pin direction to output
pinMode(D_pin, OUTPUT); // set D4 pin direction to output
pinMode(E_pin, OUTPUT); // set D5 pin direction to output
pinMode(F_pin, OUTPUT); // set D6 pin direction to output
pinMode(G_pin, OUTPUT); // set D7 pin direction to output
}

void loop()
{
////// print 0
digitalWrite(A_pin, HIGH); // make D1 to HIGH
digitalWrite(B_pin, HIGH); // make D2 to HIGH
digitalWrite(C_pin, HIGH); // make D3 to HIGH
digitalWrite(D_pin, HIGH); // make D4 to HIGH
digitalWrite(E_pin, HIGH); // make D5 to HIGH
digitalWrite(F_pin, HIGH); // make D6 to HIGH
digitalWrite(G_pin, LOW); // make D7 to LOW
delay(1000); // wait for a second
/// print 1
digitalWrite(A_pin, LOW); // make D1 to LOW
digitalWrite(B_pin, HIGH); // make D2 to HIGH
digitalWrite(C_pin, HIGH); // make D3 to HIGH
digitalWrite(D_pin, LOW); // make D4 to LOW
digitalWrite(E_pin, LOW); // make D5 to LOW
digitalWrite(F_pin, LOW); // make D6 to LOW
digitalWrite(G_pin, LOW); // make D7 to LOW
delay(1000); // wait for a second
///// print2
digitalWrite(A_pin, HIGH); // make D1 to HIGH
digitalWrite(B_pin, HIGH); // make D2 to HIGH
digitalWrite(C_pin, LOW); // make D3 to LOW
digitalWrite(D_pin, HIGH); // make D4 to HIGH
digitalWrite(E_pin, HIGH); // make D5 to HIGH
digitalWrite(F_pin, LOW); // make D6 to LOW
digitalWrite(G_pin, HIGH); // make D7 to HIGH
delay(1000); // wait for a second
}

//Program to interface common anode seven segment with 8051


#include<reg52.h>
void delay();
44 Interfacing with I/O devices

//function: main Objective: to glow seven segment display


void main()
{
P3=0xff;
P2=0xff;
while(1)
{
P3=0xc0; // Command to display 0
delay(); // Some delay between the two digit display
P3=0xf9; // Command to display 1
delay(); // Some delay between the two digit display
P3=0xa4; // Command to display 2
delay(); // Some delay between the two digit display
P3=0xb0; // Command to display 3
delay(); // Some delay between the two digit display
P3=0x99; // Command to display 4
delay(); // Some delay between the two digit display
P3=0x92; // Command to display 5
delay(); // Some delay between the two digit display
P3=0x82; // Command to display 6
delay(); // Some delay between the two digit display
P3=0xf8; // Command to display 7
delay(); // Some delay between the two digit display
P3=0x80; // Command to display 8
delay(); // Some delay between the two digit display
P3=0x90; // Command to display 9
delay(); // Some delay between the two digit display
}
}
//Function: delay Objective: it provides delay to glow seven segment display
void delay()
{
int i,j;
for(j=0;j<=100;j++)
{
for(i=0;i<=2000;i++)
{

}
}
}
7 Interfacing of 8051
and NuttyFi/ESP8266 with LCD
This chapter discusses the basic of liquid crystal display and its features. The interfacing of liquid crys-
tal display with 8051 and NuttyFi is also discussed with the help of circuit diagram and programming.

7.1 INTRODUCTION OF LIQUID CRYSTAL DISPLAY


“Liquid Crystal Display” (LCD) is used to display alpha numeric characters. The liquid crystals
matrixes are manufactured from complicated molecules. Similar to water, they alter their state from
solid to liquid, reckoning on the temperature to that they’re exposed. Once in an exceedingly liquid
state, the molecules move around however square measure possible to create a line in an exceed-
ingly bound direction, permitting them to replicate light-weight.
LCD has the distinct advantage of getting low power consumption than the semiconductor diode,
as shown in Figure 7.1.
A liquid cell consists of a skinny layer (about 10 um) of a liquid sandwiched between two glass
sheets with clear electrodes deposited on their within faces.
The Figure 7.2 shows the block diagram of the system. The system comprises of +12V power
supply, +12V to +5V converter, NuttyFi, resistor 330 ohm, and LCD. The objective is to display the
information on LCD using NuttyFi by writing the hex fle in the fash memory. Table 7.1 shows the
pin description of LCD.
Table 7.2 shows the list of the components used to design the system.

7.2 CIRCUIT DIAGRAM


Description of interfacing of ESP8266 with 16x2 LCD
Figure 7.3 shows the connection diagram of the system.

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the nuttyFi board and also connect to other peripheral to
power up.
3. For programming uses—connect FTDI programmer to FTDI connector of the NuttyFi
board.
4. Pins 1 to 16 of LCD are connected to GND.
5. Pins 2 to 15 of LCD are connected to +Vcc to Power Supply
6. Two fxed pins of variable resistor are connected to +5V and GND of LCD and variable
pin is connected to pin 3 of LCD.
7. E, RS, and RW pins of LCD are connected to pins D1, GND, and D2 of the NuttyFi.
8. D4 to D7 pins of LCD are connected to pins D3, D4, D5, and D6 of the NuttyFi.

45
46 Interfacing with I/O devices

LEDK
LEDA
VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E
1
2
3

4
5
6

7
8
9
10
11
12
13
14
15
16
FIGURE 7.1 Pin out of LCD.

Description of interfacing of 8051 with 16x2 LCD


The interfacing of the 16x2 LCD with 8051 is as per the given guidelines. Figure 7.4 shows the
connection diagram of the system.

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the 8051 board and also connect to other LCD to power up.
3. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
4. Pins 1, 16 of LCD are connected to GND of Power Supply.
5. Pins 2, 15 of LCD are connected to +Vcc of Power Supply.
6. Two fxed pins of POT are connected to +5V and GND of LCD and variable terminal of
POT is connected to pin 3 of LCD.
7. RS, RW, and E pins of LCD are connected to pins P30, P31, and P32 of the 8051 board
respectively.
8. D4, D5, D6, and D7 pins of LCD are connected to pins P34, P35, P36, and P37 of the 8051
board respectively.

FIGURE 7.2 Block diagram of the system.


Interfacing with LCD 47

TABLE 7.1
Pin Description of LCD
Sr. No. Pin No. Pin Name Pin Type Pin Description Pin Connection
1 1 Ground Source Pin This is a ground pin of LCD Connected to the ground of the MCU/
Power source
2 2 Vcc Source Pin This is the supply voltage pin Connected to the supply pin of Power
of LCD source
3 3 V0/VEE Control Pin Adjusts the contrast of the Connected to a variable POT that can
LCD. source 0–5V
4 4 Register Control Pin Toggles between Command/ Connected to a MCU pin and gets either
Select Data Register 0 or 1
0 -> Command Mode
1-> Data Mode
5 5 Read/ Control Pin Toggles the LCD between Connected to a MCU pin and gets either
Write Read/Write Operation 0 or 1
0 -> Write Operation
1-> Read Operation
6 6 Enable Control Pin Must be held high to perform Connected to MCU and always held
Read/Write Operation high
7 7–14 Data Bits Data/Command Pins used to send Command In 4-Wire Mode
(0–7) Pin or data to the LCD Only 4 pins (0–3) is connected to MCU
In 8-Wire Mode
All 8 pins(0–7) are connected to MCU
8 15 LED LED Pin Normal LED like operation Connected to +5V
Positive to illuminate the LCD
9 16 LED LED Pin Normal LED like operation Connected to ground
Negative to illuminate the LCD
connected with GND

TABLE 7.2
Components List
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface ESP8266 with LCD


1 +12V power supply 1 Output- +12V/1A
2 +12V to 5V convertor 1 Output- +5V/1A
3 NuttyFi 1 Analog pin-1
Digital pin-10
FTDI connector for programmer
4 LCD breakout board 1 4 Pins for +5V and 4 Pins for GND
LCD 20*4 1 16 pins LCD with backlight pins

Components Used to Interface 8051 uC with LCD


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 330 Ohm resistor 01 0.25 Watt
4 LED 02 RED
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 LCD breakout board 1 4 Pins for +5V and 4 Pins for GND
7 LCD 20*4 1 16 pins LCD with backlight pins
48 Interfacing with I/O devices

FIGURE 7.3 Circuit diagram of the system with NuttyFi.

FIGURE 7.4 Circuit diagram of the system with 8051.

7.3 PROGRAM
Program to interface liquid crystal display (LCD) with ESP8266

#include <LiquidCrystal.h>
const int RS = D1, E = D2, D4 = D3, D5 = D4, D6 = D4, D7 = D5;// assign variable to LCD pins
LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
void setup()
{
lcd.begin(20, 4); // initialize LCD
lcd.setCursor(0, 0); // set cursor of the LCD
lcd.print("DISPLAY SYSTEM"); // print string on LCD
Interfacing with LCD 49

lcd.setCursor(0, 1); // set cursor of the LCD


lcd.print("UsingLCD + NodeMCU"); // print string on LCD
}

void loop()
{
lcd.clear(); // clear the previous contents of LCD
lcd.setCursor(0, 2); // set cursor of the LCD
lcd.print("Hi Gentleman"); // print string on LCD
delay(2000); // wait for 2000 ms
lcd.clear();// clear the previous contents of LCD
lcd.setCursor(0, 2); // set cursor of the LCD
lcd.print("what’s UP"); // print string on LCD
delay(2000); // wait for 2000 ms
}

Program to interface liquid crystal display (LCD) with 8051

#include<reg51.h>
sbit lcd_E = P3^2; //Set P32 as enable pin
sbit lcd_RW = P3^1; //Set P31 as read write pin
sbit lcd_RS = P3^0; //Set P30 as resistor select pin

void lcd_init(); //LCD initialize command


void send_command(char command); // Command function
void send_data(char databyte); //Data function
void delay_lcd(); // LCD delay function
void display(char, char[]); //Display function
void delay(int);
void main() //Main function to display the contents
{
lcd_init(); //Initialize the LCD
delay(1); // Some delay for LCD
while(1) //Endless loop
{
display(0x80, "Praveen Malik"); //Display at first row of LCD
display(0xC0, "Rajesh Singh"); //Display at second row of LCD
}
}

void display(char x, char databyte[]) //Function to display at different row of LCD


{
char y=0;
send_command(x);
while(databyte[y]!='\0')
{
send_data(databyte[y]);
y++;
}
}

void lcd_init() //LCD initialize function


{
send_command(0x03);
delay_lcd();
send_command(0x28);
50 Interfacing with I/O devices

delay_lcd();
send_command(0x0E);
delay_lcd();
send_command(0x01);
delay_lcd();
send_command(0x0c);
delay_lcd();
send_command(0x06);
delay_lcd();
}

void send_command(char command)


{
char x,y;
lcd_RS = 0; /* (lcd_RS) P2.3---->RS , RS=0 for Instruction Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */
x=command;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;

lcd_E = 0;/* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 1; /* RS=1 */

lcd_RS = 0; /* (lcd_RS) P2.3---->RS , RS=0 for Instruction Write */


lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

y=command;
y=y & 0x0f;
P3=P3 & 0xf0;
P3=P3 | y;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 1; /* RS=1 */
}

void send_data(char databyte)


{
char x,y;
lcd_RS = 1; /* (lcd_RS) P2.3---->RS , RS=1 for Data Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

x=databyte;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 0; /* RS=0 */
Interfacing with LCD 51

lcd_RS = 1;/* (lcd_RS) P2.3---->RS , RS=1 for Data Write */


lcd_RW = 0;/* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1;/* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

y=databyte;
y=y & 0x0f;
P3=P3 & 0xf0;
P3=P3 | y;

lcd_E = 0;/* E=0; Disable lcd */


lcd_RW = 1;/* W=1; Disable write signal */
lcd_RS = 0;/* RS=0 */
}

void delay_lcd() //Some random delay for LCD


{
int j;
for(j=0;j<100;j++);
}

void delay(int x) //Some random delay


{
int i,j;
for(i=0;i<=x;i++)
{
for(j=0;j<=32000;j++);
}
8 Interfacing of 8051
and NuttyFi/ESP8266
with Analog Sensor
This chapter discusses the basic of analog sensors and its features. The interfacing of analog sensors
with 8051 and NuttyFi is also discussed with the help of circuit diagram and programming.
Analog sensor converts the environmental parameters into electrical signal like voltage and cur-
rent. The output voltage may be in the range of 0 to 5V.
There are different types of sensors, which produce continuous analog output signal. The signal
produced by the analog sensor is proportional to the change in the environmental parameter.

8.1 POTENTIOMETER
A potentiometer (POT) is a three-terminal resistor with a sliding or rotating contact. It forms an
adjustable voltage divider. The rotating terminal is a wiper and it helps POT to act as a variable
resistor or rheostat. POTs can be used to control electrical devices such as volume controls on audio
equipment. Figure 8.1 shows the POT.
Figure 8.2 shows the block diagram to interface POT with NuttyFi/8051. The system comprises
+12V power supply, +12V to +5V converter, NuttyFi, LED with resistor 330 Ohm, POT, and LCD.
The objective of the system is to display the levels generated by ADC on liquid crystal display
(LCD) using NuttyFi. If the levels increased by certain level indicator will be “ON”. Table 8.1 shows
the list of the components to design the system.

8.1.1 CIRCUIT DIAGRAM


Description of interfacing of POT with ESP8266

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the NuttyFi board and also connect to other peripherals
to power up.
3. For programming connect FTDI programmer to FTDI connector of the NuttyFi board.
4. Pins 1, 16 of LCD are connected to GND of power supply.
5. Pins 2, 15 of LCD are connected to +Vcc of power supply.
6. Two fxed terminals of POT are connected to +5V and GND of LCD and variable terminal
of POT is connected to pin 3 of LCD.
7. RS, RW, and E pins of LCD are connected to pins D1, GND, and D2 of the NuttyFi
respectively.
8. D4, D5, D6, and D7 pins of LCD are connected to pins D3, D4, D5, and D6 of the NuttyFi
respectively.
9. Connect +Vcc, GND and OUT pin of the POT to +5V, GND and A0 pin of the NuttyFi.
10. Connect input and GND pins of the LED breakout board to D7 and GND pins of NuttyFi
respectively.

53
54 Interfacing with I/O devices

FIGURE 8.1 Potentiometer.

Figure 8.3 shows the circuit diagram for interfacing of POT with NuttyFi/ESP8266.
Description of interfacing of POT with 8051

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the ADC 0804 board and also connect to LCD to power
up.
3. LCD data lines are connected to the port of P3 of 8051 uC.
4. Control lines of LCD RS, RW, and E are connected to P2.0, P2.1, and P2.2 of 8051
respectively.
5. Connect +Vcc, GND and OUT pin of the POT to +5V, GND and input pins of the ADC
0804 respectively.
6. Connect 8 output pin of ADC 0804 to the P1 port of the 8051 uC.
7. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.

FIGURE 8.2 Block diagram to interface POT with NuttyFi.


Interfacing with Analog Sensor 55

TABLE 8.1
Components to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface ESP8266 with Variable Resistor


1 +12V power supply 1 Output- +12V/1A
2 +12V to 5V convertor 1 Output- +5V/1A
3 NuttyFi 1 Analog pin-1
Digital pin-10
FTDI connector for programmer
4 LCD 20*4 1 16 pins LCD with backlight pins
5 POT with breakout board 1 Three pins
6 LED with breakout board 1 One RED color LED

Components Used to Interface 8051 uC with Variable Resistor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to
+5V/1A
3 330 Ohm resistor 01 0.25 Watt
4 LED 01 RED
5 8051 development board 01 Atmel Make including crystal
oscillator and reset circuit
6 LCD 16x2 1 16 pins LCD with backlight pins
7 Variable resistor with breakout board 1 5K resistor
8 ADC 0804 breakout board 01 Single channel 8 bit

FIGURE 8.3 Circuit diagram for interfacing of POT with NuttyFi/ESP8266.


56 Interfacing with I/O devices

FIGURE 8.4 Circuit diagram for interfacing of POT with 8051.

Figure 8.4 shows the circuit diagram for interfacing of POT with 8051.

8.1.2 PROGRAM
Program to interface POT with ESP8266
#include <LiquidCrystal.h>
LiquidCrystal lcd(D1, D2, D3, D4, D5, D6); // add library of LCD

const int POT_Pin=A0; // assign A0 pin to POT


const int INDICATOR_PIN=D7; // assign D7 as indicator pin
void setup()
{
pinMode(INDICATOR_PIN, OUTPUT); // set D7 pin as an output
lcd.begin(20, 4); // initialize LCD
lcd.print("POT Control..."); // print string on LCD
}

void loop()
{
int POT_Pin_LEVEL = digitalRead(POT_Pin);// Read POT pin
lcd.setCursor(0, 1); // set cursor of LCD
lcd.print("ACTUAL_LEVEL:"); //print string on LCD
if (POT_Pin_LEVEL >= 240) // compare level
{
lcd.setCursor(0, 3); // set cursor of LCD
lcd.print("LEVEL_EXCEED "); // print string on LCD
digitalWrite(INDICATOR_PIN, HIGH); // set D7 to HIGH
delay(20); // wait for 20mSec
}
Interfacing with Analog Sensor 57

else
{
lcd.setCursor(0, 3); // set cursor of LCD
lcd.print("LEVEL NORMAL "); // print string on LCD
digitalWrite(INDICATOR_PIN, LOW); // set D7 to HIGH
delay(20); // wait for 20mSec
}
}

Program to interface POT with ADC0804 and 8051


#include<reg52.h> //initializing of reg52 header file
#include<lcd.h> //initializing of LCD header file
void disp_name(); //declaration of disp_name function
void delay(); //declaration of delay function
void disp_name1(); //declaration of disp_name1 function
char databyte[]={"0123456789"};
char a,b,c;

void main() //definition of main function


{
P1=0xff; //initialize the port 1 as 11111111
lcd_init(); //call the LCD initialize function
disp_name(); //call the disp_name function
lcd_init1(); //call the LCD1 initialize function
disp_name1(); //call the disp_name1 function

while(1) //endless loop


{
i=P1; // read P1 port for getting analog sensor data
a=i%10; // add scaling factor
b=i/10; // add scaling factor
c=i/100; // add scaling factor
send_command(0xc0); // send command to first location of LCD
send_data(databyte[c]); //send first data
send_command(0xc1); // send command to second location of LCD
send_data(databyte[b]); //send second data
send_command(0xc2); // send command to third location of LCD
send_data(databyte[a]); //send third data
delay(); //delay for some moment
}
}

void disp_name() //function to display data on first line of LCD


{
char i=0;
char databyte[]={"POT Voltage Level "}; //Contents to be displayed
send_command(0x80); //Command of first line
while(databyte[i]!='\0')
{
send_data(databyte[i]); //send the data
i++;
}
}

void disp_name1() //function to display data on second line of LCD


{
58 Interfacing with I/O devices

char i=0;
char databyte[]={" "}; //contents to be displayed
send_command(0xC0); //command of second line
while(databyte[i]!='\0')
{
send_data(databyte[i]); //send the data
i++;
}
}

8.2 TEMPERATURE SENSOR


Temperature sensors are available in both forms—digital and analog. Commonly used analog tem-
perature sensor is thermistor. For different applications, different types of thermistors are available.
Thermistor detects change in temperature as a thermally sensitive resistor. If the temperature increases,
then the electrical resistance of thermistor increases and resistance decreases as temperature decreases.
LM35 is an integrated analog temperature sensor whose electrical output is proportional to
degree Centigrade. LM35 sensor does not require any external calibration. Figure 8.5 shows LM35.
Figure 8.6 shows the block diagram for interfacing of LM35 with NuttyFi/8051. The system
comprises of +12V power supply, +12V to +5V converter, NuttyFi, 8051 board, LED with resistor
330 Ohm, LM35 with breakout board, and LCD. The objective of the system is to display the envi-
ronmental temperature on LCD using NuttyFi. If the temperature exceed by certain level, indicator
will be “ON”. Table 8.2 shows the list of the components to design the system.

8.2.1 CIRCUIT DIAGRAM


Description of interfacing of LM35 with ESP8266

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the NuttyFi board and also connect to other peripherals
to power up.
3. For programming, connect FTDI programmer to FTDI connector of the NuttyFi board.
4. Pins 1, 16 of LCD are connected to GND of power supply respectively.

FIGURE 8.5 LM35 sensor.


Interfacing with Analog Sensor 59

FIGURE 8.6 Block diagram for interfacing of LM35 with NuttyFi/8051.

5. Pins 2, 15 of LCD are connected to +Vcc of power supply respectively.


6. Two fxed terminals of POT are connected to +5V and GND of LCD and variable terminal
of POT is connected to pin 3 of LCD.
7. RS, RW, and E pins of LCD are connected to pins D1, GND, and D2 of the NuttyFi respectively.
8. D4, D5, D6, and D7 pins of LCD are connected to pins D3, D4, D5, and D6 of the NuttyFi.
9. Connect +Vcc, GND and OUT pin of the LM35 to +5V, GND and A0 pins of the NuttyFi
respectively.
10. Connect input and GND pins of the LED breakout board to D7 and GND pins of NuttyFi
respectively.

TABLE 8.2
Components Used to Design System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface ESP8266 with Variable Resistor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 Output- +5V/1A
3 NuttyFi 01 Analog pin-1
Digital pin-10
FTDI connector for programmer
4 LCD 20*4 01 16 pins LCD with backlight pins
5 POT with breakout board 01 Three pins
6 LED with breakout board 01 One RED color LED
7 LM35 with breakout board 01 Three pins
Components Used to Interface 8051 uC with Variable Resistor
1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to
+5V/1A
3 330 Ohm resistor 01 0.25 Watt
4 LED 01 RED
5 8051 development board 01 Atmel Make including crystal
oscillator and reset circuit
6 LCD 16x2 01 16 pins LCD with backlight pins
7 LM35 with breakout board 01 Three pins
8 ADC 0804 breakout board 01 Single channel 8 bit
60 Interfacing with I/O devices

GND GND
+5V
Vinput 3.3V

NuttyFi-Cloud Device
D4 3.3V 330E
R1
D3 D7 D7
D2 D6 D2
LED-GREEN
D1 D5
Rx D0
Tx A0 A0
RESISTOR
GND
330E
Vcc

Rx

3
D1
Tx REGULATOR 7812

VO
LED-yellow
7812
2
GND
LM044LCD1
Vcc

GND
+5V
VI
+12V Power Supply
RV1

1
+50V D1
330

+12V to +5V converter


45%

U1 LED-RED
10k

3
1000uF
3 VDD
VSS

VEE

VO
RW
RS

D0
D1
D2
D3
11 D4
D5
D6
D7
E
GND

12
13
14
10
1
2

4
5
6

7
8
9

2
GND

1N4007
GND=LED–

C1

VI
GND
Vcc

+5V
7805
D1
GND
D2

D3
D4
D5
D6

1
U2
Vcc=LED+

1
1000uF
9-0-9

27.0
+12V

GND
2
VOUT
AC

A0
3 LM35

GND

FIGURE 8.7 Circuit diagram for interfacing of LM35 with NuttyFi/ESP8266.

Figure 8.7 shows the circuit diagram for interfacing of LM35 with NuttyFi/ESP8266.
Description of interfacing of LM35 temperature sensor with 8051

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the ADC 0804 board and also connect to LCD to power up.
3. LCD data lines are connected to the port of P3 of 8051 uC.
4. Control lines of LCD RS, RW, and E are connected to P2.0, P2.1, and P2.2 respectively.
5. Connect +Vcc, GND and OUT pin of the LM35 to +5V, GND and input pins of the ADC
0804 respectively.
6. Connect 8 output pin of ADC 0804 to the P1 port of the 8051 uC.
7. For programming, user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
Interfacing with Analog Sensor 61

FIGURE 8.8 Circuit diagram for interfacing of LM35 with 8051.

Figure 8.8 shows the circuit diagram for interfacing of LM35 with 8051.

8.2.2 PROGRAM
Program to interface POT with ESP8266

#include <LiquidCrystal.h>
LiquidCrystal lcd(D1, D2, D3, D4, D5, D6); // add library of LCD
const int LM35Sensor_Pin=A0; // assign A0 to LM35 data pin
const int INDICATOR_PIN=D7; // assign D7 to indicator pin
void setup()
{
pinMode(INDICATOR_PIN, OUTPUT); // set D7 as an output pin
lcd.begin(20, 4); // initialize LCD
lcd.print("TEMP Monitoring..."); // print string on LCD
}

void loop()
{
int LM35SENSOR_Pin_LEVEL = digitalRead(LM35Sensor_Pin);// Read LM35 Sensor pin
int TEMP_ACTUAL=LM35SENSOR_Pin_LEVEL/2; // scale level
lcd.setCursor(0, 1); // set cursor on LCD
lcd.print("ACTUAL_LEVEL:"); // print string on LCD
lcd.setCursor(0, 2); // set cursor on LCD
lcd.print(TEMP_ACTUAL); // print integer on LCD
if (LDRSENSOR_Pin_LEVEL >= 40) // compare level
{
lcd.setCursor(0, 3); // set cursor on LCD
62 Interfacing with I/O devices

lcd.print("TEMP_EXCEED "); // print string on LCD


digitalWrite(INDICATOR_PIN, HIGH); // make indicator pin HIGH
delay(20); // wait for 20mSec
}
else
{
lcd.setCursor(0, 3); // set cursor on LCD
lcd.print("TEMP NORMAL "); // print string on LCD
digitalWrite(INDICATOR_PIN, LOW); // make indicator pin HIGH
delay(20); // wait for 20mSec
}
}

Program to interface POT with ADC0804 and 8051

#include<reg52.h> //initializing of reg52 header file


#include<lcd.h> //initializing of LCD header file
void disp_name(); //declaration of disp_name function
void delay(); //declaration of delay function
void disp_name1(); //declaration of disp_name1 function
char databyte[]={"0123456789"};
char a,b,c;

void main() //definition of main function


{
P1=0xff; //initialize the port 1 as 11111111
lcd_init(); //call the LCD initialize function
disp_name(); //call the disp_name function
lcd_init1(); //call the LCD1 initialize function
disp_name1(); //call the disp_name1 function

while(1) //endless loop


{
i=P1; // read P1 port for getting analog sensor data
a=i%10; // add scaling factor
b=i/10; // add scaling factor
c=i/100; // add scaling factor
send_command(0xc0); // send command to first location of LCD
send_data(databyte[c]); //send first data
send_command(0xc1); // send command to second location of LCD
send_data(databyte[b]); //send second data
send_command(0xc2); // send command to third location of LCD
send_data(databyte[a]); //send third data
delay(); //delay for some moment
}
}

void disp_name() //function to display data on first line of LCD


{
char i=0;
char databyte[]={"Temperature reading is "}; //Contents to be displayed
send_command(0x80); //Command of first line
while(databyte[i]!='\0')
{
send_data(databyte[i]); //send the data
i++;
Interfacing with Analog Sensor 63

}
}

void disp_name1() //function to display data on second line of LCD


{
char i=0;
char databyte[]={" Degree cent"}; //contents to be displayed
send_command(0xC0); //command of second line
while(databyte[i]!='\0')
{
send_data(databyte[i]); //send the data
i++;
}
}

8.3 LIGHT DEPENDENT RESISTOR


Light dependent resistor (LDR) is an analog sensor, which can detect light intensity of a light source,
also known as light intensity sensor. The sensors can be classifed into different types—photoresis-
tor, Cadmium Sulfde (CdS), and photocell. LDR can be used to switch on and off loads automati-
cally. The resistance of the LDR increases with decrease in light and decreases with increase in
light. Figure 8.9 shows the LDR sensor.
Figure 8.10 shows the block diagram for interfacing of LDR with NuttyFi/8051. The system com-
prises of +12V power supply, +12V to +5V converter, NuttyFi, LED with resistor 330 Ohm, LDR

FIGURE 8.9 LDR sensor.

FIGURE 8.10 Block diagram for interfacing of LDR with NuttyFi/ESP8266.


64 Interfacing with I/O devices

TABLE 8.3
Components Used to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface ESP8266 with Variable Resistor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 Output- +5V/1A
3 NuttyFi 01 Analog pin-1
Digital pin-10
FTDI connector for programmer
4 LCD 20*4 01 16 pins LCD with backlight pins
5 LCD breakout board 1 4 Pins for +5V and 4 Pins for GND
6 LED with breakout board 01 One RED color LED
7 LDR with breakout board 1 Three pins

Components Used to Interface 8051 uC with Variable Resistor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 330 Ohm resistor 01 0.25 Watt
4 LED 01 RED
5 8051 development board 01 Atmel Make including crystal oscillator
and reset circuit
6 LCD 16x2 01 16 pins LCD with backlight pins
7 LDR with breakout board 1 Three pins
8 ADC 0804 breakout board 01 Single channel 8 bit

with breakout board, and LCD. The objective of the system is to display the light intensity levels
generate by LDR sensor using ADC on LCD using NuttyFi. If the light intensity exceeds by certain
level, LED indicator will be “ON”. Table 8.3 shows the list of the components to design the system.

8.3.1 CIRCUIT DIAGRAM


Description of interfacing of LDR with ESP8266

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the NuttyFi board and also connect to other peripheral
to power up.
3. For programming, connect FTDI programmer to FTDI connector of the NuttyFi board.
4. Pins 1, 16 of LCD are connected to GND of power supply.
5. Pins 2, 15 of LCD are connected to +Vcc of power supply.
6. Two fxed terminals of POT are connected to +5V and GND of LCD and variable terminal
of POT is connected to pin 3 of LCD.
7. RS, RW, and E pins of LCD are connected to pins D1, GND, and D2 of the NuttyFi
respectively.
8. D4, D5, D6, and D7 pins of LCD are connected to pins D3, D4, D5, and D6 of the NuttyFi
respectively.
9. Connect +Vcc, GND and OUT pin of the LDR sensor to +5V, GND and A0 pins of the
NuttyFi respectively.
10. Connect input and GND pins of the LED breakout board to D7 and GND pins of NuttyFi
respectively.
Interfacing with Analog Sensor 65

GND GND
+5V
Vinput 3.3V

NuttyFi-Cloud Device
D4 3.3V
330E
R1 D3 D7
D7 D2 D6
D2 D1 D5
LED-GREEN
Rx D0
Tx A0 A0

GND
RESISTOR

Vcc

Rx
Tx
330E
3

D1 REGULATOR 7812
VO

LED-yellow
7812
LM044LCD1

Vcc
2
GND
GND
RV1
+5V
VI

+5V
+12V Power Supply

+50V
330
D1 45%
10k
+12V to +5V converter

LDR1

VDD
VSS

VEE
U1 1.0

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
LED-
3

LDR

E
1000uF
GND

RED
VO

12
13
14
10
11
1
2
3
4
5
6
7
8
9
2
GND

GND=LED–
GND
Vcc
1N4007

A0
D1 R2
GND
D2
C1
VI

D3
D4
D5
D6
7805 10k
1

Vcc=LED+
1000uF GND
9-0-9

+12V

GND
AC

FIGURE 8.11 Circuit diagram for interfacing of LDR with ESP8266.

Figure 8.11 shows the circuit diagram for interfacing of LDR with ESP8266.
Description of interfacing of LDR with 8051

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the ADC 0804 board and also connect to LCD to power
up.
3. LCD data lines are connected to the port of P3 of 8051 uC.
4. Control lines of LCD RS, RW, and E are connected to P2.0, P2.1, and P2.2 respectively.
5. Connect LDR as per the circuit diagram to the input pins of the ADC 0804.
6. Connect 8 output pin of ADC 0804 to the P1 port of the 8051 uC.
7. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.

Figure 8.12 shows circuit diagram for interfacing of LDR with 8051.

8.3.2 PROGRAM
Program to interface LDR with ESP8266

#include <LiquidCrystal.h>
LiquidCrystal lcd(D1, D2, D3, D4, D5, D6); // add LCD library
const int LDRSensor_Pin=A0; // assign A0 as LDR pin
const int LED_PIN=D7; // assign D7 as LED pin
66 Interfacing with I/O devices

void setup()
{
pinMode(LED_PIN, OUTPUT); // set D7 pin as an OUTPUT
lcd.begin(20, 4); // initialize LCD
lcd.print("LDR monitoring..."); // print string on LCD
}
void loop()
{
int LDR_Pin_LEVEL = digitalRead(LDRSensor_Pin);// Read Fire Sensor pin
lcd.setCursor(0, 1); // set cursor of LCD
lcd.print("ACTUAL_LEVEL:"); // print string on LCD
lcd.setCursor(0, 2); // set cursor of LCD
lcd.print(LDR_Pin_LEVEL);// print integer on LCD
if (LDR_Pin_LEVEL >= 100) // compare the levels
{
lcd.setCursor(0, 3); // set cursor of LCD
lcd.print("LEVEL_EXCEED "); // print string on LCD
digitalWrite(LED_PIN, HIGH); // set D7 to HIGH
delay(20); // wait for 20mSec
}
else
{
lcd.setCursor(0, 3); // set cursor of LCD
lcd.print("LEVEL NORMAL "); // print string on LCD
digitalWrite(LED_PIN, LOW); // set D7 to HIGH
delay(20); // wait for 20mSec
}
}

Program to interface LDR with ADC0804 and 8051

#include<reg52.h> //initializing of reg52 header file


#include<lcd.h> //initializing of LCD header file
void disp_name(); //declaration of disp_name function
void delay(); //declaration of delay function
void disp_name1(); //declaration of disp_name1 function
char databyte[]={"0123456789"};
char a,b,c;

void main() //definition of main function


{
P1=0xff; //initialize the port 1 as 11111111
lcd_init(); //call the LCD initialize function
disp_name(); //call the disp_name function
lcd_init1(); //call the LCD1 initialize function
disp_name1(); //call the disp_name1 function
while(1) //endless loop
{
i=P1; // read P1 port for getting analog sensor data
a=i%10; // add scaling factor
b=i/10; // add scaling factor
c=i/100; // add scaling factor
send_command(0xc0); // send command to first location of LCD
send_data(databyte[c]); //send first data
send_command(0xc1); // send command to second location of LCD
send_data(databyte[b]); //send second data
Interfacing with Analog Sensor 67

send_command(0xc2); // send command to third location of LCD


send_data(databyte[a]); //send third data
delay(); //delay for some moment
}
}

void disp_name() //function to display data on first line of LCD


{
char i=0;
char databyte[]={"Intensity Level "}; //Contents to be displayed
send_command(0x80); //Command of first line
while(databyte[i]!='\0')
{
send_data(databyte[i]); //send the data
i++;
}
}

void disp_name1() //function to display data on second line of LCD


{
char i=0;
char databyte[]={" level (0-255)"}; //contents to be displayed
send_command(0xC0); //command of second line
while(databyte[i]!='\0')
{
send_data(databyte[i]); //send the data
i++;
}
}

FIGURE 8.12 Circuit diagram for interfacing of LDR with 8051.


68 Interfacing with I/O devices

FIGURE 8.13 Flex sensor.

8.4 FLEX SENSOR


The fex sensor is a variable resistor where the resistance of the sensor changes with respect to the
bend of the sensor. This sensor can be used as door sensor, robot whisker sensor, a sentient stuffed
animal, etc. Figure 8.13 shows the fex sensor.
One side of the sensor is printed with a polymer ink with a conductive particles embedded in it.
When the sensor is straight, the particles give the ink a resistance of about 30k Ohm. When the sen-
sor is bent away from the ink, the conductive particles move further apart, increasing this resistance
(to about 50k–70k Ohm when the sensor is bent to 90°). By measuring the resistance, the bent angle
of sensor can be calculated.
Figure 8.14 shows the block diagram for interfacing of fex sensor with NuttyFi/8051. The system
comprises of +12V power supply, +12V to +5V converter, NuttyFi, LED with resistor 330 Ohm, fex
sensor with breakout board, and LCD. The objective of the system is to display the bent levels gener-
ated by fex sensor using ADC on LCD using NuttyFi. If the bent exceed to a certain level, buzzer
indicator will be “ON”. Table 8.4 shows the list of the components to design the system.

8.4.1 CIRCUIT DIAGRAM


Description of interfacing of fex sensor with ESP8266
1. Connect +12V power supply output to input of +12V to +5V convertor.
2. Connect +5V output to power up the NuttyFi board and also connect to other peripherals
to power up.

FIGURE 8.14 Block diagram for interfacing of fex sensor with NuttyFi/8051.
Interfacing with Analog Sensor 69

TABLE 8.4
Components Used to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface ESP8266 with Variable Resistor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 Output- +5V/1A
3 NuttyFi 01 Analog pin-1
Digital pin-10
FTDI connector for programmer
4 LCD 20*4 01 16 pins LCD with backlight pins
5 LED with breakout board 01 One RED color LED
6 Flex sensor with breakout board 1 Three pins
7 Buzzer with breakout board 1 +5V operated buzzer

Components Used to Interface 8051 uC with Variable Resistor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to
+5V/1A
3 330 Ohm resistor 01 0.25 Watt
4 LED 01 RED
5 8051 development board 01 Atmel Make including crystal
oscillator and reset circuit
6 LCD 16x2 01 16 pins LCD with backlight pins
7 ADC 0804 breakout board 01 Single channel 8 bit
8 Flex sensor with breakout board 1 Three pins
9 Buzzer with breakout board 1 +5V operated buzzer

3. For programming, connect FTDI programmer to FTDI connector of the NuttyFi board.
4. Pins 1, 16 of LCD are connected to GND of power supply.
5. Pins 2, 15 of LCD are connected to +Vcc of power supply.
6. Two fxed terminals of POT are connected to +5V and GND of LCD and variable terminal
of POT is connected to pin 3 of LCD.
7. RS, RW, and E pins of LCD are connected to pins D1, GND, and D2 of the NuttyFi respectively.
8. D4, D5, D6, and D7 pins of LCD are connected to pins D3, D4, D5, and D6 of the NuttyFi
respectively.
9. Connect +Vcc, GND and OUT pin of the Flex sensor to +5V, GND and A0 pins of the
NuttyFi respectively.
10. Connect input and GND pins of the buzzer breakout board to D7 and GND pins of NuttyFi.
Figure 8.15 shows the circuit diagram for interfacing of fex sensor with ESP8266.
Description of interfacing of fex sensor with 8051
1. Connect +12V power supply output to input of +12V to +5V convertor.
2. Connect +5V output to power up the ADC 0804 board and also connect to LCD to power up.
3. LCD data lines are connected to the port of P3 of 8051 uC.
4. Control lines of LCD RS, RW, and E are connected to P2.0, P2.1, and P2.2 respectively.
5. Connect +Vcc, GND and OUT pin of the fex sensor to +5V, GND and input pins of the
ADC 0804, as shown in the circuit diagram.
6. Connect 8 output pin of ADC 0804 to the P1 port of the 8051 uC.
70 Interfacing with I/O devices

1 7805 VO 3
VI +5V FLEX1
GND GND

GND
FLEX SENSOR
+5V +12V
Vinput 3.3V 330
C1

NuttyFi-Cloud Device

2
3.3V
D4 1000uF D1
D3 D7 LED-RED
GND +12V to +5V converter
D2 D6 GND
D1 D5 RESISTOR

Rx D0 330E

www.TheEngineeringProjects.com
3
Tx A0 A0 D1 REGULATOR 7812

VO
LED-yellow
7812
D7 BUZ1
GND 2
GND

Vcc

Rx
Tx
GND

VI
BUZZER

+12V Power Supply

1
+50V
LM044LCD1
Vcc

RV1 1000uF
45%

10k

1N4007
VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E

TestPin
GND

12
13
14
10
11
1
2
3
4
5
6
7
8
9

GND=LED–
GND
Vcc

+5V
D1
GND
D2

D3
D4
D5
D6

9-0-9
+5V
Vcc=LED+

AC GND

A0
FIGURE 8.15 Circuit diagram for interfacing of fex sensor with ESP8266.

7. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
8. Connect the buzzer to P2.7 of 8051 uC.
Figure 8.16 shows the circuit diagram for interfacing of fex sensor with 8051.

FIGURE 8.16 Circuit diagram for interfacing of fex sensor with 8051.
Interfacing with Analog Sensor 71

8.4.2 PROGRAM
Program to interface fex sensor with ESP8266

#include <LiquidCrystal.h>
LiquidCrystal lcd(D1, D2, D3, D4, D5, D6); // add LCD library
const int FLEXSensor_Pin=A0; // assign A0 as flex sensor pin
const int BUZZER_PIN=D7; // assign D7 as buzzer pin
void setup()
{
pinMode(LED_PIN, OUTPUT); // set D7 as an output
lcd.begin(20, 4); // initialize LCD
lcd.print("Bent monitoring..."); // print string on LCD
}

void loop()
{
int FLEX_Pin_LEVEL = digitalRead(FLEXSensor_Pin);// Read Fire Sensor pin
int FLEX_SCALING = FLEX_Pin_LEVEL/4; // scale parameters
lcd.setCursor(0, 1); // set cursor on LCD
lcd.print("ACTUAL_LEVEL:"); // print string on LCD
lcd.setCursor(0, 2); // set cursor on LCD
lcd.print(FLEX_SCALING); // print integer on LCD
if (FLEX_SCALING >= 50) // compare scale factor
{
lcd.setCursor(0, 3); // set cursor on LCD
lcd.print("bent exceed "); // print string on LCD
digitalWrite(BUZZER_PIN, HIGH); // make D7 pin to HIGH
delay(20); // wait for 20mSec
}
else
{
lcd.setCursor(0, 3); // set cursor on LCD
lcd.print("No bent "); // print string on LCD
digitalWrite(BUZZER_PIN, LOW); // make D7 pin to HIGH
delay(20); // wait for 20mSec
}
}

Program to interface fex sensor with ADC0804 and 8051

#include<reg52.h> //initializing of reg52 header file


#include<lcd.h> //initializing of LCD header file
void disp_name(); //declaration of disp_name function
void delay(); //declaration of delay function
void disp_name1(); //declaration of disp_name1 function
char databyte[]={"0123456789"};
char a,b,c;

void main() //definition of main function


{
P1=0xff; //initialize the port 1 as 11111111
lcd_init(); //call the LCD initialize function
disp_name(); //call the disp_name function
72 Interfacing with I/O devices

lcd_init1(); //call the LCD1 initialize function


disp_name1(); //call the disp_name1 function

while(1) //endless loop


{
i=P1; // read P1 port for getting analog sensor data
a=i%10; // add scaling factor
b=i/10; // add scaling factor
c=i/100; // add scaling factor
send_command(0xc0); // send command to first location of LCD
send_data(databyte[c]); //send first data
send_command(0xc1); // send command to second location of LCD
send_data(databyte[b]); //send second data
send_command(0xc2); // send command to third location of LCD
send_data(databyte[a]); //send third data
delay(); //delay for some moment
}
}

void disp_name() //function to display data on first line of LCD


{
char i=0;
char databyte[]={"Actual Level "}; //Contents to be displayed
send_command(0x80); //Command of first line
while(databyte[i]!='\0')
{
send_data(databyte[i]); //send the data
i++;
}
}

void disp_name1() //function to display data on second line of LCD


{
char i=0;
char databyte[]={" Flex scaling"}; //contents to be displayed
send_command(0xC0); //command of second line
while(databyte[i]!='\0')
{
send_data(databyte[i]); //send the data
i++;
}
}

8.5 GAS SENSOR


A gas sensor detects the presence of gases in an area for the safety. This type of device can be used
to detect a gas leak or other emissions. It can be interfaced with a control system to control it auto-
matically as per condition. Figure 8.17 shows gas sensor.
Figure 8.18 shows the block diagram for interfacing of gas sensor with NuttyFi/8051. The system
comprises of +12V power supply, +12V to +5V converter, NuttyFi, LED with resistor 330 Ohm,
gas sensor with breakout board, and LCD. The objective of the project is display the gas levels and
PPM generated by gas sensor using ADC on LCD using NuttyFi by writing the hex fle in the fash
memory. If the gas exceed by certain level, hooter will “ON” to show the status. Table 8.5 shows the
list of the components to design the system.
Interfacing with Analog Sensor 73

FIGURE 8.17 Gas sensor.

8.5.1 CIRCUIT DIAGRAM


Description of interfacing of ESP8266 with gas sensor
1. Connect +12V power supply output to input of +12V to +5V convertor.
2. Connect +5V output to power up the NuttyFi board and also connect to other peripheral
to power up.
3. For programming, connect FTDI programmer to FTDI connector of the NuttyFi board.
4. Pins 1, 16 of LCD are connected to GND of power supply.
5. Pins 2, 15 of LCD are connected to +Vcc of power supply.
6. Two fxed terminals of POT are connected to +5V and GND of LCD and variable terminal
of POT is connected to pin 3 of LCD.
7. RS, RW, and E pins of LCD are connected to pins D1, GND, and D2 of the NuttyFi
respectively.
8. D4, D5, D6, and D7 pins of LCD are connected to pins D3, D4, D5, and D6 of the NuttyFi
respectively.
9. Connect +Vcc, GND and OUT pin of the gas sensor to +5V, GND and A0 pins of the
NuttyFi respectively.
10. Connect input, +12V and GND pins of the relay breakout board to D7, +12V and GND
pins of NuttyFi.
Figure 8.19 shows the circuit diagram for interfacing of gas sensor with NuttyFi.

FIGURE 8.18 Block diagram for interfacing of gas sensor with NuttyFi.
74 Interfacing with I/O devices

TABLE 8.5
Components Used to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface ESP8266 with Gas Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 Output- +5V/1A
3 NuttyFi 01 Analog pin-1
Digital pin-10
FTDI connector for programmer
4 LCD 20*4 break out board 01 16 pins LCD with backlight pins
5 LED with breakout board 01 One RED color LED
6 Gas sensor with breakout board 1 Three pins
7 Relay with breakout board 1 +5V operated buzzer

Components Used to Interface 8051 uC with Gas Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 330 Ohm resistor 01 0.25 Watt
4 LED 01 RED
5 8051 development board 01 Atmel Make including crystal oscillator
and reset circuit
6 LCD 16x2 01 16 pins LCD with backlight pins
7 ADC 0804 breakout board 01 Single channel 8 bit
8 Buzzer with breakout board 1 +5V operated buzzer
9 Gas sensor with breakout board 1 Three pins

GND GND GAS1


+5V MQ-2 GAS SENSOR
NuttyFi-Cloud Device

Vinput 3.3V
MQ-2
D4 3.3V U1
D3 D7 1 7805
VI VO 3 +5V
D2 D6
GND

D1 D5 +12V
330
Rx D0 C1
2

VCCOUT GND
Tx A0 A0 1000uF D1
LED-RED
GND +12V to +5V converter
GND

GND
A0
Vcc

Rx
Tx

D1
+12V Power Supply
LM044LCD1 +50V
1000uF LED-yellow RESISTOR
Vcc

1N4007
GND 2

AC 330E
RV1 7812
1 3
VI VO
9-0-9
45%

REGULATOR 7812
10k
+12V
VDD
VSS
VEE

RW

HOOTER
RS

D0
D1
D2
D3
D4
D5
D6
D7
E

220V
GND

12
13
14
10
11
1
2
3
4
5
6
7
8
9

12V
RELAY
220V/50HZ
GND=LED–
GND
Vcc

1N4007 D3
D1
GND
D2

D3
D4
D5
D6
Vcc=LED+

R3 Q1
D7 2N2222
330 ohm

GND

FIGURE 8.19 Circuit diagram for interfacing of gas sensor with NuttyFi.
Interfacing with Analog Sensor 75

Description of interfacing of gas sensor with 8051

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the ADC 0804 board and also connect to LCD to power up.
3. LCD data lines are connected to the port of P3 of 8051 uC.
4. Control lines of LCD RS, RW, and E are connected to P2.0, P2.1, and P2.2 respectively.
5. Connect +Vcc, GND and OUT pin of the gas sensor to +5V, GND and input pins of the
ADC 0804.
6. Connect 8 output pin of ADC 0804 to the P1 port of the 8051 uC.
7. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.

8.5.2 PROGRAM
Program to interface gas sensor with ESP8266

#include <LiquidCrystal.h>
LiquidCrystal lcd(D1, D2, D3, D4, D5, D6); // add library of LCD
const int GASSensor_Pin=A0; // assign A0 pin to gas sensor
const int HOOTER_PIN=D7; // assign D7 to hooter
void setup()
{
pinMode(LED_PIN, OUTPUT); // set D7 as an output
lcd.begin(20, 4); // initialize LCD
lcd.print("Gas Contents"); // print string on LCD
}

void loop()
{
int GAS_Pin_LEVEL = digitalRead(GASSensor_Pin);// Read GAS Sensor pin
int GAS_PPM = FLEX_Pin_LEVEL*1000; // scale factor
lcd.setCursor(0, 1); // set cursor on LCD
lcd.print("GAS_PPM:"); // print string on LCD
lcd.setCursor(0, 2); // set cursor on LCD
lcd.print(GAS_PPM); // print integer on LCD
if (GAS_PPM >= 10000) // compare gas levels
{
lcd.setCursor(0, 3); // set cursor on LCD
lcd.print("Contents increase"); // print string on LCD
digitalWrite(HOOTER_PIN, HIGH); // make D7 pin to HIGH
delay(20); // wait for 20 mSec
}
else
{
lcd.setCursor(0, 3); // set cursor on LCD
lcd.print("Contents Normal "); // print string on LCD
digitalWrite(HOOTER_PIN, LOW); // make D7 pin to LOW
delay(20); // wait for 20 mSec
}
}

Program to interface gas sensor with ADC0804 and 8051

#include<reg52.h> //initializing of reg52 header file


#include<lcd.h> //initializing of LCD header file
76 Interfacing with I/O devices

void disp_name(); //declaration of disp_name function


void delay(); //declaration of delay function
void disp_name1(); //declaration of disp_name1 function
char databyte[]={"0123456789"};
char a,b,c;
void main() //definition of main function
{
P1=0xff; //initialize the port 1 as 11111111
lcd_init(); //call the LCD initialize function
disp_name(); //call the disp_name function
lcd_init1(); //call the LCD1 initialize function
disp_name1(); //call the disp_name1 function

while(1) //endless loop


{
i=P1; // read P1 port for getting analog sensor data
a=i%10; // add scaling factor
b=i/10; // add scaling factor
c=i/100; // add scaling factor
send_command(0xc0); // send command to first location of LCD
send_data(databyte[c]); //send first data
send_command(0xc1); // send command to second location of LCD
send_data(databyte[b]); //send second data
send_command(0xc2); // send command to third location of LCD
send_data(databyte[a
]); //send third data
delay(); //delay for some moment
}
}

void disp_name() //function to display data on first line of LCD


{
char i=0;
char databyte[]={"Gas Contents "}; //Contents to be displayed
send_command(0x80); //Command of first line
while(databyte[i]!='\0')
{
send_data(databyte[i]); //send the data
i++;
}
}

void disp_name1() //function to display data on second line of LCD


{
char i=0;
char databyte[]={" Gas_ PPM "}; //contents to be displayed
send_command(0xC0); //command of second line
while(databyte[i]!='\0')
{
send_data(databyte[i]); //send the data
Interfacing with Analog Sensor 77

i++;
}
}

void delay() //function to provide some delay


{
int i;
for(i=0;i<=250;i++);
}
9 Interfacing of 8051
and NuttyFi/ESP8266
with Digital Sensor
Electronic sensors which act on occurrence on an event and transmission takes place digitally are
referred as digital sensors. Digital sensors provide output as logic “HIGH” or “LOW”, which can
be directly processed by the microcontroller and action can be taken accordingly. This chapter dis-
cusses the basic of digital sensors and its features. The interfacing of digital sensors with 8051 and
NuttyFi is also discussed with the help of circuit diagram and programming.

9.1 PUSH BUTTON


A push button is an easy switch mechanism. Buttons usually made of plastic or metal. The surface
can be fat or formed to accommodate the human fnger on it. It is the simplest example of a digital
sensor. Figure 9.1 shows the push buttons.
Figure 9.2 shows the block diagram for interfacing of push button with NuttyFi/8051 board. The
system comprises of a +12V power supply, a +12V to +5V converter, a NuttyFi/8051, a LED with
resistor 330 Ohm, and a button with breakout board. The objective is to read the status of button, if
it is pressed then LED will be “ON” otherwise “OFF”. Table 9.1 shows the list of the components
required to design the system.

9.1.1 CIRCUIT DIAGRAM


Description of interfacing of push button with ESP8266/NuttyFi
The interfacing of the devices is as follows:

1. Connect output of +12V power supply to input of +12V to +5V convertor.


2. Connect output of +5V power supply to +5V of the NuttyFi/ESP8266.
3. Connect +Vcc, GND and OUT pin of the button to +5V, GND and D1 pins of the NuttyFi
respectively.
4. Connect anode terminal of LED to D2 pin of NuttyFi.
5. Connect cathode terminal of LED to the ground.
6. Connect FTDI programmer to FTDI connector of the NuttyFi board.

Figure 9.3 shows the circuit diagram for interfacing of push button with NuttyFi.
Description of interfacing of push button with 8051
The interfacing of the devices is as follows:

1. Connect output of +12V power supply to input of +12V to +5V convertor.


2. Connect output of +5V power supply to Vcc of 8051.
3. Connect anode terminal of LED to P2.1 of 8051 through a 330 Ohm resistor.
4. Connect cathode terminal of LED to the ground.
5. Push to ON switch is connected to port P2.0 through a pull up resistance of 10k Ohm.

Figure 9.4 shows the circuit diagram for the interfacing of push button with 8051.

79
80 Interfacing with I/O devices

FIGURE 9.1 Push buttons.

FIGURE 9.2 Block diagram for interfacing of push button with NuttyFi/8051 board.

TABLE 9.1
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface ESP8266 with Button


1 +12V power supply 1 Output- +12V/1A
2 +12V to 5V convertor 1 Output- +5V/1A
3 NuttyFi 1 Analog pin-1
Digital pin-10
FTDI connector for programmer
4 Button with breakout board 1 Three pins
5 LED with breakout board 1 Red led

Components Used to Interface 8051 with Button


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 330 Ohm resistor 01 0.25 Watt
4 LED 02 RED
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 Button with breakout board 1 Three pins
Interfacing with Digital Sensor 81

9.1.2 PROGRAM
//Program to interface button sensor with ESP8266

const int BUTTON_Pin=D1; // assign pin D1 to button


const int LED_PIN = D2; // assign pin D2 to LED pin
int BUTTON_Pin_STATE; // assume integer

void setup()
{
pinMode(LED_PIN, OUTPUT); // set D2 as an output
pinMode(BUTTON_Pin, INPUT); // set D1 as an input
}
void loop()
{
BUTTON_Pin_STATE = digitalRead(BUTTON_Pin);// Read button pin
if (BUTTON_Pin_STATE == HIGH) // check the state
{
digitalWrite(LED_PIN, HIGH); // make D2 pin to HIGH
delay(20); // wait for 20mSec
}
else
{
digitalWrite(LED_PIN, LOW); // make D2 pin to LOW
delay(20); // wait for 20mSec
}
}

//Program to interface button sensor with 8051


#include <REG52.h> // Initialization of header file
void delay(); // Declaration of delay function
void main() //Definition of main function
{
P20=1; // initially make the LED switch OFF
while(1)
{
if(P20==0) //Check the output of the switch is 0 or not. (Switch is pressed or not)
{
P21=0; //Glow the LED for some time
delay(); // Make the LED On for some time
P21=1; //Switch OFF the LED
}
}
}
void delay() //Delay function for some random delay generation
{
int i,j;
for(i=0;i<=100;i++)
{
for(j=0;j<=500;j++);
}
}
82 Interfacing with I/O devices

RESISTOR GND GND


+5V +5V
330E Vinput 3.3V

3
D1

NuttyFi-Cloud Device
3.3V

VO
LED- yellow D4
7812 D3 D7
2
GND
D2 D6
REGULATOR
VI D1 D5
+12V Power Supply

Rx D0
1

+50V
A0 A0 D1
Tx
R1

GND
10k
1000uF

Vcc

Rx
-
Tx
GND

-
U1
D2
1N4007

7805
1 3 R2
VI VO +5V 10k
GND
+12V
330
2

D2
Power Supply to board
9-0-9

LED-RED
C1
1000uF D1
AC

LED-RED
GND GND
GND
~

FIGURE 9.3 Circuit diagram for the interfacing of push button with NuttyFi/ESP8266.
+5V

C2 C3
+5V
30pF 30pF GND
X1 8051
19 P0.0/AD0 39
XTAL1
R1 P0.1/AD1 38
P0.2/AD2 37
330R 11.0592MHz 18 36
XTAL2 P0.3/AD3
U2 P0.4/AD4 35 R2
P0.5/AD5 34
10k
3

R7 P0.6/AD6 33
D6 9 32
VO

RST P0.7/AD7
LED-RED
+5V Power Supply

220 21
P2.0/A8
AT89C51

2 22
GND

GND C4 P2.1/A9
23
+ 29 P2.2/A10
24
PSEN P2.3/A11
30 ALE P2.4/A12
25 R3
VI

10uF 31 26 330R
EA P2.5/A13
C1 7805 P2.6/A14 27
P2.7/A15 28
1

R5 1 10 D3
10k P1.0 P3.0/RXD
2 11
P1.1 P3.1/TXD
1000u
3
P1.2 P3.2/INT0
12 LED
4 13
P1.3 P3.3/INT1
D1 5
P1.4 P3.4/T0
14
1N4007 6 15
P1.5 P3.5/T1
GND 7 16
P1.6 P3.6/WR
8 17
P1.7 P3.7/RD
B1
12V
GND

FIGURE 9.4 Circuit diagram for the interfacing of push button with 8051.
Interfacing with Digital Sensor 83

FIGURE 9.5 PIR sensor.

9.2 PIR SENSOR


PIR is a passive infrared device, which measures infrared (IR) light diverging from objects in its
feld of read. The objects with a temperature emit radiation, which is not visible to the human eye
and falls in infrared wavelength range. The term passive refers to the fact that PIR devices don’t
generate or radiate energy for detection functions. They work entirely by infrared emission emitted
by or mirrored from objects. Figure 9.5 shows the PIR sensor.
Figure 9.6 shows the block diagram for the interfacing of PIR sensor with NuttyFi/8051. The
system comprises of a +12V power supply, a +12V to +5V converter, a NuttyFi/ESP8266, a LED, a
resistor 330 Ohm, and a PIR sensor. The objective is to read the status of PIR sensor, if motion sen-
sor state is true then LED will be “ON” otherwise “OFF”. Table 9.2 shows the list of the components
to design the system.

9.2.1 CIRCUIT DIAGRAM


Description of interfacing of a fre sensor with ESP8266
The interfacing of the devices is as follows:

1. Connect output of +12V power supply to input of +12V to +5V convertor.


2. Connect output of +5V power supply to +5V of the NuttyFi/ESP8266.

FIGURE 9.6 Block diagram of the system.


84 Interfacing with I/O devices

TABLE 9.2
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface ESP8266 with Fire Sensor


1 +12V power supply 1 Output- +12V/1A
2 +12V to 5V convertor 1 Output- +5V/1A
3 NuttyFi 1 Analog pin-1
Digital pin-10
FTDI connector for programmer
4 LCD breakout board 1 4 Pins for +5V and 4 Pins for GND
5 LCD 20*4 1 16 pins LCD with backlight pins
6 PIR sensor with breakout board 1 Three pins
7 LED with breakout board 1 RED led
8 +12V power supply 1 Output- +12V/1A
9 +12V to 5V convertor 1 Output- +5V/1A

Components Used to Interface 8051 with Fire Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 330 Ohm resistor 01 0.25 Watt
4 LED 02 RED
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 PIR sensor with breakout board 1 Three pins

3. Connect +Vcc, GND and OUT pin of PIR to +5V, GND and D1 pins of the NuttyFi
respectively.
4. Connect anode terminal of LED to D2 pin of NuttyFi.
5. Connect cathode terminal of LED to the ground.
6. Connect FTDI programmer to FTDI connector of the NuttyFi board.

Figure 9.7 shows the circuit diagram for the interfacing of PIR sensor with NuttyFi/ESP8266.
Description of interfacing of PIR sensor with 8051
The interfacing of the devices is as follows:

1. Connect output of +12V power supply to input of +12V to +5V convertor.


2. Connect output of +5V power supply to Vcc of 8051.
3. Connect +Vcc, GND and OUT pin of PIR to +5V, GND and P2.0 pins of 8051 respectively.
4. Connect anode terminal of LED to P2.1 of 8051.
5. Connect cathode terminal of LED to the ground.

Figure 9.8 shows the circuit diagram for the interfacing of PIR sensor with 8051.

9.2.2 PROGRAM
//Program to interface PIR sensor with ESP8266

const int MOTIONSENSOR_Pin=D1; // assign D1 pin to motion sensor


const int INDICATOR_PIN = D2; // assign D2 pin to indicator
int MOTIONSENSOR_Pin_STATE; // assign integer
Interfacing with Digital Sensor 85

void setup()
{
pinMode(INDICATOR_PIN, OUTPUT); // set D2 as an output
pinMode(MOTIONSENSOR_Pin, INPUT_PULLDOWN); // set D1 as an input
}

void loop()
{
MOTIONSENSOR_Pin_STATE = digitalRead(MOTIONSENSOR_Pin);// Read PIR Sensor pin
if (MOTIONSENSOR_Pin_STATE == HIGH) // check state
{
digitalWrite(INDICATOR_PIN, HIGH); // set D2 pin to HIGH
delay(20); // wait for 20 mSec
}
else
{
digitalWrite(INDICATOR_PIN, LOW); // set D2 pin to LOW
delay(20); // wait for 20 mSec
}
}

+5V

D1

GND
GND GND
RESISTOR +5V TestPin
Vinput 3.3V Vcc OUT GND
NuttyFi-Cloud Device

330E
D4 3.3V
3

D1
D3 D7
VO

LED-yellow

2
7812 D2 D6
GND
REGULATOR
D1 D5
Rx D0
VI

Tx A0 A0
+12V Power Supply

+50V
GND

Vcc

Rx
Tx

1000uF
www.TheEngineeringProjects.com

U1 PIR1
1N4007

7805 PIR SENSOR3


1 3
VI VO +5V
GND

+12V 330E
330 R1
D2
2

D2
LED-GREEN
9-0-9

Power Supply to board


C1
1000uF D1
AC

LED-RED
GND
GND

FIGURE 9.7 Circuit diagram for the interfacing of PIR sensor with NuttyFi/ESP8266.
86 Interfacing with I/O devices

//Program to interface PIR with 8051

#include <REG52.h> // Initialization of header file


void delay(); // Declaration of delay function
void main() //Definition of main function
{
P20=1; // initially make the LED switch OFF
while(1)
{
if(P20==0) //Check the output of the PIR sensor is it 0 or not.
{
P21=0; //Glow the LED for some time
delay(); // Make the LED On for some time
P21=1; //Switch OFF the LED
}
}
}

void delay() //Delay function for some random delay generation


{
int i,j;
for(i=0;i<=100;i++)
{
for(j=0;j<=500;j++);
}
}
+5V

C2 C3
30pF 30pF GND
X1 U1
19 39
R1 XTAL1 P0.0/AD0
38
P0.1/AD1
330R 37
11.0592MHz P0.2/AD2
18 P0.3/AD3 36
XTAL2
U2 P0.4/AD4 35
3

P0.5/AD5 34
R7 33
D6 9
P0.6/AD6
32
VO

LED-RED RST P0.7/AD7


220 21
2 P2.0/A8
GND

AT89C51

GND 22
C4 P2.1/A9
+5V Power Supply

23
+ 29 P2.2/A10
24
PSEN P2.3/A11
30 25 R3
VI

ALE P2.4/A12
10uF 31 26
C1 7805 EA P2.5/A13
27 330R
P2.6/A14
28
1

R5 P2.7/A15
10k 1 10 D3
P1.0 P3.0/RXD
2 11
1000u P1.1 P3.1/TXD
3 12 LED
P1.2 P3.2/INT0
D1 4 13
P1.3 P3.3/INT1
1N4007 5 14
P1.4 P3.4/T0
6 15
P1.5 P3.5/T1
7 16
PIR1 GND
8
P1.6 P3.6/WR
17
B1 PIR SENSOR P1.7 P3.7/RD
12V

GND
www.TheEngineeringProjects.com

Vcc OUT GND


TestPin

FIGURE 9.8 Circuit diagram for the interfacing of PIR sensor with 8051.
Interfacing with Digital Sensor 87

FIGURE 9.9 Flame sensor.

9.3 FLAME/FIRE SENSOR


A fame sensor is designed to detect the presence of a fre in the surroundings. It can be used in
the applications like industrial furnaces, home safety, etc. A fame sensor responds or will respond
quicker and accurate than a smoke or heat detector. Figure 9.9 shows fre sensor.
Figure 9.10 shows the block diagram for interfacing the fre sensor with NuttyFi/8051. The sys-
tem comprises of a +12V power supply, a +12V to +5V converter, a NuttyFi/8051, a LED with resis-
tor 330 Ohm, and a fre sensor. The objective is to read the status of fre sensor, if fre sensor state is
true then LED will be “ON” otherwise “OFF”. Table 9.3 shows the list of the components to design
the system.

9.3.1 CIRCUIT DIAGRAM


Description of interfacing of fre sensor with ESP8266/NuttyFi
The interfacing of the devices is as follows:

1. Connect output of +12V power supply to input of +12V to +5V convertor.


2. Connect output of +5V power supply to +5V of the NuttyFi/ESP8266.
3. Connect +Vcc, GND and OUT pin of fame sensor to +5V, GND and D1 pins of the NuttyFi
respectively.
4. Connect anode terminal of LED to D2 pin of NuttyFi.
5. Connect cathode terminal of LED to the ground.
6. Connect FTDI programmer to FTDI connector of the NuttyFi.

FIGURE 9.10 Block diagram of the system.


88 Interfacing with I/O devices

TABLE 9.3
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface ESP8266 with Fire Sensor


1 +12V power supply 1 Output- +12V/1A
2 +12V to 5V convertor 1 Output- +5V/1A
3 NuttyFi 1 Analog pin-1
Digital pin-10
FTDI connector for programmer
LCD breakout board 1 4 Pins for +5V and 4 Pins for GND
LCD 20*4 1 16 pins LCD with backlight pins
4 Fire sensor with breakout board 1 Three pins
5 Relay with breakout board 1 To run the hooter

Components Used to Interface 8051 with Fire Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 330 Ohm resistor 01 0.25 Watt
4 LED 02 RED
5 8051 development board 01 Atmel Make including crystal oscillator
and reset circuit
6 Fire sensor with breakout board 1 Three pins

FLAME 1 FLAME SENSOR

RESISTOR
GND GND
+5V 330E
Vinput 3.3V
3

D1
NuttyFi-Cloud Device

VO

3.3V LED-yellow
D4
2 7812
D3 D7 GND
D2 D6 REGULATOR
RE GULATOR
VI

Flame Sensor D1 D5
+12V Power Supply

Rx D0
1

GND OUT Vcc +50V


Tx A0 A0
+5V
D1

GND

GND 1000uF
Vcc

Rx
Tx

+12V
HOOTER
1N4007

220V U1
12V
7805
RELAY 1 3
VI VO +5V
GND

1N4007 +12V
D3 330
2

AC SOURCE
R3 Q1 Power Supply to board
9-0-9

D2 2N2222 C1
330 ohm D1
1000uF
AC

LED-RED
GND
GND
GND

FIGURE 9.11 Circuit diagram for the interfacing of fre sensor with NuttyFi.
Interfacing with Digital Sensor 89

+5V
C2 C3
30pF 30pF GND
X1 U1
19 39
R1 XTAL1 P0.0/AD0
38
P0.1/AD1
330R 37
11.0592MHz P0.2/AD2
18 P0.3/AD3 36
XTAL2
U2 P0.4/AD4 35
3

P0.5/AD5 34
D6 R7 9
P0.6/AD6 33
32
VO

LED-RED RST P0.7/AD7


220 21
2

AT89C51
P2.0/A8

GND
GND 22
C4 P2.1/A9
+5V Power Supply

23
+ 29 P2.2/A10
24
PSEN P2.3/A11
30 25 R3
VI

ALE P2.4/A12
10uF 31 26 330R
EA P2.5/A13
C1 7805 27
1

P2.6/A14
28
R5 P2.7/A15
10k 1
P1.0 P3.0/RXD 10 D3
2 11
1000u P1.1 P3.1/TXD
3 12 LED
P1.2 P3.2/INT0
D1 4
P1.3 P3.3/INT1 13
1N4007 5 14
P1.4 P3.4/T0
FLAME1 6 15
P1.5 P3.5/T1
GND 7 16
FLAME SENSOR P1.6 P3.6/WR
8 17
B1 P1.7 P3.7/RD
12V
GND
TestPin

Flame Sensor
GND OUT Vcc

www.TheEngineeringProjects.com

FIGURE 9.12 Circuit diagram for the interfacing of fre sensor with 8051.

Figure 9.11 shows the circuit diagram for interfacing fre sensor with NuttyFi.
Description of interfacing of fre sensor with 8051
The interfacing of the devices is as follows:

1. Connect output of +12V power supply to input of +12V to +5V convertor.


2. Connect output of +5V power supply to Vcc pin of 8051.
3. Connect +Vcc, GND and OUT pin of fame sensor to +5V, GND and P2.0 pins of 8051
respectively.
4. Connect anode terminal of LED to P2.1 pin of 8051.
5. Connect cathode terminal of LED to the ground.

Figure 9.12 shows the circuit diagram for interfacing fre sensor with 8051.

9.3.2 PROGRAM
//Program to interface fre sensor with ESP8266

const int FIRESENSOR_Pin=D1; // assign pin D1 to fire sensor


const int RELAY_PIN = D2; // // assign pin D1 to relay
int FIRESENSOR_Pin_STATE; // assign integer
void setup()
{
pinMode(RELAY_PIN, OUTPUT); // set D2 pin as an output
pinMode(FIRESENSOR_Pin, INPUT_PULLUP); // set D1 pin as an input
}
90 Interfacing with I/O devices

void loop()
{
FIRESENSOR_Pin_STATE = digitalRead(FIRESENSOR_Pin);// Read fire Sensor pin
if (FIRESENSOR_Pin_STATE == LOW) // check status
{
digitalWrite(RELAY_PIN, HIGH); // set D2 pin to HIGH
delay(20); // wait for 20 msec
}
else
{
digitalWrite(RELAY_PIN, LOW); // set D2 pin to HIGH
delay(20); // wait for 20 msec
}
}

//Program to interface fre sensor with 8051

#include <REG52.h> // Initialization of header file


void delay(); // Declaration of delay function

void main() //Definition of main function


{
P20=1; // initially make the LED switch OFF
while(1)
{
if(P20==0) //Check the output of the fire sensor is it 0 or not.
{
P21=0; //Glow the LED for some time
delay(); // Make the LED On for some time
P21=1; //Switch OFF the LED
}
}
}

void delay() //Delay function for some random delay generation


{
int i,j;
for(i=0;i<=100;i++)
{
for(j=0;j<=500;j++);
}
}
Section C
Interfacing of 8051 Microcontroller and
NuttyFi/ESP8266 with Special Devices
10 Interfacing of 8051
and NuttyFi/ESP8266
with UART Based Devices
A universal asynchronous receiver-transmitter is used for asynchronous serial communication. In this
the data format and transmission speeds are confgurable. A device, the universal synchronous and asyn-
chronous receiver-transmitter (USART), also supports synchronous operation. The universal asynchro-
nous receiver-transmitter (UART) takes bytes of data and communicates the bits in a sequential fashion.
This chapter discusses the interfacing of devices in UART with 8051 and NuttyFi.

10.1 ULTRASONIC SENSOR


Ultrasonic module works on the principle of SONAR. The module has specifcation of working on
UART. When a pulse of 10 µsec or more is given to the Trig pin, 8 pulses of 40 kHz are generated.
Figure 10.1 shows the block diagram for interfacing of ultrasonic sensor (UART) with 8051. The
system comprises of +12V power supply, +12V to +5V converter, 8051 microcontroller, 16x2 LCD,
and UART. The objective of the system is to read the status of UART using 8051 and display on
LCD. Table 10.1 shows the list of the components to design the system.

10.1.1 CIRCUIT DIAGRAM


Description of interfacing of ultrasonic sensor with 8051 in UART
1. Connect +12V power supply output to input of +12V to +5V convertor.
2. Connect +5V output to power up the 8051 board.
3. LCD is connected with port three in 4 pin mode.
4. Rx of UART is connected to port P3.0.
5. Connect one pin of buzzer to P2.3.
6. Connect another pin of buzzer to ground.
7. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
Figure 10.2 shows the connection diagram of the system.

10.1.2 PROGRAM
#include <REGX51.H>
#include <studio.h>
#include "lcd.h"
#include "utils.h"
sbit BUZZER=P2^3;
charsbuf[10], c, length;
charbuf[16];
unsigned char pos;
intrange;

charmygetchar(void)
{

93
94
Interfacing with Special Devices

charc;
while(!RI);
RI =0;
c = SBUF;
myputchar(c); // echo to terminal
returnSBUF;
}
voidconvert()
{
range= (sbuf[0]-0x30)*100;
range= range+ (sbuf[1]-0x30)*10;
range= range+ (sbuf[2]-0x30)*1;
}
voidmain()
{
// -=-=- Intialize variables -=-=-=
BUZZER = 0;
SCON = 0x52; // 8-bit UART mode
TMOD = 0x20; // timer 1 mode 2 auto reload
TH1= 0xfd; // 9600 8-n-1
TR1 = 1; // run timer1
lcdInit();
lcdGotoXY(0,0); // 1st Line of LCD
lcdPrint("WELCOME TO ");
lcdGotoXY(0,1); // 2nd Line of LCD
lcdPrint("COLLISION SYSTEM");
delayms(5000); // 5 sec
pos= 0;
length= 0;
// -=-=- Program Loop -=-=-=
while(1)
{
c = mygetchar(); //loop till character received
if(c==0x0D) // if received character is <CR> end of line, time to display
{
length= pos;
pos= 0;
convert(); // convert serial buffer to integer
lcdClear();
lcdGotoXY(0,0); // 1st Line of LCD
sprintf (buf, "Range: %c%c%c%c%c%c cm", sbuf[0], sbuf[1], sbuf[2],
sbuf[3], sbuf[4], sbuf[5]);
lcdPrint(buf);
if(range <20) // we check if range is less than 20
{
BUZZER= 1;
lcdGotoXY(0,1); // 2nd Line of LCD
lcdPrint("Collision!!!!");
} else
{
BUZZER= 0;
lcdGotoXY(0,1); // 2nd Line of LCD
lcdPrint("Preset:20");
}
} else
{
sbuf[pos] = c;
pos++;
}
}
}
Interfacing with UART Based Devices 95

FIGURE 10.1 Block diagram of the system.

TABLE 10.1
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface 8051 uC with Ultrasonic Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 Crystal Oscillator 01 12 MHz
4 LCD 01 16x2
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 Ultrasonic sensor 01 UART

FIGURE 10.2 Circuit diagram of the system.


96 Interfacing with Special Devices

FIGURE 10.3 Block diagram of the system.

10.2 DHT11
DHT is very basic temperature and humidity sensor. The DHT sensors are made of two parts,
a capacitive humidity sensor and a thermistor. The digital signal is fairly easy to read using any
microcontroller.
Figure 10.3 shows the block diagram for interfacing of UART with 8051. The system comprises
of +12V power supply, +12V to +5V converter, 8051 microcontroller, 16x2 LCD, and DHT11. The
objective of the system is to read the DHT sensor using 8051 and display on LCD. Table 10.2 shows
the list of the components to design the system.

10.2.1 CIRCUIT DIAGRAM


Description of interfacing of DHT11 sensor with 8051 in UART

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the 8051 board.
3. LCD is connected with port three in 4 pin mode.
4. Rx of UART is connected to port P1.0.
5. Connect one pin of buzzer to P2.3.

TABLE 10.2
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface 8051 uC with Ultrasonic Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 Crystal Oscillator 01 12 MHz
4 LCD 01 16x2
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 DHT11 sensor 01 UART
Interfacing with UART Based Devices 97

FIGURE 10.4 Circuit diagram of the system.

6. Connect another pin of buzzer to ground.


7. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.

Figure 10.4 shows the connection diagram of the system.

10.2.2 PROGRAM
#include<reg51.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include "LCD16x2_4bit.h"

sbit DHT11=P2^1; /* Connect DHT11 output Pin to P2.1 Pin */


intI_RH,D_RH,I_Temp,D_Temp,CheckSum;

void timer_delay20ms() /* Timer0 delay function */


{
TMOD = 0x01;
TH0 = 0xB8;
TL0 = 0x0C;
TR0 = 1;
while(TF0 == 0);
TR0 = 0;
TF0 = 0;
}

void timer_delay30us() /* Timer0 delay function */


{
98 Interfacing with Special Devices

TMOD = 0x01; /* Timer0 mode1 (16-bit timer mode) */


TH0 = 0xFF; /* Load higher 8-bit in TH0 */
TL0 = 0xF1; /* Load lower 8-bit in TL0 */
TR0 = 1; /* Start timer0 */
while(TF0 == 0); /* Wait until timer0 flag set */
TR0 = 0; /* Stop timer0 */
TF0 = 0; /* Clear timer0 flag */
}

void Request() /* Microcontroller send request */


{
DHT11 = 0; /* set to low pin */
timer_delay20ms(); /* wait for 20ms */
DHT11 = 1; /* set to high pin */
}

void Response() /* Receive response from DHT11 */


{
while(DHT11==1);
while(DHT11==0);
while(DHT11==1);
}

intReceive_data() /* Receive data */


{
intq,c=0;
for (q=0; q<8; q++)
{
while(DHT11==0);/* check received bit 0 or 1 */
timer_delay30us();
if(DHT11 == 1) /* If high pulse is greater than 30ms */
c = (c<<1)|(0x01);/* Then its logic HIGH */
else /* otherwise its logic LOW */
c = (c<<1);
while(DHT11==1);
}
return c;
}

void main()
{
unsigned char dat[20];
LCD_Init(); /* initialize LCD */

while(1)
{
Request();
Response();

I_RH=Receive_data();
D_RH=Receive_data();
I_Temp=Receive_data();
D_Temp=Receive_data();
CheckSum=Receive_data();

if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)


Interfacing with UART Based Devices 99

{
LCD_String_xy(0,0,"Error");
}

else
{
sprintf(dat,"Hum = %d.%d",I_RH,D_RH);
LCD_String_xy(0,0,dat);
sprintf(dat,"Tem = %d.%d",I_Temp,D_Temp);
LCD_String_xy(1,0,dat);
LCD_Char(0xDF);
LCD_String("C");
memset(dat,0,20);
sprintf(dat,"%d ",CheckSum);
LCD_String_xy(1,13,dat);
}
delay(100);
}
}
11 Interfacing of 8051
with I2C Based Devices
This chapter discusses the interfacing of 8051 with peripheral devices in I2C mode. I2C stands for
inter IC communication. The interfacing of devices with 8051 and NuttyFi is discussed with the help
of circuit diagram and programming.

11.1 DHT11
DHT11 is a single wire digital humidity and temperature sensor, which provides humidity and
temperature values serially. It can measure relative humidity in percentage (20–90% RH) and tem-
perature in degree Celsius in the range of 0–50°C. It has four pins of which two pins are connected
with power supply, one is not used and the last one is used for data. The data is the only pin used for
communication. Pulses of different TON and TOFF are decoded as logic “1” or logic “0” or start
pulse or end of the frame.
Figure 11.1 shows the block diagram for interfacing of DHT11 with 8051. The system comprises
of +12V power supply, +12V to +5V converter, 8051 microcontroller, 16x2 LCD, DHT11/22 sensor
with breakout board, and Buzzer. The objective of the system is to read the status of DHT11/22 sen-
sor using 8051. Table 11.1 shows the list of the components to design the system.

11.1.1 CIRCUIT DIAGRAM


Description of interfacing of DHT11 sensor with 8051

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the 8051 board.
3. LCD is connected with port three in 4-pin mode.
4. DHT11 sensor is connected to port P2.0.
5. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming also.

FIGURE 11.1 Block diagram for interfacing of DHT11 with 8051.

101
102 Interfacing with Special Devices

TABLE 11.1
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface 8051 uC with Fire Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 Crystal Oscillator 01 12 MHz
4 LCD 02 16x2
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 DHT11/22 sensor 1 Three pins

FIGURE 11.2 Circuit diagram for interfacing of DHT11 with 8051.

Figure 11.2 shows the circuit diagram of the system.

11.1.2 PROGRAM
Program to interface DHT11 sensor with 8051

#include<reg51.h>
#include "lcd.h"
#include<stdio.h>
#include<string.h>
#include <stdlib.h>

sbit DHT11=P2^1; /* Connect DHT11 output Pin to P2.1 Pin */


int I_RH,D_RH,I_Temp,D_Temp,CheckSum;

void timer_delay20ms() /* Timer0 delay function */


{
TMOD = 0x01;
TH0 = 0xB8; /* Load higher 8-bit in TH0 */
TL0 = 0x0C; /* Load lower 8-bit in TL0 */
Interfacing with I2C Based Devices 103

TR0 = 1; /* Start timer0 */


while(TF0 == 0); /* Wait until timer0 flag set */
TR0 = 0; /* Stop timer0 */
TF0 = 0; /* Clear timer0 flag */
}

void timer_delay30us() /* Timer0 delay function */


{
TMOD = 0x01; /* Timer0 mode1 (16-bit timer mode) */
TH0 = 0xFF; /* Load higher 8-bit in TH0 */
TL0 = 0xF1; /* Load lower 8-bit in TL0 */
TR0 = 1; /* Start timer0 */
while(TF0 == 0); /* Wait until timer0 flag set */
TR0 = 0; /* Stop timer0 */
TF0 = 0; /* Clear timer0 flag */
}

void Request() /* Microcontroller send request */


{
DHT11 = 0; /* set to low pin */
timer_delay20ms(); /* wait for 20ms */
DHT11 = 1; /* set to high pin */
}
void Response() /* Receive response from DHT11 */
{
while(DHT11==1);
while(DHT11==0);
while(DHT11==1);
}

int Receive_data() /* Receive data */


{
int q,c=0;
for (q=0; q<8; q++)
{
while(DHT11==0);/* check received bit 0 or 1 */
timer_delay30us();
if(DHT11 == 1) /* If high pulse is greater than 30ms */
c = (c<<1)|(0x01);/* Then its logic HIGH */
else /* otherwise its logic LOW */
c = (c<<1);
while(DHT11==1);
}
return c;
}

void main()
{
unsigned char dat[20];
LCD_Init(); /* initialize LCD */

while(1)
{
Request(); /* send start pulse */
Response(); /* receive response */
104 Interfacing with Special Devices

I_RH=Receive_data(); /* store first eight bit in I_RH */


D_RH=Receive_data(); /* store next eight bit in D_RH */
I_Temp=Receive_data(); /* store next eight bit in I_Temp */
D_Temp=Receive_data(); /* store next eight bit in D_Temp */
CheckSum=Receive_data();/* store next eight bit in CheckSum */
if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
{
LCD_String_xy(0,0,"Error");
}

else
{
sprintf(dat,"Hum = %d.%d",I_RH,D_RH);
LCD_String_xy(0,0,dat);
sprintf(dat,"Tem = %d.%d",I_Temp,D_Temp);
LCD_String_xy(1,0,dat);
LCD_Char(0xDF);
LCD_String("C");
memset(dat,0,20);
sprintf(dat,"%d ",CheckSum);
LCD_String_xy(1,13,dat);
}

delay(100);
}
}

11.2 ULTRASONIC SENSOR


Ultrasonic Module HC-SR04 works on the principle of SONAR system. The HC-SR04 module has
ultrasonic transmitter, receiver, and control circuit on a single board.
The module has four pins, Vcc, Gnd, Trig, and Echo. When a pulse of 10µsec or more is given to
the Trig pin, eight pulses of 40 kHz are generated. After this, the Echo pin is made high by the control
circuit in the module. Echo pin remains high till it gets echo signal of the transmitted pulses back. The
time for which the echo pin remains high, that is, the width of the Echo pin gives the time taken for gen-
erated ultrasonic sound to travel toward the object and return. Using this time and the speed of sound in
air, we can fnd the distance of the object by a simple formula for distance using speed and time.
Figure 11.3 shows the block diagram for interfacing of ultrasonic sensor with 8051. The system
comprises of +12V power supply, +12V to +5V converter, 8051 microcontroller, 16x2 LCD, and
Ultrasonic sensor with breakout board. The objective of the system is to read the status of Ultrasonic
sensor using 8051. Table 11.2 shows the list of the components to design the system.

11.2.1 CIRCUIT DIAGRAM


Description of interfacing of ultrasonic sensor with 8051
1. Connect +12V power supply output to input of +12V to +5V convertor.
2. Connect +5V output to power up the 8051 board.
3. LCD is connected with port three in 4-pin mode.
4. Ultrasonic sensor is connected to port P2.0 and P2.1.
5. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
Figure 11.4 shows the connection diagram of the system.
Interfacing with I2C Based Devices 105

FIGURE 11.3 Block diagram for interfacing of ultrasonic sensor with 8051.

TABLE 11.2
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface 8051 uC with Ultrasonic Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 Crystal Oscillator 01 12 MHz
4 LCD 01 16x2
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 Ultrasonic sensor 1 Four pins

FIGURE 11.4 Circuit diagram for interfacing of ultrasonic sensor with 8051.
106 Interfacing with Special Devices

11.2.2 PROGRAM
#include<reg52.h>
#include <stdio.h>
#include <lcd.h>
#include <math.h>
#define sound_velocity 34300 /* sound velocity in cm per second */
#define period_in_us pow(10,-6)
#define Clock_period 1.085*period_in_us /* period for clock cycle of 8051*/

sbit Trigger_pin=P2^0; /* Trigger pin */


sbit Echo_pin=P2^1; /* Echo pin */

void main()
{
float distance_measurement, value;
unsigned char distance_in_cm[10];
lcd_init(); /* Initialize 16x2 LCD */
display(0x80, "Distance:");
init_timer(); /* Initialize Timer*/

while(1)
{
send_trigger_pulse(); /* send trigger pulse of 10us */
while(!Echo_pin); /* Waiting for Echo */
TR0 = 1; /* Timer Starts */
while(Echo_pin && !TF0); /* Waiting for Echo goes LOW */
TR0 = 0; /* Stop the timer */

/* calculate distance using timer */


value = Clock_period * sound_velocity;
distance_measurement = (TL0|(TH0<<8)); /* read timer register for time count */
distance_measurement = (distance_measurement*value)/2.0; /* find distance(in cm) */
display(0xC0, distance_in_cm); /* show distance on 16x2 LCD */
display (0x80, " cm ");
delay(100);
}
}

void Delay_us()
{
TL0=0xF5;
TH0=0xFF;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}

void init_timer()
{
TMOD=0x01; /*initialize Timer*/
TF0=0;
TR0 = 0;
}

void send_trigger_pulse()
Interfacing with I2C Based Devices 107

{
Trigger_pin= 1; /* pull trigger pin HIGH */
Delay_us(); /* provide 10uS Delay*/
Trigger_pin = 0; /* pull trigger pin LOW*/
}

void display(char x, char databyte[])


{
char y=0;
send_command(x);
while(databyte[y]!='\0')
{
send_data(databyte[y]);
y++;
}
}

11.3 EEPROM
An EEPROM (Electrically Erasable Programmable Read Only Memory) is a non-volatile fash
memory, which has the capability to retain data even if the power is removed. The most commonly
used EEPROM family is 24CXX series such as 24C02, 24C04, 24C08Â, etc. The most common
IC is EEPROMIC24C04. Interfacing an EEPROM to microcontroller is simple. Only need to make
two connections between the 24C04 IC and 8051 microcontroller.
Figure 11.5 shows the block diagram for interfacing EEPROM with 8051. The system comprises
of +12V power supply, +12V to +5V converter, 8051 microcontroller, 16x2 LCD, and EEPROM
with breakout board. The objective of the system is to store and read the data using 8051. Table 11.3
shows the list of the components to design the system.

11.3.1 CIRCUIT DIAGRAM


Description of interfacing of EEPROM with 8051

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the 8051 board.

FIGURE 11.5 Block diagram for interfacing EEPROM with 8051.


108 Interfacing with Special Devices

TABLE 11.3
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface 8051 uC with Ultrasonic Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 Crystal Oscillator 01 12 MHz
4 LCD 02 16x2
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 EEPROM 1 24C04

FIGURE 11.6 Circuit diagram for interfacing EEPROM with 8051.

3. LCD is connected with port three in 4-pin mode.


4. EEPROM is connected to port P2.6 and P2.7.
5. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.

Figure 11.6 shows the circuit diagram for interfacing EEPROM with 8051.

11.3.2 PROGRAM
#include<reg52.h>
sbit REG_MSB=B^7;
sbit REG_LSB=B^0;
sbit SDA=P2^6;
sbit SCL=P2^7;
void delay();

void start();
void stop();
void send_data(char);
char receive_data();
Interfacing with I2C Based Devices 109

void main()
{
start();
send_data(0xa0); // device address
send_data(0x01); // word address
send_data(0xaa); // data send
stop();
start();
send_data(0xa1); // device address
receive_data();
stop();

P0=B;
}
/////////////////////////////////////////////
void send_data(char x)
{
int i;
B=x;
for(i=0;i<8;i++)
{
SCL=0;
delay();
SDA=REG_MSB;
SCL=1;
delay();
SCL=0;
delay();
B=B<<1;
}
SDA=1;
SCL=1;
delay();
SCL=0;
delay();
}
//////////////////////////////////////////////
char receive_data()
{
int i;
for(i=0;i<8;i++)
{
SCL=1;
delay();
SCL=0;
delay();
REG_LSB=SDA;
B=B<<1;
delay();
}
return B;
}
////////////////////////////////////////////////
void start()
{
SCL=0;
110 Interfacing with Special Devices

SDA=1;
delay();
SCL=1;
delay();
SDA=0;
delay();
SCL=0;
}
///////////////////////////////////////////////
void stop()
{
delay();
SDA=0;
SCL=1;
delay();
SDA=1;
delay();
SCL=0;
}
//////////////////////////////////////////////
void delay()
{
int i;
for(i=0;i<=100;i++);
}

11.4 DS1307
A Real Time Clock module is basically a time tracking device, which gives the current time and
date. The most commonly used RTC module is DS1307 IC. Only two connections are required to
connect between the RTC module and 8051.
Figure 11.7 shows the block diagram for interfacing RTC with 8051. The system comprises of
+12V power supply, +12V to +5V converter, 8051 microcontroller, 16x2 LCD, and EEPROM with
breakout board. The objective of the system is to store and read the data using 8051.
Table 11.4 shows the list of the components to design the system.

11.4.1 CIRCUIT DIAGRAM


Description of interfacing of DS1307 with 8051
Figure 11.8 shows the circuit diagram for interfacing RTC with 8051.

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the 8051 board.
3. LCD is connected with port three in 4-pin mode.
4. DS1307 is connected to port P2.6 and P2.5.
5. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
Interfacing with I2C Based Devices 111

FIGURE 11.7 Block diagram for interfacing RTC with 8051.

TABLE 11.4
Components List to Design System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface 8051 uC with Ultrasonic Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 Crystal Oscillator 01 12 MHz
4 LCD 02 16x2
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 RTC 1 DS1307

FIGURE 11.8 Circuit diagram for interfacing RTC with 8051.


112 Interfacing with Special Devices

11.4.2 PROGRAM
#include<reg51.h>
sbit SCL=P2^5;
sbit SDA=P2^6;
void start();
void write(unsigned char);
delay(unsigned char);

void main()
{
start();
write(0xA2); //slave address//
write(0x00); //control register address//
write(0x00); //control register 1 value//
write(0x00); //control regiter2 value//
write (0x28); //sec value//
write(0x50) ;//minute value//
write(0x02);//hours value//
}

void start()
{
SDA=1; //processing the data//
SCL=1; //clock is high//
delay(100);
SDA=0; //sent the data//
delay(100);
SCL=0; //clock signal is low//
}
void write(unsigned char d)
{
unsigned char k, j=0×80;
for(k=0;k<8;k++)
{
SDA=(d&j);
J=j>>1;
SCL=1;
delay(4);
SCL=0;
}
SDA=1;
SCL=1;
delay(2);
c=SDA;
delay(2);
SCL=0;
}

void delay(int p)
{
unsigned int a,b;
For(a=0;a<255;a++); //delay function//
For(b=0;b<p;b++);
}
//Read Operation from Slave to Master
Interfacing with I2C Based Devices 113

#include<reg51.h>
sbit SCL=P2^5;
sbit SDA=P2^6;
void start();
void write(usigned char );
void read();
void ack();
void delay(unsigned char);

void main()
{
start();
write(0xA3);// slave address in read mode//
read();
ack();
sec=value;
}
void start()
{
SDA=1; //processing the data//
SCL=1; //clock is high//
delay(100);
SDA=0; //sent the data//
delay(100);
SCL=0; //clock signal is low//
}
void write(unsigned char d)
{
unsigned char k, j=0×80;
for(k=0;k<8;k++)
{
SDA=(d&j);
J=j>>1;
SCL=1;
delay(4);
SCL=0;
}
SDA=1;
SCL=1;
delay(2);
c=SDA;
delay(2);
SCL=0;
}
void delay(int p)
{
unsignedinta,b;
For(a=0;a<255;a++); //delay function//
For(b=0;b<p;b++);
}
Void read ()
{
Unsigned char j, z=0×00, q=0×80;
SDA=1;
for(j=0;j<8;j++)
{
114 Interfacing with Special Devices

SCL=1;
delay(100);
flag=SDA;
if(flag==1)
{
z=(z|q);
q=q>>1;
delay (100);
SCL=0;
}
void ack()
{
SDA=0; //SDA line goes to low//
SCL=1; //clock is high to low//
delay(100);
SCL=0;
}
12 Interfacing of 8051
with SPI Based Devices
The SPI communication stands for serial peripheral interface communication protocol, which was
developed by the Motorola in 1972. SPI interface can be done on popular communication con-
trollers such as 8051, PIC, AVR, ARM controller, etc. It has synchronous serial communication
data link that operates in full duplex, which means the data signals carry on both the directions
simultaneously.

12.1 INTRODUCTION
SPI protocol consists of four wires such as MISO, MOSI, CLK, and SS used for master/slave com-
munication. The master is a microcontroller, and the slaves are other peripherals like sensors, GSM
modem, GPS modem, etc. The multiple slaves are interfaced to the master through a SPI serial
bus. The SPI protocol does not support the Multi-master communication and it is used for a short
distance within a circuit board.

12.1.1 SPI LINES


MISO (Master in Slave Out): The MISO line is confgured as an input in a master device and as an
output in a slave device.
MOSI (Master out Slave In): The MOSI is a line confgured as an output in a master device and
as an input in a slave device wherein it is used to synchronize the data movement.
SCK (Serial Clock): This signal is always driven by the master for synchronous data transfer
between the master and the slave. It is used to synchronize the data movement both in and out
through the MOSI and MISO lines.
SS (Slave Select) and CS (Chip Select): This signal is driven by the master to select individual
slaves/peripheral devices. It is an input line used to select the slave devices.
Figure 12.1 shows the block diagram to interface SPI device with 8051. The system comprises
of +12V power supply, +12V to +5V converter, 8051 microcontroller, 16x2 LCD, and EEPROM
with breakout board. The objective of the system is to interface the devices in SPI mode with 8051.
Table 12.1 shows the list of the components to design the system.

12.2 CIRCUIT DIAGRAM


Description of interfacing of SPI device with 8051

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the 8051 board.
3. LCD is connected with port three in 4-pin modes.
4. SPI sensor is connected to P1.4 to P1.7.
5. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.

Figure 12.2 shows the circuit diagram of the system.

115
116 Interfacing with Special Devices

FIGURE 12.1 Block diagram of the system.

TABLE 12.1
List of the Components to Design the System
Sr. No. Name of the Components Quantity Specifcations
1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 Crystal Oscillator 01 12 MHz
4 LCD 02 16x2
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 SPI enabled device 1 Any SPI device

FIGURE 12.2 Circuit diagram of the system.


Interfacing with SPI Based Devices 117

12.3 PROGRAM
#define char unsigned char

sbit RF_MISO= P2^3; //say


sbit RF_MOSI= P2^2;
sbit RF_SCK= P2^1;
sbit RF_CSN= P2^0;

void spiWr(uchar);
uchar spiRd(void);

/*Low level SPI WRITE FUNCTION*/


void spiWr(uchar dat)
{
/* software SPI, send MSB first */
static uchar i,c;
c = dat;
for(i=0;i<8;i++)
{
if((c&0x80)==0x80)
RF_MOSI = 1;
else
RF_MOSI = 0;
RF_SCK = 1;
c=c<<1;
RF_SCK = 0;
}
}

/* Low level SPI READ FUNCTION*/


char spiRd(void)
{
/* software SPI read, MSB read first */
static uchar i, dat;
for(i=0;i<8;i++)
{
dat = dat<<1;
RF_SCK = 1;
if(RF_MISO)
dat = dat+1;
RF_SCK = 0;
}
return dat;
}
13 Interfacing of 8051 with One
Wire Interface-based Devices
This chapter explores one wire hardware and software interfacing with 8051 microcontroller. The
interfacing of devices with 8051 and NuttyFi is discussed with the help of circuit diagram and
programming.

13.1 DS1820
DS18B20 is a temperature sensor, which can measure temperature from −55°C to +125°C with an
accuracy of ±5%. It can be used in industrial applications where high accuracy is required. Data
received from a single wire is of 9–12 bit. Sensor has three pins in total, Vcc, Gnd, and data pin.
Figure 13.1 shows the block diagram for the system. The system comprises +12V power supply,
+12V to +5V converter, 8051 microcontroller, 16x2 LCD, and DS1820 sensor with breakout board.
The objective of the system is to read the data from sensor with one wire protocol. Table 13.1 shows
the list of the components to design the system.

13.1.1 CIRCUIT DIAGRAM


Description of interfacing of DS1820 with 8051

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the 8051 board.
3. LCD is connected with port three in 4-pin mode.
4. DS1820 is connected to port P1.7.
5. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.

Figure 13.2 shows the circuit diagram of the system.

FIGURE 13.1 Block diagram of the system.

119
120 Interfacing with Special Devices

TABLE 13.1
List of the Components to Design the System
Sr. No. Name of the Components Quantity Specifcations
1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 Crystal Oscillator 01 12 MHz
4 LCD 02 16x2
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 Sensor 1 DS 1820

FIGURE 13.2 Circuit diagram of the system.

13.1.2 PROGRAM
# include<reg51.h>

sbit dq = P1^7; // connect with DS1820 Data pin

sbit rs=P3^0;
sbit en=P3^1;

void delay_ms(int j)
{
unsigned char i;

for(;j;j--)
for(i=122;i<=0;i--);
}

void data_lcd(unsigned char dat)


{
P2=dat;
Interfacing with Wire Interface-based Devices 121

rs=1;
en=1;
delay_ms(200);
en=0;
}
void cmd_lcd(unsigned char cmd)
{
P2=cmd;
rs=0;
en=1;
delay_ms(200);
en=0;
}

void init_lcd()
{
cmd_lcd(0x38);
cmd_lcd(0x01);
cmd_lcd(0x0c);
}

void str_lcd(unsigned char *p)


{
while(*p)
data_lcd(*p++);
}
void delayus(int us)
{
int i;
for (i=0; i<us; i++);
}

bit reset(void)
{
bit presence;
dq = 0;
delayus(29);
dq = 1;
delayus(3);
presence = dq;
delayus(25);
return(presence);
}

bit readbit(void)
{
unsigned char i=0;
dq = 0;
dq=1;
for (i=0; i < 3; i++);
return(dq);
}

void writebit(bit Dbit)


{
unsigned char i=0;
122 Interfacing with Special Devices

dq=0;
dq = Dbit?1:0;
delayus(5);
dq = 1;
}

unsigned char readbyte(void)


{
unsigned char i;
unsigned char din = 0;
for (i=0;i<8;i++)
{
din|=readbit()? 0x01<<i:din;
delayus(6);
}
return(din);
}
void writebyte(unsigned char dout)
{
unsigned char i;
for (i=0; i<8; i++)
{
writebit((bit)(dout & 0x1));
dout = dout >> 1;
}
delayus(5);
}

unsigned char * ReadTemp()


{
unsigned char n;
unsigned char buff[2]=0;
reset();

writebyte(0xcc);
writebyte(0x44);

while (readbyte()==0xff);
delay_ms(500);
reset();

writebyte(0xcc);
writebyte(0xbe);

for (n=0; n<9; n++)


buff[n]=readbyte();

return buff;
}

void int_lcd(int dat)


{
int str[5]={0},i=0;
if(dat==0)
data_lcd('0');
Interfacing with Wire Interface-based Devices 123

else
while(dat>0)
{
str[i]= (dat%10)+48;
dat=dat/10;
i++;
}
i--;
for(;i>=0;i--)
data_lcd(str[i]);
}

void main()
{
unsigned char tp,*temp,t=0x00;
init_lcd();
cmd_lcd(0x80);
while(1)
{

temp=ReadTemp();
temp[1]=temp[1]&0x07;
tp=temp[0]>>4;
temp[1]=temp[1]<<4;
tp=tp+temp[1];
cmd_lcd(0x80);
str_lcd("temperature is ");
cmd_lcd(0xc0);

int_lcd(tp);
data_lcd(223);
}
}

13.2 8051 TO 8051 COMMUNICATION


Figure 13.3 shows the block diagram to interface two 8051. The system comprises of +12V power
supply, +12V to +5V converter, and two 8051 microcontroller board with breakout board. The objec-
tive of the system is to transfer the data from one 8051 microcontroller to another using Rx and Tx
pins. Table 13.2 shows the list of the components to design the system.

13.2.1 CIRCUIT DIAGRAM


Description of interfacing of 8051 with 8051
The interfacing of 8051 with other 8051 is as per the given guidelines. Figure 13.4 shows the
connection diagram of the system.

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the 8051 board.
3. LCD is connected with port three in 4-pin modes as per the above diagram.
4. Rx and Tx pins of one controller is connected to other Tx and Rx pins of controller.
5. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
124 Interfacing with Special Devices

FIGURE 13.3 Block diagram to interface two 8051.

TABLE 13.2
List of the Components to Design the System
Sr. No. Name of the Components Quantity Specifcations
1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 Crystal Oscillator 01 12 MHz
4 Two 8051 development board 01 Atmel Make including crystal oscillator and reset circuit

FIGURE 13.4 Circuit diagram of the system.


Interfacing with Wire Interface-based Devices 125

13.2.2 PROGRAM
Serial transmit program:

#include<reg52.h>
#include<lcd.h>
void disp_key(char key,unsigned char);
void delay();
void transmit(char);
char key;
char add=0x80;
char databyte[]="ABCDEFGHIJKLMNOP";
////////////////////////////////////////////////////////////////
void main()
{
TMOD=0x20; // timer 1 8 bit auto reload
TH1=0xfa; // 4800 baud rate
SCON=0x50; // serial mode 1 8 bit data 1 start bit 1 stop bit.
TR1=1; // start the timer
TI=0;
RI=0;
lcd_init();
while(1)
{ P0=0xfe;
key=P0;
disp_key(key,add);

P0=0xfd;
key=P0;
disp_key(key,add);

P0=0xfb;
key=P0;
disp_key(key,add);

P0=0xf7;
key=P0;
disp_key(key,add);
}
}
/////////////////////////////////////////////////////
void disp_key(char key,unsigned char add)
{
switch(key)
{
send_command(add);
case 0xee: // A
send_data(databyte[0]); transmit('A'); delay(); add++;;
break;
case 0xDe: // B
send_data(databyte[1]); transmit('B'); delay(); add++;;
break;
case 0xBe: // C
send_data(databyte[2]); transmit('C'); delay(); add++;;
break;
126 Interfacing with Special Devices

case 0x7e: // D
send_data(databyte[3]); transmit('D'); delay(); add++;;
break;
case 0xeD: // E
send_data(databyte[4]); transmit('E');delay(); add++;;
break;
case 0xDD: // F
send_data(databyte[5]); transmit('F'); delay(); add++;;
break;
case 0xBD: // G
send_data(databyte[6]); transmit('G'); delay(); add++;;
break;
case 0x7D: // H
send_data(databyte[7]); transmit('H'); delay(); add++;;
break;
case 0xeB: // I
send_data(databyte[8]); transmit('I'); delay(); add++;;
break;
case 0xDB: // J
send_data(databyte[9]); transmit('J'); delay(); add++;;
break;
case 0xBB: // K
send_data(databyte[10]); transmit('K'); delay(); add++;;
break;
case 0x7B: // L
send_data(databyte[11]); transmit('L'); delay(); add++;;
break;
case 0xE7: // M
send_data(databyte[12]); transmit('M'); delay(); add++;;
break;
case 0xD7: // N
send_data(databyte[13]); transmit('N'); delay(); add++;;
break;
case 0xB7: // O
send_data(databyte[14]); transmit('O'); delay(); add++;;
break;
case 0x77: // P
send_data(databyte[15]); transmit('P'); delay(); add++;;
break;
}
}
////////////////////////////////////////////////////////////////////
void transmit(char x)
{
SBUF=x; // place value in buffer
while(TI==0); // wait till transmited
TI=0;
}
/////////////////////////////////////////////////////////////////////
void delay()
{
int i,j;
for(i=0;i<=75;i++)
{
for(j=0;j<=2500;j++)
{}
Interfacing with Wire Interface-based Devices 127

}
}

Serial receive:

#include<reg52.h>
void main()
{
char byte;
TMOD=0x20; // timer 1 8 bit auto reload
TH1=0xfa; // 4800 baud rate
SCON=0x50; // serial mode 1 8 bit data 1 start bit 1 stop bit.
TR1=1; // start the timer
TI=0;
RI=0;
while(1)
{
while(RI==0);
{
byte=SBUF;
P0=byte;
RI=0;
}
}
}
14 Interfacing of 8051
with Bluetooth
This chapter discusses the interfacing of 8051 with Bluetooth. The interfacing of devices with 8051
and NuttyFi is discussed with the help of circuit diagram and programming.
Bluetooth is a wireless technology standard used for exchanging data between fxed and mobile
devices over short distances. It works on UHF radio waves in the industrial, scientifc, and medical
radio bands, from 2.402 GHz to 2.480 GHz.

14.1 INTRODUCTION
HC-05 is a Bluetooth device used for wireless communication. It works on serial communication
(UART). It is a 6-pin module. It can be used in two modes; data mode and command mode. AT
commands are required in command mode. The module works on 5V or 3.3V. As HC-05 Bluetooth
module has 3.3 V level for Rx/Tx and microcontroller can detect 3.3 V level.
Figure 14.1 shows the block diagram for interfacing of Bluetooth with 8051. The system com-
prises of +12V power supply, +12V to +5V converter, 8051 microcontroller, 16x2 LCD, Bluetooth
with breakout board, and Buzzer. The objective of the system is to make LED “ON” and “OFF”
with Bluetooth. Table 14.1 shows the list of the components to design the system.

14.2 CIRCUIT DIAGRAM


Description of interfacing of Bluetooth with 8051

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the 8051 board.
3. Rx of Bluetooth is connected to port P3.1.
4. Tx of Bluetooth is connected to P3.0.

FIGURE 14.1 Block diagram for interfacing of Bluetooth with 8051.

129
130 Interfacing with Special Devices

TABLE 14.1
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations

Components Used to Interface 8051 uC with Fire Sensor


1 +12V power supply 01 Output- +12V/1A
2 +12V to 5V convertor 01 To regulate the input voltage to +5V/1A
3 Crystal Oscillator 01 12 MHz
4 LCD 02 16x2
5 8051 development board 01 Atmel Make including crystal oscillator and reset circuit
6 Bluetooth 1 Four pin

5. Anode of LED is connected to P2.0 through a resistor of 330 Ohm and cathode is con-
nected to GND.
6. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.

Figure 14.2 shows the circuit diagram of the system.

14.3 PROGRAM
sbit LED=P2^0; //LED at port 2.0

voidmain()
{
charData_in;
UART_Init();
P2 = 0;
LED = 0;
while(1)
{
Data_in = UART_RxChar();
if(Data_in == '1')
{
LED = 1; //Turn ON LED
UART_SendString("LED_ON");
}
elseif(Data_in == '2')
{
LED = 0; // Turn OFF LED
UART_SendString("LED_OFF"); // Send status of LED

}
else
UART_SendString("Select option");

}
}
Interfacing with Bluetooth 131

+5V
C2 C3
30pF 30pF GND
X1
U1
19 XTAL1 P0.0/AD0 39
R1 P0.1/AD1 38
330R 11.0592MHz P0.2/AD2 37
18 P0.3/AD3 36
XTAL2
35
U2 P0.4/AD4
P0.5/AD5 34
3

D6 R7 9
P0.6/AD6 33
32
VO

RST P0.7/AD7
LED-RED 220 21
R2
P2.0/A8
+5V Power Supply

AT89C51
2 22

GND
GND C4 P2.1/A9 330E
23
+ 29 P2.2/A10
24 D2
PSEN P2.3/A11
30 25 LED-RED
VI

ALE P2.4/A12
10uF 31 EA P2.5/A13 26
C1 27
7805 P2.6/A14
1

P2.7/A15 28
GND R5 1 10
10k P1.0 P3.0/RXD
2 P1.1 P3.1/TXD 11
1000u 3 12
P1.2 P3.2/INTO
D1 GND 4 P1.3 P3.3/INT1 13
14
1N4007 5 P1.4 P3.4/T0
6 P1.5 P3.5/T1 15
7 P1.6 P3.6/WR 16
8 17
B1 GND GND P1.7 P3.7/RD
12V +5V
Vinput 3.3V
NuttyFi-Cloud Device

D4 3.3V HC1
D3 D7 Key 5V
Vcc GND
D2 D6 HC-05 GND
TXD P3.0
D1 D5 RXD P3.1
State
Rx D0
Tx A0 A0 BLUETOOTH HC-05
GND

Nc
Vcc

Rx
NC

Tx

FIGURE 14.2 Circuit diagram of the system.


Section D
Case Study Based on Data
Logger to Cloud Server
15 Health Monitoring System for
Solar Panel with Cayenne APP
The solar system inspection is important on regular basis to improve longevity and ensure perfor-
mance of the solar system. Intelligent monitoring can improve the effciency of solar panels. This
chapter discusses the health monitoring system for solar panel with Cayenne App.

15.1 INTRODUCTION
The system comprises of +12V/500m, a power supply, NuttyFi, Liquid crystal display (LCD) and
UV index sensor, voltage sensor, and current sensor. The main objective of the system is to dis-
play the solar PV parameters data on LCD by reading the UV index sensor, voltage sensor, and
current sensor from solar panel confguration. The sensory information from sensors are sampled
and recorded by Arduino. 8051 and NuttyFi/Wi-Fi modem are connected serially. NuttyFi/Wi-Fi
modem transfers the sensory data packet to cloud and Cayenne App using Wi-Fi. Figure 15.1 shows
the block diagram of the system. Table 15.1 shows the components list required to develop the
system.

15.2 CIRCUIT DIAGRAM


Connect the components as follows:

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the ADC 0804 board and also connect to LCD to power
up.
3. Connect 1, 16 pins of LCD to GND and 2, 15 pins to +5V.
4. Connect the variable terminal of 10K POT to pin 3 of LCD to control the contrast.
5. Control lines RS, RW, and E of LCD are connected to P2.0, P2.1, and P2.2 respectively.
6. Connect D4, D5, D6, and D7 of LCD to P3.4, P3.5, P3.6, and P3.7 pins of 8051 uC
respectively.
7. Connect eight output pins of ADC0804 to the P1 port of the 8051 uC.
8. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
9. Connect the control lines C(9), B(10), and C(11) of MUX 74HC4051 to P2.5, P2.6, and P2.7
pins of 8051 uC respectively.
10. Assign pins X0(13), X1(14), X2(15), X3(12), X4(1),X5(5),X6(2), and X7(4) pins of MUX
74HC4051to A0, A1, A2, A3, A4, A5, A6, and A7 respectively. These are useful to connect
analog sensor with it.
11. Connect pin 3 of MUX 74HC4051 to Vin+ pins of ADC0804.
12. Connect +Vcc, GND, and OUT pin of voltage sensor to +5V, GND and A0 pin of the MUX
74HC4051. Select the A0 by giving the control signal (ABC=000) to MUX 74HC4051
from 8051 uC.
13. Connect +Vcc, GND, and OUT pin of current sensor to +5V, GND, and A1 pin of the MUX
74HC4051. Select the A1 by giving the control signal (ABC=001) to MUX 74HC4051
from 8051 uC.

135
136 Data Logger to Cloud Server

FIGURE 15.1 Block diagram of the system.

14. Connect +Vcc, GND, and OUT pin of UV index sensor to +5V, GND, and A2 pin of
the MUX 74HC4051. Select the A2 by giving the control signal (ABC=010) to MUX
74HC4051 from 8051 uC.
15. Connect Vin, GND, Tx and Rx of NuttyFi to +5V, GND, Rx and Tx pins of the 8051 uC
respectively.

Figure 15.2 shows the circuit diagram of the system.

15.3 PROGRAM
To connect the system with Wi-Fi network, 8051 and NuttyFi both needs to be programmed
separately.

TABLE 15.1
Components List
Component/Specifcation Quantity
Power supply 12V/1Amp 1
2 Relay Board 1
Jumper wire M-M 20
Jumper wire M-F 20
Jumper wire F-F 20
Power supply extension (To get more +5V and GND) 1
LCD16*2 1
LCD patch/explorer board 1
NuttyFi patch 1
NuttyFi 1
Voltage sensor 1
Current sensor 1
UV index sensor 1
8051 board with ADC and MUX74HC4051 1
Health Monitoring System 137

FIGURE 15.2 Circuit diagram of the system.

15.3.1 PROGRAM FOR 8051 MICROCONTROLLER


#include<reg52.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void delay();
void transmit(char);
sbit lcd_E = P2^2;
sbit lcd_RW = P2^1;
sbit lcd_RS = P2^0;

void lcd_init();
void send_command(char command);
void send_data(char databyte);
void delay_lcd();
void display(char, char[]);

char databyte[]={"0123456789"};

char a,b,c;
char x,y,z;
void main(void)
{
138 Data Logger to Cloud Server

TMOD=0x20; // Timer1 Mode2 8 bit auto reload


TH1=0xfd; // 9600 bps
SCON=0x50; // 8 Data bit, 1 start bit, 1 stop bit
TR1=1; // Timer1 ON
while(1)
{
unsigned char i;
i=P1;
a=i%10;
b=i/10;
c=i/100;
lcd_init();

P25=0; P26=0; P27=0;


display(0x80, "Welcome to IoT ");
display(0xc0, "Voltage: ");
send_command(0xc9); send_data(databyte[c]);
send_command(0xca); send_data(databyte[b]);
send_command(0xcb); send_data(databyte[a]);
delay();
x=P1;

P25=0; P26=0; P27=1;


display(0x80, "Welcome to IoT ");
display(0xc0, "Current : ");
send_command(0xc9); send_data(databyte[c]);
send_command(0xca); send_data(databyte[b]);
send_command(0xcb); send_data(databyte[a]);
delay();
y=P2;
P25=0; P26=1; P27=0;
display(0x80, "Welcome to IoT ");
display(0xc0, "UV Index: ");
send_command(0xc9); send_data(databyte[c]);
send_command(0xca); send_data(databyte[b]);
send_command(0xcb); send_data(databyte[a]);
delay();
z=P3;

transmit(x); transmit(','); transmit(y); transmit(',');


transmit(z); transmit('\n');
}
}

void transmit(char y1)


{
SBUF=y1;
while(TI==0);
TI=0;
}

void display(char x, char databyte[])


{
char y=0;
send_command(x);
Health Monitoring System 139

while(databyte[y]!='\0')
{
send_data(databyte[y]);
y++;
}
}

void lcd_init()
{
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x28); delay_lcd();
send_command(0x28); delay_lcd();
send_command(0x0E); delay_lcd();
send_command(0x01); delay_lcd();
send_command(0x0c); delay_lcd();
send_command(0x06); delay_lcd();
}

void send_command(char command)


{
char x,y;
lcd_RS = 0; /* (lcd_RS) P2.3---->RS , RS=0 for Instruction Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

x=command;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 1; /* RS=1 */

lcd_RS = 0; /* (lcd_RS) P2.3---->RS , RS=0 for Instruction Write */


lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

y=command;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 1; /* RS=1 */
}

void send_data(char databyte)


{
char x,y;
140 Data Logger to Cloud Server

lcd_RS = 1; /* (lcd_RS) P2.3---->RS , RS=1 for Data Write */


lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

x=databyte;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 0; /* RS=0 */

lcd_RS = 1; /* (lcd_RS) P2.3---->RS , RS=1 for Data Write */


lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

y=databyte;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 0; /* RS=0 */
}

void delay_lcd()
{
int j;
for(j=0;j<100;j++)
{
}
}

void delay()
{
int i,j;
for(i=0;i<=75;i++)
{
for(j=0;j<=2500;j++)
{}
}
}

15.3.2 PROGRAM FOR NUTTYFI


#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include "StringSplitter.h"
char ssid[] = "ESPServer"; // add hotspot id here
char wifiPassword[] = "DDDD@12345"; // add hotspot password here

char username[] = "fac81bb0-7283-11e7-85a3-9540e9f7b5aa";


char password[] = "3745eb389f4e035711428158f7cdc1adc0475946";
Health Monitoring System 141

char clientID[] = "386b86f0-7284-11e7-b0bc-87cd67a1f8c7";

unsigned long lastMillis = 0;


String VOLT,AMP, UV;
String inputString_NUTTY= "";
void setup()
{
Serial.begin(9600); // initialize serial communication
Cayenne.begin(username, password, clientID, ssid, wifiPassword);// start cayenne app
}

void loop() {
Cayenne.loop();
serialEvent_NODEMCU() ;
if (millis() - lastMillis > 10000)
{
lastMillis = millis();
Cayenne.virtualWrite(0, UV);
Cayenne.virtualWrite(2, VOLT);
Cayenne.virtualWrite(1,AMP);
}
}

CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.
getId(), getValue.asString());
}

void serialEvent_NUTTY()
{

while (Serial.available()>0)
{
inputString_NODEMCU = Serial.readStringUntil('\n');// Get serial input
StringSplitter *splitter = new StringSplitter(inputString_NUTTY, ',', 2); // new
StringSplitter(string_to_split, delimiter, limit)
int itemCount = splitter->getItemCount();

for(int i = 0; i < itemCount; i++)


{
String item = splitter->getItemAtIndex(i);
VOLT = splitter->getItemAtIndex(0);
AMP = splitter->getItemAtIndex(1);
UV = splitter->getItemAtIndex(2);

delay(200);
}
inputString_NUTTY = "";
}

}
142 Data Logger to Cloud Server

FIGURE 15.3 Snapshot showing username, password to MQTT.

15.4 CLOUD SERVER


Steps to add NuttyFi in Cayenne cloud:

1. Install the Arduino IDE and add Cayenne MQTT Library to Arduino IDE.
2. Install the ESP8266 board package to Arduino IDE.
3. Install required USB driver on computer to program the ESP8266.
4. Connect the ESP8266 to PC/Mac via data-capable USB cable.
5. In the Arduino IDE, go to the tools menu, select the board, and now the port ESP8266 is
connected to.
6. Use the MQTT username, MQTT password, client ID as well as ssid[], and wif-Passord[]
in the Arduino IDE to write code, as shown in Figure 15.3.
7. Burn the code in 8051 and NuttyFi then window will open; Figure 15.4 shows the snap-
shots for the developed mobile app after burning program.
8. Figure 15.5 shows the front end of App, showing the data on channel.

FIGURE 15.4 Cayenne App.


Health Monitoring System 143

FIGURE 15.5 Sensory data on App.


16 Smart Irrigation System
in Agricultural Field
with Blynk APP
Smart irrigation system is an Internet of things (IoT) based device, capable of automatic irrigation
and feld monitoring system. Automation can be achieved with the help of monitoring the feld and
control with respect to the sensory data received. This chapter discusses the smart irrigation system
in agricultural feld with Blynk App.

16.1 INTRODUCTION
The objective of the system is to develop a smart control for site-specifc management of irriga-
tion system with Blynk App. Blynk mobile app is designed to control the water management in the
feld as per requirement. The complete system comprises of two sections—feld device and mobile
app. Field device comprises of 8051, NuttyFi, power supply, LCD, relay board, ADC 0804, MUX
74HC4051, soil moisture sensor, temperature and humidity sensor, water level sensor, motor IN,
and motor OUT. The system is designed to establish control for specifc agricultural feld by taking
sensory data from the sensors and control the PUMP IN motor and PUMP OUT motor. It is done
with the help of mobile app. Figure 16.1 shows the block diagram of the system. Table 16.1 shows
the list of components required to develop the system.

16.2 CIRCUIT DIAGRAM


The connections of circuit are as follows:

1. Connect SOIL sensor output pin OUTPUT_SS to pinA0 of MUX 74HC4051.


2. Connect +Vcc and GND pins of SOIL sensor to +5V and GND.
3. Connect Ultrasonic sensorpin trigger and ECHO to pin P3.2 and P3.3 of 8051.
4. Connect +Vcc and GND pins of ultrasonic sensor to +5V and GND of power supply
5. Connect DHT 11 pin 2 to pin P2.3 of 8051.
6. Connect +5V output to power up the ADC 0804 board and also connect to LCD to power up.
7. Connect 1, 16 pins of LCD to GND and 2, 15 pins to +5V.
8. Connect the variable terminal of 10K POT to pin 3 of LCD to control the contrast.
9. A control line like RS, RW, and E of LCD is connected to P2.0, P2.1, and P2.2 respectively.
10. Connect D4, D5, D6, and D7 of LCD to P3.4, P3.5, P3.6, and P3.7 pins of 8051 uC respectively.
11. Connect +Vcc, GND, and OUT pin of the gas sensor to +5V, GND, and input pins of the
ADC 0804.
12. Connect 8 output pin of ADC0804 to the P1 port of the 8051 uC.
13. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
14. Connect the control lines C(9), B(10), and C(11) of MUX 74HC4051 to P2.5, P2.6, and P2.7
pins of 8051 uC respectively.
15. Assign pin X0(13), X1(14), X2(15), X3(12), X4(1),X5(5),X6(2), and X7(4) of MUX
74HC4051to A0, A1, A2, A3, A4, A5, A6, and A7 respectively. These are useful to connect
analog sensors with it.

145
146 Data Logger to Cloud Server

FIGURE 16.1  Block diagram of the system.

16. Connect pin 3 of MUX 74HC4051 to Vin+ pins of ADC0804.


17. Connect VIN, GND, Tx, and Rx of NuttyFi to +5V, GND, Rx, and Tx pins of the 8051 uC.
18. Water Pump IN motor and Water Pump OUT motor to D3and D4 pins of NuttyFi using
relay board.
19. The base of NPN transistor 2N2222 is to be connected with pin D3 and D4 of NuttyFi.
Emitter of transistor is grounded.
20. Collector of transistor is to be connected with L2 of relay and Li of relay to positive termi-
nal of 12V battery.
21. Negative terminal of battery is connected with ground.
22. One terminal of appliance (pump motor) is connected with “NO” of relay and other to one
end the AC source.
23. Other end of AC source is connected to “Common” terminal of relay.
24. Connect +Vcc, GND, and OUT pin of MQ2 gas sensor to +5V, GND, and A0 pin of
the MUX 74HC4051. Select the A0 by giving the control signal (ABC=000) to MUX
74HC4051 from 8051 uC.
25. Connect +Vcc, GND, Trig, and Echo pin of HC05 sensor to +5V, GND, P2.3, and P2.4 pin
of the 8051 respectively.
26. Connect +Vcc, GND, 2 pin of DHT11 sensor to +5V, GND, and P3.3 pin of the 8051 respectively.
Figure 16.2 shows the circuit diagram of the system.

TABLE 16.1
Components List
Component/Specification Quantity
Power supply 12V/1Amp 1
2 Relay Board 1
Jumper wire M-M 20
Jumper wire M-F 20
Jumper wire F-F 20
Power supply extension (To get more +5V and GND) 1
LCD20*4 1
LCD patch/explorer board 1
NuttyFi patch 1
NuttyFi 1
Soil moisture sensor 1
Ultrasonic sensor patch 1
DHT11 1
8051 board with ADC and MUX74HC4051 1
Smart Irrigation System 147

FIGURE 16.2 Circuit diagram of the system.

16.3 PROGRAM
16.3.1 PROGRAM FOR 8051
#include<reg52.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void delay();
void transmit(char);
sbit lcd_E = P2^2;
sbit lcd_RW = P2^1;
sbit lcd_RS = P2^0;

void lcd_init();
void send_command(char command);
void send_data(char databyte);
void delay_lcd();
void display(char, char[]);

char databyte[]={"0123456789"};

char a,b,c;
char x,y,z;
void main(void)
{
TMOD=0x20; // Timer1 Mode2 8 bit auto reload
148 Data Logger to Cloud Server

TH1=0xfd; // 9600 bps


SCON=0x50; // 8 Data bit, 1 start bit, 1 stop bit
TR1=1; // Timer1 ON
while(1)
{
unsigned char i;
i=P1;
a=i%10;
b=i/10;
c=i/100;
lcd_init();

P25=0; P26=0; P27=0;


display(0x80, "Welcome to IoT ");
display(0xc0, "Gas Sensor : ");
send_command(0xc9); send_data(databyte[c]);
send_command(0xca); send_data(databyte[b]);
send_command(0xcb); send_data(databyte[a]);
delay();
x=P1;

P25=0; P26=0; P27=1;


display(0x80, "Welcome to IoT ");
display(0xc0, "UltraSonic: ");
send_command(0xc9); send_data(databyte[c]);
send_command(0xca); send_data(databyte[b]);
send_command(0xcb); send_data(databyte[a]);
delay();
y=P2;
transmit(x); transmit(','); transmit(y); transmit('\n');
}
}

void transmit(char y1)


{
SBUF=y1;
while(TI==0);
TI=0;
}

void display(char x, char databyte[])


{
char y=0;
send_command(x);
while(databyte[y]!='\0')
{
send_data(databyte[y]);
y++;
}
}
void lcd_init()
{
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
Smart Irrigation System 149

send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x28); delay_lcd();
send_command(0x28); delay_lcd();
send_command(0x0E); delay_lcd();
send_command(0x01); delay_lcd();
send_command(0x0c); delay_lcd();
send_command(0x06); delay_lcd();
}

void send_command(char command)


{
char x,y;
lcd_RS = 0; /* (lcd_RS) P2.3---->RS , RS=0 for Instruction Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

x=command;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;
lcd_E = 0; /* E=0; Disable lcd */
lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 1; /* RS=1 */
lcd_RS = 0; /* (lcd_RS) P2.3---->RS , RS=0 for Instruction Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */
y=command;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;
lcd_E = 0; /* E=0; Disable lcd */
lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 1; /* RS=1 */
}

void send_data(char databyte)


{
char x,y;
lcd_RS = 1; /* (lcd_RS) P2.3---->RS , RS=1 for Data Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */
x=databyte;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 0; /* RS=0 */

lcd_RS = 1; /* (lcd_RS) P2.3---->RS , RS=1 for Data Write */


lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

y=databyte;
150 Data Logger to Cloud Server

y=y & 0x0f;


P2=P2 & 0xf0;
P2=P2 | y;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 0; /* RS=0 */
}

void delay_lcd()
{
int j;
for(j=0;j<100;j++)
{
}
}

void delay()
{
int i,j;
for(i=0;i<=75;i++)
{
for(j=0;j<=2500;j++)
{}
}
}

16.3.2  Program for NuttyFi


#include "StringSplitter.h"
#define BLYNK_PRINT Serial
////// library for NodeMCU
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "5c8e33bf09a04b03b2fa153928b035c5";///add token here
char ssid[] = "ESPServer "; // SSID of hotspot
char pass[] = "CCCC@12345"; // Password of hotspot
//////// library for internal LCD of blynk APP
WidgetLCD LCD_BLYNK(V8);
///// for timer
BlynkTimer timer;

int PUMP_IN=D3;//Connect relay input to D3 of motor IN


int PUMP_OUT=D4;// Connect relay input to D4 of motor OUT
String ULTRA,TEMP,HUM,SOIL;
String CONT_NEW_STRING= "";
/////////////////// use button
BLYNK_WRITE(V1)
{
int PUMP_IN_VAL = param.asInt();
if(PUMP_IN_VAL==HIGH)
{
lcd.clear(); // clear the contents of LCD
digitalWrite(PUMP_IN,HIGH); // Make D3 to HIGH
digitalWrite(PUMP_OUT,LOW); // Make D4 to LOW
////// external LCD with NOdeMCU
Smart Irrigation System 151

lcd.setCursor(0,0); // set cursor on LCD


lcd.print("PUMP_In Tigger");
//// LCD blynk
LCD_BLYNK.print(0,0,"PUMP_In Tigger"); // print string on Blynk LCD
delay(10); // delay of 10 mSec
}

BLYNK_WRITE(V2)
{
int PUMP_OUT_VAL = param.asInt();
if(PUMP_OUT_VAL==HIGH)
{
lcd.clear();
digitalWrite(PUMP_IN,LOW); // make D3 pin to LOW
digitalWrite(PUMP_OUT,HIGH); // Make D4 pin to HIGH
LCD_BLYNK.print(0,0,"PUMP_OUT Tigger"); // print string on blynk LCD
delay(10); // wait for 10 mSec
}
}
BLYNK_WRITE(V3)
{
int BOTH_ON = param.asInt();
if(BOTH_ON==HIGH)
{
lcd.clear(); // Clear the contents of LCD
digitalWrite(PUMP_IN,HIGH); // make D3 pin to HIGH
digitalWrite(PUMP_OUT,HIGH); // make D4 pin to HIGH
LCD_BLYNK.print(0,0,"BOTH ON"); // print string on Blynk APP
delay(10); // wait for 10 mSec
}
}
/////// Function to read analog sensor
void READ_SENSOR()
{
serialEvent_NODEMCU(); // call serial event to read serial data from 8051
Blynk.virtualWrite(V4,SOIL); // Print value on virtual pin
Blynk.virtualWrite(V5,ULTRA); // Print value on virtual pin

Blynk.virtualWrite(V6,TEMP); // Print value on virtual pin


Blynk.virtualWrite(V7,HUM); //Print value on virtual pin
}

void setup()
{
Serial.begin(9600); // initialize serial communication
lcd.begin(20, 4); // initialize LCD
Blynk.begin(auth, ssid, pass);
pinMode(PUMP_IN,OUTPUT);//D3 pin of Nuttyfi
pinMode(PUMP_OUT,OUTPUT);//D4 pin of Nuttyfi
timer.setInterval(1000L,READ_SENSOR);//// read sensor with setting delay of 1 Sec
}

void loop()
{
152 Data Logger to Cloud Server

Blynk.run(); // initial blynk run


timer.run(); // Initiates BlynkTimer
}

void serialEvent_NODEMCU()
{
while (Serial.available()>0) // check serial data availability
{
CONT_NEW_STRING = Serial.readStringUntil('\n');// Get serial input
StringSplitter *splitter = new StringSplitter(CONT_NEW_STRING, ',', 4); // new
StringSplitter(string_to_split, delimiter, limit)
int itemCount = splitter->getItemCount();
for(int i = 0; i < itemCount; i++)
{
String item = splitter->getItemAtIndex(i);
SOIL = splitter->getItemAtIndex(0); // get the first value from 8051
ULTRA = splitter->getItemAtIndex(1); // get the second value from 8051
TEMP = splitter->getItemAtIndex(2); // get the third value from 8051
HUM= splitter->getItemAtIndex(3); // get the fourth value from 8051
}
CONT_NEW_STRING= "";
delay(20); // wait for 20 mSec
}
}

16.4  CLOUD SERVER


Blynk is iOS and Android platform to design mobile app. To design the app download latest Blynk
library from:https://github.com/blynkkk/blynk-library/releases/latest
Mobile App can easily be designed just bydragging and dropping widgets on the provided space.
Tutorials can be downloaded from: http://www.blynk.cc

16.4.1  Steps to Design Blynk App


1. Step1: Download and install the Blynk App for your mobile Android or iphone from http://
www.blynk.cc/getting-started/
2. Step2: Create a Blynk Account
3. Step3: Create a new project
Click on + for creating new project and choose the theme dark (black background) or
light (white background) and click on create, as shown in Figure 16.3.
4. Step4: Auth token is a unique identifier, which will be received on the email address user,
provide at time of making account. Save this token, as this is required to copy in the main
program of receiver section.
5. Step5: Select the device to which smart phone needs to communicate, e.g., ESP8266
(NodeMCU).
6. Step6: Open widget box and select the components required for the project. For this proj-
ect five buttons are selected.
7. Step7:Tap on the widget to get its settings, select virtual terminals as V1, V2 for each but-
tons, which need to be defined later on the program.
8. Step8: After completing the widget settings, Run the project.
9. Front end of App for the system, as shown in Figure 16.4.
Smart Irrigation System 153

FIGURE 16.3 Create a new project.

FIGURE 16.4 BlynkApp.


17 Environmental Parameters
Monitoring System
with Virtuino APP
Environmental parameters monitoring system is capable of sensing different parameters to check
the health of environment. This chapter discusses the smart irrigation system in agricultural feld
with Blynk App.

17.1 INTRODUCTION
Figure 17.1 shows the block diagram of the system. The system comprises of 8051, DC 12V/1Amp
adaptor, 12V to 5V, 3.3V converter, BMP180 sensor, MQ135sensor, Dust sampler, liquid crystal
display, and NuttyFi. The objective of the system is to display the information of BMP180 sensor,
air quality using MQ135, and dust sampler of air on liquid crystal. The sensors are interfaced to
8051. The data packet is formed with 8051, which contains all sensory information. The NuttyFi/
Wi-Fi modem transfers the packet information to cloud APP/cloud server. Table 17.1 shows the list
of components required to develop the system.

17.2 CIRCUIT DIAGRAM


Circuit connections are as follows:

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the ADC 0804 board and also connect to LCD to power
up.
3. Connect 1, 16 pins of LCD to GND and 2, 15 pins to +5V.
4. Connect the variable terminal of 10K POT to pin 3 of LCD to control the contrast.
5. A control line like RS, RW, and Enable of LCD is connected to P2.0, P2.1, and P2.2
respectively.
6. Connect D4, D5, D6, and D7 of LCD to P3.4, P3.5, P3.6, and P3.7 pins of 8051 uC
respectively.
7. Connect +Vcc, GND, and OUT pin of the gas sensor to +5V, GND and input pins of the
ADC 0804.
8. Connect 8 output pin of ADC0804 to the P1 port of the 8051 uC.
9. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
10. Connect the control lines C(9), B(10), and C(11) of MUX 74HC4051 to P2.5, P2.6, and P2.7
pins of 8051 uC respectively.
11. Assign pin X0(13), X1(14), X2(15), X3(12), X4(1),X5(5),X6(2), and X7(4) pins of MUX
74HC4051to A0, A1, A2, A3, A4, A5, A6, and A7 respectively. These are useful to connect
analog sensor with it.
12. Connect pin 3 of MUX 74HC4051 to Vin+ pins of ADC0804.

155
156 Data Logger to Cloud Server

FIGURE 17.1  Circuit diagram of the system.

13. Connect +Vcc, GND, and OUT pin of MQ135 sensor to +5V, GND, and A0 pin of the MUX
74HC4051. Select the A0 by giving the control signal (ABC=000) to MUX 74HC4051
from 8051 uC.
14. Connect +Vcc, GND, and OUT pin of Dust sampler sensor to +5V, GND, and A1 pin of
the MUX 74HC4051. Select the A1 by giving the control signal (ABC=001) to MUX
74HC4051 from 8051 uC.
15. Connect +Vcc, GND, and OUT pin of BMP sensor to +5V, GND, and A2 pin of the MUX
74HC4051. Select the A2 by giving the control signal (ABC=010) to MUX 74HC4051
from 8051 uC.
16. Connect VIN, GND, Tx, and Rx of NuttyFi to +5V, GND, Rx, and Tx pins of the 8051 uC.

Figure 17.2 shows the circuit diagram of the system.

TABLE 17.1
Components List
Components Quantity
LCD20*4 1
LCD20*4 patch 1
DC 12V/1Amp adaptor 1
12V to 5V, 3.3V converter 1
LED with 330 Ohm resistor 1
Jumper wire M to M 20
Jumper wire M to F 20
Jumper wire F to F 20
BMP180 sensor 1
MQ135sensor 1
Dust sampler 1
8051 board with ADC0804 and MAX74HC4051 1
NuttyFi 1
NuttyFi breakout board/Patch 1
Environmental Parameters Monitoring System 157

FIGURE 17.2 Circuit diagram of the system.

17.3 PROGRAM
17.3.1 PROGRAM FOR 8051
#include<reg52.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void delay();
void transmit(char);
sbit lcd_E = P2^2;
sbit lcd_RW = P2^1;
sbit lcd_RS = P2^0;

void lcd_init();
void send_command(char command);
void send_data(char databyte);
void delay_lcd();
void display(char, char[]);

char databyte[]={"0123456789"};

char a,b,c;
char x,y,z;
void main(void)
{
TMOD=0x20; // Timer1 Mode2 8 bit auto reload
TH1=0xfd; // 9600 bps
158 Data Logger to Cloud Server

SCON=0x50; // 8 Data bit, 1 start bit, 1 stop bit


TR1=1; // Timer1 ON
while(1)
{
unsigned char i;
i=P1;
a=i%10;
b=i/10;
c=i/100;
lcd_init();

P25=0; P26=0; P27=0;


display(0x80, "Welcome to IoT ");
display(0xc0, "Gas Sensor : ");
send_command(0xc9); send_data(databyte[c]);
send_command(0xca); send_data(databyte[b]);
send_command(0xcb); send_data(databyte[a]);
delay();
x=P1;
transmit(x); transmit('\n');
}
}

void transmit(char y1)


{
SBUF=y1;
while(TI==0);
TI=0;
}

void display(char x, char databyte[])


{
char y=0;
send_command(x);
while(databyte[y]!='\0')
{
send_data(databyte[y]);
y++;
}
}

void lcd_init()
{
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x28); delay_lcd();
send_command(0x28); delay_lcd();
send_command(0x0E);  delay_lcd();
send_command(0x01); delay_lcd();
send_command(0x0c); delay_lcd();
send_command(0x06); delay_lcd();
}
Environmental Parameters Monitoring System 159

void send_command(char command)


{
char x,y;
lcd_RS = 0; /* (lcd_RS) P2.3---->RS , RS=0 for Instruction Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

x=command;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 1; /* RS=1 */

lcd_RS = 0; /* (lcd_RS) P2.3---->RS , RS=0 for Instruction Write */


lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

y=command;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 1; /* RS=1 */
}

void send_data(char databyte)


{
char x,y;
lcd_RS = 1; /* (lcd_RS) P2.3---->RS , RS=1 for Data Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

x=databyte;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 0; /* RS=0 */

lcd_RS = 1; /* (lcd_RS) P2.3---->RS , RS=1 for Data Write */


lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

y=databyte;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;

lcd_E = 0; /* E=0; Disable lcd */


160 Data Logger to Cloud Server

lcd_RW = 1; /* W=1; Disable write signal */


lcd_RS = 0; /* RS=0 */
}

void delay_lcd()
{
int j;
for(j=0;j<100;j++)
{
}
}

void delay()
{
int i,j;
for(i=0;i<=75;i++)
{
for(j=0;j<=2500;j++)
{}
}
}

17.3.2  Program for NuttyFi


#include <ESP8266WiFi.h>
#include "Virtuino_ESP_WifiServer.h"
const char* ssid = "ESPServer";
const char* password = "FFFF@12345";
WiFiServer server(8000); // Server port
Virtuino_ESP_WifiServer virtuino(&server);
int storedValue=0;
int counter =0;
long storedTime=0;
String PRESS,ALT,DUST,MQ135;
String inputString_NODEMCU = "";
void setup()
{
virtuino.DEBUG=true; // set this value TRUE to enable the serial monitor status
virtuino.password="1234"; // Set a password to your web server for more protection
Serial.begin(9600); // Enable this line only if DEBUG=true
delay(10);
pinMode(2, OUTPUT);
digitalWrite(2, 0);

Serial.println("Connecting to "+String(ssid));
WiFi.mode(WIFI_STA); // Config module as station only.
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
Environmental Parameters Monitoring System 161

server.begin();
Serial.println("Server started");

void loop()
{
virtuino.run();
serialEvent_NODEMCU();
int v1=virtuino.vDigitalMemoryRead(0); // Read virtual memory 0 from Virtuino app
int v2=virtuino.vDigitalMemoryRead(1); // Read virtual memory 0 from Virtuino app
if (v1!=storedValue)
{
Serial.println("-------Virtual pin DV0 is changed to="+String(v1));
if (v1==1) digitalWrite(D4,0);
else digitalWrite(D4,1);
storedValue=v1;
}
if (v2!=storedValue)
{
Serial.println("-------Virtual pin DV0 is changed to="+String(v2));
if (v2==1) digitalWrite(D4,0);
else digitalWrite(D4,1);
storedValue=v2;
}
serialEvent_NODEMCU();
virtuino.vMemoryWrite(2,PRESS);
virtuino.vMemoryWrite(3,ALT);
virtuino.vMemoryWrite(4,DUST);
virtuino.vMemoryWrite(5,MQ135);
long t= millis();
if (t>storedTime+5000)
{
counter++;
if (counter>20) counter=0; // limit = 20
storedTime = t;
virtuino.vMemoryWrite(12,counter); // write counter to virtual pin V12
}
}

void serialEvent_NODEMCU()
{

while (Serial.available()>0)
{
inputString_NODEMCU = Serial.readStringUntil('\n');// Get serial input
StringSplitter *splitter = new StringSplitter(inputString_NODEMCU, ',',5); // new
StringSplitter(string_to_split, delimiter, limit)
int itemCount = splitter->getItemCount();
for(int i = 0; i < itemCount; i++)
{
String item = splitter->getItemAtIndex(i);
PRESS= splitter->getItemAtIndex(0);
ALT= splitter->getItemAtIndex(1);
DUST= splitter->getItemAtIndex(2);
162 Data Logger to Cloud Server

MQ135= splitter->getItemAtIndex(3);

}
inputString_NODEMCU = "";
delay(200);
}
}

17.4  CLOUD SERVER


Virtuino application is human machine interface platform on cloud. Virtuino application is con-
trolled through Bluetooth, Wi-Fi, GPRS, and ThingSpeak.
Steps to develop App are as follows:
1. Step 1: Make connection among 8051, NodeMCU, and external devices.
2. Step 2: Follow the following steps:
a. Click to download the Virtuino Library ver 1.1
b. Run to add the library via Arduino IDE
c. Burn the hex code in NodeMCU
1. Step 3: Add Wi-Fi settings with android device.
2. Step 4: Make the application in Virtuino App and run it to interact with ESP8266/
NodeMCU.
Figure 17.3 shows the Virtuino App for the system.

FIGURE 17.3  Virtuino App.


18 Weather Station with
ThingSpeak Server
The weather station measures temperature, wind speed, precipitation, atmospheric pressure, and
other environmental parameters. Intelligent weather station means it collects data without human
intervention. This chapter discusses development of weather station with ThingSpeak server.

18.1 INTRODUCTION
The complete process can be understood with the help of system for the weather station. Internet of
things (IoT) makes it intelligent. The atmospheric pressure sensor, temperature sensor, wind speed
sensor, and precipitation sensor are used to make intelligent weather station using 8051 and NuttyFi.
Along with these sensors, the system comprises of 12V/1Amp DC adaptor, 12V to 5V, 3.3V con-
verter, and liquid crystal display (LCD). The objective of the system is to measure the atmospheric
parameters and display the information on LCD. The data packet is formed with Arduino, received
from different sensors. Then this data packet is communicated serially to NuttyFi. The NuttyFi,
further extract the sensory information and transfer it to cloud. Figure 18.1 shows the block diagram
of the system.
Table 18.1 shows the list of components to design a system.

18.1.1 WIND SPEED SENSOR


The Davis Anemometer has wind vane with 18k linear potentiometer attached to it. The output
from the wind direction circuit can be connected to analog pin on the controller. The controller with
a 10-bit analog to digital converter (ADC) gives digital levels variation from 0 to 1023 correspond-
ing to a voltage of 0 to 5V. The mapping for voltage to degree of direction (0–1023 to a 0–360 range)
can be done with programming.

18.1.2 CALIBRATION
The simplest way to set up the anemometer for wind direction calibration is to have the mounting
arm pointing directly to north on the compass, which means the direction value will line up cor-
rectly with North. However, if it is not possible to point the mounting arm to magnetic north, then
apply an offset to our wind direction calculation to make correct value of the wind direction read-
ing. If the magnetic north heading relative is between 0 and 180, then subtract the offset from the
Direction output and if the magnetic north heading relative is between 181 and 360, then add the
offset to the Direction output to get the adjusted wind direction.

18.2 CIRCUIT DIAGRAM


To design the system, connect the components as follows:

1. Connect +12V power supply output to input of +12V to +5V convertor.


2. Connect +5V output to power up the ADC 0804 board and also connect to LCD to power up.
3. Connect 1, 16 pins of LCD to GND and 2, 15 pins to +5V.
4. Connect the variable terminal of 10K POT to pin 3 of LCD to control the contrast.

163
164 Data Logger to Cloud Server

FIGURE 18.1  Block diagram of the system.

5. A control line like RS, RW, and E of LCD is connected to P2.0, P2.1, and P2.2 respectively.
6. Connect D4, D5, D6, and D7 of LCD to P3.4, P3.5, P3.6, and P3.7 pins of 8051 uC
respectively.
7. Connect +Vcc, GND, and OUT pin of the gas sensor to +5V, GND, and input pins of the
ADC 0804.
8. Connect eight output pin of ADC0804 to the P1 port of the 8051 uC.
9. For programming user can use hardware programmer to program the 8051 uC or one can
also use in system programming.
10. Connect the control lines C(9), B(10), and C(11) of MUX 74HC4051 to P2.5, P2.6, and P2.7
pins of 8051 uC respectively.
11. Assign pin X0(13), X1(14), X2(15), X3(12), X4(1), X5(5), X6(2), and X7(4) pins of MUX
74HC4051 to A0, A1, A2, A3, A4, A5, A6, and A7 respectively. These are useful to con-
nect analog sensor with it.
12. Connect pin 3 of MUX 74HC4051 to Vin+ pins of ADC0804.

TABLE 18.1
Components List
Sr. No. Name of Components Quantity
1 8051 board with ADC and MUX74HC4051 1
2 LCD18*4 1
3 LCD18*4 patch 1
4 DC 12V/1Amp adaptor 1
5 12V to 5V, 3.3V converter 1
6 LED with 330 Ohm resistor 1
7 Jumper wire M to M 18
8 Jumper wire M to F 18
9 Jumper wire F to F 18
10 Atmospheric pressure sensor 1
11 Temperature sensor 1
12 Wind speed sensor 1
13 Precipitation sensor 1
14 NuttyFi 1
15 Breakout board for NuttyFi 1
Weather Station with ThingSpeak Server 165

FIGURE 18.2 Circuit diagram of the system.

13. Connect VIN, GND, Tx, and Rx of NuttyFi to +5V, GND, Rx, and Tx pins of the 8051 uC
respectively.
14. Connect +Vcc, GND, and OUT pin of Atmospheric pressure sensor to +5V, GND, and
A0 pin of the MUX 74HC4051. Select the A0 by giving the control signal (ABC=000) to
MUX 74HC4051 from 8051 uC.
15. Connect +Vcc, GND and OUT pin of Temperature sensor to +5V, GND and A1 pin
of the MUX 74HC4051. Select the A1 by giving the control signal (ABC=001) to MUX
74HC4051 from 8051 uC.
16. Connect +Vcc, GND, and OUT pin of wind speed sensor to +5V, GND, and A2 pin of
the MUX 74HC4051. Select the A2 by giving the control signal (ABC=010) to MUX
74HC4051 from 8051 uC.
17. Connect +Vcc, GND, and OUT pin of Precipitation sensor to +5V, GND, and A3 pin
of the MUX 74HC4051. Select the A2 by giving the control signal (ABC=011) to MUX
74HC4051 from 8051 uC.
18. Connect solid state relay input to D3 pin of the NuttyFi to control the hooter.

Figure 18.2 shows the circuit diagram of the system.

18.3 PROGRAM
8051 and NuttyFi are required to program separately for IoT applications.

18.3.1 PROGRAM FOR 8051


#include<reg52.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void delay();
166 Data Logger to Cloud Server

void transmit(char);
sbit lcd_E = P2^2;
sbit lcd_RW = P2^1;
sbit lcd_RS = P2^0;

void lcd_init();
void send_command(char command);
void send_data(char databyte);
void delay_lcd();
void display(char, char[]);

char databyte[]={"0123456789"};

char a,b,c;
char x,y,z;
void main(void)
{
TMOD=0x18; // Timer1 Mode2 8 bit auto reload
TH1=0xfd; // 9600 bps
SCON=0x50; // 8 Data bit, 1 start bit, 1 stop bit
TR1=1; // Timer1 ON
while(1)
{
unsigned char i;
i=P1;
a=i%10;
b=i/10;
c=i/100;
lcd_init();
P25=0; P26=0; P27=0;
display(0x80, "Welcome to IoT ");
display(0xc0, "Wind Sensor: ");
send_command(0xc9); send_data(databyte[c]);
send_command(0xca); send_data(databyte[b]);
send_command(0xcb); send_data(databyte[a]);
delay();
x=P1;

P25=0; P26=0; P27=1;


display(0x80, "Welcome to IoT ");
display(0xc0, "Preciptation: ");
send_command(0xc9); send_data(databyte[c]);
send_command(0xca); send_data(databyte[b]);
send_command(0xcb); send_data(databyte[a]);
delay();
y=P2;

transmit(x); transmit(','); transmit(y); transmit('\n');


}
}

void transmit(char y1)


{
SBUF=y1;
while(TI==0);
TI=0;
Weather Station with ThingSpeak Server 167

void display(char x, char databyte[])


{
char y=0;
send_command(x);
while(databyte[y]!='\0')
{
send_data(databyte[y]);
y++;
}
}

void lcd_init()
{
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x03); delay_lcd();
send_command(0x28); delay_lcd();
send_command(0x28); delay_lcd();
send_command(0x0E); delay_lcd();
send_command(0x01); delay_lcd();
send_command(0x0c); delay_lcd();
send_command(0x06); delay_lcd();
}

void send_command(char command)


{
char x,y;
lcd_RS = 0; /* (lcd_RS) P2.3---->RS , RS=0 for Instruction Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

x=command;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 1; /* RS=1 */

lcd_RS = 0; /* (lcd_RS) P2.3---->RS , RS=0 for Instruction Write */


lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

y=command;
y=y & 0x0f;
P2=P2 & 0xf0;

P2=P2 | y;

lcd_E = 0; /* E=0; Disable lcd */


168 Data Logger to Cloud Server

lcd_RW = 1; /* W=1; Disable write signal */


lcd_RS = 1; /* RS=1 */

void send_data(char databyte)


{
char x,y;
lcd_RS = 1; /* (lcd_RS) P2.3---->RS , RS=1 for Data Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

x=databyte;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 0; /* RS=0 */
lcd_RS = 1; /* (lcd_RS) P2.3---->RS , RS=1 for Data Write */
lcd_RW = 0; /* (lcd_RW) P2.2---->R/W, W=0 for write operation */
lcd_E = 1; /* (lcd_E) P2.5---->E ,E=1 for Instruction execution */

y=databyte;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;

lcd_E = 0; /* E=0; Disable lcd */


lcd_RW = 1; /* W=1; Disable write signal */
lcd_RS = 0; /* RS=0 */
}

void delay_lcd()
{
int j;
for(j=0;j<100;j++)
{
}
}

void delay()
{
int i,j;
for(i=0;i<=75;i++)
{
for(j=0;j<=2500;j++)
{}
}
}

18.3.2  Program for NuttyFi


#include <ESP8266WiFi.h> // add ESP library
#include "StringSplitter.h" // add string splitter header
Weather Station with ThingSpeak Server 169

String apiKey1 = "O44YTW0Z5WNO17N8"; // add api key


const char* ssid = "ESPServer"; // add hotspot ID
const char* password = "CCCC@12345"; // add hotspot password
const char* server = "api.thingspeak.com";
WiFiClient client;
String Precipitation,VaneValue,CalDirection,TEMP,PRESS,ALT;
String inputString_Nutty = ""; // a string to hold incoming data

void setup()
{
Serial.begin(115180); // start serial at 115180 baud rate
delay(10); // set delay of 10 mSec
WiFi.begin(ssid, password); // start Wi-Fi communication
Serial.println(); // print serial
Serial.println(); // print serial
Serial.print("Connecting to ") // print serial
Serial.println(ssid); //print serial
WiFi.begin(ssid, password); // begin Wi-Fi
while (WiFi.status() != WL_CONNECTED)
{
delay(500); // wiat for 500mSec
Serial.print("."); // print serial
}
Serial.println(""); // print serial
Serial.println("WiFi connected"); // // print serial
}

void loop()
{
if (client.connect(server,80))
{
NUTTYFi_serialEvent_Nutty();
send1_TX_NUTTY_PARA();
}
client.stop(); // stop client
Serial.println("Waiting"); // print serial
delay(18000); // wait for 18 Sec

void send1_TX_NUTTY_PARA() // function to send data to cloud server


{

String postStr = apiKey1;


postStr +="&field1="; // AT command
postStr += String(Precipitation); // AT command
postStr +="&field2="; // AT command
postStr += String(VaneValue); // AT command
postStr +="&field3="; // AT command
postStr += String(CalDirection); // AT command
postStr += String(TEMP); // AT command
postStr +="&field2="; // AT command
postStr += String(PRESS); // AT command
postStr +="&field3="; // AT command
postStr += String(ALT); // AT command
postStr += "\r\n\r\n";
170 Data Logger to Cloud Server

client.print("POST /update HTTP/1.1\n");


client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey1+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Send data to channel-1 "); // print serial
Serial.print("Content-Length: "); // print serial
Serial.print(postStr.length()); // print serial
Serial.print("Field-1: "); // print serial
Serial.print(Precipitation); // print serial
Serial.print("Field-2: "); // print serial
Serial.print(VaneValue); // print serial
Serial.print("Field-3: "); // print serial
Serial.print(CalDirection); // print serial
Serial.print("Field-4: "); // print serial
Serial.print(TEMP); // print serial
Serial.print("Field-5: "); // print serial
Serial.print(PRESS); // print serial
Serial.print("Field-6: "); // print serial
Serial.print(ALT); // print serial
Serial.println(" data send"); // print serial

void NUTTYFi_serialEvent_Nutty() // function to receive serial data


{
while (Serial.available()>0) // check serial
{
inputString_Nutty = Serial.readStringUntil('\n');// Get serial input

StringSplitter *splitter = new StringSplitter(inputString_NODEMCU, ',', 7); // new


StringSplitter(string_to_split, delimiter, limit)
int itemCount = splitter->getItemCount();

for(int i = 0; i < itemCount; i++)


{
String item = splitter->getItemAtIndex(i);
Precipitation= splitter->getItemAtIndex(0); // get data of sensor
VaneValue = splitter->getItemAtIndex(1); // get data of sensor
CalDirection= splitter->getItemAtIndex(2); // get data of sensor
TEMP= splitter->getItemAtIndex(3); // get data of sensor
PRESS = splitter->getItemAtIndex(4); // get data of sensor
ALT= splitter->getItemAtIndex(5); // get data of sensor
}
inputString_NODEMCU = ""; // make string empty
delay(180); // wait for 180 mSec
}
}
Weather Station with ThingSpeak Server 171

FIGURE 18.3 Window for ThingSpeak.

18.4 CLOUD SERVER


18.4.1 STEPS TO CREATE A CHANNEL
1. Sign In to ThingSpeak by creating a new MathWorks account.
2. Click Channels > My Channels, as shown in Figure 18.3.
3. Click New Channel, as shown in Figure 18.4.
4. Check the boxes next to Fields 1–1. Enter the channel setting values as follows:
Click Save Channel at the bottom of the settings.
5. Check API write key (this key needs to write in the program for local server).

Figures 18.5, 18.6, and 18.7 show the reading of sensors recorded in ThingSpeak server.

FIGURE 18.4 New channel in My Channels.

FIGURE 18.5 Wind speed.


172 Data Logger to Cloud Server

FIGURE 18.6  Wind direction.

FIGURE 18.7  Temperature.


Index
Page numbers in italics refer to figures, and those in bold refer to tables.

8051 microcontroller, 15; see also NuttyFi/NuttyFi Analog-to-digital conversion (ADC)0804, 25, 26, 163
ESP8266 board Anode, 39
analog sensor interfacing with, see Analog sensor Artifcial Intelligence (AI), 3
block diagram of, 16 Atmospheric pressure sensor, 165
Bluetooth interfacing with, 129–131
customized board, see Customized board B
digital sensor interfacing with, see Digital sensors
environmental parameters monitoring system and, Bluetooth, 12, 129–130; see also Classic Bluetooth
157–160 8051 interfacing with, 129, 129
health monitoring system and, 137–140 circuit diagram, 129–130, 131
I2C based devices interfacing with, 101–114 components, 130
LCD interfacing with, 45–51 program, 130
LED interfacing with, 33–37 Bluetooth Low Energy (BLE), 12–13
one wire hardware and software interfacing with, 119–127 Bluetooth Special Interest Group (Bluetooth SIG),
pin diagram of, 16 12–13
seven segment display interfacing with, 39–44 Blynk App, 145–153, 153
smart irrigation system and, 147–150 Bump sensor, 5
SPI based devices interfacing with, 115–117
UART based devices interfacing with, 93–99 C
weather station and, 165–168
8051 to 8051 communication, 123–127 Camera, see Thermal camera
block diagram of system, 124 Cathode, 39
circuit diagram, 123, 124 Cayenne App, 135–143, 142, 143
components, 124 Circuit diagram
interfacing with 8051, 123, 124 8051 to 8051 communication, 123, 124
program, 125–127 Bluetooth, 129–130
DHT11 sensors, 96–97, 101
A DS18B20, 119
DS1307, 110
Accelerometer, 7 EEPROM, 107–108
Access Control List (ACL), 14 environmental parameters monitoring system,
AC motor, 8 155–156, 156, 157
Actuators, 7–9 fame/fre sensor, 87–89
AC motor, 8 fex sensor, 68–70
DC linear, 9 gas sensor, 73–75
DC motors, 8 health monitoring system, 135–136
defned, 7 LCD, 45–46
geared DC motor, 8 LDR, 64–65
industrial servo motor, 8 LED, 33
linear, 8 passive infrared (PIR) sensor, 83–84
muscle wire, 9 potentiometer (POT), 53–54
pneumatic and hydraulic, 9 push button, 79
rotational, 8 seven segment display, 39–42
servo motor, 8 smart irrigation system, 145–146
solenoid, 9 SPI based devices, 115
stepper motor, 8 temperature sensors, 58–60
Alcohol sensor, 6 ultrasonic sensor, 93
Analog sensor, 53–77 weather station, 163–165, 165
described, 53 Classic Bluetooth, 13; see also Bluetooth
fex sensor, 68, 68–72 Cloud computing model, 9–12
gas sensor, 72–77, 73 described, 9
interfacing, 53–77 Infrastructure as a Service (IaaS), 11–12
light dependent resistor (LDR), 63, 63–67 Platform as a Service (PaaS), 10–11
potentiometer (POT), 53–58, 54 Software-as-a-Service (SaaS), 9–10
temperature sensors, 58–63 Common anode seven segment, 39, 40

173
174 Index

Common cathode seven segment, 39, 40 interfacing with 8051, 119


Compass sensor, 7 program, 120–123
Components, IoT, 4, 4–7 DS1307, 110–114
accelerometer, 7 circuit diagram, 110
alcohol sensor, 6 components, 111
bump sensor, 5 interfacing with 8051, 110, 111
compass sensor, 7 program, 112–114
current and voltage sensor, 7
fngerprint sensor, 7 E
fre sensor, 5
EEPROM, 107–110
gas sensor, 6
8051 interfacing with, 107–108, 107–108
GPS, 6
circuit diagram, 107–108
gyroscope, 6
components, 108
humidity sensor, 6
program, 108–110
inertial measurement unit, 7
Electrically Erasable Programmable Read Only Memory
infrared (IR) sensor, 5
(EEPROM), 107
laser sensor, 5
Environmental parameters monitoring system
load cell, 7
circuit diagram, 155–156, 156, 157
magnetometers, 6
cloud server, 162
metal sensor, 5
components, 156
passive infrared (PIR) sensor, 6
overview, 155
potentiometer, 6
program
pressure sensor, 5
for 8051, 157–160
proximity and touch sensor, 5
for NuttyFi, 160–162
push button, 5
with Virtuino app, 155–162, 162
RFID, 7
ESP8266, see NuttyFi/ESP8266 board
sensor, 4–5
temperature sensor, 6 F
thermal camera, 6
ultrasonic sensor, 5 Fingerprint sensor, 7
vibration sensor, 7 Flame/fre sensor, 5, 87, 87–90
Crystal circuit, 28–29, 29 8051 interfacing with, 89, 89, 90
Crystal oscillators, 28–29 circuit diagram, 87–89
Current and voltage sensor, 7 components, 88
Customized board, 25–30 diagram system, 87
ADC0804, 25, 26 NuttyFi/ESP8266 interfacing with, 87, 88, 89–90
crystal circuit, 28–29, 29 program, 89–90
LCD circuitry, 29, 29–30 Flex sensor, 68, 68–72
MUX 74HC4051, 25–27, 27 8051 interfacing with, 68, 69–70, 70, 71–72
NuttyFi, 27 ADC0804 interfacing with, 71–72
reset circuit, 27–28, 28 circuit diagram, 68–70
schematics of, 26 components, 69
described, 68
ESP8266 interfacing with, 68–69, 70, 71
D
program, 71–72
Davis Anemometer, 163 Functional numerical displays (FND), 39
DC linear actuator, 9
DC motors, 8 G
Device Database, 15 Gas sensor, 6, 72–77, 73
DHT11 sensors, 96–99, 101–104 8051 interfacing with, 75–77
circuit diagram, 96–97, 101 ADC0804 interfacing with, 75–77
components, 96, 102 circuit diagram, 73–75
diagram of system, 97 components, 74
interfacing with 8051, 96–97, 101, 101, 102, 102–104 described, 72
program, 97–99, 102–104 NuttyFi/ESP8266 interfacing with, 73, 73, 74, 75
Digital sensors, 4–5 program, 75–77
described, 79 Geared DC motor, 8
fame/fre sensor, 87, 87–90 Global Positioning System (GPS), 6
PIR sensor, 83, 83–86 Gyroscope, 6
push button, 79–81, 80
DS18B20, 119–123 H
circuit diagram, 119
components, 120 Health monitoring system
diagram of system, 119, 120 circuit diagram, 135–136
Index 175

cloud server, 142 routing for mesh topologies in, 14


components, 136 security, 14
diagram of system, 136, 137
overview, 135 K
program, 136–141
for 8051 microcontroller, 137–140 Keil compilers, 15–18, 17
for NuttyFi, 140–141 described, 15
for solar panel with Cayenne App, 135–143, 142, 143 execution, 17–18
Humidity sensor, 6 uVision3 IDE, 15–17
uVision Debugger, 17
I
L
I2C based devices, 101–114
DHT11, 101–104 Laser sensor, 5
DS1307, 110–114 Light dependent resistor (LDR), 63, 63–67
EEPROM, 107–110 8051 interfacing with, 65, 66–67, 67
ultrasonic sensor, 104–107 ADC0804 interfacing with, 66–67
Industrial, scientifc, and medical (ISM) radio bands, circuit diagram, 64–65
12 components, 64
Industrial servo motor, 8 described, 63–64
Inertial measurement unit, 7 ESP8266 interfacing with, 63, 64, 65, 65–66
Infrared (IR) sensor, 5 program, 65–67
Infrastructure as a Service (IaaS), 11–12 Light Emitting Diode (LED)
Intel, 15 circuit for current limiting, 34
Interfacing of 8051 and NuttyFi/ESP8266 color with semiconductor material, 34
with analog sensor, 53–77 components, 35
with Bluetooth, 129–131 interfacing, 33–37
with digital sensor, 79–90 Linear actuator, 8
with I2C based devices, 101–114 Liquefed natural gas (LNG), 6
with LCD, 45–51 Liquefed petroleum gas (LPG), 6
with LED, 33–37 Liquid crystal display (LCD), 163
with one wire interface-based devices, 119–127 circuitry, 29, 29–30
with seven segment display, 39–44 components, 47
with SPI based devices, 115–117 described, 45
with UART based devices, 93–99 diagram of system, 46
Inter IC communication (I2C), 5 interfacing
Internet of Things (IoT), 3–14 of 8051 with 16x2 LCD, 46, 48, 49–51
active engagement, 3 circuit diagram, 45–46
actuators, 7–9 of ESP8266 with 16x2 LCD, 45, 48, 48–49
advantages, 3 program, 48–51
Artifcial Intelligence (AI), 3 pin description of, 47
Bluetooth Low Energy (BLE), 12–13 pin out of, 46
cloud computing model, 9–12 LM35, 6, 58; see also Temperature sensor
components, 4, 4–7 Load cell, 7
connectivity, 3
disadvantages, 4 M
features, 3
IPv6 over Low-Power WPAN (6LoWPAN), 13–14 Magnetometers, 6
overview, 3–4 Maximum transmission unit (MTU), 14
sensors, 3 Medium Earth Orbit, 6
small devices, 3 Metal sensor, 5
in society, 4 Mobile operating systems, 13
Zigbee, 12 MQTT username/password, 142, 142
IPv6 over Low-Power WPAN (6LoWPAN), 13–14 Muscle wire, 9
adaptation mechanism, 14 MUX 74HC4051, 25–27, 27
adapting packet sizes of two networks, 13
addressing management mechanisms, 14 N
address resolution, 13
defned, 13 NuttyFi/ESP8266 board, 19–24, 20, 27; see also 8051
device and service discovery, 14 microcontroller
differing device designs, 13 analog sensor interfacing with, see Analog sensor
differing focus on parameter optimization, 14 customized board with, see Customized board
functions, 13–14 described, 19
routability of packets, 14 digital sensor interfacing with, see Digital sensors
176 Index

environmental parameters monitoring system and, Q


160–162
health monitoring system and, 140–142 Quartz crystal, 28
installing in Arduino IDE, 20–22, 21–24
LCD interfacing with, 45–51 R
LED interfacing with, 33–37
list of, 23 Radio-frequency (RF) oscillators, 28
pin description, 19 Real Time Clock (RTC) module, 110
programming steps, 20–22, 21–24 Reset circuit, 27–28, 28
seven segment display interfacing with, 39–44 Resistor Capacitor (RC), 25
smart irrigation system and, 150–152 RFID, 7
specifcation of, 19 Rotational actuator, 8
UART based devices interfacing with, 93–99
weather station and, 168–170 S
O Sensors, 3, 4–5
alcohol, 6
One wire interface, 5, 119–127 analog, 53–77
8051 to 8051 communication, 123–127 atmospheric pressure, 165
circuit diagram, 123 bump, 5
components, 124 compass, 7
diagram of system, 124 current and voltage, 7
interfacing with 8051, 123, 124 DHT11, 96–99, 101–104
program, 125–127 digital, 4–5
DS18B20, 119–123 fngerprint, 7
circuit diagram, 119 fame/fre, 5, 87, 87–90
components, 120 fex, 68, 68–72
diagram of system, 119, 120 gas, 6, 72–77, 73
interfacing with 8051, 119 infrared (IR), 5
program, 120–123 laser, 5
metal, 5
P passive infrared (PIR), 6, 83, 83–86
Passive infrared (PIR) sensor, 6, 83, 83–86 precipitation, 165
8051 interfacing with, 84, 86, 86 pressure, 5
circuit diagram, 83–84 proximity and touch, 5
components, 84 temperature, 6, 58–63, 165
ESP8266 interfacing with, 83–85, 85 ultrasonic, 5, 93–94, 104–107
program, 84–86 vibration, 7
Personal area network (PAN), 14 wind speed, 165
Platform as a Service (PaaS), 10–11 Serial peripheral interface (SPI), 5, 115
Advantages, 10 Servo motor, 8
described, 10 Seven segment display, 40
disadvantages, 11 components, 41
Pneumatic and hydraulic actuators, 9 described, 39
Potentiometer (POT), 6, 53–58, 54 diagram of system, 41
8051 interfacing with, 54, 56, 57–58 for displaying numbers, 41
ADC0804 interfacing with, 57–58 interfacing, 39–44
circuit diagram, 53–54 with 8051, 42, 42, 43–44
components, 55 circuit diagram, 39–42
ESP8266 interfacing with, 53, 54, 55, 56–57 with ESP8266, 39, 42, 43
program, 56–58 program, 43–44
Precipitation sensor, 165 Smart irrigation system
Pressure sensor, 5 in agricultural feld with Blynk App, 145–153
Proximity and touch sensor, 5 circuit diagram, 145–146
Push button, 5, 79–81, 80 cloud server, 152
8051 interfacing with, 79, 80, 81, 82 components, 146
circuit diagram, 79 diagram of system, 146, 147
components, 80 overview, 145
ESP8266 interfacing with, 79, 80, 81, 82 program
program, 81 for 8051, 147–150
for NuttyFi, 150–152
Index 177

Software-as-a-Service (SaaS), 9–10 Universal synchronous and asynchronous receiver-


Solenoid, 9 transmitter (USART), 93
SPI based devices, 115–117 uVision3 IDE, 15–17
circuit diagram, 115 uVision Debugger, 17
components, 116
diagram of system, 116 V
interfacing with 8051, 115
overview, 115 Variable resistor/rheostat, 53
program, 117 Vibration sensor, 7
SPI Lines, 115 Virtuino app, 155–162, 162
Stepper motor, 8
W
T
Weather station
Temperature sensors, 6, 58–63, 165; see also LM35 block diagram of system, 164
circuit diagram, 58–60 calibration, 163
components, 59 circuit diagram, 163–165, 165
described, 58 cloud server, 171
program, 61–63 components, 164
Thermal camera, 6 overview, 163
ThingSpeak server, 163–172, 171 program
Three wire interface, 5 for 8051, 165–168
Two-wire interface (TWI), 5 for NuttyFi, 168–170
temperature, 172
U with ThingSpeak server, 163–172
wind direction, 172
Ultrasonic sensor, 5, 93–94, 104–107 wind speed, 171
circuit diagram, 93, 104 Wi-Fi, 12
components, 96, 105 Wind speed sensor, 165
diagram of system, 95–96 Wireless personal area networks (WPAN),
interfacing with 8051, 93, 104, 105 12
program, 93–94, 106–107
Universal asynchronous receiver-transmitter (UART), Z
93–99
DHT11 sensors, 96–99 Zigbee, 12
ultrasonic sensor, 93–94 Zigbee Device Objects (ZDO), 12

You might also like