Introduction and Programming
Arduino
Agenda
• Introduction to Arduino Boards
• Getting started with Arduino IDE
• Arduino Programming and Proteus designs
• Interfacing Output (LED) & Input (Key) devices
• Working with IR Sensor, LDR and its Interfacing
• Serial Communication feature of Arduino
• Working with DHT11/22 and its interfacing
• Ultrasonic Sensor Interfacing
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 can it do?
• Sensors ( to sense stuff )
– Push buttons, touch pads, tilt switches.
– Variable resistors (eg. 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.
UN
O
Meg
a
LilyPa
d
Arduino
BT
Arduino
Nano
Arduino
Mini
Different types of Arduino boards:
Arduino & Arduino compatible boards:
Arduino Uno Board
Digital output
~: PWM.
0,1: Serial
port.
Connectors/Cables to work with Uno:
Arduino Uno Board Description
Arduino Uno Board Description (Cont..)
Arduino Uno Board Description (Cont..)
Arduino Uno Board Description (Cont..)
Arduino Uno Board Description (Cont..)
INPUT v/s OUTPUT
Referenced from the perspective of the microcontroller (electrical board).
Inputs is a signal / information
going into the Arduino board.
Output is any signal exiting the
Arduino board.
Examples: Buttons Switches,
Light Sensors, Flex Sensors,
Humidity Sensors, Temperature
Sensors…
Examples: LEDs, DC motor,
servo motor, a piezo buzzer,
relay, an RGB LED
https://getintopc.com/softwares/3d-cad/proteus-professional-2020-free-download/
Getting Started (Installing Arduino IDE and Setting up Arduino Board)
Check out: http://arduino.cc/en/Reference/HomePage ,
http://arduino.cc/en/Guide/HomePage .
1. Download & install the Arduino environment (IDE)
https://www.arduino.cc/en/Main/Software
2. Connect the board to your computer via the USB cable
3. If needed, install the drivers
4. Launch the Arduino IDE
5. Select your board
6. Select your serial port
7. Open the blink example
8. Upload the program
Basic Procedure
•Design the circuit:
– What are electrical requirements of the sensors or actuators?
– Identify inputs (analog inputs)
– Identify digital outputs
•Write the code:
– Build incrementally
• Get the simplest piece to work first
• Add complexity and test at each stage
• Save and Backup frequently
– Use variables, not constants
– Comment liberally
Writing and Uploading Code into Arduino
Running Code
• Running Code While Tethered
• Running Code Stand-Alone
Arduino IDE
IDE =
Integrated Development
Environment
http://www.arduino.cc/en/Guide/Environ
ment
Arduino IDE (Cont..)
Two required functions /
methods / routines:
void setup()
{
// runs once
}
void loop()
{
// repeats
}
Arduino IDE (Cont..)
Settings: Tools → Serial Port
Your computer
communicates to the
Arduino microcontroller via a
serial port → through a USB-
Serial adapter.
Check to make sure that the
drivers are properly installed.
Arduino IDE (Cont..)
Settings: Tools → Board
Next, double-check that the proper board is selected under the
Tools→Board menu.
Breadboard Layout:
Arduino Digital I/0
pinMode(pin, mode);
Sets pin to either INPUT or OUTPUT
Eg1. pinMode(13, OUTPUT);
digitalRead(pin);
Reads HIGH or LOW from a pin
Eg3. digitalRead(2);
digitalWrite(pin, value);
Writes HIGH or LOW to a pin
Eg2. digitalWrite(13, HIGH);
Our first Arduino Sketch/Program
/*
* Arduinos ketch to toggle the LED connected to pin-13 with a rate/delay of 1sec
*/
void setup()
{
// put your setup code here, to run once: -->I*
pinMode(13, OUTPUT); //pin-13 configures as o/p -->II
}
void loop()
{
// put your main code here, to run repeatedly: -->1*
digitalWrite(13, HIGH); //HIGH Value or Bunary-1 send to pin-13 -->2
//delay(x); //x-ms second(s) delay -->3*
//delayMicroseconds(y); //y-us second(s) delay -->4*
delay(1000); //1000-milliseconds=1second delay -->5
digitalWrite(13, LOW); //LOW Value or Bunary-1 send to pin-13 -->6
delay(1000); //1000-milliseconds=1second delay -->7
//Toggling rate of led connected to pin-13 is of 1second -->8*
}
Uploading and Running the blink sketch
In Arduino, open up:
File → Examples → 01.Basics → Blink
const int ledPin = 13; // LED connected
to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED
on
delay(2000); // wait for two seconds
digitalWrite(ledPin, LOW); // set the LED
off
delay(2000); // wait for two seconds
}
Uploading and Running the blink sketch (Cont..)
• Write your sketch/program
• Press compile button
(to check for errors)
• Press upload button to program
Arduino board with your sketch
• Try it out with the “Blink” sketch!
Arduino programs can be divided in three
main parts: Values(Variables & Constants),
Structure, and functions.
Proteus design
Arduino Data Types
• Except in situations where maximum performance or memory efficiency
is required, variables declared using int will be suitable for numeric
values if the values do not exceed the range (shown in the first row in
below Table) and if you don’t need to work with fractional values.
• Most of the official Arduino example code declares numeric variables as
int.
• But sometimes you do need to choose a type that specifically suits your
application.
Arduino Data Types (Cont..)
• Sometimes you need negative numbers and sometimes you
don’t, so numeric types come in two varieties: signed and
unsigned.
• unsigned values are always positive. Variables without the
keyword unsigned in front are signed so that they can represent
negative and positive values.
• One reason to use unsigned values is when the range of signed
values will not fit the range of the variable (an unsigned variable
has twice the capacity of a signed variable).
• Another reason programmers choose to use unsigned types is to
clearly indicate to people reading the code that the value
expected will never be a negative number.
Arduino Data Types (Cont..)
Arduino Data Types (Cont..)
• Boolean types have two possible values: true or false. They are
commonly used for things like checking the state of a switch (if
it’s pressed or not).
• You can also use HIGH and LOW as equivalents to true and
false where this makes more sense;
– digitalWrite(pin, HIGH) is a more expressive way to turn on
an LED than digitalWrite(pin, true) or digitalWrite(pin,1),
although all of these are treated identically when the sketch
actually runs.
Data Types and Operators
Integer: used with integer variables with value between
2147483647 and -2147483647.
Eg: int x=1200;
Character: used with single character, represent value from -
127 to 128.
Eg. char c=‘r’;
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.
Eg. long u=199203;
Floating-point numbers can be as large as 3.4028235E+38
and as low as -3.4028235E+38. They are stored as 32 bits (4
bytes) of information.
Statements and operators:
Statement represents a command, it ends with ;
Eg: int x; x=13;
Operators are symbols that used to
indicate a specific function:
Math operators: [+,-,*,/,%,^]
Logic operators: [==, !=, &&, ||]
Comparison/Boolean operators: [==, >, <, !=, <=, >=]
Syntax:
; Semicolon, {} curly braces,
// single line comment,
/*Multi-line comments*/
Compound Operators:
++ (increment)
-- (decrement)
+= (compound addition)
-= (compound subtraction)
*= (compound
multiplication)
Boolean Operators:
== (is equal?)
!= (is not equal?)
> (greater than)
>= (greater than or equal)
< (less than)
<= (less than or equal)
Control statements:
If Conditioning:
if(condition)
{
statements-1;
…
Statement-N;
}
else if(condition2)
{
Statements;
}
else
{
statements;
}
Switch case:
switch (var)
{
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
}
Loop statements:
Do…
while:
do
{
Statement
s;
}
// the statements are run at least
once.
while(condition)
; While:
While(condition
)
{ statements; }
for
for (int i=0; i <= val; i++)
{
statements;
}
Use break statement to stop the loop whenever
Digital Input
• Connect digital input to your Arduino using Pins # 0 – 13
(Although pins # 0 & 1 are also used for programming)
• Digital Input needs a pinMode command: pinMode
(pinNumber, INPUT); Make sure to use ALL
CAPS for INPUT
• To get a digital reading:
int buttonState = digitalRead (pinNumber);
• Digital Input values are only HIGH (On) or LOW (Off)
Digital Input (Cont..)
// Pushbutton sketch: a switch is connected to pin 2 lights the LED on pin 13
// (or) Control an LED connected to pin-13 w.r.to. a switch connected to pin-2,
with the help of external resister
const int ledPin = 13; // choose the pin for the LED
const int inputPin = 2; // choose the input pin (for a pushbutton)
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop()
{
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED on if switch is pressed
}
else
{
digitalWrite(ledPin, LOW); // turn LED off
}
}
Proteus design (with external resister)
 The digitalRead function monitors the voltage on the input pin
