0% found this document useful (0 votes)
72 views

Be Mech Iot-1

This document describes an IoT lab course offered as part of a mechanical engineering program. The course aims to teach microcontroller programming using 8051 and Arduino boards, interfacing devices with microcontrollers, wireless data exchange, and logging data to the cloud. The course objectives, outcomes, list of experiments, and assessment criteria are provided. Key topics covered include 8051 and Arduino programming, interfacing sensors and actuators, and wireless communication protocols.

Uploaded by

znbsshsh anha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Be Mech Iot-1

This document describes an IoT lab course offered as part of a mechanical engineering program. The course aims to teach microcontroller programming using 8051 and Arduino boards, interfacing devices with microcontrollers, wireless data exchange, and logging data to the cloud. The course objectives, outcomes, list of experiments, and assessment criteria are provided. Key topics covered include 8051 and Arduino programming, interfacing sensors and actuators, and wireless communication protocols.

Uploaded by

znbsshsh anha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

IOT Lab –MEL802

Department Of Mechanical Engineering

SEMESTER -VIII

Course Code Course Name Credits


MEL802 Laboratory based on IoT 01

Objectives:
1. To learn microcontroller programming using 8051 and Arduino Development Board.
2. To acquaint with interfacing of simple peripheral devices to a microcontroller.
3. To acquaint with exchange of data using wireless communication.
4. To familiarize with logging the data on cloud platform.
Outcomes: Learner will able to...
1. Develop simple applications using microcontrollers 8051 and Arduino.
2. Interface simple peripheral devices to a Microcontroller.
3. Use microcontroller based embedded platforms in IoT.
4. Use wireless peripherals for exchange of data.
5. Setup cloud platform and log sensor data.
List of Experiments:
1. Interfacing experiments using 8051 Trainer kit and interfacing modules
a. display (LCD/LED/Seven Segment)
b. Stepper / DC Motor
2. Introduction to Arduino platform and programming
3. Simple Applications using Arduino Development Board (Any two)
a. Simple LED Blinking using development board
b. Building IOT Smart Switch using IOT
c. Pulse Width Modulation
d. Analog to Digital / Digital to Analog Conversion
4. Interfacing Arduino with a Sensor (Any one): Temperature Sensor / PIR/ Ultrasonic sensor/ IR

1 MECH Dept
IOT Lab –MEL802

Sensor/ Flame Sensor/ MQ6 Sensor/ Humidity sensor/ Raindrop Sensor, magnetometers, cameras,
accelerometers etc.
5. Interfacing Arduino with an Actuator (Any One): Motors / solenoids / Controllers etc.
6. Communication using Wireless Medium (Any One): WiFi / Bluetooth / Zigbee / RFID etc.
7. Setting up and Cloud Platform and logging Sensor Data on the platform.
Assessment:
Term Work
Term work shall consist of the experiments as mentioned above.
The distribution of marks for term work shall be as follows:
1. Laboratory work (Experiments): 20 marks
2. Attendance: 05 marks
End Semester Practical/Oral Examination:
Pair of Internal and External Examiner should conduct practical examination followed by Oral.

VISION
To nurture the talents of Mechanical engineers, making them highly
competent for growth of the society.

MISSION
 To deliver excellence in teaching - learning process.
 Promote safe, orderly, caring and supportive environment to learners.
 Development of skilled engineers to perform innovative Research for
betterment of the society.

2 MECH Dept
IOT Lab –MEL802

 To encourage industry - institute interaction, career advancement,


innovation and entrepreneurship development.

PROGRAM EDUCATIONAL OUTCOME


(PEO)

PEO1: To acquire a strong foundation in mathematics and scientific


fundamentals, to develop an ability to analyze various functional
elements of different disciplines of electronics and communication
engineering.
PEO2: Develop technical competence to move in pace with rapid changes
in technology. PEO3: Equip learners to strengthen knowledge and soft
skills for carrier advancement.
PEO4: Adhere to ethics to contribute for betterment of the society.

PROGRAM SPECIFIC OUTCOMES (PSO)

PSO1. To understand principles and applications of various electronic


components/devices and circuits.
PSO2. Enable learners to solve complex problems using modern hardware and software tools.
COURSE OUTCOME

C334.1 Students will be able to understand fundamental programming concepts of microcontrollers.

C334.2 Students will be able to have an in-depth knowledge on interfacing the external devices to the
controllers.
C334.3 Students will be able to design a microcontroller based system with the help of the interfacing
devices.

3 MECH Dept
IOT Lab –MEL802

C334.4 Students will be able to have an in-depth knowledge of applying the concepts on real- time
applications.

