CSE 3009 Internet of Things FALL 20-21: Dr. M.Sudha Associate Professor School of Information Technology & Engineering
CSE 3009 Internet of Things FALL 20-21: Dr. M.Sudha Associate Professor School of Information Technology & Engineering
FALL 20-21
Module 3
Dr. M.Sudha
Associate Professor
School of Information technology & Engineering
Content Source : Arshdeep Bahga and Vijay Madisetti, Internet of Things A Hand-on Approach, Universities press, 2015
Text Books :
1. Dieter Uckelmann et.al, Architecting the Internet of Things, Springer, 2011
2. Arshdeep Bahga and Vijay Madisetti, Internet of Things A Hand-on Approach, Universities press, 2015
Arduino programming -Terminology
• Sketch
– A program you write to run on an Arduino board
• Pin
– An input or output connected to something
• Eg: output to an LED, input from a knob
• Digital
– Value is either HIGH or LOW
• Aka on/off, one/zero
• Eg: switch state
• Analog
– Values ranges, usually from 0-255
• Eg: LED brightness, motor speed, etc.,
Digital I/0
• pinMode(pin, mode)
– Sets pin to either INPUT or OUTPUT
• digitalRead(pin)
– Reads HIGH or LOW from a pin
• digitalWrite(pin, value)
– Writes HIGH or LOW to a pin
• Electronic stuff
– Output pins can provide 40 mA of current
– Writing HIGH to an input pin installs a 20KΩ pullup
Arduino Timing
• delay(ms)
– Pauses for a few milliseconds
• delayMicroseconds(us)
– Pauses for a few microseconds
• More commands:
arduino.cc/en/Reference/HomePage
Digital? Analog?
• Digital has two values: on and off
• Analog has many (infinite) values
• Computers don’t really do analog, they quantize
• Remember the 6 analog input pins---here’s how
they work
Variables
Putting It Together
• Complete the sketch
(program) below.
• What output will be
generated by this program?
• What if the schematic were
changed?
Structure
– Variables
• setup()
• loop()
– User Defined functions
setup()
• The setup() function is called when a sketch starts.
• Use it to
– initialize variables,
– pin modes, \
– start using libraries, etc.
• The setup function will only run once, after each
powerup or reset of the Arduino board.
loop()
• After creating a setup() function, which initializes
and sets the initial values,
• the loop() function loops consecutively
• Use it to actively control the Arduino board.
Control Structures
• if
• if...else
• for
• switch case
• while
• do... while
• break
• continue
• return
• goto
Further Syntax
• ; (semicolon)
• {} (curly braces)
• // (single line comment)
• /* */ (multi-line comment)
• #define
• #include
Operators
• Arithmetic Operators
= (assignment operator), + (addition), - (subtraction), *
(multiplication), / (division), % (modulo)
• Comparison Operators
== (equal to), != (not equal to), < (less than), > (greater
than), <= (less than or equal to), >= (greater than or equal
to)
• Boolean Operators
– && (and), || (or), ! (not)
• Pointer Access Operators
• Bitwise Operators
• Compound Operators
Operators
• Pointer Access Operators
* dereference operator, & reference operator
• Bitwise Operators
& (bitwise and), | (bitwise or), ^ (bitwise xor), ~ (bitwise
not), << (bitshift left), >> (bitshift right)
• Compound Operators
++ (increment), -- (decrement), += (compound addition)
-= (compound subtraction), *= (compound multiplication)
/= (compound division), %= (compound modulo), &=
(compound bitwise and), |= (compound bitwise or)
Variables
• Constants
• Data Types
• Conversion
• Variable Scope & Qualifiers
• Utilities
Variables: Constants
• HIGH | LOW
• INPUT | OUTPUT | INPUT_PULLUP
• LED_BUILTIN
• true | false
• integer constants
• floating point constants
Variables: Constants: High/Low
• In reference to a pin , whether a pin is set to
an INPUT or OUTPUT.
– When a pin is configured as an
• INPUT with pinMode(), and read with digitalRead(),
the Arduino will report HIGH/Low if:
– 5V boards
» greater than 3.0V is present at the pin; (High)
» less than 1.5V is present at the pin (Low)
– 3.3 V boards
» greater than 2.0V is present at the pin; High
» less than 1.0V (Approx) Low
Variables: Constants: High/Low
• In reference to a pin , whether a pin is set to
an INPUT or OUTPUT.
– When a pin is configured as an
• OUTPUT with pinMode(), and set to HIGH with
digitalWrite(), the pin is at:
– 5 volts (5V boards);, 3.3 volts (3.3V boards)
• OUTPUT with pinMode(), and set to Low with
digitalWrite(), the pin is at:
– the pin is at 0 volts (both 5V and 3.3V boards).
– In this state it can sink current,
– e.g. light an LED that is connected through a series resistor
to +5 volts (or +3.3 volts).
Functions
• Segmenting code into functions allows a
programmer to create modular pieces of code that
perform a defined task
• Standardizing code fragments into functions has
several advantages
– Help the programmer stay organized.
– only has to be thought out and debugged once.
– reduces chances for errors in modification, if the code
needs to be changed.
– sections of code are reused many times. And by making
it more modular, and as a nice side effect, using
functions also often makes the code more readable.
Anatomy of a C function
Sample Program
Builtin Functions
• Digital I/O
• Analog I/O
• Due & Zero only
• Advanced I/O
• Time
• Math
• Trigonometry
• Characters
• Random Numbers
• Bits and Bytes
• External Interrupts
• Interrupts
• Communication
• USB (32u4 based boards and Due/Zero only)
What is Arduino?
• Arduino is an open-source platform used for building
electronics projects.
• Arduino consists of both a physical programmable circuit
board (often referred to as a microcontroller) and a piece
of software, or IDE (Integrated Development Environment)
that runs on your computer, used to write and upload
computer code to the physical board.
• The Arduino platform has become quite popular with
people just starting out with electronics
• Arduino does not need a separate piece of hardware (called a
programmer) in order to load new code onto the board
• It can be used simply using 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 out
the functions of the micro-controller into a more accessible
package.
This is an Arduino Uno
• The Uno is one of the more popular boards in the Arduino
family and a great choice for beginners.
Arduino Uno, what it does?
• The Arduino hardware and software was designed for artists, designers, hobbyists,
hackers, and anyone interested in creating interactive objects or environments.
• Arduino can interact with buttons, LEDs, motors, speakers, GPS units, cameras, the
internet, and even your smart-phone or your TV!
• This flexibility combined with the fact that the Arduino software is free, the hardware
boards are pretty cheap, and both the software and hardware are easy to learn has
led to a large community of users who have contributed code and released
instructions for a huge variety of Arduino-based projects.
• For everything from robots and a heating pad hand warming blanket to
honest fortune-telling machines, and even a
Dungeons and Dragons dice-throwing gauntlet, the Arduino can be used as the brains
behind almost any electronics project.
Arduino characteristics
1. Inexpensive - Arduino boards are relatively inexpensive compared to other
microcontroller platforms. The least expensive version of the Arduino module can
be assembled by hand, and even the pre-assembled Arduino modules cost less than
$50
2. Cross-platform - The Arduino Software (IDE) runs on Windows, Macintosh OSX, and
Linux operating systems. Most microcontroller systems are limited to Windows.
3. Simple, clear programming environment - The Arduino Software (IDE) is easy-to-
use for beginners, yet flexible enough for advanced users to take advantage of as
well. For teachers, it's conveniently based on the Processing programming
environment, so students learning to program in that environment will be familiar
with how the Arduino IDE works.
• Open source and extensible software - The Arduino software is published as
open source tools, available for extension by experienced programmers. The
language can be expanded through C++ libraries, and people wanting to
understand the technical details can make the leap from Arduino to the AVR C
programming language on which it's based. Similarly, you can add AVR-C code
directly into your Arduino programs if you want to.
• Open source and extensible hardware - The plans of the Arduino boards are
published under a Creative Commons license, so experienced circuit designers
can make their own version of the module, extending it and improving it. Even
relatively inexperienced users can build the breadboard version of the module in
order to understand how it works and save money.
Arduino Sensors , shields
• With some simple code, the Arduino can control and interact with a wide
variety of Sensors - things that can measure light, temperature,
degree of flex, pressure, proximity, acceleration, radioactivity, humidity,
barometric pressure, you name it, you can sense it!
• Shields
• Additionally, there are these things called shields -- basically they are pre-
built circuit boards that fit on top of your Arduino and provide additional
capabilities -- controlling motors, connecting to the internet,
providing cellular or other wireless communication,
controlling an LCD screen, and much more.
Why Arduino is used in IoT?