0% found this document useful (0 votes)
10 views56 pages

UNIT6

The document provides an overview of sensors, specifically PIR and ultrasonic sensors, and their applications with Arduino. It explains the working principles, circuit diagrams, and code examples for interfacing these sensors with Arduino, as well as details on controlling DC motors using an H-Bridge. Additionally, it covers Bluetooth technology, including the HC-05 and HC-06 modules, their specifications, and how to configure them using AT commands.

Uploaded by

Alan John
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views56 pages

UNIT6

The document provides an overview of sensors, specifically PIR and ultrasonic sensors, and their applications with Arduino. It explains the working principles, circuit diagrams, and code examples for interfacing these sensors with Arduino, as well as details on controlling DC motors using an H-Bridge. Additionally, it covers Bluetooth technology, including the HC-05 and HC-06 modules, their specifications, and how to configure them using AT commands.

Uploaded by

Alan John
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

SENSORS

AND
ARDUINO
Sensor
• A sensor is a device that detects and responds to
some type of input from the physical
environment.
• The specific input could be light, heat, motion,
moisture, pressure, or any one of a great number
of other environmental phenomena.
• The output is generally a signal that is converted
to human-readable display at the sensor location
or transmitted electronically over a network for
reading or further processing.
Sensors Example
Sensors we need to
learn
• PIR sensor--- Detecting Movement

• Ultrasonic Sensor--- Measuring Distance


PIR SENSOR
• PIR sensors allow you to sense motion, almost
always used to detect whether a human has
moved in or out of the sensors range.
• They are small, inexpensive, low-power, easy to
use and don't wear out.
• For that reason they are commonly found in
appliances and gadgets used in homes or
businesses.
• They are often referred to as PIR, "Passive
Infrared", "Pyroelectric", or "IR motion" sensors.
PIR Sensor
• PIRs are basically made of a pyroelectric sensor
(which you can see above as the round metal
can with a rectangular crystal in the center),
which can detect levels of infrared radiation.
• Everything emits some low level radiation, and
the hotter something is, the more radiation is
emitted.
• The PIR sensor itself has two slots in it, each slot is
made of a special material that is sensitive to IR.
• When the sensor is idle, both slots detect the same
amount of IR, the ambient amount radiated from the
room or walls or outdoors.
• When a warm body like a human or animal passes by,
it first intercepts one half of the PIR sensor, which
causes a positive differential change between the two
halves.
• When the warm body leaves the sensing area, the
reverse happens, whereby the sensor generates a
negative differential change. These change pulses are
what is detected.
Circuit Diagram
• Pin1 -VCC
• Pin 2- Output of PIR sensor connected to 2 pin of
arduino
• Pin 3-Ground
int ledPin = 13; // choose the pin for the LED

int inputPin = 2; // choose the input pin (for PIR


sensor)

int pirState = LOW; // we start, assuming no motion detected

int val = 0; // variable for reading the pin status

void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output

pinMode(inputPin, INPUT); // declare sensor as input

Serial.begin(9600);
}
void loop()
{
val = digitalRead(inputPin); // read input value
if (val == HIGH)
{ // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW)
{
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state

pirState = HIGH;
}
}
else
{
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){ // we have just turned of

Serial.println("Motion ended!"); // We only want


to print on the output change, not state
pirState = LOW;
}
}
}
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.
• 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.
• The Trig pin will be used to send the signal and
the Echo pin will be used to listen for returning
signal
Example- Distance Measurement

#define echoPin 7 // Echo Pin


#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() /* The following trigPin/echoPin
cycle is used to determine the distance of the
nearest object by bouncing soundwaves off of it. */
{
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed
of sound.
distance = duration/58.2;
Serial.print(distance);
delay(1000);}
Interfacing
DC Motor
With Arduino
DC Motor
• Working principle of a DC motor --A motor is an
electrical machine which converts electrical energy
into mechanical energy.
• The principle of working of a DC motor is that
"whenever a current carrying conductor is placed in
a magnetic field, it experiences a mechanical force".
• 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.
• To control the direction of the spin of DC motor,
without changing the way that the leads are
connected, you can use a circuit called an H-
Bridge.
• An H bridge is an electronic circuit that can drive
the motor in both directions.
• H-bridges are used in many different applications,
one of the most common being to control motors
in robots.
• It is called an H-bridge because it uses four
transistors connected in such a way that the
schematic diagram looks like an "H."
• You can use discrete transistors to make this circuit, but
for this lecture, we will be using the L293d/L298 H-Bridge
IC.
• In L293 all four input- output lines are independent, while
in L298, a half H driver cannot be used independently, full
H driver has to be used.
• Protective Diodes against back EMF are provided internally
in L293D but must be provided externally in L298.
• The L298 can control the speed and direction of DC motors
and stepper motors and can control two motors
simultaneously.
• Its current rating is 2A for each motor. At these currents,
however, you will need to use heat sinks.
L293D Motor Driver
IC
Controlling Speed of
DC Motor
• We will connect the Arduino to IN1 (pin 5), IN2
(pin 7), and Enable1 (pin 6) of the L293d IC. Pins
5 and 7 are digital, i.e. ON or OFF inputs, while
pin 6 needs a pulse-width modulated (PWM)
signal to control the motor speed.
const int pwm = 2 ; //initializing pin 2 as pwm
const int in_1 = 8 ;
const int in_2 = 9 ; //For providing logic to
L293d IC to choose the direction of the DC motor
void setup()
{
pinMode(pwm,OUTPUT) ; //we have to set PWM pin
as output
pinMode(in_1,OUTPUT) ; //Logic pins are also set as
output
pinMode(in_2,OUTPUT) ; }
void loop()
{
//For Clock wise motion , in_1 = High , in_2 = Low
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,LOW) ;
analogWrite(pwm,255) ;
delay(3000) ;
/*setting pwm of the motor to 255 we can change the
speed of rotaion by chaning pwm input but we are only
using arduino so we are using higest value to driver the
motor */
//For brake //For brake

