Iot - Unit II Ar19 Ece
Iot - Unit II Ar19 Ece
Course Name
Internet of Things [ IoT ]
Unit II-Introduction to Arduino, Raspberry Pi:
Course Instructor
P Bujjibabu , M. Tech.,(Ph.D.), Senior Member IEEE
Assistant Professor
Dept of Electronics and Communication Engineering
Aditya Engineering College(A)
Surampalem.
5/15/2023 IoT P. Bujjibabu
Course objectives:
COB 1: To introduce the concepts of the Internet of Things (IoT).
COB 2: To impart knowledge on IoT and Machine to Machine application areas.
COB 3: To introduce various Integrated Development Environments (IDEs) used in IoT.
COB 4: To enable the students learn the effective usage of device connectivity and cloud
connectivity models.
COB 5: To illustrate various cloud connectivity protocols.
COB 6: To make the students understand the implementation of IoT for various
applications.
Course outcomes:
CO 1: Explain the basic building blocks of IoT.
CO 2: Make use of the features provided by the Arduino & Raspberry Pi IDEs and
develop basic programming.
CO 3: Explain various sensors and actuators used in IoT.
CO 4: Outline the protocols for device and cloud connectivity in IoT.
CO 5: Demonstrate the case studies of IoT applications
Introduction
Arduino Uno Architecture,
Setup the IDE,
Editing in Arduino,
Arduino Libraries,
Embedded C programming for Arduino,
Introduction to Raspberry Pi,
Types of Raspberry Pi,
Architecture of Raspberry pi 3B+,
Basic Embedded C/ Python programming for Raspberry Pi,
5/15/2023 IoT P. Bujjibabu
Examples
Industry / Robot /
System
Industry /
System
IoT P. Bujjibabu
4
System & Control system
System / Plant:
“A system may be a piece of equipment, perhaps just a set of machine
parts functioning together, the purpose of which is to perform a
particular operation”.
Control System:
“A control system is an interconnection of components forming a system
configuration that provide a desired system performance”
Un
Open loop No
controlled
systems feedback
O/P
Systems
Closed loop Controlled
feedback
systems O/P
5
IoT P. Bujjibabu
Open loop system
6
IoT P. Bujjibabu
Multivariable control system
7
IoT P. Bujjibabu
Embedded Systems
• "It is a combination of hardware and software to perform a specific task"
Communication
Power Supply Processor Memory Timers & Counters
Ports
• Assembler
Application Specific Software • Emulator
Input & Output
Circuits Components • Debugger
• Compiler
8
IoT P. Bujjibabu
Why Arduino..?
Arduino ( Arrr-dween-oh )
(n.) It's an open-source physical computing platform based on a simple
microcontroller board, and a development environment for writing
software for the board.
9
IoT P. Bujjibabu
Types of Arduino Boards
Arduino Mega
Arduino Nano
Arduino LilyPad
Arduino Leonardo
Arduino Uno Arduino Mini
10
IoT P. Bujjibabu
Arduino Uno
UPLOAD
NEW TAB
14
IoT P. Bujjibabu
Tools
• Auto Format This formats your code nicely: i.e. indents it so that
opening and closing curly braces line up, and that the statements
inside curly braces are indented more.
• Archive Sketch Archives a copy of the current sketch in .zip format.
The archive is placed in the same directory as the sketch.
• Fix Encoding & Reload Fixes possible discrepancies between the
editor char map encoding and other operating systems char maps.
• Serial Monitor Opens the serial monitor window and initiates the
exchange of data with any connected board on the currently selected
Port. This usually resets the board, if the board supports Reset over
serial port opening.
• Serial Monitor Opens the serial monitor window and initiates the
exchange of data with any connected board on the currently selected
Port. This usually resets the board, if the board supports Reset over
serial port opening.
15
IoT P. Bujjibabu
Tools
• Board Select the board that you're using. See below for descriptions
of the various boards.
• Port This menu contains all the serial devices (real or virtual) on your
machine. It should automatically refresh every time you open the top-
level tools menu.
• Programmer For selecting a hardware programmer when
programming a board or chip and not using the onboard USB-serial
connection. Normally you won't need this, but if you're burning a
bootloader to a new microcontroller, you will use this.
• Burn Bootloader The items in this menu allow you to burn
a bootloader onto the microcontroller on an Arduino board. This is not
required for normal use of an Arduino board but is useful if you
purchase a new ATmega microcontroller (which normally come
without a bootloader). Ensure that you've selected the correct board
from the Boards menu before burning the bootloader on the target
board. This command also set the right fuses. 16
IoT P. Bujjibabu
Libraries
17
IoT P. Bujjibabu
Basic Coding structure
18
IoT P. Bujjibabu
Basic Coding structure
setup() function
• Called when a sketch starts.
• To initialize variables, pin modes, start using libraries, etc.
• Will only run once, after each power-up or reset of the Arduino
board.
loop() function
• Loops consecutively.
• Code in the loop() section of the sketch is used to actively control the
Arduino board.
Commenting
• Any line that starts with two slashes (//) will not be read by the
compiler, so you can write anything you want after it.
19
IoT P. Bujjibabu
pinMode()
• Instruction used to set the mode (INPUT or OUTPUT) in which we
are going to use a pin. •D13 - 101
• E.g.: pinMode (13, OUTPUT); •D13 - Due
•D1 - Gemma
• i.e.. setting pin13 as output. •D13 - Intel Edison
•D13 - Intel Galileo Gen2
•D13 - Leonardo and Micro
•D13 - LilyPad
digitalWrite() •D13 - LilyPad USB
• Write a HIGH or a LOW value to a digital pin. •D13 - MEGA2560
•D13 - Mini
• E.g.: digitalWrite (13, HIGH); •D6 - MKR1000
•D13 - Nano
• i.e.. setting pin 13 to high or ON •D13 - Pro
• E.g.: digitalWrite (13, LOW); •D13 - Pro Mini
•D13 - UNO
• i.e.. setting pin 13 to high or OFF •D13 - Yún
•D13 - Zero
20
IoT P. Bujjibabu
external LED with this sketch
• You need to build this circuit, where you connect one end of the
resistor to the digital pin correspondent to
the LED_BUILTIN constant.
• Connect the long leg of the LED (the positive leg, called the anode)
to the other end of the resistor.
• Connect the short leg of the LED (the negative leg, called the
cathode) to the GND.
• In the diagram below we show an UNO board that has D13 as the
LED_BUILTIN value.
• The value of the resistor in series with the LED may be of a different
value than 220 ohm; the LED will lit up also with values up to 1K
ohm.
21
IoT P. Bujjibabu
22
IoT P. Bujjibabu
digitalRead()
• Reads the value from a specified digital pin, either HIGH or LOW
• E.g.: int inPin=7;
val = digitalRead(inPin);
• i.e.. Reads the value from inPin and assigns it to val.
int ledPin = 13; // LED connected to digital pin 13
Sets pin 13 to
int inPin = 7; // pushbutton connected to digital pin 7 the same
int val = 0; // variable to store the read value value as pin 7,
void setup() { declared as an
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output input
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop() {
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}
23
IoT P. Bujjibabu
delay()
• Pauses the program for the amount of time (in milliseconds)
specified as parameter.
• E.g.: delay(1000);
• i.e.. waits for a second (1000 ms = 1 s)
24
IoT P. Bujjibabu
Libraries
• Open the IDE and click to the "Sketch" menu and then Include
Library > Manage Libraries.
• Then the Library Manager will open and you will find a list of
libraries that are already installed or ready for installation.
• In this example we will install the Drive library. Scroll the list to find
it, click on it, then select the version of the library you want to install.
• Sometimes only one version of the library is available. If the version
selection menu does not appear, don't worry: it is normal.
26
IoT P. Bujjibabu
Importing a .zip Library
27
IoT P. Bujjibabu
Embedded C programming for Arduino-Introduction
Platform-agnostic (cross-
Platform-specific
platform)
Compiled (Mostly) interpreted
Slower at runtime Faster at runtime
Less code-intensive More code-intensive
Creates apps as part of a stack Creates standalone apps
JavaScript/ECMAScript PHP
Python Ruby
Examples Groovy Perl
Lua Bash
PowerShell R
30
VBA Emacs
IoT
Lisp GMLP. Bujjibabu
C-Language:
C is a general-purpose programming language, used to design
any type of desktop-based applications.
It was developed by Dennis Ritchie as a system programming
language to develop the operating system.
The main features of C language include low-level access to
memory, a simple set of keywords, and clean style
These features make C language suitable for system
programming like OS or compiler development.
In nature it uses a native platform development scheme, i.e. the
development of the application by it is platform-dependent and
can only be used on a single platform.
31
IoT P. Bujjibabu
Embedded C-Language:
Embedded C is an extension of C language and it is used to develop
micro-controller based applications.
The extensions in the Embedded C language from normal C
Programming Language is
– the I/O Hardware Addressing,
– fixed-point arithmetic operations,
– accessing address spaces, etc.
Embedded C Program has five layers of Basic Structures.
They are:
– Comment:
– Pre-processor directives:
– Global Declaration:
– Local Declaration:
– Main function(declaration part and the execution part)
32
IoT P. Bujjibabu
Embedded C Program has five layers of Basic Structures
COMPILER A specific compilers that are able to The standard compilers can be
generate particular hardware/micro- used to compile and execute
controller based output is used. the program.
Popular Compiler are: Popular Compilers are:
Keil compiler GCC (GNU Compiler
BiPOM ELECTRONIC collection)
Green Hill software Borland turbo C,
Intel C++
34
IoT P. Bujjibabu
Parameter Embedded C Language C language
USABILITY AND
• Formatting depends upon the type
APPLICATION • C language has a free-format
of microprocessor that is used.
of program coding.
• It is used for limited resources like
• It is specifically used for
RAM and ROM.
desktop application.
• High level of optimization.
• Optimization is normal.
• It is not easy to read and modify the
• It is very easy to read and
Embedded C language.
modify the C language.
• Bug fixing is complicated in a
• Bug fixing are very easy in a C
Embedded C language program.
language program.
• It supports only required processor
• It supports other various
of the application, and not the
programming languages
programming languages.
during application.
• Only the pre-defined input can be
• Input can be given to the
given to the running program.
program while it is running.
• Applications of Embedded C
• Applications of C Program:
Program:
• Logical programs
DVD
• System software
TV
programs
Digital camera 35
IoT P. Bujjibabu
Factors for Selecting the Programming Language
The following are few factors that are to be considered while selecting
the Programming Language.
Size: The memory that the program occupies is very important as
Embedded Processors like Microcontrollers have a very limited
amount of ROM (Program Memory).
Speed: The programs must be very fast i.e., they must run as fast as
possible. The hardware should not be slowed down due to a slow
running software.
Portability: The same program can be compiled for different
processors.
Ease of Implementation
Ease of Maintenance
Readability
Adaptability
36
IoT P. Bujjibabu
Programming Embedded Systems
http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-
7810-Automotive-Microcontrollers-
ATmega328P_Datasheet.pdf
• Now when you have datasheet you need to see the pin configuration
or the schematic of your microcontroller so as to get the
understanding of which the pin mapping and port numbers.
• The below picture is the pin diagram of Atmega 328P-
39
IoT P. Bujjibabu
Atmega32 has 32-gpio's grouped into four 8-bit ports namely PORTA-PORTD
https://www.microchip.com/webdoc/AVRLibcReferenceManual/
After this you also need to have the AVR libC reference manual. It is an important
document if you want to get started with programming in embedded C. You can find the
reference manual here-
IoT P. Bujjibabu
We need reference manual because it provide information about the
set of C libraries for AVR architecture.
They provide support and are vendor provided.
These libraries helps you in creating a C code that would be
understood by your microcontroller.
And this the exact reason why we call Embedded C a hardware
dependent language. Because your set of C library will change if you
will change your hardware.
Advantages with Programming in C or Embedded C
• Significantly easy to write code in C
• Consumes less time when compared to Assembly
• Maintenance of code (modifications and updates) is very simple
• Make use of library functions to reduce the complexity of the main
code
• One can easily port the code to other architecture with very little
41
modifications IoT P. Bujjibabu
Basics of Embedded C Program
46
IoT P. Bujjibabu
#include<reg51.h>
void main(void)
{
bit mydata P1^0;
//char mydata[]={};
for(z=0;z<=50000;z++)
{
P1= 0x55;
P1= 0xAA;
}
//P1= mydata[z];
}
47
IoT P. Bujjibabu
Introduction to Raspberry Pi
The Raspberry Pi is a single-board computer developed by
48
https://datasheets.raspberrypi.com/ IoT P. Bujjibabu
Introduction to Raspberry Pi
• A low cost, credit-‐card sized computer
• And to figure out how to program in dialects like Scratch and Python
49
https://realpython.com/python-raspberry-pi/
IoT P. Bujjibabu
Embedded C programming for Arduino
50
IoT P. Bujjibabu
Raspberry Pi Kit Contents
Raspberry XXX Raspberry Pi Case Power Supply
1. Raspberry Pi B+ or newer.
2. USB WiFi Dongle.
3. Pi Camera
Pi Camera WiFi Dongle
4. SD Card (8 GB+ recommended).
5. SD Card Reader.
6. Keyboard and/or mouse.
7. Laptop or HDMI display monitor
8. Power Supply
51
Micro SD Card IoT P. Bujjibabu
Types of Raspberry Pi
• Raspberry Pi 1 model B
• Raspberry Pi Zero
1. No of GPIOs
• Raspberry Pi 2 2. No of USBs
• Raspberry Pi 3 model B 3. No of Micro SDs
4. Power Consumption
• Raspberry Pi Zero W 5. Audio features
6. The Shape or the accessibility
52
IoT P. Bujjibabu
Architecture of Raspberry pi 3B+
• The Pi 3 Model B+ has a 1.4GHz 64-bit quad-core
Broadcom Arm Cortex A53-architecture processor
compared with the Raspberry Pi 3 Model B's 1.2GHz CPU.
• It is with a 15 percent performance improvement.
• It also supports dual-band wireless local-area networks at
2.4GHz and 5GHz, Bluetooth 4.2, and Bluetooth Low
Energy.
• Raspberry Pi Foundation says the dual-band wireless LAN
comes with modular compliance certification, which should
make it easier and cheaper to commercially launch products
based on the Raspberry Pi 3 Model B+.
• This move should help cut down on wireless LAN
compliance testing.
53
IoT P. Bujjibabu
Architecture of Raspberry pi 3B+
• Like every other Raspberry Pi board, R-Pi 3 B+ is a single-
board computer.
• But it has a fast and power-efficient 1.4 GHz
processor (1.2GHz in model B) and a faster gigabit
Ethernet (it’s limited to 300 Mbit/s by the internal USB
2.0 connection) or dual-channel 2.4 / 5 GHz 802.11ac Wi-
Fi (100 Mbit/s).
• It also comes with a USB boot, network boot, and Power
over Ethernet(PoE) option that are not present in the B
model.
• Raspberry Pi 3 B+ Pinout with GPIO functions, schematic,
and specs are given in detail below
54
IoT P. Bujjibabu
Architecture of Raspberry pi 3B+
55
IoT P. Bujjibabu
Architecture of Raspberry pi 3B+
• Processor: The BCM2837B0 processor is the main component of
this tiny board that helps in carrying out a large set of instructions
based on mathematical and logical formulas. BCM2837B0 is a
1.4GHz 64bit ARM quad-core Cortex A53 processor.
• RAM: RAM used in R-Pi 3 B+ is 1GB LPDDR2 SDRAM (similar to
the previous version).
• GPU: It stands for Graphics Processing Unit and is used for
performing out the image calculation. The GPU uses OpenGL ES
version 2.0, hardware-accelerated OpenVG API, and 1080p30 H.264
high-profile decoder. It can provide up to 1Gpixel/s, 1.5Gtexel/s, or 24
GFLOPs of a general-purpose computer.
• USB Ports: Similar to model B, model B+ also consists of 4 USB
ports. Thus removing the hassle of connecting the USB hub in order to
increase the ports.
56
IoT P. Bujjibabu
Architecture of Raspberry pi 3B+
• Micro USB Power Source Connector: This connector is used
for delivering 5V power to the board. It consumes approx. 170 to
200mA more power than model B. The power connector is also
repositioned in the new B+ model and placed next to the HDMI
socket.
• HDMI and Composite Connection: Both the audio output
socket and the video composite socket reside in a single 4-pole 3.5mm
socket which is placed near the HDMI port, and now all the power
and audio-video composite socket are placed on the one side of the
board which gives it a clean and nice look.
• USB Hard Drive: The board is capable of using an external USB
hard drive.
• PoE: B+ model comes with a facility of Power over Ethernet (PoE); a
new feature added in this device which allows us to power the board
57
using the ethernet cables. IoT P. Bujjibabu
Raspberry pi Features
• Superior software implementation
• 64-bit Quad-core processor
• Large RAM (latest Raspberry Pi 4 Model B Board has up to
8G of RAM)
• Processor speed- 700MHz- 1.5GHz
• Raspberry Pi has 40 input/output pins.
• It can be connected to the Internet.
• It can run all kinds of applications (including MS Office and
Email).
• It contains everything- CPU (Central Processing Unit), GPU
(Graphics Processing Unit), Ethernet port, GPIO (General-
Purpose Input/Output) pins, and power source connector.
58
IoT P. Bujjibabu
Difference Between Arduino and Raspberry Pi
Logic level Arduino’s logic level is 5V. Raspberry Pi’s logic level is 3V.
62
IoT P. Bujjibabu
Raspberry Pi Software
• The operating system for the Raspberry Pi is stored on a
microSD card.
• If your card did not come from an official Raspberry Pi kit,
then you’ll need to install the operating system on it.
• There are multiple ways to set up the operating system on
your Raspberry Pi.
• You can find out more about the different installation
options on the Raspberry Pi website.
• You’ll look at two ways to install Raspbian, the officially
supported Raspberry Pi operating system, which is based
on Debian Linux.
• The Raspberry Pi foundation recommends that you use
the Raspberry Pi Imager for the initial setup of your63SD
IoT P. Bujjibabu
• The Raspberry Pi Foundation specifically selected Python
as the main language because of its power, versatility, and
ease of use.
• Python comes preinstalled on Raspbian, the Raspbian
operating system comes with several preinstalled Python
IDEs that you can use to write your programs.
• One of these IDEs is Mu. It can be found in the main menu
• Raspberry Pi Icon → Programming → Mu
• There’s a chance that Mu may not be preinstalled on your
version of Raspbian.
• If Mu isn’t installed, then you can always install it by going
to the following file location:
• Raspberry Pi Icon → Preferences → Recommended Software 64
IoT P. Bujjibabu
Using Mu
65
IoT P. Bujjibabu
Over SSH
If you’d rather use SSH to access your Raspberry Pi, then you’ll use the
command line to create the python-projects directory.
Both nano and vim come preinstalled on Raspbian and can be used to
edit the project files.
You can also use VS Code to remotely edit files on the Raspberry Pi, but
some setup is required.
66
IoT P. Bujjibabu
THANK YOU