CO-PO MAPPING

PO PO PO PO PO PO PO PO PO PO PO1 PO1 PO1 PSO PSO


CO 1 2 3 4 5 6 7 8 9 0 1 2 1 2

C334.1 3 - 1 - - - - - - - - - - -

C334.2 3 - 1 - - - - - - - - - 2 -

C334.3 3 - 3 - - - - - - - - - 2 -

C334.4 3 - 2 - - - - - - - - - 2 -

C334 3 - 1.7 - - - - - - - - - 1.5 -

LIST OF EXPERIMENTS

EXPT. NO NAME OF THE EXPERIMENT

1 INTERFACING LED WITH 8051

2 INTERFACING OF SEVEN SEGMENT LED WITH 8051

4 MECH Dept
IOT Lab –MEL802

3 INTERFACING OF STEPPER MOTOR WITH 8051

6
7

10

11
12

13

14

15

16

17

18

19

INTRODUCTION TO 8051 MICROCONTROLLER

8051 microcontroller is designed by Intel in 1981. It is an 8-bit microcontroller. It is built


with 40 pins DIP (dual inline package), 4kb of ROM storage and 128 bytes of RAM storage, 2
16-bit timers. It consists of are four parallel 8-bit ports, which are programmable as well as
addressable as per the requirement. An on-chip crystal oscillator is integrated in the
microcontroller having crystal frequency of 12 MHz.

The 8051 memory is organized in a Harvard Architecture. Both the code memory space
5 MECH Dept
IOT Lab –MEL802

and data memory space begin at location 0x00 for internal or external memory which is different
from the Princeton Architecture where code and data share same memory space. The advantage
of the Harvard Architecture is not only doubling the memory capacity of the microcontroller
with same number of address lines but also increases the reliability of the microcontroller, since
there are no instructions to write to the code memory which is read only.

1. Features of 8051 Microcontroller


An 8051 microcontroller comes bundled with the following features.

 64K bytes on-chip program memory (ROM)


 128 bytes on-chip data memory (RAM)
 Four register banks
 128 user defined software flags
 8-bit bidirectional data bus
 16-bit unidirectional address bus
 32 general purpose registers each of 8-bit
 16 bit Timers (usually 2, but may have more or less)
 Three internal and two external Interrupts
 Four 8-bit ports,(short model have two 8-bit ports)
 16-bit program counter and data pointer

2. Architecture Of 8051 Microcontroller

Figure 1.4 shows the architecture block diagram of 8051.

6 MECH Dept
IOT Lab –MEL802

2.1 8051 Memory Organization


8051 microcontroller has an internal program of 4K size and if needed an external
memory can be added (by interfacing) of size 60K maximum. So in total 64K size memory is
available for 8051 micro controller. By default, the External Access (EA) pin should be
connected Vcc so that instructions are fetched from internal memory initially. When the limit of
internal memory (4K) is crossed, control will automatically move to external memory to fetch
remaining instructions. If the programmer wants to fetch instruction from external memory only
(bypassing the internal memory), then he must connect External Access (EA) pin to ground
(GND).

2.2 Timers and Counters

Timer means which can give the delay of particular time between some events. For
example on or off the lights after every 2 sec. This delay can be provided through some assembly

7 MECH Dept
IOT Lab –MEL802

program but in microcontroller two hardware pins are available for delay generation. These
hardware pins can be also used for counting some external events. How much times a number is
repeated in the given table is calculated by the counter.

In MC8051, two timer pins are available T0 and T1, by these timers we can give the
delay of particular time if we use these in timer mode. We can count external pulses at these
pins if we use these pins in counter mode. 16 bits timers are available. Means we can generate
delay between 0000H to FFFFH. Two special function registers are available. If we want to
load T0 with 16 bit data then we can load separate lower 8 bit in TL0 and higher 8 bit in TH0. In
the same way for T1. TMOD, TCON registers are used for controlling timer operation.

2.3 Serial Port


There are two pins available for serial communication TXD and RXD. Normally TXD is used
for transmitting serial data which is in SBUF register, RXD is used for receiving the serial data.
SCON register is used for controlling the operation.

2.4 Input Output Ports