(inputPin), and it returns a value of HIGH if the voltage is high (5
volts) and LOW if the voltage is low (0 volts).
 Actually, any voltage that is greater than 2.5 volts (half of the voltage
powering the chip) is considered HIGH and less than this is treated as
LOW.
 If the pin is left unconnected (known as floating) the value returned
from digitalRead is indeterminate (it may be HIGH or LOW, and it
cannot be reliably used).
 The resistor shown in Figure shown in previous slide ensures that the
voltage on the pin will be low when the switch is not pressed,
because the resistor “pulls down” the voltage to ground.
 When the switch is pushed, a connection is made between the pin
and +5 volts, so the value on the pin interpreted by digital Read
changes from LOW to HIGH.
Using a key/push button with external resistor
InfraRed (IR) Sensor
Features-
• Can be used for obstacle sensing, fire detection, line sensing, etc
• Input Voltage: 5V DC
• Comes with an easy to use digital output
• Can be used for wireless communication and sensing IR remote
signals
IR Sensor have three Pins
1. VCC = +5V DC
2. GND
3. D0 or OUT (Digital Output)
Arduino sketch & Circuit diagram
const int ProxSensor=2;
void setup()
{
pinMode(13, OUTPUT);
pinMode(ProxSensor, INPUT);
}
void loop()
{
if(digitalRead(ProxSensor)==HIGH)
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
delay(100);
}
IR Sensor Interfacing, display the o/p on Serial Monitor
/*
IR Proximity Sensor interface code
Turns on an LED on when obstacle is
detected, else off.
*/
const int ProxSensor=2;
void setup()
{
// initialize the digital Serial port.
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(13, OUTPUT);
pinMode(ProxSensor,INPUT);
}
void loop()
{
if(digitalRead(ProxSensor)==LOW)
//Check the sensor output
{
digitalWrite(13, HIGH); // set the LED on
Serial.println("Stop something is ahead!! ");
//Message on Serial Monitor
}
else
{
digitalWrite(13, LOW); // set the LED off
Serial.println("Path is clear");
//Message on Serial Monitor
}
delay(1000); // wait for a second
}
Proteus design:
Light Dependent Resistor:
• LDR ( light dependent resistor ) also called
as photoresistor is responsive to light.
• Photoresistors are used to indicate the light
intensity (or) the presence or absence of
light.
• When there is darkness then the resistance
of photoresistor increases, and when there
is sufficient light it’s resistance decreases.
Light Dependent Resistor (Cont..):
• LDR ( light dependent resistor ) also called
as photoresistor is responsive to light.
• Photoresistors are used to indicate the light
intensity (or) the presence or absence of
light.
• When there is darkness then the resistance
of photoresistor increases, and when there
is sufficient light it’s resistance decreases.
LDR with Arduino: sketch & Circuit
const int ledPin = 13; //pin at which LED is connected
const int ldrPin = A0; //pin at which LDR is connected
int threshold = 600;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //Make LED pin as output
pinMode(ldrPin, INPUT); //Make LDR pin as input
}
void loop()
{
int ldrStatus = analogRead(ldrPin); //saving the analog values received from LDR
if (ldrStatus <= threshold) //set the threshold value below at which the LED will turn on
{ //you can check in the serial monior to get approprite value for your LDR
digitalWrite(ledPin, HIGH); //Turing LED ON
Serial.print("Its DARK, Turn on the LED : ");
Serial.println(ldrStatus);
}
else
{
digitalWrite(ledPin, LOW); //Turing OFF the LED
Serial.print("Its BRIGHT, Turn off the LED : ");
Serial.println(ldrStatus);
}
}
Proteus design:
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 NTC 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:
Prerequisites to work with DHT11/22:
• Before you can use the DHT11 on the Arduino, you’ll need to install the DHTLib library.
• https://github.com/adafruit/DHT-sensor-library or
• https://www.arduino.cc/reference/en/libraries/dht-sensor-library/
• It has all the functions needed to get the humidity and temperature readings from the
sensor.
• It’s easy to install, just download the DHTLib.zip and open up the Arduino IDE.
• Then go to Sketch>Include Library>Add .ZIP Library and select the DHTLib.zip file.
Arduino Sketch:
#include <Adafruit_Sensor.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
Serial.println("DHT11 test!");
dht.begin();
}
void loop()
{
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Humidity: t" );
Serial.print(h);
Serial.print(" %n");
delay(500);
Serial.print("Temperature: t");
Serial.print(t);
Serial.print(" *C n");
}
Proteus design:
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 (Cont..)
• Since it is known that sound travels through air at about 344
m/s (1129 ft/s), you can take the time for the sound wave to
return and multiply it by 344 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.
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 IO duration is the time from
sending ultrasonic to returning.
Arduino Sketch
/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*/
// defines pins numbers
const int trigPin = 9;
const int echoPin = 8;
// defines variables
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
Arduino Sketch (Cont..)
void loop()
{ // Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microsec.
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= (duration*0.034)/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
delay(500); //500 m.sec = 0.5 sec
}
pulsein()
• Reads a pulse (either HIGH or LOW) on a pin.
• For example, if value is HIGH, pulseIn() waits for the pin
to go from LOW to HIGH, starts timing, then waits for the pin
to go LOW and stops timing.
• Returns the length of the pulse in microseconds or
gives up and returns 0 if no complete pulse was received
within the timeout.
Proteus design:
Links to download the software's:
Link to download Proteus 8.1:
• https://getintopc.com/softwares/3d-cad/proteus-professional-
2020-free-download/
Link to download Proteus 8.13:
• https://getintopc.com/softwares/3d-cad/proteus-professional-2020-free-
download/?id=001623037415
Link to download Proteus Libraries:
• https://github.com/officialdanielamani/proteus-library-collection
Link to download Arduino IDE 1.8.19:
• https://www.arduino.cc/en/software
Arduino_CSE ece ppt for working and principal of arduino.ppt

