iot_file
iot_file
BCA 372
Theory:
Arduino is an open-source prototyping platform in electronics based on easy-to-use hardware
and software. Arduino is a microcontroller based prototyping board which can be used in
developing digital devices that can read inputs like finger on a button, touch on a screen, light
on a sensor etc. and turning it in to output like switching on an LED, rotating a motor, playing
songs through a speaker etc.
The Arduino board can be programmed to do anything by simply programming the
microcontroller on board using a set of instructions for which, the Arduino board consists of a
USB plug to communicate with your computer and a bunch of connection sockets that can be
wired to external devices like motors, LEDs etc.
Reset Button
The Arduino has a reset button (10). Pushing it will temporarily connect the reset pin to
ground and restart any code that is loaded on the Arduino.
Voltage Regulator
Voltage regulator (14) controls amount of voltage that is let into the Arduino board. It will turn
away an extra voltage that might harm the circuit.
The Arduino Programming Language is basically a framework built on top of C++. It’s not a
real programming language.
A program written in the Arduino Programming Language is called sketch. A sketch is
normally saved with the .ino extension (from Arduino).
The main difference from “normal” C or C++ is that you wrap all your code into 2 main
functions.
One is called setup(), the other is called loop(). The first is called once, when program starts,
the second is repeatedly called while your program is running.
We don’t have a main() function like you are used to in C/C++ as the entry point for a
program. Once you compile your sketch, the IDE will make sure the end result is a correct
C++ program and will basically add the missing glue by pre-processing it.
There are many types of Arduino boards available in the market which can be programmed
using the Arduino IDE. The reasons for different types of boards are different power supply
requirements, connectivity options, their applications etc.
Arduino boards are available in different sizes, form factors, different no. of I/O pins etc.
Some of the commonly known and frequently used Arduino boards are Arduino UNO,
Arduino Mega, Arduino Nano, Arduino Micro and Arduino Lilypad.
There are add-on modules called Arduino Shields which can be used to extend the
functionalities of the Arduino boards. Some of the commonly used shields are Arduino Proto
shield, Arduino WiFi Shield and Arduino Yun Shield.
What is a Sensors?
A Sensor is a converter that measures a physical quantity and converts it into a signal which
can be read by an observer or by an (today mostly electronic) instrument.
SENSORS
We frequently use different types of sensors in several electrical and electronic applications,
which are classified as chemical, pressure, temperature, position, force, proximity, thermal,
presence, flow, optical, automotive, sound, speed, magnetic, electric, heat, fiber-optic
sensors, analog and digital sensors. A sensor can be defined as an appliance that detects
changes in
physical or electrical or other quantities and by this means, generally, produces an electrical
or optical signal output as an acknowledgement of the change in that specific quantity.
What is an Actuator?
An Actuator is a type of motor for moving or controlling a mechanism or system. It is
operated by a source of energy, typically electric current, hydraulic fluid pressure, or
pneumaticpressure, and converts that energy into motion. An actuator is the mechanism by
which a control system acts upon an environment.
Actuators are devices which drive a machine (robot) including its grippers. Muscles of a
human arm and hand. While human arm provides motion, hand is used for object
manipulation. So, actuators in robots (machine) provides motion while grippers manipulates
objects. An actuator system comprises of several subsystems, namely,
(i) Power supply; (ii) Power amplifier; (iii) Servomotor; (iv) Transmission system.
1. Temperature Sensor
2. Proximity Sensor
3. Accelerometer
4. IR Sensor (Infrared Sensor)
5. Pressure Sensor
6. Light Sensor
7. Ultrasonic Sensor
8. Smoke, Gas and Alcohol Sensor
9. Touch Sensor
10. Color Sensor
ANALOG SENSORS
1. Accelerometers
Analog sensors that detect changes in position, velocity, orientation, shock, vibration, and
tilt by sensing motion are called as accelerometers. These analog accelerometers are again
classified into different types based on the variety of configurations and sensitivities.
These accelerometers are available as analog and digital sensors, based on the output signal.
Analog accelerometer produces a constant variable voltage based on the amount of
acceleration applied to the accelerometer
2. Light Sensors
Analog sensors that are used for detecting the amount of light striking the sensors are
called as light sensors. These analog light sensors are again classified into various types
such as photo-resistor, Cadmium Sulfide (CdS), and, photocell.
Light dependent resistor (LDR) can be used as analog light sensor which can be used to
switch on and off loads automatically based on the day light incident on the LDR. The
resistance of the LDR increases with decrease in light and decreases with increase in
light.
3. Sound Sensors
Analog sensors that are used to sense sound level are called as sound sensors. These analog
sound sensors translate the amplitude of the acoustic volume of the sound into an electrical
voltage for sensing sound level. This process requires somecircuitry, and utilizes
microcontroller along with a microphone for creating an analog output signal.
4. Pressure Sensor
The analog sensors that are used to measure the amount of pressure applied to a sensor are
called as analog pressure sensors. Pressure sensor will produce an analog output signal
that is proportional to the amount of applied pressure. These piezoelectric sensors are one
type of pressure sensors that can produce an analog output voltage signal proportional to
the pressure applied to the piezoelectric sensor.
5. Analog Temperature sensor
Temperature sensors are widely available as both digital and analog sensors. Typically
used analog temperature sensors are thermistors. There are different types of thermistors
thatare used for different applications. Thermistor is a thermally sensitive resistor that is
used for detecting changes in temperature. If the temperature increases, then the electrical
resistance of thermistor increases. Similarly, if temperature decreases, then the resistance
decreases. It is used in various temperature sensor applications.
DHT11 and DHT22 digital temperature sensor available for measurement of temperature
and humidity DHT11 sensor includes a resistive-type humidity measurement component,
an NTC temperature measurement component and a high- performance 8-bit
microcontroller inside, and provides calibrated digital signal output. It has high reliability
and excellent longterm stability
2. Ultrasonic Sensor
Ultrasonic sensor distance measuring module is stable, measure the distance accurately. The
sensor with High precision, blind spots (3cm) super close. Its provides a full set of ranging
process
5. Gas Sensor
It is a hazardous gas detection apparatus for the family, the environment, suitable for
ammonia, aromatic compounds, Sulphur, benzene vapors, smoke and other gases harmful
gas detection, gas-sensitive element test. Air quality sensor is for detecting a wide range of
gases, including NH3, NOx, alcohol, benzene, smoke and CO2. Ideal for use in office
or factory with simple drive and monitoring circuit.
Line Tracker sensor consists of 3 IR transmitter and IR receiver pairs. This tracker
sensor is typically used for robots in line following task. It can be used for either dark or
bright line following. The tracker sensor have 3 digital outputs to user indicating the
existence of the line. Every sensor is provided with its own LEDs as indication of line
detection
ACTUATORS
1. Relay
Relay provides an electrical connection between two or more points in response to the
application of a control signal. Relays are basically electrically operated switches that come
in many shapes, sizes and power ratings suitable for all types of applications. Relays can
also have single or multiple contacts within a single package
2. DC Motor
A DC motor (Direct Current motor) is the most common type of motor. DC motors
normally have just two leads, one positive and one negative. If you connect these two
leads directly to a battery, the motor will rotate. If you switch the leads, the motor will
rotate in the opposite direction.
3. Servo Motor
A Servo Motor is a small device that has an output shaft. This shaft can be positioned to
specific angular positions by sending the servo a coded signal. If the coded signal exists on
the input line, the servo will maintain the angular position of the shaft. If the coded signal
changes, the angular position of the shaft changes. In practice, servos are used in radio-
controlled airplanes to position control surfaces like the elevators and rudders. They are
also used in radio-controlled cars, puppets, and of course, robots.
4. Stepper Motor
A Stepper Motor or a step motor is a brushless, synchronous motor, which divides a full
rotation into several 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
6. LCD Display
An LCD is an electronic display module which uses liquid crystal to produce a visible image.
The 16×2 LCD display is a very basic module commonly used in DIYs and circuits. The
16×2 translates display 16 characters Per line in 2 such lines. In this LCD each character is
displayed in a 5×7 pixel matrix
PRACTICAL 2(A): Arduino IDE and Operators in IDE.
Writing Sketches
Programs written using Arduino Software (IDE) are called sketches. These sketches are written
in text editor and are saved with file extension. ino. Editor has features for cutting/pasting and for
searching/replacing text. Message area gives feedback while saving and exporting and also
displays errors. Console displays text output by Arduino Software (IDE), including complete
error messages and other information. Bottom right-hand corner of the window displays the
configured board and serial port. Toolbar buttons allow you to verify and upload programs,
create, open, and save sketches, and open serial monitor.
NB: Versions of the Arduino Software (IDE) prior to 1.0 saved sketches with the extension
.pde. It is possible to open these files with version 1.0, you will be prompted to save the
Verify Checks your code for errors compiling it.
Upload Compiles your code and uploads it to the configured board. If you are using an
external programmer with your board, you can hold down the "shift" key on your computer when
using this icon. The text will change to "Upload using Programmer"
Open Presents a menu of all the sketches in your sketchbook. Clicking one will open it
within the current window overwriting its content.
Note: due to a bug in Java, this menu doesn't scroll; if you need to open a sketch late in the
list, use the File | Sketchbook menu instead.
Additional commands are found within the five menus: File, Edit, Sketch, Tools, Help. The
menus are context sensitive, which means only those items relevant to the work currently being
carried out are available.
File
• New Creates a new instance of the editor, with the bare minimum structure of a
sketch already in place.
• Open Allows to load a sketch file browsing through the computer drives and folders.
• Open Recent Provides a short list of the most recent sketches, ready to be opened.
• Sketchbook Shows the current sketches within the sketchbook folder structure; clicking
on any name opens the corresponding sketch in a new editor instance. Examples Any
example provided by the Arduino Software (IDE) or library shows up in this menu item.
All the examples are structured in a tree that allows easy access by topic or library.
• Close Closes the instance of the Arduino Software from which it is clicked.
• Save Saves the sketch with the current name. If the file hasn't been named before, a
name will be provided in a "Save as.." window.
• Save as... Allows to save the current sketch with a different name.
• Page Setup It shows the Page Setup window for printing.
• Print Sends the current sketch to the printer according to the settings defined in Page Setup.
• Preferences Opens the Preferences window where some settings of the IDE may be
customized, as the language of the IDE interface.
• Quit Closes all IDE windows. The same sketches open when Quit was chosen will be
automatically reopened the next time you start the IDE.
Edit
• Undo/Redo Goes back of one or more steps you did while editing; when you go back,
you may go forward with Redo.
• Cut Removes the selected text from the editor and places it into the clipboard.
• Copy Duplicates the selected text in the editor and places it into the clipboard.
• Copy for Forum Copies the code of your sketch to the clipboard in a form suitable for
posting to the forum, complete with syntax coloring.
• Copy as HTML Copies the code of your sketch to the clipboard as HTML, suitable for
embedding in web pages.
• Paste Puts the contents of the clipboard at the cursor position, in the editor.
• Select All Selects and highlights the whole content of the editor.
• Comment/Uncomment Puts or removes the // comment marker at the beginning of
each selected line.
• Increase/Decrease Indent Adds or subtracts a space at the beginning of each selected
line, moving the text one space on the right or eliminating a space at the beginning.
• Find Opens the Find and Replace window where you can specify text to search inside the
current sketch according to several options.
• Find Next Highlights the next occurrence - if any - of the string specified as the
search item in the Find window, relative to the cursor position.
• Find Previous Highlights the previous occurrence - if any - of the string specified as
the search item in the Find window relative to the cursor position.
Sketch
• Verify/Compile Checks your sketch for errors compiling it; it will report memory usage
for code and variables in the console area.
• Upload Compiles and loads the binary file onto the configured board through
the configured Port.
• Upload Using Programmer This will overwrite the bootloader on the board; you will
need to use Tools > Burn Bootloader to restore it and be able to Upload to USB serial
port again. However, it allows you to use the full capacity of the Flash memory for your
sketch. Please note that this command will NOT burn the fuses. To do so Tools -> Burn
Bootloader command must be executed.
• Export Compiled Binary Saves a .hex file that may be kept as archive or sent to the board
using other tools.
Show Sketch Folder Opens the current sketch folder.
• Include Library Adds a library to your sketch by inserting #include statements at the start
of your code. For more details, see libraries below. Additionally, from this menu item you
can access the Library Manager and import new libraries from .zip files.
• Add File Adds a supplemental file to the sketch (it will be copied from its current
location). The file is saved to the data subfolder of the sketch, which is intended for assets
such as documentation. The contents of the data folder are not compiled, so they do not
become part of the sketch program.
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.
• 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.
Help
Here you find easy access to a number of documents that come with the Arduino Software
(IDE). You have access to Getting Started, Reference, this guide to the IDE and other
documents locally, without an internet connection. The documents are a local copy of the online
ones and may link back to our online website.
Find in Reference This is the only interactive function of the Help menu: it directly selects the
relevant page in the local copy of the Reference for the function or command under the cursor.
Sketchbook
The Arduino Software (IDE) uses the concept of a sketchbook: a standard place to store your
programs (or sketches). The sketches in your sketchbook can be opened from the File >
Sketchbook menu or from the Open button on the toolbar. The first time you run the Arduino
software, it will automatically create a directory for your sketchbook.
You can view or change the location of the sketchbook location from with the Preferences
dialog. Beginning with version 1.0, files are saved with a .ino file extension. Previous versions
use the .pde extension. You may still open .pde named files in version 1.0 and later, the
software will automatically rename the extension to .ino.
Allows you to manage sketches with more than one file (each of which appears in its own tab).
These can be normal Arduino code files (no visible extension), C files (.c extension), C++ files
(.cpp), or header files (.h).
Libraries
Libraries provide extra functionality for use in sketches, e.g. working with hardware or
manipulating data. To use a library in a sketch, select it from the Sketch > Import Library
menu. This will insert one or more #include statements at the top of the sketch and compile
the library with your sketch.
Serial Monitor
This displays serial sent from the Arduino board over USB or serial connector. To send
data to the board, enter text and click on the "send" button or press enter. Choose the baud
rate from the drop-down menu that matches the rate passed to Serial.begin in your sketch.
1. Arithmetic Operators
2. Compound Operators
3. Boolean Operators
4. Comparison Operators
5. Bitwise Operators
Arithmetic Operators
There are six basic operators responsible for performing mathematical operations in Arduino,
which are listed below:
Assignment Operator ( = ): Used to set variable's value. It is quite different from equal
symbol (=) normally used in mathematics.
Addition ( + ): Used for addition of two numbers. For example, P + Q.
Subtraction ( - ): Used to subtract one value from the another. For example, P - Q.
Multiplication ( * ): Used to multiply two numbers. For example, P * Q.
Division ( / ): Used to determine result of one number divided with another. For
example, P/Q. Modulo ( % ): Used to calculate remainder after division of one number
by another number.
Code 1: Add two numbers and print their result on serial monitor
int b; void setup ( )
{
Serial.begin( 9600 );
}
void loop ( )
{ b = 5 + 2;
Serial.println(b
);
}
Result: 7
Result: 3
If we want decimal values to be printed, we need to use the float instead of int.
Code 3: Use float datatype and divide operator to write
a code.
float b; void setup ( )
{
Serial.begin( 9600 );
}
void loop ( )
{ b = 20.0 / 3; // decimal value is used to force the
compiler to print decimal value.
Serial.println(b);
Output: 6
Compound Operators
Compound operators perform two or more calculations at once. Result of right operand is
assigned to left operand. Same condition will apply to all the compound operators, which are
listed below:
Now, let's use the above operators with two variables, b and c.
o b + = c ( b = b+c)
o b - = c ( b = b - c)
o b * = c ( b = b *c)
o b / = c ( b = b / c)
o b % = c (b=b%c)
Boolean Operators
The Boolean Operators are NOT (!), Logical AND (& &), and Logical OR (| |).
Logical AND (& &): The result of the condition is true if both the operands in the condition are true.
if (a = = b & & b = = c)
Above statement is true if both conditions are true. If any of conditions is false, statement will be false.
For example, a! = 2.
The NOT operator returns value 1 or TRUE when specified operand is FALSE. It also reverses
the value of the specified expression.
Comparison Operators
The comparison operators are used to compare the value of one variable with the other. The
comparison operators are listed below:
• less than ( < ): The less than operator checks that the value of the left operand is less than
the right operand. The statement is true if the condition is satisfied.
Output: 3
In above code, if any of two statements is correct, corresponding value of variable will be
printed. Here, only first condition is correct. Hence, value of b will be printed.
• greater than (>): Less than operator checks that value of left side of a statement is greater
than right side. Statement is true if the condition is satisfied. For example, a > b. If a is
greater than b, the condition is true, else false.
• equal to (= =): It checks the value of two operands. If the values are equal, the condition is
satisfied. For example, a = = b. The above statement is used to check if the value of a is equal
to b or not.
• not equal to (! = ): It checks the value of two specified variables. If the values are not equal,
the condition will be correct and satisfied. For example, a! = b.
• less than or equal to (< =): The less or equal than operator checks that the value of left side
of a statement is less or equal to the value on right side. The statement is true if either of the
condition is satisfied. For example, a < = b. It checks the value of a is less or equal than b.
• greater than or equal to (> =): The greater or equal than operator checks that the value of
the left side of a statement is greater or equal to the value on the right side of that statement.
The statement is true if the condition is satisfied. For example, a > = b. It checks the value of
a is greater or equal than b. If either of the conditions satisfies, the statement is true.
Bitwise Operators
The Bitwise operators operate at the binary level. These operators are quite easy to use.
There are various bitwise operators. Some of the popular operators are listed below:
1. bitwise NOT (~): The bitwise NOT operator acts as a complement for reversing the
bits. For example, if b = 1, the NOT operator will make the value of b = 0.
• 0 0 1 1 // Input or operand 1 (decimal value 3)
• 1 1 0 0 // Output (reverses the input bits) decimal value is 12
2. bitwise XOR (^): The output is 0 if both the inputs are same, and it is 1 if the two input
bits are different. For example,
1. 1 0 0 1 // input 1 or operand 1
2. 0 1 0 1 // input 2
3. 1 1 0 0 // Output (resultant - XOR)
3. bitwise OR (|): Output is 0 if both of the inputs in the OR operation are 0. Otherwise,
the output is 1. The two input patterns are of 4 bits. For example,
1. 1 1 0 0 // input 1 or operand 1
2. 0 0 0 1 // input 2
3. 1 1 0 1 // Output ( resultant - OR)
4. bitwise AND (&): Output is 1 if both the inputs in the AND operation are 1.
Otherwise, the output is 0. The two input patterns are of 4 bits. For example,
1. 1 1 0 0 // input 1 or operand 1
2. 0 1 0 1 // input 2
3. 0 1 0 0 // Output ( resultant - AND)
5. bitwise left shift ( < < ): The left operator is shifted by the number of bits defined by
the right operator.
6. bitwise right shift ( > > ): The right operator is shifted by the number of bits defined by
the left operator.
PRACTICAL 2(B): Frequently used functions in Arduino IDE.
Functions allow structuring the programs in segments of code to perform individual tasks. The
typical case for creating a function is when one needs to perform the same action multiple times
in a program.
Standardizing code fragments into functions has several advantages −
• Functions help the programmer stay organized. Often this helps to conceptualize the program.
• Functions codify one action in one place so that the function only has to be thought about
and debugged once.
• This also reduces chances for errors in modification, if the code needs to be changed.
• Functions make the whole sketch smaller and more compact because sections of code are
reused many times.
• They make it easier to reuse code in other programs by making it modular, and using
functions often makes the code more readable.
There are two required functions in an Arduino sketch or a program i.e. setup () and loop().
Other functions must be created outside the brackets of these two functions.
The most common syntax to define a function is −
Function Declaration
A function is declared outside any other functions, above or below the loop function. We
can declare the function in two different ways −
The first way is just writing the part of the function called a function prototype above the loop
function, which consists of –
Serial.println("Hello, Arduino!");
Example:
void setup() {
Serial.begin(960
0);
}
void loop() {
Serial.print("Hello,
World!"); delay(1000);
}
We will learn in easy steps, how to set up the Arduino IDE on our computer and prepare the
board to receive the program via USB cable.
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.
The IoT-Based Ultrasonic Radar System is a basic real-time object detection system that simulates
radar functionality using simple electronic components and Arduino programming. The system
detects the presence and distance of nearby objects by rotating an ultrasonic sensor mounted on a
servo motor and scanning the surroundings.
This setup includes an ultrasonic sensor (HC-SR04) to measure distance by emitting and
receiving ultrasonic waves, and a servo motor to rotate the sensor for a 180° sweep. The core of
the system is the Arduino UNO, which processes the sensor data and controls the motor's
movement. When the sensor detects an object within a certain range, the system can display the
distance on a serial monitor or transmit it further through IoT integration.
For enhanced functionality, this setup can be expanded with an ESP8266 Wi-Fi module to send
real-time data to a mobile application or cloud server, making it suitable for remote monitoring and
data visualization in defense, automation, or smart surveillance systems.
Key Features:
#include <Servo.h>
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
WiFiClient client;
Servo radar;
const int trig = 9, echo = 10, servoPin = 11;
void setup() {
pinMode(trig, OUTPUT); pinMode(echo, INPUT);
radar.attach(servoPin);
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
void loop() {
for (int angle = 0; angle <= 180; angle += 10) {
radar.write(angle); delay(200);
ThingSpeak.setField(1, angle);
ThingSpeak.setField(2, dist);
ThingSpeak.writeFields(channelID, writeAPIKey);