There are four input output ports available P0, P1, P2, P3. Each port is 8 bit wide and has
special function register P0, P1, P2, P3 which are bit addressable means each bit can be set or
reset by the Bit instructions (SETB for high, CLR for low) independently. The data at any port
which is transmitting or receiving is in these registers. The port 0 can perform dual works. It is
also used as Lower order address bus (A0 to A7) multiplexed with 8 bit data bus P0.0 to P0.7 is
AD0 to AD7 respectively the address bus and data bus is demultiplex by the ALE signal and
latch which is further discussed in details. Port 2 can be used as I/O port as well as higher order
address bus A8 to A15. Port 3 also have dual functions it can be worked as I/O as well as each
pin of P3 has specific function. P3.0 – RXD – {Serial I / P for Asynchronous communication
Serial O / P for synchronous communication. P3.1 – TXD – Serial data transmit. P3.2 – INT0 –
External Interrupt 0. P3.3 – INT1 – External Interrupt 1. P3.4 – T0 – Clock input for counter 0.
P3.5 – T1 – Clock input for counter 1. P3.6 – WR – Signal for writing to external memory. P3.7
– RD – Signal for reading from external memory. When external memory is interfaced with
8051 then P0 and P2 can’t be worked as I/O port they works as address bus and data bus,
otherwise they can be accessed as I/O ports.

8 MECH Dept
IOT Lab –MEL802

2.5 Oscillator
It is used for providing the clock to MC8051 which decides the speed or baud rate of
MC. We use crystal which frequency vary from 4MHz to 30 MHz, normally we use 11.0592
MHz frequency.

2.6 Interrupts
Interrupts are defined as requests because they can be refused (masked) if they are not
used, that is when an interrupt is acknowledged. A special set of events or routines are followed
to handle the interrupts. These special routines are known as interrupt handler or interrupt service
routines (ISR). These are located at a special location in memory. • INT0 and INT1 are the pins
for external interrupts.

3. Pin Diagram Of 8051 microcontroller

3.1 Pin Description


The EA' (External Access) pin is used to control the internal or external memory access.
The signal 0 is for external memory access and signal 1 for internal memory access. The
PSEN' (Program Store Enable) is for reading external code memory when it is low (0) and EA

9 MECH Dept
IOT Lab –MEL802

is also 0. The ALE (Address Latch Enable) activates the port 0 joined with port 2 to provide 16
bit external address bus to access the external memory. The ALE multiplexes the P0: 1 for
latching address on P0 as A0-A7 in the 16 bit address buss, 0 for latching P0 as data I/O. P0.x
is named ADx because P0 is multiplexed for Address bus and Data bus at different clock time.
WR' provides the signal to write external data memory RD' provides the signal to read external
data and code memory.
 PORT P1 (Pins 1 to 8): The port P1 is a port dedicated for general I/O purpose. The
other ports P0, P2 and P3 have dual roles in addition to their basic I/O function.
• PORT P0 (pins 32 to 39): When the external memory access is required then Port P0 is
multiplexed for address bus and data bus that can be used to access external memory in
conjunction with port P2. P0 acts as A0-A7 in address bus and D0-D7 for port data. It can be
used for general purpose I/O if no external memory presents.
• PORT P2 (pins 21 to 28): Similar to P0, the port P2 can also play a role (A8-A15) in the
address bus in conjunction with PORT P0 to access external memory.
 PORT P3 (Pins 10 to 17): In addition to acting as a normal I/O port,
 P3.0 can be used for serial receive input pin(RXD) • P3.1 can be used for serial
transmit output pin(TXD) in a serial port,
 P3.2 and P3.3 can be used as external interrupt pins(INT0’ and INT1’),
 P3.4 and P3.5 are used for external counter input pins(T0 and T1),
 P3.6 and P3.7 can be used as external data memory write and read control signal
pins(WR’ and RD’)read and write pins for memory access.

10 MECH Dept
IOT Lab –MEL802

Experiment No:01 Interfacing Led With 8051

Date:

AIM: Write an assembly language program to for toggling the LED connected to one of the port
pins of 8051.

THEORY:

Interfacing comprises of hardware (Interface device) and Software (source code to


communicate, also called as the Driver). Simply, to use an LED as the output device, LED
should be connected to Microcontroller port and the MC has to be programmed inside make
LED ON or OFF or blink or dim. This program is called as the driver/firmware. The driver
software can be developed using any programming language like Assembly, C etc.

There are two ways which we can interface LED to the Microcontroller 8051. But the
connections and programming techniques will be different. This article provides the information
on LED interfacing with 8051 and LED blinking code for AT89C52/ AT89C51 Microcontroller.

Interfacing LED to 8051 Methods

11 MECH Dept
IOT Lab –MEL802

Observe carefully the interface LED 2 is in forward biased because the input voltage of 5v
connected to the positive terminal of the LED, So here the Microcontroller pin should be at
LOW level and vice versa with the interface 1 connections.

The resistor is important in LED interfacing to limit the flowing current and avoid damaging the
LED and/or MCU.

 Interface 1 will glow LED, only if the PIN value of the MC is HIGH as current flows towards
the ground.
 Interface 2 will glow LED, only if the PIN value of the MC is LOW as current flows
towards PIN due to its lower potential.

PROGRAM:
ORG 0000H
LOOP: SETB P1.0
ACALL DELAY
CLR P1.0
ACALL DELAY
SJMP LOOP
DELAY: MOV R0,#100
AGAIN: MOV R1,#200
BACK: DJNZ R1,BACK
DJNZ R0,AGAIN
RET
END

12 MECH Dept
IOT Lab –MEL802

SCHEMATIC DIAGRAM:

INFERENCE:

13 MECH Dept
IOT Lab –MEL802

Experiment No:02 Interfacing Of 7 Segment Display With 8051


Date:

AIM: Write an assembly language program for displaying the decimal numbers in 7 Segment
display.

THEORY:

The 7-segment display, also written as “seven segment display”, consists of seven LEDs
(hence its name) arranged in a rectangular fashion as shown. Each of the seven LEDs is called a
segment because when illuminated the segment forms part of a numerical digit (both Decimal
and Hex) to be displayed. An additional 8th LED is sometimes used within the same package
thus allowing the indication of a decimal point, (DP) when two or more 7-segment displays are
connected together to display numbers greater than ten.

Each one of the seven LEDs in the display is given a positional segment with one of its
connection pins being brought straight out of the rectangular plastic package. These individually
LED pins are labelled from a through to g representing each individual LED. The other LED
pins are connected together and wired to form a common pin.So by forward biasing the
appropriate pins of the LED segments in a particular order, some segments will be light and
others will be dark allowing the desired character pattern of the number to be generated on the
display. This then allows us to display each of the ten decimal digits 0 through to 9 on the same
7-segment display.

The displays common pin is generally used to identify which type of 7-segment display it
is. As each LED has two connecting pins, one called the “Anode” and the other called the
“Cathode”, there are therefore two types of LED 7-segment display called: Common
Cathode (CC) and Common Anode (CA).

The difference between the two displays, as their name suggests, is that the common
cathode has all the cathodes of the 7-segments connected directly together and the common
anode has all the anodes of the 7-segments connected together and is illuminated as follows.The
Common Cathode (CC) – In the common cathode display, all the cathode connections of the
LED segments are joined together to logic “0” or ground. The individual segments are

14 MECH Dept
IOT Lab –MEL802

illuminated by application of a “HIGH”, or logic “1” signal via a current limiting resistor to
forward bias the individual Anode terminals (a-g).

PROGRAM:
ORG 0000H
LOOP:MOV DPTR,#0100H
MOV R1,#0AH
BACK:CLR A
MOVC A,@A+DPTR
MOV P2,A
ACALL DELAY
INC DPTR
DJNZ R1,BACK
SJMP LOOP

DELAY:MOV R5,#05
BACK2: MOV R3,#255

15 MECH Dept
IOT Lab –MEL802

BACK1: MOV R4,#255


AGAIN: DJNZ
R4,AGAIN
DJNZ R3,BACK1
DJNZ R5,BACK2
RET
ORG 0100H
DB 40H,79H,24H,30H,19H,12H,02H,58H,00H,10H
END

SCHEMATIC DIAGRAM:

INFERENCE:

16 MECH Dept
IOT Lab –MEL802

Experiment No:03 Interfacing Of Stepper Motor With 8051

Date:

AIM: Write an assembly language program for interfacing stepper motor with
8051.

THEORY:

A Stepper Motor or a step motor is a brushless, synchronous motor which divides a full
rotation into a number of steps. Unlike a brushless DC motor which rotates continuously when a
fixed DC voltage is applied to it, a step motor rotates in discrete step angles. The Stepper
Motors therefore are manufactured with steps per revolution of 12, 24, 72, 144, 180, and 200,
resulting in stepping angles of 30, 15, 5, 2.5, 2, and 1.8 degrees per step. The stepper motor can
be controlled with or without feedback. Stepper motors work on the principle of
electromagnetism. There is a soft iron or magnetic rotor shaft surrounded by the electromagnetic
stators. The rotor and stator have poles which may be teethed or not depending upon the type of
stepper. When the stators are energized the rotor moves to align itself along with the stator (in
case of a permanent magnet type stepper) or moves to have a minimum gap with the stator (in
case of a variable reluctance stepper). This way the stators are energized in a sequence to rotate
the stepper motor..

17 MECH Dept
IOT Lab –MEL802

PROGRAM:
ORG 0000H
MOV A,#66H
LOOP:MOV P2,A
ACALL DELAY
RR A
SJMP LOOP
DELAY:MOV R5,#0AH
AGAIN:MOV R3,#0FFH
BACK:DJNZ R3,BACK
DJNZ R5,AGAIN
RET
END
SCHEMATIC DIAGRAM:

INFERENCE:

18 MECH Dept
IOT Lab –MEL802

Experiment No: 04 Introduction to Arduino platform and programming


Date:

AIM: T o S t u d y Arduino platform and programming

THEORY:
Arduino is a prototype platform (open-source) based on an easy-to-use hardware and software. It
consists of a circuit board, which can be programed (referred to as a microcontroller) and a
ready-made software called Arduino IDE (Integrated Development Environment), which is used
to write and upload the computer code to the physical board.
Arduino provides a standard form factor that breaks the functions of the micro-controller into a
more accessible package.

Arduino is a prototype platform (open-source) based on an easy-to-use hardware and software. It


consists of a circuit board, which can be programed (referred to as a microcontroller) and a
ready-made software called Arduino IDE (Integrated Development Environment), which is used
to write and upload the computer code to the physical board.
The key features are −
 Arduino boards are able to read analog or digital input signals from different sensors and
turn it into an output such as activating a motor, turning LED on/off, connect to the cloud
and many other actions.
 You can control your board functions by sending a set of instructions to the
microcontroller on the board via Arduino IDE (referred to as uploading software).
 Unlike most previous programmable circuit boards, Arduino does not need an extra piece
of hardware (called a programmer) in order to load a new code onto the board. You can
simply use a USB cable.
 Additionally, the Arduino IDE uses a simplified version of C++, making it easier to learn
to program.
 Finally, Arduino provides a standard form factor that breaks the functions of the micro-
controller into a more accessible package.
Board Types
Various kinds of Arduino boards are available depending on different microcontrollers used.
However, all Arduino boards have one thing in common: they are programed through the
Arduino IDE.
The differences are based on the number of inputs and outputs (the number of sensors, LEDs,
and buttons you can use on a single board), speed, operating voltage, form factor etc. Some
boards are designed to be embedded and have no programming interface (hardware), which you
would need to buy separately. Some can run directly from a 3.7V battery, others need at least
5V.

19 MECH Dept
IOT Lab –MEL802

Here is a list of different Arduino boards available.


Arduino boards based on ATMEGA328 microcontroller
 Open source based electronic programmable board (micro controller) and software (IDE)
 Accepts analog and digital signal as input and gives desired output
 No extra hardware required to load a program into the controller board

Types of Arduino Board

 Arduino boards based on ATMEGA328 microcontroller


 Arduino boards based on ATMEGA32u4 microcontroller
 Arduino boards based on ATMEGA2560 microcontroller
 Arduino boards based on AT91SAM3X8E microcontroller

Arduino UNO

Board Details

20 MECH Dept
IOT Lab –MEL802

Different components on the Arduino board :

Power USB
Arduino board can be powered by using the USB cable from your computer. All you
need to do is connect the USB cable to the USB connection (1).

Power (Barrel Jack)


Arduino boards can be powered directly from the AC mains power supply by
connecting it to the Barrel Jack (2).

21 MECH Dept
IOT Lab –MEL802

Voltage Regulator
The function of the voltage regulator is to control the voltage given to the Arduino
board and stabilize the DC voltages used by the processor and other elements.

Crystal Oscillator
The crystal oscillator helps Arduino in dealing with time issues. How does Arduino
calculate time? The answer is, by using the crystal oscillator. The number printed on
top of the Arduino crystal is 16.000H9H. It tells us that the frequency is 16,000,000
Hertz or 16 MHz.

Arduino Reset
You can reset your Arduino board, i.e., start your program from the beginning. You
can reset the UNO board in two ways. First, by using the reset button (17) on the
board. Second, you can connect an external reset button to the Arduino pin labelled
RESET (5).

Pins (3.3, 5, GND, Vin)


 3.3V (6) − Supply 3.3 output volt
 5V (7) − Supply 5 output volt
 Most of the components used with Arduino board works fine with 3.3 volt and
5 volt.
 GND (8)(Ground) − There are several GND pins on the Arduino, any of which
can be used to ground your circuit.
 Vin (9) − This pin also can be used to power the Arduino board from an
external power source, like AC mains power supply.

Analog pins
The Arduino UNO board has six analog input pins A0 through A5. These pins can
read the signal from an analog sensor like the humidity sensor or temperature sensor
and convert it into a digital value that can be read by the microprocessor.

Main microcontroller
Each Arduino board has its own microcontroller (11). You can assume it as the brain
of your board. The main IC (integrated circuit) on the Arduino is slightly different
from board to board. The microcontrollers are usually of the ATMEL Company. You
must know what IC your board has before loading up a new program from the
Arduino IDE. This information is available on the top of the IC. For more details
about the IC construction and functions, you can refer to the data sheet.

22 MECH Dept
IOT Lab –MEL802

ICSP pin
Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting
of MOSI, MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI
(Serial Peripheral Interface), which could be considered as an "expansion" of the
output. Actually, you are slaving the output device to the master of the SPI bus.

Power LED indicator


This LED should light up when you plug your Arduino into a power source to indicate
that your board is powered up correctly. If this light does not turn on, then there is
something wrong with the connection.

TX and RX LEDs
On your board, you will find two labels: TX (transmit) and RX (receive). They appear
in two places on the Arduino UNO board. First, at the digital pins 0 and 1, to indicate
the pins responsible for serial communication. Second, the TX and RX led (13). The
TX led flashes with different speed while sending the serial data. The speed of
flashing depends on the baud rate used by the board. RX flashes during the receiving
process.

Digital I/O
The Arduino UNO board has 14 digital I/O pins (15) (of which 6 provide PWM
(Pulse Width Modulation) output. These pins can be configured to work as input
digital pins to read logic values (0 or 1) or as digital output pins to drive different
modules like LEDs, relays, etc. The pins labeled “~” can be used to generate PWM.

AREF
AREF stands for Analog Reference. It is sometimes, used to set an external reference
voltage (between 0 and 5 Volts) as the upper limit for the analog input pins.

Arduino IDE
 Arduino IDE is an open source software that is used to program the Arduino controller
board
 Based on variations of the C and C++ programming language
 It can be downloaded from Arduino's official website and installed into PC

23 MECH Dept
IOT Lab –MEL802

Set Up
 Power the board by connecting it to a PC via USB cable
 Launch the Arduino IDE
 Set the board type and the port for the board
 TOOLS -> BOARD -> select your board
 TOOLS -> PORT -> select your port

Arduino IDE Overview

Program coded in Arduino IDE is called a SKETCH

24 MECH Dept
IOT Lab –MEL802

 Verify : Checks the code for compilation errors


 Upload : Uploads the final code to the controller board
 New : Creates a new blank sketch with basic structure
 Open : Opens an existing sketch
 Save : Saves the current sketch

Sketch Structure
 A sketch can be divided into two parts :
 - Setup ( )
       - Loop ( )

 The function setup( ) is the point where the code starts , just like the main ( ) function in
C and C++
 I/O Variables, pin modes are initialized in the Setup( ) function
 Loop( ) function, as the name suggests, iterates the specified task in the program

 Arduino supports the following data types -

25 MECH Dept
IOT Lab –MEL802

       void           Long


       Int              Char
       Boolean     Unsigned char
       Byte           Unsigned int
       Word          Unsigned long
       Float          Double
       Array         String-char array
       String-object     Short

Arduino Function Libraries


Input/Output Functions :
 The Arduino pins can be configured to act as input or output pins using the pinMode( )
function
                    Void setup ( )
                         { 
                          pinMode (pin , mode);
large;">                           }
Pin-pin number on the Arduino board
Mode - INPUT/OUTPUT

Conclusion:
Arduino IDE

Step 1 − First you must have your Arduino board (you can choose your favorite board) and a
USB cable. In case you use Arduino UNO, Arduino Duemilanove, Nano, Arduino Mega 2560,
or Diecimila, you will need a standard USB cable (A plug to B plug), the kind you would
connect to a USB printer as shown in the following image.

In case you use Arduino Nano, you will need an A to Mini-B cable instead as shown in the
following image.

26 MECH Dept
IOT Lab –MEL802

Step 2 − Download Arduino IDE Software.


You can get different versions of Arduino IDE from the Download page on the Arduino Official
website. You must select your software, which is compatible with your operating system
(Windows, IOS, or Linux). After your file download is complete, unzip the file.

Step 3 − Power up your board.


The Arduino Uno, Mega, Duemilanove and Arduino Nano automatically draw power from
either, the USB connection to the computer or an external power supply. If you are using an
Arduino Diecimila, you have to make sure that the board is configured to draw power from the
USB connection. The power source is selected with a jumper, a small piece of plastic that fits
onto two of the three pins between the USB and power jacks. Check that it is on the two pins
closest to the USB port.
Connect the Arduino board to your computer using the USB cable. The green power LED
(labeled PWR) should glow.

27 MECH Dept
IOT Lab –MEL802

Step 4 − Launch Arduino IDE.


After your Arduino IDE software is downloaded, you need to unzip the folder. Inside the folder,
you can find the application icon with an infinity label (application.exe). Double-click the icon to
start the IDE.

Step 5 − Open your first project.


Once the software starts, you have two options −

 Create a new project.


 Open an existing project example.
To create a new project, select File → New.

28 MECH Dept
IOT Lab –MEL802

To open an existing project example, select File → Example → Basics → Blink.

29 MECH Dept
IOT Lab –MEL802

Here, we are selecting just one of the examples with the name Blink. It turns the LED on and off
with some time delay. You can select any other example from the list.
Step 6 − Select your Arduino board.
To avoid any error while uploading your program to the board, you must select the correct
Arduino board name, which matches with the board connected to your computer.
Go to Tools → Board and select your board.

Here, we have selected Arduino Uno board according to our tutorial, but you must select the
name matching the board that you are using.

30 MECH Dept
IOT Lab –MEL802

Step 7 − Select your serial port.


Select the serial device of the Arduino board. Go to Tools → Serial Port menu. This is likely to
be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find
out, you can disconnect your Arduino board and re-open the menu, the entry that disappears
should be of the Arduino board. Reconnect the board and select that serial port.

Step 8 − Upload the program to your board.


Before explaining how we can upload our program to the board, we must demonstrate the
function of each symbol appearing in the Arduino IDE toolbar.

31 MECH Dept
IOT Lab –MEL802

A − Used to check if there is any compilation error.


B − Used to upload a program to the Arduino board.
C − Shortcut used to create a new sketch.
D − Used to directly open one of the example sketch.
E − Used to save your sketch.
F − Serial monitor used to receive serial data from the board and send the serial data to the
board.
Now, simply click the "Upload" button in the environment. Wait a few seconds; you will see the
RX and TX LEDs on the board, flashing. If the upload is successful, the message "Done
uploading" will appear in the status bar.
Note − If you have an Arduino Mini, NG, or other board, you need to press the reset button
physically on the board, immediately before clicking the upload button on the Arduino Software.
Arduino - Program Structure

he Arduino software is open-source. The source code for the Java environment is released under
the GPL and the C/C++ microcontroller libraries are under the LGPL.
Sketch − The first new terminology is the Arduino program called “sketch”.
Structure
Arduino programs can be divided in three main parts: Structure, Values (variables and
constants), and Functions. In this tutorial, we will learn about the Arduino software program,
step by step, and how we can write the program without any syntax or compilation error.
Let us start with the Structure. Software structure consist of two main functions −

 Setup( ) function
 Loop( ) function

32 MECH Dept
IOT Lab –MEL802

Void setup ( ) {

}
 PURPOSE − The setup() function is called when a sketch starts. Use it to initialize the
variables, pin modes, start using libraries, etc. The setup function will only run once, after
each power up or reset of the Arduino board.
 INPUT − -
 OUTPUT − -
 RETURN − -
Void Loop ( ) {

}
 PURPOSE − After creating a setup() function, which initializes and sets the initial
values, the loop() function does precisely what its name suggests, and loops
consecutively, allowing your program to change and respond. Use it to actively control
the Arduino board.
 INPUT − -
 OUTPUT − -
 RETURN − -

33 MECH Dept
IOT Lab –MEL802

Conclusion:
Experiment No. 5 Simple Applications using Arduino Development Board
(Any two)
AIM:
To write a program to make LED blink using Arduino.
COMPONENTS REQUIRED:
1. Arduino Uno, LED
2. Jumper wires.
3. Connecting cable or USB cable.

ALGORITHM:
STEP 1: Start the process.
STEP 2: Start ->Arduino IDE -2.0.3
STEP 3: Then enter the coding in Arduino Software.
STEP 4: Compile the coding in Arduino Software.
STEP 5: In Arduino board, connect VCC to power supply 5V and connect to
ground
STEP 6: Connect the Arduino board with USB cable to the system.
STEP 7: Select tools -> select board ->Arduino UNO -> select processor -> AT
Mega 328 p and the select port.
STEP 8: Upload the coding in Arduino board and now for the LED to blink.
STEP 9: Then, the output will be displayed in the serial monitor.
STEP 10: Stop the process.

Circuit Diagram:

34 MECH Dept
IOT Lab –MEL802

Coding:

Code for LED control Using Serial Monitor:


int led1= 13;
int entry = 0;
void setup() {
  // put your setup code here, to run once:
pinMode(13,OUTPUT);
Serial.begin(9600);
}

35 MECH Dept
IOT Lab –MEL802

void loop() {
  // put your main code here, to run repeatedly:
if(Serial.available()>0) {
  entry= Serial.read();
  if(entry== '1'){
    digitalWrite(led1,HIGH);
    {
      Serial.println("LED High");
      }
                 }
  else if (entry == '0'){
    digitalWrite(led1,LOW);
    {
      Serial.println("LED LOW");
      }

                      }
  else{
    Serial.println("Invalid input");
  }
}
}

Result :

Conclusion :

36 MECH Dept
IOT Lab –MEL802

Expeiment No. 7.Temperature Notification Using Arduino


AIM:
To write a program to get temperature notification using
Arduino.
COMPONENTS REQUIRED:
1. Temperature and humidity sensor.
2. Jumper wires
3. Connectivity cable or USB cable.
ALGORITHM:
STEP 1: Start the process. STEP 2: Start
Arduino 1.8.8
STEP 3: Include the DHT library to the Arduino software. STEP 4: Then
enter the coding in Arduino software.
STEP 5: Complete the coding in Arduino.
STEP 6: In Arduino board connect VCC to the power supply 5V and connect SIG
to digital signal DT and connect SND to ground GND using jumper wires.
STEP 7: Connect the arduino board with USB cable to the system. STEP 8: Select
tools  Selected.
STEP 9: Upload the coding to arduino board. Then the output will be
displayed in the serial monitor.
STEP 10: Stop the process.

BLOCK DIAGRAM:

37 MECH Dept
IOT Lab –MEL802

CODING:
#include <dht.h>
#define dht_apin A0 // Analog Pin sensor is connected to dht DHT;
void setup()
{
pinMode(A0,INPUT);
Serial.begin(9600); delay(500);
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);
}
void loop()
{
DHT.read11(dht_apin);
Serial.print("THS:th01:None:");
Serial.print(DHT.humidity); Serial.print("%,");
//Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("degC");
delay(2000);//Wait 5 seconds before accessing sensor again.
}

38 MECH Dept
IOT Lab –MEL802

OUTPUT:

Conclusion:
Thus the output to get temperature notification using Arduino has
successfully executed.

EXPERIMENT NO.6 HOW TO USE PULSE WIDTH MODULATION


ON THE ARDUINO
Aim: To perform PWM using Arduino uno.
COMPONENTS REQUIRED:
1. Arduino Uno, LED
2. Jumper wires.
3. Connecting cable or USB cable.
Theory :
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital
means. Digital control is used to create a square wave, a signal switched between on and off.
This on-off pattern can simulate voltages in between the full Vcc of the board (e.g., 5 V on
UNO, 3.3 V on a MKR board) and off (0 Volts) by changing the portion of the time the signal
spends on versus the time that the signal spends off. The duration of "on time" is called the
pulse width. To get varying analog values, you change, or modulate, that pulse width. If you
repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a
steady voltage between 0 and Vcc controlling the brightness of the LED.
In the graphic below, the green lines represent a regular time period. This duration or period is
the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about

39 MECH Dept
IOT Lab –MEL802

500Hz, the green lines would measure 2 milliseconds each. A call to  analogWrite () is on a
scale of 0 - 255, such that  analogWrite(255)  requests a 100% duty cycle (always on), and 
analogWrite(127)  is a 50% duty cycle (on half the time) for example.

Waveform:

On some microcontrollers PWM is only available on selected pins. Please consider the pinout
diagram of your board to find out which ones you can use for PWM. They are denoted with a
tilde sign (~).

Once you get this example running, grab your Arduino and shake it back and forth. What you
are doing here is essentially mapping time across the space. To our eyes, the movement blurs
each LED blink into a line. As the LED fades in and out, those little lines will grow and shrink
in length.

Circuit Diagram:

40 MECH Dept
IOT Lab –MEL802

Coding:

int ledPin = 6;

int brightness = 0;

int fadeAmount = 5;

void setup() { pinMode(ledPin, OUTPUT); }

void loop() { analogWrite(ledPin, brightness);

brightness = brightness + fadeAmount; if (brightness <= 0 || brightness >= 255)

{ fadeAmount = -fadeAmount; } delay(30); }

41 MECH Dept
IOT Lab –MEL802

EXPLANATION OF THE CODE


At the top of the sketch, we declare an int variable called ledPin to store the pin number
connected to the LED. Then we declare an int variable called brightness, which will store the
analogWrite values as they cycle between 0 and 255. We also declare a variable
called fadeAmount, which will be used to control how quickly the LED fades on and off.
In the setup() section, we use the pinMode() function to set the ledPin as an output.

42 MECH Dept

You might also like