You are on page 1of 9

Introduction to

Engineering

1
Outline

Sensors
1. Ultrasonic Distance Sensor

2. Passive Infrared Sensor (PIR)

2
Ultrasonic Distance Sensor
(HC-SR04 sensor)

3
Ultrasonic sensor: HC-SR04
 The HC-SR04 ultrasonic distance sensor estimates distance by transmitting (sensor T) an ultrasonic sound
wave and measuring the time taken to receive (sensor R) the echo.

 The distance, in centimeters, between the sensor and the target point is half the echo time, measured in
microseconds (10^-6), multiplied by 0.0343, assuming a speed of sound of 343m/s = 34300cm/s.

 The minimum and maximum measureable distances are 2cm and 4m, respectively.

4
Ultrasonic sensor: HC-SR04
To initiate the ultrasonic distance sensor:
1. The trigger pin is held HIGH for at least 10μs.

2. The sensor then sends out an eight-cycle signal at 40kHz with the pulseIn() function, automatically setting the echo
pin to HIGH, and waits for the signal to return, when the echo pin is set to LOW.

3. The time interval between the echo pin changing from HIGH to LOW is the echo time. If the echo pin is HIGH when
the pulseIn() function is called, the pulseIn() function waits until the echo pin is set to LOW and then to HIGH before
timing the signal.

For this, we force both signals (trigger and echo) to zero before starting the process.

11
12

5
Ultrasonic sensor: HC-SR04 Code
int trigPin = 6; // HC-SR04 trigger pin
int echoPin = 7; // HC-SR04 echo pin
float duration, distance;
void setup()
{ Serial.begin(9600); // define Serial output baud rate
pinMode(trigPin, OUTPUT); // define trigger pin as output
}
void loop()
{ digitalWrite(echoPin, LOW); // set the echo pin LOW
digitalWrite(trigPin, LOW); // set the trigger pin LOW
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // set the trigger pin HIGH for 10μs
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // measure the echo time (μs)
distance = (duration/2.0)*0.0343; // convert echo time to distance (cm)
if(distance>400 || distance<2)
Serial.println("Out of range");
else
{ Serial.print("Distance : ");
Serial.print(distance, 1);
Serial.println(" cm"); } }
6
Passive Infrared Sensor
(HR-SC501 PIR sensor)

7
PIR sensor: HR-SC501
 All objects with a temperature above absolute zero emit heat energy in the form of infrared radiation.

 The HR-SC501 passive infrared (PIR) sensor converts the infrared radiation into an output voltage.

 The PIR sensors are used in motion detector alarms.

 The PIR sensor requires up to 60s to stabilize after switching on and the output stays HIGH for a minimum
of 2.5s after movement is detected.

 The time delay (Tx) and the sensor sensitivity (Sx) are increased by turning clockwise the appropriate
potentiometer on the side of the module. Smaller movements are detected with high sensitivity with a
distance range between 3m and 7m.

8
PIR sensor: HR-SC501 Code
int PIRpin = 11; // PIR sensor pin
int LEDpin = 8; // LED pin
int PIRstate = LOW; // set PIR state to LOW
int reading;
unsigned long detectTime; // time lag on PIR sensor
float moveTime;
void setup()
{ Serial.begin(9600); // set Serial Monitor baud rate
pinMode(LEDpin, OUTPUT); // LED pin as output
}
void loop()
{ reading = digitalRead(PIRpin); // read PIR pin
if (reading == HIGH && PIRstate == LOW) // PIR detected new
{ // movement
Serial.print("New movement detected"); // print to Serial Monitor
detectTime = millis(); // time of movement
PIRstate = HIGH; // update PIR state to HIGH
digitalWrite(LEDpin, PIRstate); // turn LED on
} // movement no longer detected
else if (reading == LOW && PIRstate == HIGH)
{ moveTime = millis() - detectTime; // duration of movement
moveTime = moveTime/1000.0;
Serial.print(" and lasted for "); // print to Serial Monitor
Serial.print(moveTime,1); // print detect time (s) with 1DP
Serial.println(" seconds"); // print text with a new line
PIRstate = LOW; // update PIR state to LOW
digitalWrite(LEDpin, PIRstate); // turn LED off
}
9 }

You might also like