Internet of Things With 8051 and ESP8266
Internet of Things With 8051 and ESP8266
and by
CRC Press
2 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN
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.
v
vi Contents
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
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 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
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.
xiii
xiv Authors’ Biographies
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.
This chapter discusses the introduction to the Internet of Things (IoT). The chapter also describes
the features of IoT and its components.
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.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
as inter IC communication (I2C), two-wire interface (TWI), serial peripheral interface (SPI), three-
wire interface, or one wire interface.
1.2.12 POTENTIOMETER
A potentiometer is a simple knob which provides a variable resistance.
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.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.22 ACCELEROMETER
An accelerometer measures the linear acceleration. It can measure the static (earth gravity) or
dynamic acceleration in three axes.
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.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. 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.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.
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.
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.
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.
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.
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.
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.
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
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)
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.3 EXECUTION
uVision offers many ways by which you can control and manipulate program execution.
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.
19
20 Basics of 8051 Microcontroller and IoT
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
25
26 Basics of 8051 Microcontroller and IoT
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
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
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.
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.
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.
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.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
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
-
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
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
}
+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
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
39
40 Interfacing with I/O devices
+12V Power
Supply
TABLE 6.1
Components List
Sr. No. Name of the Components Quantity Specifcations
FIGURE 6.6 Circuit diagram for the interfacing of seven segment display with NuttyFi.
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
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
}
}
}
}
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.
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.
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
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
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
}
#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
delay_lcd();
send_command(0x0E);
delay_lcd();
send_command(0x01);
delay_lcd();
send_command(0x0c);
delay_lcd();
send_command(0x06);
delay_lcd();
}
y=command;
y=y & 0x0f;
P3=P3 & 0xf0;
P3=P3 | y;
x=databyte;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;
y=databyte;
y=y & 0x0f;
P3=P3 & 0xf0;
P3=P3 | y;
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.
53
54 Interfacing with I/O devices
Figure 8.3 shows the circuit diagram for interfacing of POT with NuttyFi/ESP8266.
Description of interfacing of POT with 8051
TABLE 8.1
Components to Design the System
Sr. No. Name of the Components Quantity Specifcations
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
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
}
}
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++;
}
}
TABLE 8.2
Components Used to Design System
Sr. No. Name of the Components Quantity Specifcations
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
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 shows the circuit diagram for interfacing of LM35 with NuttyFi/ESP8266.
Description of interfacing of LM35 temperature sensor 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
}
}
TABLE 8.3
Components Used to Design the System
Sr. No. Name of the Components Quantity Specifcations
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.
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 shows the circuit diagram for interfacing of LDR with ESP8266.
Description of interfacing of LDR with 8051
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
}
}
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
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
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
}
}
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
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
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
}
}
i++;
}
}
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:
Figure 9.4 shows the circuit diagram for the interfacing of push button with 8051.
79
80 Interfacing with I/O devices
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
9.1.2 PROGRAM
//Program to interface button sensor with ESP8266
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
}
}
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
TABLE 9.2
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations
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:
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
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
+12V 330E
330 R1
D2
2
D2
LED-GREEN
9-0-9
LED-RED
GND
GND
FIGURE 9.7 Circuit diagram for the interfacing of PIR sensor with NuttyFi/ESP8266.
86 Interfacing with I/O devices
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
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
FIGURE 9.8 Circuit diagram for the interfacing of PIR sensor with 8051.
Interfacing with Digital Sensor 87
TABLE 9.3
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations
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
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
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:
Figure 9.12 shows the circuit diagram for interfacing fre sensor with 8051.
9.3.2 PROGRAM
//Program to interface fre sensor with ESP8266
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
}
}
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
TABLE 10.1
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations
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.
TABLE 10.2
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations
10.2.2 PROGRAM
#include<reg51.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include "LCD16x2_4bit.h"
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();
{
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.
101
102 Interfacing with Special Devices
TABLE 11.1
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations
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>
void main()
{
unsigned char dat[20];
LCD_Init(); /* initialize LCD */
while(1)
{
Request(); /* send start pulse */
Response(); /* receive response */
104 Interfacing with Special Devices
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);
}
}
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
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*/
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 */
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*/
}
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.
TABLE 11.3
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations
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.
TABLE 11.4
Components List to Design System
Sr. No. Name of the Components Quantity Specifcations
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.
115
116 Interfacing with Special Devices
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
12.3 PROGRAM
#define char unsigned char
void spiWr(uchar);
uchar spiRd(void);
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.
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
13.1.2 PROGRAM
# include<reg51.h>
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--);
}
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);
}
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);
}
dq=0;
dq = Dbit?1:0;
delayus(5);
dq = 1;
}
writebyte(0xcc);
writebyte(0x44);
while (readbyte()==0xff);
delay_ms(500);
reset();
writebyte(0xcc);
writebyte(0xbe);
return buff;
}
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);
}
}
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
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.
129
130 Interfacing with Special Devices
TABLE 14.1
Components List to Design the System
Sr. No. Name of the Components Quantity Specifcations
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.
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
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.
135
136 Data Logger to Cloud Server
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.
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
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
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();
}
x=command;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;
y=command;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;
x=databyte;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;
y=databyte;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;
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++)
{}
}
}
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();
delay(200);
}
inputString_NUTTY = "";
}
}
142 Data Logger to Cloud Server
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.
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.
145
146 Data Logger to Cloud Server
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
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
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();
}
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 */
}
y=databyte;
150 Data Logger to Cloud Server
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++)
{}
}
}
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
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
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
}
}
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.
155
156 Data Logger to Cloud Server
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.
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
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
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
x=command;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;
y=command;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;
x=databyte;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;
y=databyte;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;
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++)
{}
}
}
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);
}
}
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.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.
163
164 Data Logger to Cloud Server
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
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.
18.3 PROGRAM
8051 and NuttyFi are required to program separately for IoT applications.
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;
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();
}
x=command;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;
y=command;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;
x=databyte;
x=x >> 4;
P3=P3 & 0xf0;
P3=P3 | x;
y=databyte;
y=y & 0x0f;
P2=P2 & 0xf0;
P2=P2 | y;
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++)
{}
}
}
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
Figures 18.5, 18.6, and 18.7 show the reading of sensors recorded in ThingSpeak server.
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