0% found this document useful (0 votes)
17 views

Arduino Unit4

Uploaded by

as.thakuraniket
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Arduino Unit4

Uploaded by

as.thakuraniket
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

uNIT-5

BASIC ELECTRICAL AND


ELECTRONICS ENGINEERING
ECE-249
Introduction and Programming
Arduino
Introduction of Arduino and Sensors :
Arduino board (pin configuration and
description), basic principle of ultrasonic
sensor, Temperature, IR sensor.
Outline
Introduction of Arduino and Sensors

Arduino board (pin configuration and


description)
What is Arduino?

• “Strong Friend” Created in Ivrea, Italy


• in 2005 by Massimo Banzi & David Cuartielles
• Open Source Hardware
• Processor
• Coding is accessible & transferrable  (C++,
Processing, java)
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.
Why Arduino?

• It is Open Source, both in terms of Hardware and Software.

• It is cheap

• It can communicate with a computer via serial connection over


USB.

• 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.


What can it do?
• Sensors

– Push buttons, touch pads


– Variable resistors (eg. volume knob / sliders)
– Photoresistors (sensing light levels)
– Thermistors (temperature sensors)
– Ultrasound (obstacle detection )
•Actuators

– Lights, LED’s
– Motors
– Speakers
– Displays (LCD)
Sensors and Actuators
• The sensor is a device which
converter the physical or chemical
stimulus into an output signal.
• Physical parameters could be light,
heat, motion, moisture, force,
pressure, displacement, etc.
• It produces output signal as
electrical, mechanical, magnetic,
etc.
Sensors and Actuators
• Actuators changes electrical signals into mechanical signal.
• Actuators are connected to a system's output.
• It receives an electrical signal as input and produces
mechanical movement as output.
• Electrical signals are generated via sensors. On the other
hand, an actuator produces energy in the form of heat or
motion.
• Example of sensors are cameras, microphones, etc. Example
of actuators are LEDs, loudspeakers, motor controllers,
lasers, etc.
Sensors
Arduino

• Arduino board only sends and receives information


through its ports and processes it if necessary.

• The data stored in the Arduino comes from the software


sent from a computer through a USB port or from
different accessories/complements added to the board
to increase its “functionality”

• Sensors are useful complements widely used for


Arduino board project development to obtain
information from the environment or external elements.
Different types of Arduino boards:

UNO Mega LilyPad

Arduino BT Arduino Nano Arduino Mini


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 Output is any signal exiting the


going into the Arduino board. Arduino board.

Examples: Buttons Switches, Examples: LEDs, DC motor,


Light Sensors, Flex Sensors, servo motor, a piezo buzzer,
Humidity Sensors, Temperature relay, an RGB LED
Sensors…

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
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
Arduino IDE
Arduino IDE
Arduino IDE
Arduino IDE
Arduino IDE
Arduino IDE (Cont..)
Two required functions /
methods / routines:

void setup()
{
// runs once
}

void loop()
{
// repeats
}
Arduino IDE

IDE =
Integrated Development
Environment
http://www.arduino.cc/en/Guide/Environ
ment
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.
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);
Arduino IDE
Arduino IDE
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.
Arduino IDE
Arduino IDE
Arduino IDE
Arduino IDE
Arduino IDE
Arduino IDE
Arduino IDE
Statements and operators:
Statement represents a command, it ends with ; Eg: int
x; x=13; Boolean Operators:
== (is equal?)
Operators are symbols that used to indicate
!= (is not equal?)
a specific function:
> (greater than)
Math operators: [+,-,*,/,%,^] >= (greater than or equal)
< (less than)
Logic operators: [==, !=, &&, ||] <= (less than or equal)

Comparison/Boolean operators: [==, >, <, !=, <=, >=]

Syntax:

; Semicolon, {} curly braces,


Compound Operators:
// single line comment, ++ (increment)
/*Multi-line comments*/ -- (decrement)
+= (compound addition)
-= (compound subtraction)
*= (compound multiplication)
/= (compound division)
Control statements:
If Conditioning: Switch case:
if(condition) switch (var)
{
statements-1;
{
… case 1:
Statement-N; //do something when var equals 1 break;
} case 2:
else if(condition2) //do something when var equals 2 break;
{
Statements;
default:
} // if nothing else matches, do the default
else // default is optional
{ }
statements;
}
Loop statements:
Do… while:
do
{
Statements;
}
while(condition); // the statements are run at least once.
While:
While(condition)
{ statements; }
for
for (int i=0; i <= val; i++)
{
statements;
}
Use break statement to stop the loop whenever needed.
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
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()
{
delay(100);
int ldrStatus = analogRead(ldrPin); //saving the analog values received from LDR into "ldrStatus" variable
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, Turning ON the LED : ");
Serial.println(ldrStatus);
}
else
{
digitalWrite(ledPin, LOW); //Turing OFF the LED
Serial.print("Its BRIGHT, Turning OFF the LED : ");
Serial.println(ldrStatus);
}
}
Proteus design:
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
InfraRed (IR) Sensor
InfraRed (IR) Sensor
InfraRed (IR) Sensor
Arduino sketch & Circuit diagram

const int ProxSensor=4;


const int led=13;
void setup()
{
pinMode(led, OUTPUT);
pinMode(ProxSensor, INPUT);
Serial.begin(9600);
}
void loop()
{
if(digitalRead(ProxSensor)==HIGH)
{
digitalWrite(led, HIGH);
Serial.println("Stop...! Something is ahead");
}
else
{
digitalWrite(led, LOW);
Serial.println("Path is clear");
}
delay(100); //detect the presence of the object after every 0.1 second
}
IR Sensor Interfacing, display the o/p on Serial Monitor

/* void loop()
IR Proximity Sensor interface code {
Turns on an LED on when obstacle is if(digitalRead(ProxSensor)==LOW)
detected, else off. //Check the sensor output
*/ {
digitalWrite(13, HIGH); // set the LED on
const int ProxSensor=2; Serial.println("Stop something is ahead!!
"); //Message on Serial Monitor
void setup() }
{ else
// initialize the digital Serial port. {
digitalWrite(13, LOW); // set the LED off
Serial.begin(9600); Serial.println("Path is clear");
// initialize the digital pin as an output. //Message on Serial Monitor
pinMode(13, OUTPUT); }
pinMode(ProxSensor,INPUT); delay(1000); // wait for a second
} }
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:

You might also like