digitalWrite(in_1,HIGH) ; digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(1000) ;
delay(1000) ; }
//For Anti Clock-wise motion
- IN_1 = LOW , IN_2 = HIGH

digitalWrite(in_1,LOW) ;
digitalWrite(in_2,HIGH) ;
delay(3000) ;
Bluetooth Technology
Introduction

Bluetooth is a wireless communication technology used
for exchange of data over short distances.

It is found in many devices ranging from mobile phones
and computers.

Bluetooth technology is a combination of both hardware
and software.

It is intended to create a personal area networks (PAN)
over a short range.

It operates in the unlicensed industrial, scientific and
medical (ISM) band at 2.4 to 2.485 GHz.

It uses a radio technology called frequency hopping
spread spectrum (FHSS).
FHSS
• Frequency hopping spread spectrum (FHSS) is a method
of transmitting radio signals by shifting carriers across
numerous channels with pseudorandom sequence
which is already known to the sender and receiver.
• Frequency hopping spread spectrum is defined in the
2.4 GHz band and operates in around 79 frequencies
ranging from 2.402 GHz to 2.480 GHz.
• Every frequency is GFSK modulated with channel width
of 1MHz and rates defined as 1 Mbps and 2 Mbps
respectively.
• Frequency hopping spread spectrum is a robust
technology with only very little influence from
reflections, noise and other environmental factors

It is a packet-based protocol with a master-slave
structure.

Each master can communicate with up to 7 slaves in
a piconet.

The range of Bluetooth depends upon the class of
radio using.

Class 3 radios have range up to 1 meter or 3 feet.

Class 2 radios have range of 10 meters or 33 feet.

Class 1 radios have range of 100 meters or 300
feet.

The most commonly used radio is Class 2.
Advantages and
Disadvantages
Advantages

The biggest advantage of using this technology is that there
are no cables or wires required for the transfer of data over
short ranges.

Bluetooth technology consumes less power when compared
with other wireless communication technologies.

For example Bluetooth technology using Class 2 radio uses
power of 2.5 mW.

As it is using frequency hopping spread spectrum radio
technology there is less prone to interference of data if the
other device also operates in the same frequency range.

Bluetooth doesn’t require clear line of sight between the
synced devices.
Disadvantage

Since it uses the greater range of Radio
Frequency (RF), it is much more open to
interception and attack.

It can be used for short range communications
only.

Although there are fewer disadvantages,
Bluetooth still remains best for short wireless
technology.
Introduction to HC-05
and HC-06 Modules
HC 05

HC-05 is a class 2 Bluetooth
module with Serial Port Profile
(SPP).

It can be configure either as
master or salve.

It is a replacement for wired
serial connection.

The module has two modes
of operation, Command Mode
where we can send AT
commands to it and Data
Mode where it transmits and
receives data to another
bluetooth module.

HC-05 Specification:
 Bluetooth Protocol : Bluetooth Specificatio v2.0 + EDR
 Frequency : 2.4 GHz ISM band
 Profiles : Bluetooth Serial Port
 Working Temperature : -20 to + 75 centigrade
 Power of emitting : 3 dBm

The pins on the module are:
 Vcc – Power supply for the module.
 GND – Ground of the module.
 TX – Transmitter of Bluetooth module.
 RX – Receiver of the module.
 Key – Used for the module to enter into AT command mode.

The default mode is DATA Mode, and this is the
default configuration, that may work fine for
many applications:
Baud Rate: 9600 bps, Data : 8 bits, Stop Bits: 1
bit, Parity : None, Handshake: None

Passkey: 1234

Device Name: HC-05
HC 06

HC-06 is a drop-in replacement for wired serial
connection.

This can be used as serial port replacement to
establish connection between PC and MCU
(Microcontroller).

This is a Slave Mode only Bluetooth device.

HC-06 offers encrypted connection

This module can be configured for baud rates
1200 to 115200 bps
Hooking up with
Arduino

The connections are:
HC-05 Arduino
Vcc -----------> 5V
GND -----------> GND
TX -----------> RX
RX -----------> TX
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
}

