Unit 4 Shared
Unit 4 Shared
Programming-Unit 4
Microcontroller vs. Arduino
• The difference between an Arduino development board and a microcontroller chip is
that the development board contains almost every essential part needed to start a
project on the board itself.
• It has a microcontroller chip and ports for connection or extension.
• On the other hand, for a single microcontroller chip (such as Atmega328
microcontroller), you need a lot of things. First, you need to buy a separate
programmer board to program the chip. For building a project or experimentation
with the chip, you need a breadboard for circuit wiring/connections with other
components, a DC power supply, jumper wires, etc. You also need to search for
program examples from various sources, books, and the Internet.
What is Arduino?
• A microcontroller board, contains on-board power supply, USB port to
communicate with PC, and an Atmel microcontroller chip.
• It simplify the process of creating any control system by providing the standard board
that can be programmed and connected to the system without the need to any
sophisticated PCB design and implementation.
• It is an open source hardware, any one can get the details of its design & modify it
or make his own one himself.
What Arduino Can Do?
• Sensors ( to sense stuff )
– Push buttons, touch pads, tilt switches.
– Variable resistors (e.g. volume knob / sliders)
– Photoresistors (sensing light levels)
– Thermistors (temperature)
– Ultrasound (proximity range finder)
•Actuators ( to do stuff )
– Lights, LED’s
– Motors
– Speakers
– Displays (LCD)
Why Arduino?
• It is Open Source, both in terms of Hardware and Software.
• It is cheap(1300र), the hardware can be built from components or a prefab board
can be purchased for approx. 900र.
• It can communicate with a computer via serial connection over USB.
• It can be powered from USB or standalone DC power.
• It can run standalone from a computer (chip is programmable) and it has memory
(a small amount).
• It can work with both Digital and Analog electronic signals. Sensors and
Actuators.
• You can make cool stuff! Some people are even making simple robots.
Arduino Architecture
• Arduino’s processor basically uses the
Harvard architecture where the program
code and program data have separate
memory.
• It consists of two memories- Program
memory and the data memory.
• The code is stored in the flash program
memory, whereas the data is stored in the
data memory.
• The Atmega328 has 32 KB of flash
memory for storing code (of which 0.5
KB is used for the bootloader), 2 KB of
SRAM and 1 KB of EEPROM and
operates with a clock speed of 16MHz.
Arduino Uno Board Description (Pin Diagram)
Arduino Uno Board Description (Pin Diagram)
Arduino Uno Board Description (Pin Diagram)
ICSP is basically "in circuit serial programming" which allows you to upload software to your MCU without having to pull it out every time.
Arduino Uno Board Description (Pin Diagram)
Arduino Uno Board Description
NOTE: Do NOT use a power supply greater
than 20 Volts as you will overpower (and
thereby destroy) your Arduino. The
recommended voltage for most Arduino
models is between 6 and 12 Volts
Types of Arduino
1. Arduino UNO
2. Arduino Nano
3. Arduino Mega
4. Arduino Micro
5. Arduino Leonardo
6. Arduino Due
7. Arduino Lilypad
8. Arduino Bluetooth
9. Arduino Robot
10. Arduino Ethernet
11. Arduino Zero
12. Arduino Esplora
13. Arduino Pro Micro
Classic Arduino Family
• Structure
• Functions.
Arduino Program Structure
Structure: Software structure consist of two main functions −
• Setup( ) function
• Loop( ) function
Arduino Program Structure
Setup()
• The loop() function does precisely what its name void loop()
suggests, and loops consecutively, allowing your
program to change and respond.
{
// repeats
• Use it to actively control the Arduino board. }
Bare Minimum Code
• Bare minimum of code you need for a
sketch to compile properly on Arduino
Software (IDE):
void
• The void keyword is used only in
function declarations.
• e.g.
Data Types
Char
• A data type that takes up one byte of memory that stores a character value. Range
from -128 to 127.
• Character literals are written in single quotes like this: 'A' and for multiple characters,
strings use double quotes: "ABC".
• e.g.
• However, characters are stored as numbers. You can see the specific encoding in the
ASCII chart.
• Unsigned char is an unsigned data type that occupies one byte of memory.
• e.g.
Data Types
byte
e.g.
Data Types
int
• Integers are the primary data-type for number storage. int stores a 16-bit (2-
byte) value.
• The int size varies from board to board. On the Arduino Due, for example, an
int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to
2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1).
• e.g.
Data Types
Unsigned int
• Unsigned int (unsigned integers) are the same as int in the way that they store a
2 byte value.
• Instead of storing negative numbers, however, they only store positive values,
yielding a useful range of 0 to 65,535 (2^16) - 1).
• The Due stores a 4 byte (32-bit) value, ranging from 0 to 4,294,967,295 (2^32
- 1).
• e.g.
Data Types
Word
• On the Uno and other ATMEGA based boards, a word stores a 16-bit
unsigned number. WORD is an unsigned 16-bit value (0 to +65535
range).
• e.g.
Data Types
Long
• Long variables are extended size variables for number storage, and
store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647.
• e.g.
Data Types
unsigned long
• e.g.
Data Types
short
• On all Arduinos (ATMega and ARM based), a short stores a 16-bit (2-
byte) value.
• e.g.
Data Types
float
• Data type for floating-point number is a number that has a decimal point.
• e.g.
Data Types
double
• On the Uno and other ATMEGA based boards, Double precision floating-
point number occupies four bytes.
• That is, the double implementation is exactly the same as the float, with
no gain in precision.
• e.g.
Variables
Local Variables
digitalRead(pin);
Reads HIGH or LOW from a pin
Eg3. digitalRead(2);
digitalWrite(pin, value);
Writes HIGH or LOW to a pin
Arduino Analogl I/O
analogRead(pin);
Reads between 0 and 1023 if 10 bit A/D is used
Eg3. analogRead(A3);
analogWrite(pin, value);
Writes an analog value (PWM wave) to a pin. Can be used to
light a LED at varying brightnesses or drive a motor at various
speeds. Value: the duty cycle: between 0 (always off) and 255
(always on)
Eg2. analogWrite(3, 127);
IR (Infrared) Sensor
• IR sensor is a simple electronic device which emits and detects IR radiation in
order to find out certain objects/obstacles in its range.
– Some of its features are heat and motion sensing.
• IR sensors use infrared radiation of wavelength between 0.75 to 1000µm which
falls between in-visible and microwave regions of electromagnetic spectrum.
• IR region is not visible to human eyes. Infrared spectrum is categorized into three
regions based on its wavelength i.e. Near Infrared, Mid Infrared, Far Infrared.
– Wavelength Regions of Infrared Spectrum
• Near IR – 0.75µm to 3 µm
• Mid IR – 3 µm to 6 µm
• Far IR – > 6 µm
IR (Infrared) Sensor
An Infrared Sensor works in the following sequence:
• IR source (transmitter) is used to emit radiation of required wavelength.
• This radiation reaches the object and is reflected back.
• The reflected radiation is detected by the IR receiver.
Specifications:
• Operating Voltage (Volts): 3.6 - 5 (DC)
• Distance Measuring Range (CM): 2 – 30 (can be varied with on-board pot)
• Average Current Consumption (mA): 0.06
• Comes with an easy-to-use digital output
Ultrasonic Sensor
• An Ultrasonic sensor is a device that can measure the distance to an object by using sound
waves.
• It measures distance by sending out a sound wave at a specific frequency and listening for
that sound wave to bounce back.
• By recording the elapsed time between the sound wave being generated and the sound wave
bouncing back, it is possible to calculate the distance between the sonar sensor and the object.
Ultrasonic Sensor
• Since it is known that sound travels through air at about 343 m/s (1129 ft/s), you can take the
time for the sound wave to return and multiply it by 343 meters (or 1129 feet) to find the total
round-trip distance of the sound wave.
• Round-trip means that the sound wave traveled 2 times the distance to the object before it was
detected by the sensor; it includes the 'trip' from the sonar sensor to the object AND the 'trip'
from the object to the Ultrasonic sensor (after the sound wave bounced off the object).
• To find the distance to the object, simply divide the round-trip distance in half.
Ultrasonic Sensor
Working principle
The Trig pin will be used to send the signal and the Echo pin will be used to listen for returning
signal;.
(1) Using IO trigger for at least 10us high level signal, Trig -> Pin-9 (o/p) of Arduino
(2) The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back.
(3) IF the signal back, through high level , time of high output duration is the time from sending
ultrasonic to returning.
DHT11 Sensor Interfacing with Arduino UNO
• DHT11 sensor measures and provides humidity and temperature values serially over
a single wire.
• It can measure relative humidity in percentage (20 to 90% RH) and temperature in
degree Celsius in the range of 0 to 50°C.
• It has 4 pins; one of which is used for data communication in serial form.
• DHT11 sensor uses resistive humidity measurement component, and temperature
measurement component.
DHT11 Specifications
• Ultra-low cost
• 3 to 5V power and I/O
• 2.5mA max current use during conversion (while requesting data)
• Good for 20-80% humidity readings with 5% accuracy
• Good for 0-50°C temperature readings ±2°C accuracy
DHT22 Specifications
• Low cost
• 3 to 5V power and I/O
• 2.5mA max current use during conversion (while requesting data)
• Good for 0-100% humidity readings with 2-5% accuracy
• Good for -40 to 125°C temperature readings ±0.5°C accuracy
Circuit connections of 3-pin sensor:
Circuit connections of 4-pin sensor:
Light Dependent Resistor (LDR)
• LDR is an acronym for Light Dependent Resistor.
• LDRs are tiny light-sensing devices also known
as photoresistors.
• An LDR is a resistor whose resistance changes
as the amount of light falling on it changes.
• The resistance of the LDR decreases with an
increase in light intensity, and vice-versa.
Light Dependent Resistor (LDR)
• Light Dependent Resistor – LM393
LDR’s resistance is very high in the
darkness, almost as high as 1MΩ but
when there is light that falls on the LDR,
the resistance falls down to a few KΩ
depending on the model.
Interface Pin :
1. VCC: +ve power supply (Operating voltage 3.3V-
5V).
2. GND: Ground.
3. D0: Digital output signal.
4. A0: Analog Output Signal.
Light Dependent Resistor (LDR)
The LDR module produces an output voltage according
to the resistance of the LDR and is made available at
Analog Output (AO) pin.
The same signal is fed to an LM393 High Precision
Comparator to digitize it and is made available at a Digital
Output (DO) pin.
The module has a built-in potentiometer for sensitivity
adjustment of the digital output (DO).
You can set a threshold by using a potentiometer; So that
when the incident light level on the LDR exceeds the
threshold value, the module will output LOW otherwise
HIGH.
Apart from this, the module has two LEDs. The Power Strong light, LED light, output low level;
LED will light up when the module is powered. The Status Weak light, LED off, output high level;
LED will light up when the digital output goes LOW. Sensitivity is adjustable.
Working of LDR
• First of all, we need to connect the LDR sensor module to a 5v power supply. Then set the
threshold voltage at the Non-Inverting input of the IC according to the present light
intensity by rotating the preset knob for setting the sensor sensitivity.
• When light intensity increase on the surface of the LDR then the resistance of the LDR
decreases. So, a Low amount of voltage from the LDR is given to the Inverting input of the
IC. Then the Comparator IC compares this voltage with the threshold voltage. In this
condition, this input voltage is less than the threshold voltage, so the sensor output goes
LOW.
• In contrast, when light intensity decrease (low/dark) on the surface of the LDR then the
resistance of the LDR increases. Then the maximum amount of voltage will be allocated
across the LDR. So, a High amount of voltage from the LDR is given to the Inverting input of
the IC. Then the Comparator IC compares this voltage with the threshold voltage. In this
condition, this input voltage is greater than the threshold voltage, so the sensor output goes
high.
•
MQ2 Sensor-Gas and Smoke Detection
Gas sensor is a device which detects the concentration
level of the gases present in the atmosphere.
• When reducing gases are present on the sensor, oxygen atoms try to
interact with reducing gases and reduce the absorbed surface
density of the oxygen atom. Hence current can flow through the
sensor to generate an analog output signal.
• The digital pin will go high only when the concentration of the gas is
above the threshold value. The sensor will work in analog mode to
detect the concentration of the gas.
How to Adjust the Sensitivity of Gas Sensor
• In order to adjust the sensitivity of this sensor for the particular type of
gas, you need to keep this sensor in the particular gas type you want to
detect. Now we have to rotate the potentiometer on the sensor.
• Rotate it till the Red LED on the sensor gets ON. If we have to increase
the sensitivity, rotate it clockwise, and if we have to decrease the
sensitivity we have to rotate it anticlockwise.
• Once this threshold gets crossed the digital pin goes high and the LED
will turn on.
Interfacing of MQ2 Sensor with Arduino
Connect your MQ2 sensor with the Arduino as
shown in the above diagram.
PWR, GND and Signal. For a very simple circuit, all that is
on the Arduino:
Components Required
• I2C LCD uses I2C communication interface to transfer the information required to display the
content.
• I2C LCD requires only 2 lines (SDA and SCL) for transferring the data. So, the complexity of the
circuit is reduced.
• I2C LCD can be connected to the Arduino directly with SDA pin to SDA pin and SCL pin to SCL
pin as per the below circuit diagram.
• I2C LCD requires additional library to be installed (install the “LiquidCrystal I2C” library
in the Arduino IDE).
Case Studies of Smart Home
Verisure
Background
Verisure is Europe’s leading provider of professionally
monitored alarms and connected services for homes and
small businesses. The company serves millions of
customers in numerous countries, and is fast expanding
and growing.
Results
For Verisure, the IoT solution has enabled them to expand
its business into adjacent areas of expertise, stretch the
brand to become a trustworthy provider of not only home
alarms but to smart homes.
Case Studies of Smart Cities
Anticimex
Background
Pest control has traditionally relied on traps filled with
toxins to exterminate pests. Anticimex developed an
intelligent system of connected traps.
Results
For Anticimex, connecting its traps has created benefits
for end users, businesses and the environment.
Case Studies of Smart Cities
Kamstrup
Background
Kamstrup A/S is a leading solution provider within smart
metering. The company develops and sells monitoring
meters for water, district heating, and electricity globally.
Results
Kamstrup provided with ongoing real-time diagnostics and
can supervise its meters remotely.
Case Studies of Smart HealthCare
8 West
Background
8 West Consulting is an enterprise software development
company based in Ireland that has created one of the
world’s leading maritime search and rescue applications,
SafeTrx. SafeTrx is a remote monitoring and alerting
system that provides automatic real-time alerting for
sports enthusiasts, isolated or at-risk workers and
vulnerable older people. The device enables location
alerts to be sent, and requests for emergency help or
assistance to be communicated, so everyone from surfers
to lone workers and people in a healthcare setting can be
tracked and monitored
Results
SafeTrx incorporates wind, weather and wave alerts, and
is in active use by surfers across Europe.
Case Studies of Smart Wearable Devices
Amber
Background
Amber’s COVID-19 Pandemic Management Solution is a
comprehensive, interactive website and mobile application
(JamCovid19 App) for Android and iOS that can be paired
with a wearable GPS tracker wristband.
Results
The solution enables governments to centrally develop,
manage and disseminate crucial information related to the
COVID-19 pandemic and includes provisions to efficiently
enable travel authorization, airport screening,
tracking/tracing management and support quarantine
measures.