Arduino_CSE ece ppt for working and principal of arduino.ppt

  • 1.
  • 2.
    Agenda • Introduction toArduino Boards • Getting started with Arduino IDE • Arduino Programming and Proteus designs • Interfacing Output (LED) & Input (Key) devices • Working with IR Sensor, LDR and its Interfacing • Serial Communication feature of Arduino • Working with DHT11/22 and its interfacing • Ultrasonic Sensor Interfacing
  • 3.
    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.
  • 4.
    What can itdo? • Sensors ( to sense stuff ) – Push buttons, touch pads, tilt switches. – Variable resistors (eg. volume knob / sliders) – Photoresistors (sensing light levels) – Thermistors (temperature) – Ultrasound (proximity range finder) • Actuators ( to do stuff ) – Lights, LED’s – Motors – Speakers – Displays (LCD)
  • 5.
    Why Arduino? • Itis 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.
  • 6.
  • 7.
    Arduino & Arduinocompatible boards:
  • 8.
    Arduino Uno Board Digitaloutput ~: PWM. 0,1: Serial port.
  • 9.
  • 10.
    Arduino Uno BoardDescription
  • 11.
    Arduino Uno BoardDescription (Cont..)
  • 12.
    Arduino Uno BoardDescription (Cont..)
  • 13.
    Arduino Uno BoardDescription (Cont..)
  • 14.
    Arduino Uno BoardDescription (Cont..)
  • 15.
    INPUT v/s OUTPUT Referencedfrom the perspective of the microcontroller (electrical board). Inputs is a signal / information going into the Arduino board. Output is any signal exiting the Arduino board. Examples: Buttons Switches, Light Sensors, Flex Sensors, Humidity Sensors, Temperature Sensors… Examples: LEDs, DC motor, servo motor, a piezo buzzer, relay, an RGB LED https://getintopc.com/softwares/3d-cad/proteus-professional-2020-free-download/
  • 16.
    Getting Started (InstallingArduino IDE and Setting up Arduino Board) Check out: http://arduino.cc/en/Reference/HomePage , http://arduino.cc/en/Guide/HomePage . 1. Download & install the Arduino environment (IDE) https://www.arduino.cc/en/Main/Software 2. Connect the board to your computer via the USB cable 3. If needed, install the drivers 4. Launch the Arduino IDE 5. Select your board 6. Select your serial port 7. Open the blink example 8. Upload the program
  • 17.
    Basic Procedure •Design thecircuit: – What are electrical requirements of the sensors or actuators? – Identify inputs (analog inputs) – Identify digital outputs •Write the code: – Build incrementally • Get the simplest piece to work first • Add complexity and test at each stage • Save and Backup frequently – Use variables, not constants – Comment liberally
  • 18.
    Writing and UploadingCode into Arduino
  • 19.
    Running Code • RunningCode While Tethered • Running Code Stand-Alone
  • 20.
    Arduino IDE IDE = IntegratedDevelopment Environment http://www.arduino.cc/en/Guide/Environ ment
  • 21.
    Arduino IDE (Cont..) Tworequired functions / methods / routines: void setup() { // runs once } void loop() { // repeats }
  • 22.
    Arduino IDE (Cont..) Settings:Tools → Serial Port Your computer communicates to the Arduino microcontroller via a serial port → through a USB- Serial adapter. Check to make sure that the drivers are properly installed.
  • 23.
    Arduino IDE (Cont..) Settings:Tools → Board Next, double-check that the proper board is selected under the Tools→Board menu.
  • 24.
  • 25.
    Arduino Digital I/0 pinMode(pin,mode); Sets pin to either INPUT or OUTPUT Eg1. pinMode(13, OUTPUT); digitalRead(pin); Reads HIGH or LOW from a pin Eg3. digitalRead(2); digitalWrite(pin, value); Writes HIGH or LOW to a pin Eg2. digitalWrite(13, HIGH);
  • 26.
    Our first ArduinoSketch/Program /* * Arduinos ketch to toggle the LED connected to pin-13 with a rate/delay of 1sec */ void setup() { // put your setup code here, to run once: -->I* pinMode(13, OUTPUT); //pin-13 configures as o/p -->II } void loop() { // put your main code here, to run repeatedly: -->1* digitalWrite(13, HIGH); //HIGH Value or Bunary-1 send to pin-13 -->2 //delay(x); //x-ms second(s) delay -->3* //delayMicroseconds(y); //y-us second(s) delay -->4* delay(1000); //1000-milliseconds=1second delay -->5 digitalWrite(13, LOW); //LOW Value or Bunary-1 send to pin-13 -->6 delay(1000); //1000-milliseconds=1second delay -->7 //Toggling rate of led connected to pin-13 is of 1second -->8* }
  • 27.
    Uploading and Runningthe blink sketch In Arduino, open up: File → Examples → 01.Basics → Blink const int ledPin = 13; // LED connected to digital pin 13 void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); // set the LED on delay(2000); // wait for two seconds digitalWrite(ledPin, LOW); // set the LED off delay(2000); // wait for two seconds }
  • 28.
    Uploading and Runningthe blink sketch (Cont..) • Write your sketch/program • Press compile button (to check for errors) • Press upload button to program Arduino board with your sketch • Try it out with the “Blink” sketch! Arduino programs can be divided in three main parts: Values(Variables & Constants), Structure, and functions.
  • 29.
  • 30.
    Arduino Data Types •Except in situations where maximum performance or memory efficiency is required, variables declared using int will be suitable for numeric values if the values do not exceed the range (shown in the first row in below Table) and if you don’t need to work with fractional values. • Most of the official Arduino example code declares numeric variables as int. • But sometimes you do need to choose a type that specifically suits your application.
  • 31.
    Arduino Data Types(Cont..) • Sometimes you need negative numbers and sometimes you don’t, so numeric types come in two varieties: signed and unsigned. • unsigned values are always positive. Variables without the keyword unsigned in front are signed so that they can represent negative and positive values. • One reason to use unsigned values is when the range of signed values will not fit the range of the variable (an unsigned variable has twice the capacity of a signed variable). • Another reason programmers choose to use unsigned types is to clearly indicate to people reading the code that the value expected will never be a negative number.
  • 32.
  • 33.
    Arduino Data Types(Cont..) • Boolean types have two possible values: true or false. They are commonly used for things like checking the state of a switch (if it’s pressed or not). • You can also use HIGH and LOW as equivalents to true and false where this makes more sense; – digitalWrite(pin, HIGH) is a more expressive way to turn on an LED than digitalWrite(pin, true) or digitalWrite(pin,1), although all of these are treated identically when the sketch actually runs.
  • 34.
    Data Types andOperators Integer: used with integer variables with value between 2147483647 and -2147483647. Eg: int x=1200; Character: used with single character, represent value from - 127 to 128. Eg. char c=‘r’; 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. Eg. long u=199203; Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.
  • 35.
    Statements and operators: Statementrepresents a command, it ends with ; Eg: int x; x=13; Operators are symbols that used to indicate a specific function: Math operators: [+,-,*,/,%,^] Logic operators: [==, !=, &&, ||] Comparison/Boolean operators: [==, >, <, !=, <=, >=] Syntax: ; Semicolon, {} curly braces, // single line comment, /*Multi-line comments*/ Compound Operators: ++ (increment) -- (decrement) += (compound addition) -= (compound subtraction) *= (compound multiplication) Boolean Operators: == (is equal?) != (is not equal?) > (greater than) >= (greater than or equal) < (less than) <= (less than or equal)
  • 36.
    Control statements: If Conditioning: if(condition) { statements-1; … Statement-N; } elseif(condition2) { Statements; } else { statements; } Switch case: switch (var) { case 1: //do something when var equals 1 break; case 2: //do something when var equals 2 break; default: // if nothing else matches, do the default // default is optional }
  • 37.
    Loop statements: Do… while: do { Statement s; } // thestatements are run at least once. while(condition) ; While: While(condition ) { statements; } for for (int i=0; i <= val; i++) { statements; } Use break statement to stop the loop whenever
  • 38.
    Digital Input • Connectdigital input to your Arduino using Pins # 0 – 13 (Although pins # 0 & 1 are also used for programming) • Digital Input needs a pinMode command: pinMode (pinNumber, INPUT); Make sure to use ALL CAPS for INPUT • To get a digital reading: int buttonState = digitalRead (pinNumber); • Digital Input values are only HIGH (On) or LOW (Off)
  • 39.
  • 40.
    // Pushbutton sketch:a switch is connected to pin 2 lights the LED on pin 13 // (or) Control an LED connected to pin-13 w.r.to. a switch connected to pin-2, with the help of external resister const int ledPin = 13; // choose the pin for the LED const int inputPin = 2; // choose the input pin (for a pushbutton) void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare pushbutton as input } void loop() { int val = digitalRead(inputPin); // read input value if (val == HIGH) // check if the input is HIGH { digitalWrite(ledPin, HIGH); // turn LED on if switch is pressed } else { digitalWrite(ledPin, LOW); // turn LED off } }
  • 41.
    Proteus design (withexternal resister)
  • 42.
     The digitalReadfunction monitors the voltage on the input pin (inputPin), and it returns a value of HIGH if the voltage is high (5 volts) and LOW if the voltage is low (0 volts).  Actually, any voltage that is greater than 2.5 volts (half of the voltage powering the chip) is considered HIGH and less than this is treated as LOW.  If the pin is left unconnected (known as floating) the value returned from digitalRead is indeterminate (it may be HIGH or LOW, and it cannot be reliably used).  The resistor shown in Figure shown in previous slide ensures that the voltage on the pin will be low when the switch is not pressed, because the resistor “pulls down” the voltage to ground.  When the switch is pushed, a connection is made between the pin and +5 volts, so the value on the pin interpreted by digital Read changes from LOW to HIGH. Using a key/push button with external resistor
  • 44.
    InfraRed (IR) Sensor Features- •Can be used for obstacle sensing, fire detection, line sensing, etc • Input Voltage: 5V DC • Comes with an easy to use digital output • Can be used for wireless communication and sensing IR remote signals IR Sensor have three Pins 1. VCC = +5V DC 2. GND 3. D0 or OUT (Digital Output)
  • 45.
    Arduino sketch &Circuit diagram const int ProxSensor=2; void setup() { pinMode(13, OUTPUT); pinMode(ProxSensor, INPUT); } void loop() { if(digitalRead(ProxSensor)==HIGH) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); } delay(100); }
  • 46.
    IR Sensor Interfacing,display the o/p on Serial Monitor /* IR Proximity Sensor interface code Turns on an LED on when obstacle is detected, else off. */ const int ProxSensor=2; void setup() { // initialize the digital Serial port. Serial.begin(9600); // initialize the digital pin as an output. pinMode(13, OUTPUT); pinMode(ProxSensor,INPUT); } void loop() { if(digitalRead(ProxSensor)==LOW) //Check the sensor output { digitalWrite(13, HIGH); // set the LED on Serial.println("Stop something is ahead!! "); //Message on Serial Monitor } else { digitalWrite(13, LOW); // set the LED off Serial.println("Path is clear"); //Message on Serial Monitor } delay(1000); // wait for a second }
  • 47.
  • 49.
    Light Dependent Resistor: •LDR ( light dependent resistor ) also called as photoresistor is responsive to light. • Photoresistors are used to indicate the light intensity (or) the presence or absence of light. • When there is darkness then the resistance of photoresistor increases, and when there is sufficient light it’s resistance decreases.
  • 50.
    Light Dependent Resistor(Cont..): • LDR ( light dependent resistor ) also called as photoresistor is responsive to light. • Photoresistors are used to indicate the light intensity (or) the presence or absence of light. • When there is darkness then the resistance of photoresistor increases, and when there is sufficient light it’s resistance decreases.
  • 51.
    LDR with Arduino:sketch & Circuit const int ledPin = 13; //pin at which LED is connected const int ldrPin = A0; //pin at which LDR is connected int threshold = 600; void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); //Make LED pin as output pinMode(ldrPin, INPUT); //Make LDR pin as input } void loop() { int ldrStatus = analogRead(ldrPin); //saving the analog values received from LDR if (ldrStatus <= threshold) //set the threshold value below at which the LED will turn on { //you can check in the serial monior to get approprite value for your LDR digitalWrite(ledPin, HIGH); //Turing LED ON Serial.print("Its DARK, Turn on the LED : "); Serial.println(ldrStatus); } else { digitalWrite(ledPin, LOW); //Turing OFF the LED Serial.print("Its BRIGHT, Turn off the LED : "); Serial.println(ldrStatus); } }
  • 52.
  • 54.
    DHT11 Sensor Interfacingwith 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 NTC temperature measurement component.
  • 55.
    DHT11 Specifications • Ultra-lowcost • 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
  • 56.
    DHT22 Specifications • Lowcost • 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
  • 57.
  • 58.
  • 59.
    Prerequisites to workwith DHT11/22: • Before you can use the DHT11 on the Arduino, you’ll need to install the DHTLib library. • https://github.com/adafruit/DHT-sensor-library or • https://www.arduino.cc/reference/en/libraries/dht-sensor-library/ • It has all the functions needed to get the humidity and temperature readings from the sensor. • It’s easy to install, just download the DHTLib.zip and open up the Arduino IDE. • Then go to Sketch>Include Library>Add .ZIP Library and select the DHTLib.zip file.
  • 60.
    Arduino Sketch: #include <Adafruit_Sensor.h> #include"DHT.h" #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); Serial.println("DHT11 test!"); dht.begin(); } void loop() { delay(2000); float h = dht.readHumidity(); float t = dht.readTemperature(); Serial.print("Humidity: t" ); Serial.print(h); Serial.print(" %n"); delay(500); Serial.print("Temperature: t"); Serial.print(t); Serial.print(" *C n"); }
  • 61.
  • 63.
    Ultrasonic Sensor • AnUltrasonic 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.
  • 64.
    Ultrasonic Sensor (Cont..) •Since it is known that sound travels through air at about 344 m/s (1129 ft/s), you can take the time for the sound wave to return and multiply it by 344 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.
  • 65.
    Working principle The Trigpin 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 IO duration is the time from sending ultrasonic to returning.
  • 66.
    Arduino Sketch /* * UltrasonicSensor HC-SR04 and Arduino Tutorial */ // defines pins numbers const int trigPin = 9; const int echoPin = 8; // defines variables long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication }
  • 67.
    Arduino Sketch (Cont..) voidloop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microsec. duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= (duration*0.034)/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.print(distance); Serial.println("cm"); delay(500); //500 m.sec = 0.5 sec }
  • 68.
    pulsein() • Reads apulse (either HIGH or LOW) on a pin. • For example, if value is HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing. • Returns the length of the pulse in microseconds or gives up and returns 0 if no complete pulse was received within the timeout.
  • 69.
  • 70.
    Links to downloadthe software's: Link to download Proteus 8.1: • https://getintopc.com/softwares/3d-cad/proteus-professional- 2020-free-download/ Link to download Proteus 8.13: • https://getintopc.com/softwares/3d-cad/proteus-professional-2020-free- download/?id=001623037415 Link to download Proteus Libraries: • https://github.com/officialdanielamani/proteus-library-collection Link to download Arduino IDE 1.8.19: • https://www.arduino.cc/en/software