Let’s control the void loop() {
if(Serial.available())
LED on Arduino {
using a basic code ch=Serial.read();
if(ch=='h')
to send command {
digitalWrite(13,HIGH);
over Bluetooth. }
if(ch=='l')

The code is {
shown: }
digitalWrite(13,LOW);

}
}
Configuring HC-05
with AT commands

HC-05 can be configured i.e. its name and
password can be changed and many more using
AT commands.

It can also be configured as either master or
slave.

To set the HC-05 into AT command mode the
KEY pin of the module is made high i.e. it is
connected to either 5V or 3.3V pin of Arduino and
TX of Arduino is connected to TX of HC-05 and RX
of HC-05 to RX of Arduino.

To enter the AT commands open up the COM port of
the Arduino to which the Bluetooth module is
connected and set the baud rate to 38400.

After opening COM port if you enter command “AT”
(without quotes) it should return “OK” thus it can be
said that HC-05 entered into AT command mode.

Refer following link for detailed AT commands and its
descriptions:

http://www.linotux.ch/arduino/HC-
0305_serial_module_AT_commamd_set_201104_revis
ed.pdf
Popular AT
commands
1. AT
2. AT+RESET
3. AT+VERSION?
4. AT+ORGL
5. AT+ADDR?
6. AT+ ROLE?
7. AT+NAME?
8. AT+PSWD?
9. AT+UART?
10. AT+STATE?
11. AT+DISC
12. AT+BIND?
13. AT+RMAAD?
14. AT+ADCN?
15. AT+MRAD?
16. AT+PAIR
access AT command
mode
• Some bluetooth mode does not have KEY pin.
• In such module, different approach is considered:
• first connect the Bluetooth module to an Arduino
(Nano, UNO or whatever) using the following
connections…
• HC-05 GND –> Gnd
• HC-05 VCC –> +5V (initially disconnected) *
• HC-05 TX –> D2 (any pin as required)
• HC-05 RX –> D3 (any pin as required)
• STATE (output) and EN (input) are not
connected
• Follow these steps:
1. Disconnect the power to the HC-05 module.
2. Load the sketch to the Arduino
3. Depress the small reset button on the HC-05 module and
hold it down while you connect its Vcc pin to +5V.
• The red LED on the module (that would otherwise be blinking
quickly) will flash slowly indicating it is in AT mode.
• Open a serial monitor in the Arduino IDE (or any other
terminal software) and set its baud rate to 57600 and ensure
that on the IDE serial terminal, ensure “Both NL & CR” is
selected.
• You should then see the prompt “Enter AT commands: “.
• You can then type your AT commands into the terminal input
line and have them control the HC-05.
• Here are a few AT commands that I found useful:
• To ensure the unit is responding, enter AT. The unit should
respond OK
• To return the HC-05 to its default settings, enter AT+ORGL
• To see the version of your HC-05 enter AT+VERSION?
• To change the name of the device to AJCLOCK, for
example, enter AT+NAME=AJCLOCK
• To change the default security code (1234) to 2332 enter
AT+PSWD=2332
• To check baud rate, enter AT+UART? (my unit reset to
38400)
• To change baud rate to say, 115200, 1 stop bit, 0 parity,
enter AT+UART=115200,1,0

Code
#include <SoftwareSerial.h>
• SoftwareSerial btSerial(2, 3); // RX | TX
• void setup() {
• Serial.begin(57600);
• Serial.println("Enter AT commands:");
• btSerial.begin(38400); // HC-05 default speed in AT command more
• }
• void loop() {
• if (btSerial.available())
• Serial.write(btSerial.read());
• if (Serial.available())
• btSerial.write(Serial.read());
• }
Servo Motor
• A servo motor allows a precise control of the angular
position, velocity, and acceleration. It’s like you’re at
the steering wheel of your car.
• You control precisely the speed of the car and the
direction.
• These servos are essential parts if we need to control
the position of objects, rotate sensors, move arms and
legs, drive wheels and tracks, and more.
• Inside the micro servo, you will find the pieces
from the above image.
• The top cover hosts the plastic gears while the
middle cover hosts a DC motor, a controller, and
the potentiometer.
• The servo motor has three leads, with one more
than a DC motor.
• Each lead has a color code. So you have to
connect the brown wire from the micro servo to
the GND pin on the Arduino.
• Connect the red wire from the servo to the +5V
on the Arduino. And finally, connect the orange
wire from the SG90 servo to a digital pin (pin 9)
on the Arduino.
Sweep
#include <Servo.h>

Servo myservo; // create servo object to control a servo


// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Program
•#include <Servo.h> •void loop() {
•Servo myservo; // create servo • val = analogRead(potpin);
object to control a servo // reads the value of the
•int potpin = 0; // analog pin potentiometer (value between 0
used to connect the and 1023)
potentiometer • val = map(val, 0, 1023, 0,
•int val; // variable to read the 180); // scale it to use it with
value from the analog pin the servo (value between 0 and
•void setup() { 180)
• myservo.attach(9); // attaches •
the servo on pin 9 to the servo myservo.write(val); //
object sets the servo position according
to the scaled value
•}
• delay(15); //
waits for the servo to get there
•}

You might also like