Module 4
Module 4
• Analog Sensors
• Analog Sensors produce a continuous output signal or voltage
which is generally proportional to the quantity being measured.
• Physical quantities such as Temperature, Speed, Pressure,
Displacement, Strain etc. are all analog quantities as they tend to
be continuous in nature.
• For example, the temperature of a liquid can be measured using a
thermometer or thermocouple (e.g. in geysers) which
continuously responds to temperature changes as the liquid is
heated up or cooled down.
• Digital Sensors
• Digital Sensors produce discrete digital output signals or voltages
that are a digital representation of the quantity being measured.
• Digital sensors produce a binary output signal in the form of a logic
“1” or a logic “0”, (“ON” or “OFF”).
• Digital signal only produces discrete (non‐continuous) values,
which may be output as a single “bit” (serial transmission), or by
combining the bits to produce a single “byte” output (parallel
transmission).
• Scalar Sensors
• Scalar Sensors produce output signal or voltage which is generally
proportional to the magnitude of the quantity being measured.
• Physical quantities such as temperature, color, pressure, strain,
etc. are all scalar quantities as only their magnitude is sufficient to
convey an information.
• For example, the temperature of a room can be measured using a
thermometer or thermocouple, which responds to temperature
changes irrespective of the orientation of the sensor or its
direction.
• Vector Sensors
• Vector Sensors produce output signal or voltage which is generally
proportional to the magnitude, direction, as well as the
orientation of the quantity being measured.
• Physical quantities such as sound, image, velocity, acceleration,
orientation, etc. are all vector quantities, as only their magnitude
is not sufficient to convey the complete information.
• For example, the acceleration of a body can be measured using an
accelerometer, which gives the components of acceleration of the
body with respect to the x,y,z coordinate axes.
Actuator
• An actuator is a component of a machine or system that moves or
controls the mechanism or the system.
• An actuator is the mechanism by which a control system acts upon an
environment
• An actuator requires a control signal and a source of energy.
• Upon receiving a control signal is received, the actuator responds by
converting the energy into mechanical motion.
• The control system can be simple (a fixed mechanical or electronic
system), software‐based (e.g. a printer driver, robot control system), a
human, or any other input.
Arduino
• 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.
• key features
• 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
• sending a set of instructions to the microcontroller on the board via Arduino
IDE
Board
Software
• Download the software - https://www.arduino.cc
Program structure
• Sketch − The first new terminology in the Arduino program called “sketch”
• Arduino programs can be divided in three main parts: Structure, Values (variables and constants),
and Functions.
• Software structure consist of two main functions −
• Setup( ) function
• Loop( ) function
• 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.
• 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.
• Data types
• void
• The void keyword is used only in function declarations. It indicates that the
function is expected to return no information to the function from which it
was called.
Blinking LED
• Task: Get a LED to blink.
• Required equipment: Just the microcontroller board with the USB
cable.
• First part of the program: Name variables
• Second part of the program: Setup
• We only have one output – Pin 13 should put out voltage (The LED should
light up.)
void setup() //The setup begins here
{ //opening curly bracket – A program part begins here
} //closing
• Now we are going to write the setup information between the curly
brackets.
• In this case: “pin 13 is supposed to be an output” :
void setup()
{
pinMode(13, OUTPUT); //Pin 13 is supposed to be an output.
}
• Third part of the program: Loop (main part):
void loop()
{
digitalWrite(13, HIGH); //Turn on the voltage on pin 13 (LED on)
delay(1000); //Wait for 1000 milliseconds (one second)
digitalWrite(13, LOW); //Turn off the voltage on pin 13 (LED off)
delay(1000); //Wait for 1000 milliseconds (one second)
}
• Suppose if you want the LED to blink really fast then set delay to 200
Introduction to Arduino Programming
• Operators in Arduino
• Control Statement
• Loops
• Arrays
• String
• Math Library
• Random Number
• Interrupts
• Example Program
Arduino IDE
• Program coded in Arduino IDE is called a SKETCH
• To create a new sketchFile -> New
• To open an existing sketch
• File -> open ->
• There are some basic ready-to-use sketches available in the
EXAMPLES section
• File -> Examples -> select any program
• 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
• Serial Monitor: Opens the serial console
• All the data printed to the console are displayed here
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
Supported Datatype
Arduino supports the following data types
• Void Long • Unsigned long
• Int • Float
• Char • Double
• Boolean • Array
• String-char array
• Unsigned
• String-object Short
• Char
• Byte
• Unsigned int
• Word
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);
}
//name variable
int LED=9; // rename the pin this is optional
int brightness=0;
int fading=5;
void setup()
{
pinMode(LED, OUTPUT);
}
void loop()
{
analogWrite(LED, brightness); // you are starting it from 0
brightness = brightness + fading; // modify the value of brightness
delay(25); // LED should stay bright for a short duration
int buttonstatus = 0;
void setup()
{
pinMode(LEDblue, OUTPUT);
pinMode(button, INPUT);
}
void loop()
{
buttonstatus = digitalRead(button);
if (buttonstatus == HIGH)
{
digitalWrite(LEDblue, HIGH);
delay(5000);
digitalWrite(LEDblue, LOW);
}
else
{
digitalWrite(LEDblue, LOW);
}
}
RGB Light
• Task: We want a RGB LED to light up in different colours
Using sensor library
• Humidity sensor (DHT22)
• The DHT-22 (also named as AM2302) is a digital-output, relative humidity,
and temperature sensor.
• It uses a capacitive humidity sensor and a thermistor to measure the
surrounding air, and sends a digital signal on the data pin.
// Initialize DHT sensor
DHT22 dht(A0);
void setup()
{
Serial.begin(9600);
Serial.println("DHT22 Humidity Value");
dht.begin();
}
void loop()
{
dht.readHumidity();
Serial.print(dht.humidity);
delay(3000);
}
Actuators
• Servo motor
• Servo is a general term for a closed loop control system.
• A closed loop system uses the feedback signal to adjust the speed and
direction of the motor to achieve the desired result.
• The length of the pulse determines the position of the servo motor.
void setup()
{
Serial.begin(9600);
}
void loop()
{
temp = analogRead(tempPin);
temp = temp * 0.48
Serial.print(“Temperature = ”)
Serial.print(temp);
}
• Water Sensor
• Connecting a water sensor to an Arduino is a great way to detect a leak, spill,
flood, rain, etc.
• It can be used to detect the presence, the level, the volume and/or the
absence of water.
• While this could be used to remind you to water your plants, there is a better
Grove sensor for that.
• The sensor has an array of exposed traces, which read LOW when water is
detected.
int Grove_Water_Sensor = 8
int LED = 9
void setup()
{
pinMode(Grove_Water_Sensor, INPUT); // The Water Sensor is an Input
pinMode(LED, OUTPUT); // The LED is an Output
}
void loop()
{
if( digitalRead(Grove_Water_Sensor) == LOW)
{
digitalWrite(LED,HIGH);
}
else
{
digitalWrite(LED,LOW);
}
• Measure a distance with the HC-SR04 ultrasonic sensor and show the
result on the serial monitor. If the distance is less than 80cm, the
piezo speaker should beep.
• Check the notepad for program
• Passive Infra Red sensors can detect movement of objects that radiate
IR light.
• The output of PIR motion detection sensor can be connected directly
to one of the Arduino (or any microcontroller) digital pins. If any
motion is detected by the sensor, this pin value will be set to “1”.
• That means when a human or animal body will get in the range of the
sensor it will detect a movement because the human or animal body
emits heat energy in a form of infrared radiation
Communication
• IoT devices require a mechanism to send or receive data
• Connecting devices to the Internet - wired and wireless options,
Bluetooth, cellular networks, Wifi
• Factors for choosing the connectivity
• Scale and size of the network where the application will run
• Amount of data that needs to be processed and transferred
• Physical location of the device
Wired connectivity
• Attach an Ethernet shield to your Arduino Uno