Unit 3 FIOT
Unit 3 FIOT
l'),(bon
It is a simple, interactive, interpreted object oriented programming language. When compared to other programmm
it is a very robust, elegant and a powerful language.
l~ages. g
features of Python
Object-oriented Programming Paradigm
1,
Python is an object-oriented pro~an~minglang~age, as it sepa~ates data_from logic. Moreover, Python is not only object-
oriented, but also a proper combmat10n of multtple programmmg paradigms.
Easy to Learn
Python consists of very few keywords, simple structure and well defined syntaxes which makes it easy to learn.
Easily Readable
3.
Unlike other programming languages, it does not make use of symbols like ($),· semicolon (;), tildes (~) for accessino
variables, defining code blocks and pattern matching. Rather, it defines a simple and clear code, which is easier for th:
programmer to read. Python language has been designed in a way, that the code written by one programmer can be easil)
understood by the other.
4. Easily Maintainable
Since, Python is simple unlike other programming languages, it does y.ot require much difficulty for maintenance. For
example, for the existence of its code, it qoes not depend on the presence of the programmer at a company. Toe most
important advantage of Python is that, at the time of reviewing a script written very long time ago, the programmer does
not require much guidance from a reference book.
s. Uses Wgb-level Dat~ Structure
. .
Python has been designed in a way, that the programmer does not need to look after the low level details such as, memor)
management. It supports high level data structures such as, realizable arrays (lists in Python), hash tables (dictionary in
Python). As these building blocks are defined in the core language, they are extensively used. This reduces the development
time, as well as the size of the code. Thereby, making the code more simpler to read.
6. Extensibility
Python ·allows the p~ofiammer to code some part of the program in different languages like, C, C++, java and utilize them
through Python code.
7. Portability
Since Python is written in C, it can work on different platforms i.e., the Python applications built on one system, can run
on any other system, with little or no modification. Any platform that.contains ANSI C compiler supports Python. Python
is also independent of the type of archite~ture. · ·
I,• ·Robust
Python is more powerful in determining as well as handling error conditions, through software handlers. It ~uides the
' program.mer, by providing full details of errors, in case of program crashing due to errors. For example, the locatIOn where
the error has occurred in the code, by specifying the filename, line number and function. It also allows the programmer to
take an action for the elimination of the error.
· . . · -fy · th
.The exception handlers in Python are capable of reducing debuggmg blem redirecting the
process, by simp1I mg e ~ro . ' . •t case
ro
P gram flow, performing cleanup or maintenance measure, cIosmg · the 1· t· l cmntly or Just 1gnonng 1 1n
app ica 100 e e0 - · . f the ·error in
'
if ~e. errors covld not be handled properly, Python generates a stack that displays the type and l~catwn ° · '
addition to the location of module that contains the erroneous code. ·
,SPECTRUM flLL-IN-O~E JOURNAL FOR ENGINEERIN~ STUDENTS .
I .I
I
3.6 FUNDAMENTALS OF INTERNET OF THINGS. [JNTU-HYOERAa.,,,I)~
. ~
9. P~esses the Rights of a Memory Manager
O f th ' d 't It If handles all the functionalities re lat
ne o. e mam a vantages of Python progrnmming langungc is tha,l, ! se d the overall development ti ed to
~emoty management. Thereby, reducing the programmers burden. Th1s JO turn r~ uces llle an()
mcludes fewer bugs. ll handles memory management through the use of Interpreter.
10. Interpreted and (Byte) ContpU~ Language
Just like Java, Pytl10t1 is also an interpreted and a byte~com_piled language. That is, it allows theJ.rllogramd mb erpto run the
Y ython 1·s,
Pro d' l ft · ··
. gram trect Y 0111 the source code without performing the comp1lal1on pr~ · process 10 owe
cess · The
(i) It just transforms the .source code into an intermediate form known as, 'byte code'·
(ii) Then translates the byte code into a machine code (O's and 1's)
(iii) And then run it.
• • proper lihkage and loading oflibraries etc ·
The Programmer need not have to worry about the program comp1lat10n,
11. Scalability .
nd th
The term 'scalability' here, refers to ;he c~pability of the programming .language, to accept a _·haoclle e additional
performance required, upon the·addition of new hardware to the system. Unlike other programmm~ langu~g~s, Pyt?on
can expand the code size, by adding new or existing Python elements and reusing the code when reqmred. This ts possible
because, Python has a pluggable and modular architecture.
12. ~ffective Rapi(f Prototyping Tool
Python provides several interfaces to other systems, which makes it powerful and robust enough to prototype_the entire
systems within it, in a very le~s time. '
Q10. Explain the syntax of Python language.
Answer:
Python language defines a very simple syntax. Its·simplicity lies in design choice. There are several ways to represent 1
single program but this leads·to difficulty in learning and using the language. The philosophy of Pythons states that "There is on
and one way to do it".
Since, Python is a non-functional language we. mostly use assignment statement. The syntax is as follows,
,..
,, . ,,
Syntax
name = expression
Here, the 'name is binded to the value of.the expression.
In other programming languages the above line will assign the expression val~e to name.
Thus, python uses -assignment operator to bind a name to data value generated by the expression. If a reference value ·,
. taken in place of expression then the name, will be binded to the existing value.·
Unlike, other programming language name is not bound to particular data type and no type checking is needed to bind th
name to the value.
Identifiers in Python
• Python define its identifier using letters, digits and a underscore. But, the first character of a name should never be a digit
' '
If several names have to be binded different to the same value, it can be done in. single statement as follows '
>>>SI = s2 = s3 = "Hello world"
Here, S 1, S2 and S3 are binded to Hello world string.
>>>print S1
I
>>>print s2
I
j ' >>>printS3 , , •
.
The output of these three,statements is same i.e., Hello world as all three names binded tosamestnng.
WARNING: Xerox/Photocopying of this book Is a €RIMINAL act. Anyone found guilty is. LIABLE to «,ace LEGAL proceedings
•
• f
. '
.
lVtf C Modtl P•ptr•I, Ql{at
~-- J\t\) s,>llwrm• hos lwo wt sions ' 2 hit nntl htl hit Tlw 1t'I-IJll'\'tlw vcrnlon hnscd ot1 the operating system can be
downloaded
. 1lk\1 thml thl' lwfow hnk .
J il,s11
~1111
https ://www.pyth\m.1Rp,/,h1wnlonds/il•k•llNl'/pytho11,J(14
' •kl'I 'Wtl\lfo,,s ~ ~h M l'Xl'l'lltnbk instullcr' lht M bit opcruting 1-1y11tcm computer or seJcct ' windows
1,,lki ' for ~ }-hit opl'tnting system comp,1tcr with this 'pylhon-3.6.4 and 64-cxc' file will be downloaded.
1<86 executable
~l
UIS•
l),iuhll' dk1' (,n 'p) thon ,J.(1..t-nnd M.1..'xc' Ille. This will displuy a Hotup dialog box.
('Itek the dl'-'ckhox install lnunchc1 1hr nil the u:,crs oml udd python 3.6 to PATIi.
.,. lnstall'Now
C:\User~\CG3\AppOata\~oca~ProgramMython\?ython36
_. Customize installation
Choose location and features
python
r,, [j ln~tall launcher for all users (re~mmended)
windows i'.l] Add Python 3.6 to PATH
Setup Progre~s
Installing:
python
f,
windows Cancel
·python
for
windows
012. Define control statements. Discuss about
Answ er:
Decision Struc tures / Control ~tatements
if statement. -
Mod~ Paper-II, 06<•)
Decis ion struct ures or Contr ol statem ents are the statem
ents that allows the progra mmer to specif y the next statem
must be execu ted depen ding on some condit ions. · ent that
'
if State ment
The if statem ent contai ns the follow ing three compo nents,
(i) The Keyword 'if'
The 'if' clause is execu ted, only if the expres sion is
true otl)erwise, execu tion starts at the next statem ent
code. ·
follow ing thi
(ii) An Expression or a Cond ition
An expre ssion is tested for its truth value. If the expres sion
evalua tes to non-z ero or Boole an true then the code is
execu
Syntax
if expres sion:
Statei nentl
Statem ent2
Multiple Conditional Expressions
To provid e multip le condi tional expres si9ns, or to perfor
m negati on of expre ssions in the if statem ent, Boole
and, or and not are used. an operaton
ExamP.le
if not valid or (coun t< 0):
print "Inva lid input"
(iii) The Code
Single State ment Code
If the code of if clause conta ins only a single statem ent, it may
be writte n on the same line as the header statement.
ExampJe
if i = = I : print "Welc ome to Python"
Although it is easy to write tqe .code in one line, but it is difficult to read.
_
·• ,..ht •,
~ ate 1.~.
1'
..
,..
p·>> I . . __, ~ ,
Ln; 188 Col: 4
~o1scuss about If-else statement.
--1
if...elSe Statement
Ifelse statement is used to execute a set of statements if the condition is true otherwi
se it executes,another set of statements.
1be syntaX of if...else is as follows, ·
if condition :
# statements
else:
# statements
The if is succeeded by a copdition and a set of statements. If the condition
is true the statements following if condition
will be executed.
Jfthe condition is false the statements following else will be executed.
Enmple
a= int(input("Enter a number : "))
ifa%2 =0:
print("The given number is an even number")
else:
print("The given number is an odd number")
Output
Ln:192 Cor. 4
1bis In the above examp~e initiall~ t~~ input is accepted from_user !nto a. The value of 'a'
Ifthe~ r generates remainder of d1V1s10n operator. If the remainder 1s Othen the :°
is div~de~ by 2 ~s~ng operator.
statement follow mg 1f cond1t10n 1s executed.
remainder is non zero then the statement following else will be executed.
.
Q14. Give• brief Introduction about repetltloh structure.
"nswe ,· :
.
FUNDAM~NTALS OF tN
reRNET OF THINGS [JNTU+tYoe~~
.
Thc me as
progrmnmcrs sometimes need to write thb code that per forms thebest sa · t write the code t , con31.
approach is
program to calculate t Opercent sales conunission for several sales people. One o o ca1c~1-
sales person commission and then repeat the same code for every sales person. ~
With each iteration, the condition in the while loop is checked, whenever condition becomes
false control comes o
the loop.
2. Count Controlled Loop: The for Loop
The for statement is a powerful looping construction in Python. The looping mechanism
in for statement is applied, o
the sequence members. for statement calls the next() ,nethod of an iterator and ends when
Stoplteration except-ion is raised.
comprehensions and generator expressions als~ uses 'for statement'.
Syntax
for var in <object> :
<statements>
To repeat the code in the/or loop, each time the iteration v¥iable is set to the current element
of the object.
WARNING: xerox/Photocopying of this book is a CRIMINAL act. Anyone found guilty is LIABLE to face LEGAL proceedings.
3.1 1
of for Loops
1rsrs
Used "'1th Sequcncl' l)·pes
til
8115 tc Usage
tor spam in 'student':
print(C'urrcnt letter:', spmn)
output
Example
chocolates= ['Snickers','Cadbury', 'Lacto']
for x in chocolates:
print(x, "chocolate")
Output
Ln:25 Col: 4
For each iteration in the above example, the
list is iterated and the variable 'x' contains
the list elements.
(ill) Iterating by Sequence Index
Example
print(arrayelements[ x])
I
i.t.r-; rMr 1
t nlil~
t:htll!
I l-"'n
I e-~ < l
>" ' I
Ln: 31 Co~ 4
Save Ctrl+S
Save As... Ctrl+Shift+S
Save ~opy As... Alt+Shift+S
3.
Anew window will open. Type the program in it.
, ~ ,,.• q ·r ;:- •r h·.!i r------------------ - - D_.J;:..___, !
X t !
0
Clo~e Alt+F4
fxit Ctrl+Q
111n32
rl'Pt 'cop~Iight", 'ondi~s• or 'H0en1, I)' !or more lnton1.,11!on,
.,
~ ,
0
J
P)>th9n 3.6,4 (v3,6, ◄ tdl!ec,b, D~c H 20J7, 06104149) (MSC v.1~00 J~ bit (Intel) on
1
·, ) ~
' ◄ \
3. Save the file with .py extension. Even text editors such as notepad or notepad ++ to write the code and press ctrl +
exit
4. Without making the file executable it can be run by navigating the location of the script and then by typing MyScript.
5. MyScript.py can be made executable by typin~ chmod +x MyScript.py at command prompt.
6. To run the script type at command prompt.
C:\>python MyScript.py
This command will call the compiler to convert the file into byte code. This byte code will converted to machine code a
the processor will execute this machine code.
Output
i C:\.>cd JJytlwr,
J;:\p1;tho1r>µytl,on Ny8c1•ipt.py
8
G; '.pyt lion)
~ARNING: Xerox/Photocopying of this book Is a CRIMINAL act. Anyone found guilty is LIABLE to face U:GAL proceedings.
(
I
""'r-3 __ __ __ __ __ __ __ __ __ 3.15
oefln& operator. Describe different types of
Q1 1· perators supported by Python with an example
(II) 'l'1·11r Olv1!1lon (/)
0
tor each. 111 lhc Tntc IJi vi!lioti Oprn,t ion, the quo tient or res ult
ohtn lnc<I wi ll he the Hcltiol vuluc, irrcs pcclivc of the
Model P11per-l, Q7 oprn_'fll1d lypt'S, Thill type of division opcrntio11 can be
,4it8~r:
poi formed, only whc11 llw ' from fu1 11rc lm porl di v1sio11'
operstor dirllcliv c is defined.
An operator cun b~ defined ns a symhol which 11crfonn
ltxum1>lc
opcl'3ti.ons such as mn1hcmaticnl, logical 1111d other
,r. ,
1
~111\;
s:,ipulations ~sing one or more opcrnnds. Python supports, >--.-:, from •future- import di vision
11 nun,bl:r ot opcr.1tors. >» 25/2
,srsc
~ of Operators · 12.5
'l)r -
There are three main different types of operators
>>> 25 .0/2.0
..,.,rted by Python. They are listed below,
$t1rr~· 12.5
. Arithmetic or numeric operators
1
(iii) Floor Dl~ision (//)
n. Comparison/Relational operators
In the floor division operation, the fractions obtained
JU. Lo~ical operators. in the quotient are always truncated and rounded to the
Arithmetic or Numeric Operators smallest whole number which is on its immediate left
1. on the number line.
The numeric type arithmetic operators that are supported
bj python are listed below: Example
1. Division(/,//) >» 25//2
2. Modulus(%) 12
. . h , , more tighter
# Mukc11 lcfl bir1d111g wit -
- 32
'th••••
>>>2.0 •• - 5.0 # Righi binding wilh ,_, is tighter than w, .
. d t egativo powers
fl Integers cannol be raise O n '
Tmceba..:k(innennost lost);
File"<stdin>'', line l \n?
VnlueError:intcgcr to the negative power
. edence is given below.
The list of all numeric arithmetic operators in the highest-to-lowest order of prec
. S.No. Operator
.
Syntax Function
.1 12.6
WARNING; Xerox/Photocopying of this book le a CRIMINAL act. Anyone found guilty is LIABLE to face LEGAL proceedings.
r'....rr. s . 3 . 17
~ Ch•DI" (Unary(+) Operator)
~ - 11'C no change (unary(+) opcmtor) is used before n vorinblc hut the value of it docRnot change lf thc value is positive it
~oins positive ond ifthe vnlrn,' is negative it rcmoins nc~ativo. ·
g11111ple
>:;-,;,,~ 5 '
:!:f
~;,,:>print (- x:)
-5
;,,>'>X == S
;,-~>print (x)
s
Negtdon(Unary(-) Operato1·)
Una'-Y lll~~s operator_ is re~resent~d by the symbol_ minus (- ). It_is used before a variable to make its value negated. That
is the pos1t1ve value of a vanable will become negative and negative value will become positive when this operator is used.
Etample
>>>X:::: 5
';;-;>>print (- x)
-5
>>>n=-5
>>>n=-n
>>>print (ri)
5
E1amples
;j >» 2-./2
; :2. 0
1
' »~ 6 .~/
- ' 2 .;;-
1 2 · "'
-1 » > 25/ /2
!- -
l ... '
~ ~» :Si2
1;» S.. 6
7 2'?
::.>> -2---e
6
, >>> 3.5•• --:-6. 0
J o.001s 1s9~:02~11eto1 g
l~~>.L . ----·- •---·
Ln: 25 Col: 4 >
[ Comparison/Relational Operators
These operators compare only the data values of the participating objects. The comparisons perfonned are based on the
sign and magnitude for numeric values, whereas a lexicography (i.e.,) alphabetical order is followed, in writing dictionaries for
suings. The comparisons result in either Boolean -True or False values.
lhe list of standard type value comparison operators in highest-to-~owest precedence order is given below,
Operator Syntax Determines
< exp_l < exp2 expl is less than exp2
> exp]> exp2 exp 1 is greater than exp2
<= exp]<= exp2 expl is less than or equal to exp2
>= expl > = exp2 exp l is greater than or equal to exp2
== expl = = exp2 exp l is equal to exp2
!= expl ! = exp2 exp l is not equal to exp2
'~~~M
N~ td\t ~ti! ~v9 Opti~M Winttow Htlt' ' ___.. ______...
J. List dacatypes
4. Tuple datatypes
5. Dictionary datatypcs.
WARHJHG: xerox/Photocopying ofthls book ls a CRIMINAL act. Anyone found guilty ls LIABLE to face LEGAL
proceedin gs.
~,r-3 3.19
~ Nil••·
... erk Oatat\,f)t'S
.
(Iv) <'ontplex
t, ,ric d,1tntypts are tlw immutuhlc dntutypcs i11 ( 'omplcx datalypc is used lo represent complex integers.
7'11Hlll . ,
stores the 1~un,c-nc vulucs. Python suppot Is vnrmus It ,·011si1its of two parts Hwt is a real part and an imaginary
11
111thtl~·. l datat)l)CS hkc, purt. This dalalypc is usually represented as a 1- hj, in
frt~ n
11
11111
') 1n1eger which 'a' is the real part (which can either be a int or
(1 • noat vnluc), bj is imaginary part (where b can take either
Floating pomt
tii) int or float vnlue andj denotes square root of- l ).
(iii) 8tk1knn Syntax
(i' ) complex.
To declare any variable as complex, the following syntax
integer is used,
(il m-•r datatype is used to store signed numeric values.
variable_name = complex(a, b)
lotee- .
doe.s not haYe a dec1ma
. I pomt.. 1
ntegers are usua II y
~presented as int. It ranges from -2147483648 to (or)
2147483648. variable_name =a+ bj
sYDtax . Example: a= 3 + ij
; ·declare any variable as integer, the following syptax Here, 'a' represents a variable, 3 + ij represents a value
0
is used, of type c:omplex.
variable_name = integer value
Moreover, in Python adding an integer value to another
Example: a = 10 integer value results in a new integer value (i.e., 2 + 2
Here, 'a' represent variable,' IO' represents the value of = 4) whereas, adding an integer value to a fl.oat value
results in a new float value (i.e., 2 + 2.0 = 4.0).
type int.
Floating Point >>> 2 + 2
(rl)
Float datatype repre~ents numeric values. It is similar to 4
integer except that it lias a decimal point. In general it >>> 2 + 2.0
has 15 decimal places. Jbey are usually represented as
4.0
float.
2. String Datatypes
Syntax
To declare any variable as floating point, the following Strings ar~ the commonly used datatypes in Python. They
syntax is used, define sequence of characters declared within single or double
quotes. In :Python single quote, double quotes and three single
variable_name = floating point value
quotes ("') means the s~rµe.
Example: a= 3.14
Syntax
Here, 'a' represents variable, '3 .14' represents the value
of type float. strings are declared as follows,
(Iii) Boolean variable_name = '$tring_value'
Boolean datatype is used when a specific condition need (or)
to be tested while program execution. It consists of two variable_name = "string_value"
pos_sible values i.e., true and false. These two values can
be used for assigning the boolean values dµ-ectly. The (or)
numeric equivalent of true is 1 and false is 0. variable- name = "'strin°t,_value"'
Syntax Examples
To declare any variable as boolean, the following syntax (i) >>> 'Hello Python'
is used,
'Hello Python'
· variable_name = 'true' or 'false'
(ii) >» 'didn\'t'
Example
"didn't"
>» a= 10
>»b=l5 (iii) >>> "Author - 'Drake'"
proceedings.
WARNING: Xerox/Pho tocopying of this book is a CRIMINAL act. Anyone found guilty is LIABLE to face LEGAL
3.21
----
=s.No,
i...--
1 and
Keyword
❖ _xxx
It requests private name that integrntcs within classes.
2 as ❖ nx
3 assert It s1atcs thal donot import with •frnrn module import if '•
4 break ❖ __JXX
5 class
It is a system defined name.
6 continue
def Q20. What are the rules and symbols used with re-
7
del gard to statements In Python?
8
9 elif Answer:
10 else Python statemenw need to follow rules and symbols such as,
11 except
l. Comments
12 exec
2. Module
13 finally
for 3. Newline
14
15 from 4. Colon
16 global 5. Semicolon
17 if
6. Indentation.
18 import
1. Comments
19 in
Python allows programmers to comment anywhere in
20 is
the program. Comments are provided using the hash(#)
21 lambda symbol. They are placed in the beginning of the line. The
22 not text followed by'#' symbol is ignored by the interpreter.
23 or Comments ·increases the readability of programs and
makes the programmer to understand the.logic and flow.
24 pass
25 print 2. Module
26 raise A module is a python script, it exists physically on
27 return the disk in the form of files. A portion of module that
try
becomes very lengthy can be shifted to another module.
28
29 while 3. Newline
30 with Newline breaks a single python statement into multiple
31 none lines. The backsla~h symbol ('\') is used to denote a
32 yield newline.
Example
Table: Keywords
if(student_age > 17) and\
Built-in
(studenLGPA> 3.5):
Built-ins are special names identifiers used by interpreter.
These are not keywords but considered as reserved for system. admission = I
Therefore, programmers should not use them: Built-ins are However, lines can be continued on next line without
members of built-in module. Interpreter automatically imports using '\' in two exceptions.
~ built-in module before the program begins ot user enter the
❖ One when enclosing operators are used, such as
>>~ prompt in the interpreter. They can also be used as global
square brackets or parenthesis.
variables that can be accessed anywhere in programs.
❖ Another when triple quotes are used for strings.
Special Underscore Identifiers
Example
'IL In Python, ,special underscore variables are also used.
•u,;se Variab (i) "'SIA edu group pioneer in ..... today"'
~ . !es are both prefixed and suffixed. Some of these
re identifiers are as follows (ii) SIA need (240, more .... )
'
SPECTRON flLL·IN-ONE JOORNflL FOR ENGINEERING STUDENn
rT
' I
3.22
4. Colon
FUNDAMENTALS OF INTERNET OF THINGS [JNTU-HYoeRA,~
~
l ❖
'
. kn
In python, a set of individual s1ntomcnts th~t forms a siuglc code block are own as sm es .
C t --
" . 't ,,
.
import abc; p = ' Today'·
'
6. Indentation
, .
In python, indentation is used to delimit blocks of code. All lines of a program mu~t be indented th e same number of sp
i.e., it needs exact indentation. Indentation is applied with spaces or tab. However, sp~ces are preferred.
Raspberry Pi is a ·device th.at comes in the size of equal to credit/debit card works like a mini computer and has
capability of performing tasks of a desktop corhp~ter. It is a lo~ cost device which can run on different yersions of Lin,
I~ connects sensors and actuators with the help of VO pins. A Raspberry Pi board shown beiow is inc.orporated with seve
compone_nts/periph_erals.
Audio
GPIO Pins RCA Video Jack
LED Status
USB Controller
DSI (Display
Connector)
( '• USB 2.0
· SD Card Slot
Ethernet
Micro-USB
HDMI CSI (Camera
Connector)
. 2. VSB Ports
There are two ~SB ports on _Raspberry Pi that can provide power upto 100mA. However, an external power hub is-need
to connect devices that reqmre more power. . .
WARNING: xerox/Photocopying of this book is a CRIMINAL act. Anyone found guilty is LIABLE to face LEGAL proceed'mgs.
3
~~,1- ~---
- ---- ---- ---- ---- -;-- :--- -:-~ ---- ---- ~·p :.:_ 19
3. '8
~ r n e t Po•·t-s :t Areh
·), rncre is RJ45 Ethe111et porl on hoard which is ttRcd to ll i.ii a type of Arch I.lnux port dcvcl<>ped for AMf)
dcvicc11. ,
cml)lcd to1lntcmel.
4. lUSC OS
processor and RAM
II fl comprcR'lcd form of operatin~ sy3tem with gr>od
fhC processor of Raspberry Pi is ARM where
speed.
ARMI 17672-F can support 700 Ml lz power. The RAM
__ A f(lr tllis processor is about 5I 2 MB.
5. Pldorn
l)Sl,,V
lt is a type of Fedora Linux particularly developed for
GPIO Pins Raspberry Pi.
there are four types of general purpose input/output
6. OpenELEC
jns on Raspberry board such as SPl interface pins, true
~PIO pins, RX: and TX pins and 120 interface pins. 1t is XBMC based media-center distribution which is
used friendly and offers good speed.
OSI (Display Serial Interface)
Raspberry Pl Interfaces
It is used as an interface between LCD panel and
There are three types of Raspberry Pi interfaces that
Raspberry Pi. support data transfer. They are J2C, Serial and SPI.
CSI (Camera Serial Interface) 1. 12C
It is used as an in~erface between camera and Raspberry 12C interface is used to provide interconnection between
hardware modules using SDA (data line) and SCL (clock
Pi.
line) pins. Using·these pins, it supports synchronous data
8. , SD Carft Slot ,
transfer.
It is used to insert an external SD card containing Linux
2. · Serial
unage.
The serial interface allows data transfer among serial
9• BDMI Output peripherals using receive (Rx) and transmit (Tx) pins.
It is used to provide Audio and video from Raspberry 3. SPI (Serial Peripheral Interface)
Pi. SPI is a type of serial data protocol that offers
10. Audio/Video Output synchronous ·data transfer among peripheral devices
using the following five pins.
It is used to obtain audio/video output from Raspberry
Pi to televisions. (a) Chip Enable (CEO)
The CEO pin is used to enable and disable the
Q22. What are the various flavors/versions of Linux
supported by Raspberry Pi? List out the devices.
interfaces of Raspberry Pi. (b) Chip Enable 1 (CEl)
.
e fre qu ently t1s ed R ds ar e tabulated below,
So me of th · as pb cny Pi com m an
Connnand Function
mv or re na m e flJe.
lt is us ed to m ~v e
cd di re ct or y.
Jt is us ed to chattgc
It . re at e ar ch iv e.
ta r is us ed to ex tra ct /c
m disk sp ac e.
e us ag e of.file sy ste
df It is us ed to re po rt th th pa tte rn .
gr ep pr in t th e lin es wh ic h ar e m at ch in g wi
It is us ed to
ca t It is us ed to displa
y th e file contents.
e th e file.
nu It is Used to re m ov
cr ea te a directory.
m kd ir lt is us ed to m ak e or
folders an d fifes.
Js It is us ed to list the e directory.
pw d is us ed to pr in t na m e of cu rre nt ly activ
It s.
ls us b It is us ed to displa
y all the USB device
.
re ne tw or k interface
ifc on fig It is us ed to con.figu ader.
iv e ne tw or k downlo les.
w ge t It is a no n- in t~ ra ct
tio ns , in te rfa ce sta tistics an d ro ut in g tab
y ne tw or k connec
.p et sta t It is us ed to pr in t th1
wn th e device.
sh ut do w n It is us ed to sh ut do
device.
re bo ot It is us ed to re bo ot
r a file.
lo ca te It is us ed to se ar ch fo
~RY I
!:. ~~=~::.::::;~o~ ;:::P:::::::::::! .
It allow s an y python program that
Pi .
can be execute
.
Model Paper~,. j
A ns w er : ementat io n. es. It utilizl
d python for its impl tuators and its interfac
pp or t bo th Li nu x an types of se ns or s an d ac further J
Ra sp be rr y Pi c~n su it us es GP IO pi ns along wi th various m se ns or s ca n be processed tasks of
su pp or t th is, taine d fro ng Rasp '
on an y m ac hi ne . To to m ak e it us ef ul for IOT. The input ob in g an em ail etc. Consider the followi
GP IO pi ns nd
the capabilities of µ-ansferring data, se
ca n be pe rfo rm ed on the input such as
di ffe re nt ac tio ns
Pi _us in g py th on .
5, Q26.
er re fe r Unit-III, Q2
Fo r re m ai ni ng an sw
spberry Pi.
s ho w LE D is co nt ro lle d with Ra
Q 25 . Di sc us
,
A ns w er : n in the figure below
te d to th e Ra sp be rry P'i board as show
Th e LE D is co nn ec
•• k.-t
•• -e .t .t •• ' " "' ·
., . lit t ·► 1
~":t t- ·· :,'""~ ·· ,·:
·• ·: , . <i ••,,, ,
1
J'" ~ ll > .. 't 11 * 1< ·•...-..
. . . . · ••11:••
• I • • • t.
I f t I♦ f "I . • • f" 4 I ..
• \- ..-, , ••• ' ,. .
<t' t ·fli\ . , •• , ; ► 'Y. ,..' '
••• f' 1'" • • ' .
<I t•" •• •• •
,." • t • • , ,I I.. .. .... .... . .
. , ~
- · ... " ~ ••• t 't ....
f'
D with Raspberry Pi
Figure: Controlling LE
proceedings.
E to face LE GA L
act. An yo ne fou nd guilty is LIABL
AL
s book is a CRIMIN
Ph 0 t0 CO
pying of thi
ING: xe ro x!
3.25
. f,3
~ e LED can be switched ontoff from Raspberry ~i . The python code to control LED based rm light sensed
fh . console in the fullowmg way, . using LDR is an follow'!,
,ndhne .
~~((f. $ t:cho 18 > /sys/clnss/gpio/export import RPi <JPJO as <,PIO
$ cd/sys/dass /gpio/gpio l 8
itnp 11 rt tim•;
$ echo out > dirccti0n
OPfO.sctmode (OPl fJ.BCM )
$ echo l ;;, value # Switching LED on
klr_ thr - I000
$ echo O> value # Switching LED off
LDR_PI N = 18
'fhecodcto make LED l~onnectcd to Raspberry Pi) blink
second using p)1bon ts as follows, LIGHT _PIN - 25
•._e,-erf
fi iroport RPi.GPlO as GPIO defi ne readLOR(P fN ):
imPort time read o: 0
GPIO.setmode (GPIO.BCM) GPIO.setup(LJGHT_pIN, GPJ.OUT)
GPIO.setup (18, GPIO.OUT) GPIO.output(PIN, false)
while Tore: time.sleep (0. l)
GPIO, output (18, True)
GPIO.setup(PIN, GPIO.IN)
time.sleep( 1)
while. (GPIO.input{PIN) ==False):
GPIO.output (18, false)
read = read + l
time.sleep ( 1)
return read
The above code makes use of RPi and GPIO for
::d
ai.
lling the GPIO. The pin 18 is set to generate output with
false values that switch after every second.
def switchOnLight (PIN):
GPIO.setup(PIN, GPIO.OU1)
1
Explain about interfacing light sensor with GPIO.output(PIN, True)
Raspberry Pi. .
def switchOflLight (PIN):
Model Paper-Ill, Q6(b)
AD5Wd:
GPIO.se!tlp(PIN, GPIO.OUT)
A Light Dependent Resistor (LDR) which is a light
sensor can be interfaced with Raspberry Pi by connecting one GPIO.Output (PIN, False)
end to 3.3v and other end to lµF capacitor and GPIO pin. A
while True:
part from this an LED is also connected to pin 18 so that if can
be turned on/off based on the light sensed. ldr- Read= readLDR (LDR PIN)
-
., " • •.•• ,... ~ ~.••.• • P·•
j . ~ , ♦- • t .~ •'C~~•~ • •··· • -i if ldr_read < ldr_thr:
:1 • '" ' 4 i- • • • ]: .<"t • t t~ ft •.-..
- - · •
1 • •
/, .. •
l ,• '
,, •
•
t"~ • '\~· •••• ,4♦ ~~ l) ••
'
'- • ~ ~- ..
• ' .....
.: ,••·., •••t•
; .. .
:1
··•'•
......
, • • ~ ,,
t, • ,: ••·
;Ii' ♦ ••
~ •• , ••
;,r, • • ~•
t~
switchOflLight (LIGHT_PIN)
, • ·•
•• c:•••• J '' •••
, . . . . . a; • . ,. • ,.
time.sleep( 1)
,,.
~ ,••-•ifl)i'' . • ,.
In the above code, readLDR( ) function is used for
il .. :::::,.-: :::: 1· .
i,•• -···· •••.•••• returning a count proportional to light level. It carries LDR pin
;J ... , , .......... •t>
;1•· ...\'... ... . which is set to output and low followed by input respectively.
,:........
':::,:'~···... l:.:
. ... ,. . .
... ;-~·
! '· •••. , ....... . The inpqt will read high until capacitor gets charged via resistor.
When the read value of input triggers high, the counter is
i
=: : : ,
• '. • •
., . f: t •••
stopped .
••• , . *~ *•"·· ••
•• ,.;,~ •·· ... . ... ,
••
•• •••••
, I! . . . . . . . .