Find fresh tutorials related to Arduino, Raspberry Pi, 555 Timer and many more microcontrollers. Learn something new everyday!



Showing posts with label Sensor. Show all posts
Showing posts with label Sensor. Show all posts

Wednesday, 31 December 2014

Detecting motion using HC-SR501 PIR Sensor [ARDUINO]




Hope you tried yesterday's LCD tutorial. Now, let's shift our attention from displays to sensors! Yup, I am talking about the PIR motion sensor!

Here's the scenario: You have some really sensible and emotional stuff in digitized form on your laptop (:O, I hope you try to understand what I wanna say! :P) which could get you in trouble if someone sees you with them! Now if you close the door and watch your stuff,  someone's surely gonna know that something's fishy. What if you had some motion detector stuff, that could alarm you when someone's at the corridor? That would give you enough time to save yourself!!!

Yup, were not talking about high tech "James Bond" stuff here! I am talking about a simple $2 motion detector, which is accurate enough to save your a**.

Whatever be the reason, the PIR sensor can be used in a variety of projects starting from home automation to presence detector. It's your call.

What this project does?
Detects motion and turns on an LED as long as the motion is detected. Also, prints "Motion Detected" on the serial monitor.

Let's Learn The Basics! 
According to Wikipedia,
  1. A passive infrared sensor (PIR sensor) is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view. They are most often used in PIR-based motion detectors.
Read more here: PIR Sensor

Now, the HC-SR501 PIR SENSOR, is a very famous sensor, available across all major E-Shopping sites.

My PIR Sensor looks like this:

My Favorite Sensor!



Shown below, is an under view of the sensor:
Copyrights: Adafruit
 PINOUT:

GND   >>  GROUND(0V)
OUT   >>  HIGH/LOW DATA OUT PIN
VCC   >>  5V

Materials Required:
1. A HC-SR501 PIR Motion Sensor
2. Arduino
3. Jumpers
4. One LED
5. A 220OHM (or equivalent) resistor
6. Breadboard
7. and of course, a PC with Arduino IDE installed!

Procedure:
Software part:
1. FIRST UPLOAD THE SKETCH WITHOUT CONNECTING ANY COMPONENTS ACROSS THE ARDUINO PINS

2. Don't power on the Arduino, yet.

Hardware part:
1. Make the connections as shown:


The Setup



Sketch:

/* 
 * ON PUBLIC DOMAIN
 * Author         : Nitish Dash
 * Name           : Detect motion with HC-SR501 and Arduino
 * Created        : 12/31/2014
 * Webpage        : http://goo.gl/qNoi9f
 * Author Email   : nitishdash95@gmail.com
 * Author Website : http://www.nitishdash.com/
 **** DONOT COPY AND PLAGIARATE WITHOUT THE AUTHOR'S PERMISSIONS ****
 */

int ct = 10;    //calibration time
int pirPin = 9;    //PIR sensor's output
int ledPin = 10;

void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);
  
  Serial.println("Please wait, now calibrating the sensor....");
    for(int i = 0; i <= ct; i++){
      Serial.print(((i*100)/ct));
      Serial.print("% ");
      Serial.println("DONE.....");
      delay(1000);
      }
    Serial.println("Calibration Succesfully Done.");
    Serial.println("** SENSOR ACTIVE **");
    delay(50);
  }

void loop(){
     if(digitalRead(pirPin) == HIGH)
     {
       digitalWrite(ledPin, HIGH);
       Serial.println("------------------------");
       Serial.println("** MOTION DETECTED **");
       Serial.println("------------------------");
       Serial.println("");
       delay(1000);   
   }
   
     else
     {
       digitalWrite(ledPin, LOW);   
     }
   
}


How this works (Algorithm):
You might be knowing that every body emits radiations. Hot bodies emit infrared too. Now, our PIR sensor basically works on the principle of thermal imaging. Every body leaves a heat signature. The PIR sensor, continuously scans the environment for any thermal change and then compares the values on its processor. If there is a change in the thermal state of its environment, it sets the OUT pin to HIGH. And if there is no change in the thermal state, it gives a LOW value to its OUT Pin.

Now, we have simply used this theory in out code. We have used an if-else condition to set the LED on or off according to the OUT PIN's voltage. Simple. :)

Video:


Downloads:
1. HC-SR501 Datasheet
2. sketch.ino

Something for you!
Try these ideas to enhance your knowledge and test yourself:

1. Try replacing the LED with a buzzer / piezo.
2. Try making a code that would send an SMS to your phone, whenever someone's at your home
3. Try making two circuits, one with the PIR and another with a buzzer and let them communicate via Bluetooth or FM(433Mhz module)


So, guys, there you go. I tried to explain the basics of a PIR sensor. In future projects, we'll go wireless and let the PIR transmit presence data even when you are travelling the world. We'll let arduino message us, whenever the sensor detects motion. We'll also make an android app, that would communicate with the Arduino via HC-05 Bluetooth module and send us data.

Bye! Thanks for reading the tutorial. Leave comments for sure!


Saturday, 27 December 2014

Using LDR to display light level on 16X2 LCD [ARDUINO]


So, wasn't the previous project interesting? I know it was! You might have noticed, that I am slowly, but steadily increasing the application of different sensors in real life. Also, I am making the code more complex. This is actually good because it will help you judge yourself.

Now, let's see what this project is all about.

What this project does?
Measure the ambient light level using an LDR and display it on the 16X2 LCD. Also show whether it's very dark, dark, bright and very bright according to some if else statements. Refer to the code to understand. We'll measure the light level in %. Out of a total of 1023 integers, 0 represents 0% and 1023 represents 100%.

Check this project out for the basic usage of 16X2 LCD.

Let's know the basics:
In the words of the great Wikipedia:
A photoresistor or light-dependent resistor (LDR) or photocell is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity.
And it looks somewhat like this:











Don't mistake it for a photocell. A photocell develops a potential difference across it's terminals when the ends are connected to an external load, thus behaving like a battery.
Contrary to the photocell is the LDR, as the name suggests, it changes it's resistance according to the light falling on it. More is the light, lesser is the resistance.


Materials Required:
1. An LDR [Photoresistor]
2. Arduino
3. Jumpers
4. One 16X2 LCD
5. Four 1K and One 10K resistor.
6. Breadboard
7. and of course, a PC with Arduino IDE installed!

Procedure:
Hardware part:
1. Make the connections as shown:






Software part:
1. Upload the sketch given below.
2. Power on the Arduino.

NOTE: ALWAYS UPLOAD THE SKETCH TO ARDUINO FIRST, THEN DISCONNECT THE ARDUINO AND THEN CONNECT ALL COMPONENTS!

Sketch:

/* 
 * ON PUBLIC DOMAIN
 * Author         : Nitish Dash
 * Name           : LDR Light Level Display on 16X2 LCD
 * Created        : 12/27/2014
 * Webpage        : http://goo.gl/Er9y4l
 * Author Email   : nitishdash95@gmail.com
 * Author Website : http://www.nitishdash.com/
 **** DONOT COPY AND PLAGIARATE WITHOUT THE AUTHOR'S PERMISSIONS ****
 */
 
#include 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() { //initialise for at least 2s
  lcd.begin(16, 2);
  lcd.print("INITIALISING");
  delay(500);
  lcd.print(".");
  delay(500);
  lcd.print(".");  
  delay(500);
  lcd.print(".");
  delay(500);
  lcd.print(".");  
  delay(500);
}
void loop()
{
 int sensorValue = analogRead(A0);
 double dV = sensorValue;
 double le = (dV/1023)*100;
 int level = le;
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("LIGHT LEVEL:");
 lcd.print(level);
 lcd.print("%");
 lcd.setCursor(0, 1);
 
 if ((level >= 0) && (level <= 5))
 {
  lcd.print("VERY DARK"); 
 }
 else if ((level > 5) && (level <= 10))
 {
  lcd.print("DARK"); 
 }
 else if ((level > 10) && (level <= 50))
 {
  lcd.print("BRIGHT"); 
 }
 else 
 {
  lcd.print("VERY BRIGHT"); 
 }
 
 delay(500); 
}

NOTE: PLEASE REMOVE LINE 60 BEFORE UPLOADING THE SKETCH.

How this works (Algorithm):
OK, let's start from ground zero. The analog input pins A0 to A5 on the Arduino UNO are capable of taking the signal and printing the voltage across it to the serial monitor. For example, if I connect the A0 pin to GND pin of the Arduino, by using a simple code, I can see in the Serial Monitor a value of 0. And when I connect the A0 to 5V pin, I can see a reading of 1023. So, simply put, when the A0 is given a voltage of 5V(HIGH SIGNAL) it corresponds to a value of 1023(MAX) and when it's given a voltage of 0V(LOW), it corresponds to 0. So, the analog pin can measure any voltage between 0-5V and it send a value between 0-1023 to the arduino for it to understand. So this means, by developing any voltage on this pin, you can measure a value between 0-1023.

This concept was used in the project. Since an LDR changes it's resistivity according to change in the amount of light it is being subjected to. So, it can increase or decrease the flow of electric current though the circuit. And as we know, potential drop across any component is V=IR (where I is the total current in the circuit, we can change the potential drop across the LDR. To limit the current, we've used a 1K resistor. To change the sensitivity, you may change the value of resistance.



Something for you!
Try these ideas to enhance your knowledge and test yourself:
1. Use both the DHT11 sensor and LDR to display Light Level and Temperature
2. Use the LDR to display direct values from 0-1023 on lcd.



Measuring Distance with HC-SR04 Ultrasonic Module on 16X2 LCD PART-2 [ARDUINO]



So, I hope you might have got the idea of the working of the HC-SR04 Ultrasonic Distance Module. If not, read the part - 1 of the tutorial here: Measuring Distance with HC-SR04 Ultrasonic Ping Sensor and Arduino PART-1 [ARDUINO]

Also, I would recommend you to check out the previous article which used an LCD to display the temperature and humidity. Because I haven't changed the LCD connection. I only changed the HC-SR04 connection scheme.

Well, this is a simple continuation of the the tutorial. In the previous part we used a serial monitor to see the distance. This is quite inconvenient if you want to make the project more portable and small. Also, at many times, you'll need to show data on the 16X2 LCD. This project will be quite helpful to you t see how the data is displayed on the 16X2 LCD.

What this project does?
Calculate the distance of the object from the sensor and print the distance in centimeters on an 16X2 LCD.

I'm skipping the "Let's Know The Basics" part, because in the beginning I've already mentioned the links to previous posts. I've mentioned everything in those 2 posts.

Materials Required:
1. An HC-SR04 Ultrasonic Ping Sensor
2. Arduino
3. Jumpers
4. One 16X2 LCD
5. Three 1K and one 10K resistor.
6. Breadboard
7. and of course, a PC with Arduino IDE installed!

Procedure:
Hardware part:
1. Make the connections as shown:


REFERENCE:



Software part:
1. Upload the sketch given below.
2. Power on the Arduino.


Sketch:

/* 
 * ON PUBLIC DOMAIN
 * Author         : Nitish Dash
 * Name           : Measuring Distance with HC-SR04 on 16X2 LCD 
 * Created        : 12/27/2014
 * Webpage        : http://goo.gl/qcpuCa
 * Author Email   : nitishdash95@gmail.com
 * Author Website : http://www.nitishdash.com/
 **** DONOT COPY AND PLAGIARATE WITHOUT THE AUTHOR'S PERMISSIONS ****
 */
 
#include 
#define trig 10
#define echo 9
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() { //initialise for at least 2s
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  lcd.begin(16, 2);
  lcd.print("INITIALISING");
  delay(500);
  lcd.print(".");
  delay(500);
  lcd.print(".");  
  delay(500);
  lcd.print(".");
  delay(500);
  lcd.print(".");  
  delay(500);
}
void loop()
{
  long time, dist;
  digitalWrite(trig, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trig, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trig, LOW);
  time = pulseIn(echo, HIGH);
  dist = ((time/2) / 29.1); //measure in cms
 if (dist >= 200 || dist <= 0)
 {
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Distance: ");
   lcd.setCursor(0, 1);
   lcd.print("Out of range!");
 }
 else 
 {
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Distance: ");
   lcd.setCursor(0, 1);
   lcd.print((int)dist);
   lcd.print(" cms");
  }
 delay(700); //modify but don't go below 500 ms
}

NOTE:Please remove the line 61 before uploading.

How this works (Algorithm):
Refer to the previous two posts here to know more:
1. Measuring Distance with HC-SR04 Ultrasonic Ping Sensor and Arduino PART-1 [ARDUINO]
2. Temperature and Humidity display on 16X2 LCD with DHT11 Sensor [ARDUINO]

Video:
Coming soon.....

Downloads:

Something for you!

Try these ideas to enhance your knowledge and application:
1. Display the distance in feet and inches
2. Combine Part-1 with Part-2 and glow a red led when distance is less than 20cms.


Friday, 26 December 2014

Temperature and Humidity display on 16X2 LCD with DHT11 Sensor [ARDUINO]



Hello. Recently I purchased a couple of sensors to use with  my Arduino. One of them is the DHT11 Temperature and Humidity sensor. This sensor is a cheap one. I got it in India for around $1.5. I'll mention the links if you want.

What this project does?
Displays the temperature (in Centigrade) and relative humidity (in %) received from the DHT11 sensor on the 16X2 LCD.

Let's know the basics!
"DHT11 Temperature & Humidity Sensor features a temperature & humidity sensor complex with a calibrated digital  signal output. By using  the exclusive digital-signal-acquisition technique  and  temperature  &  humidity  sensing  technology,  it  ensures  high  reliability and excellent  long-term  stability.  This  sensor  includes  a  resistive-type  humidity  measurement component  and  an  NTC  temperature  measurement  component,  and  connects  to  a  high-performance  8-bit microcontroller,  offering  excellent  quality,  fast  response,  anti-interference ability and cost-effectiveness."

Source: http://www.exp-tech.de/

SPECS:
Relative humidity
Resolution: 16Bit
Repeatability: ±1% RH
Accuracy: At 25℃ ±5% RH
Interchangeability: fully interchangeable
Response time: 1 / e (63%) of 25℃ 6s
1m / s air 6s
Hysteresis: <± 0.3% RH
Long-term stability: <± 0.5% RH / yr in
Temperature
Resolution: 16Bit
Repeatability: ±0.2℃
Range: At 25℃ ±2℃
Response time: 1 / e (63%) 10S
Electrical Characteristics
Power supply: DC 3.5~5.5V
Supply Current: measurement 0.3mA standby 60μ A
Sampling period: more than 2 seconds

Pin Description:
1. VCC power supply 3.5~5.5V DC
2. DATA serial data, a single bus
3. NC, empty pin
4. GND ground, the negative power



From now on, forget about the pin 3.

Materials Required:
1. A DHT11 Sensor
2. Arduino
3. Jumpers
4. One 16X2 LCD
5. Three 1K and one 10K resistor.
6. Breadboard
7. and ofcourse, a PC with Arduino IDE installed!


Procedure:
Hardware part:
1. Connect the components as shown.


REFERENCE:
[Click to enlarge]






Software part:
1. Download this library: DHT
2. Extract the zip and you'll get a folder named: "DHT"
3. Copy this folder to [My Documents/Arduino/libraries]
4. Upload the sketch given below.
5. Power on the Arduino.

Sketch:

/* 
 * ON PUBLIC DOMAIN
 * Author         : Nitish Dash
 * Name           : Temperature and Humidity on 16X2 LCD with DHT11 Sensor
 * Created        : 12/26/2014
 * Webpage        : http://goo.gl/W5iB30
 * Author Email   : nitishdash95@gmail.com
 * Author Website : http://www.nitishdash.com/
 **** DONOT COPY AND PLAGIARATE WITHOUT THE AUTHOR'S PERMISSIONS ****
 */
 
#include 
#include 
#define dht_dpin A0
dht DHT;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//this section is for the symbol of degree
byte deg[8] = {
 0b01110,
 0b01010,
 0b01110,
 0b00000,
 0b00000,
 0b00000,
 0b00000,
 0b00000
};

void setup() { //initialise for at least 2s
  lcd.begin(16, 2);
  lcd.print("INITIALISING");
  delay(500);
  lcd.print(".");
  delay(500);
  lcd.print(".");  
  delay(500);
  lcd.print(".");
  delay(500);
  lcd.print(".");  
  delay(500);
  lcd.createChar(0, deg);
}
void loop()
{
 DHT.read11(dht_dpin); 
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print(" TEMP    : ");
 lcd.print((int)DHT.temperature);
 lcd.write((uint8_t)0);
 lcd.print("C");
 lcd.setCursor(0, 1);
 lcd.print(" HUMIDITY: ");
 lcd.print((int)DHT.humidity);
 lcd.print(" %");
 delay(1000); //modify but don't delay for less than 1s
}

NOTE: Please remove the line 58 from the code while uploading.
How this works (Algorithm):
The arduino receives the analog signal from the DHT11 sensor via A0 pin. The received signal is interpreted and processed by the library. The arduino then prints the message and measurement of temperature and relative humidity every 1s.

To test the sensor, try touching the sensor.
NOTE: Keep the sensor in normal conditions. Don't use it in excessively humid atmosphere or subject it to direct sunlight.

Video:
Coming soon.....

Downloads:
1. sketch.ino
2. Breadboard schematics
5. DHT11 Library

Any doubts? Feel free to leave a comment.

Wednesday, 17 December 2014

Measuring Distance with HC-SR04 Ultrasonic Ping Sensor and Arduino PART-1 [ARDUINO]



Hope you guys enjoyed making the last project. So, today my HC-SR04 Ping sensor arrived via courier which I had ordered 2 days back. Opened it up and there was this shiny flashing (not really!) 2 eyed thing.
See for yourself.


What this project does?
Measure the distance of a distant object using the ultrasonic ping sensor. Then, light up the red led if the distance between sensor and the object is 12cms or less. And green led remains on if the distance is more than 12cms. Also, it will print the reading on the serial monitor every 0.4 seconds.
But, in this first part, we'll use LEDs to notify us the proximity. In part 2, we'll use an LCD to directly display the values.

Let's know the basics!
So, let's discuss a little bit about this sensor. So, as the title itself suggests, this is a ping sensor or in simple form, a tiny little SONAR. With an arduino, you can harness the power of this tiny sonar, which is surprising me everyday. Some quick facts:


The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1” to 13 feet. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.

Features:
  • Power Supply :+5V DC
  • Quiescent Current : <2mA
  • Working Currnt: 15mA
  • Effectual Angle: <15°
  • Ranging Distance : 2cm – 400 cm/1" - 13ft
  • Resolution : 0.3 cm
  • Measuring Angle: 30 degree
  • Trigger Input Pulse width: 10uS
  • Dimension: 45mm x 20mm x 15mm



Parameter
Min
Typ.
Max
Unit
Operating Voltage
4.50
5.0
5.5
V
Quiescent Current
1.5
2
2.5
mA
Working Current
10
15
20
mA
Ultrasonic Frequency
-
40
-
kHz

So, hope you have got a quick idea of this device and the power it is packed with.

Now, our common algorithm follows!:

Materials Required:
1. An HC-SR04 Ultrasonic Ping Sensor
2. Arduino
3. Jumpers
4. Two 560 OHM resistors
5. One green led and one red led
6. Breadboard

Procedure:
1. Make the connections as shown:



2. Upload sketch

3. Start the serial monitor and check the runtime distance measurements

4. Bring an object close and test the project. If the red led glows, you're done with the project!


Sketch:

/*
 HC-SR04 Ultrasonic Distance Sensor
 CODED BY: Nitish Dash [nitishdash95@gmail.com]
 More info at: http://eweekend.blogspot.com
 */

#define trig 13
#define echo 12
#define red 11
#define green 10

void setup() {
  Serial.begin (9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
}

void loop() {
  long time, dist;
  digitalWrite(trig, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trig, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trig, LOW);
  time = pulseIn(echo, HIGH);
  dist = ((time/2) / 29.1); //measure in cms
  if (dist < 12) //change this to set proximity distance 
  
{  
    digitalWrite(red,HIGH); 
  digitalWrite(green,LOW);
}
  else {
    digitalWrite(red,LOW);
    digitalWrite(green,HIGH);
  }
  if (dist >= 200 || dist <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(dist);
    Serial.println(" cm");
  }
  delay(400);
}

How this works (Algorithm):
The timing diagram of HC-SR04 is shown. To start measurement, Trig of SR04 must receive a pulse of high (5V) for at least 10us, this will initiate the sensor will transmit out 8 cycle of ultrasonic burst at 40kHz and wait for the reflected ultrasonic burst. When the sensor detected ultrasonic from receiver, it will set the Echo pin to high (5V) and delay for a period (width) which proportion to distance. To obtain the distance, measure the width (Ton) of Echo pin.

>>Time = Width of Echo pulse, in uS (micro second)
>>Distance in centimeters = Time / 58
>>Distance in inches = Time / 148
>>Or you can utilize the speed of sound, which is 340m/s



Video:
Coming soon.....

Downloads:
1. Sketch.ino
2. Breadboard schematics


Homework for you!

Try these ideas to enhance your knowledge and application:
1. Add another yellow led and modify the code for testing three conditions and glow the three leds as per the distance
2. Add a speaker and change it's tone as and when the distance changes.
3. Add this near your door and turn on an alarm if someone opens the door.

Stay tuned for the next part, where we will print the readings directly to a 16X2 LCD! So much fun!

UPDATE: Guys, part-2 is online now. Check it out here: http://electro.nitishdash.com/2014/12/7-measuring-distance-with-hc-sr04.html

Any doubts? Feel free to leave a comment.



Upcoming projects!

1. Programming ESP8266 using NodeMCU and LUA Scripts

2. IoT Applications

3. NRF42L01 2.4 GhZ module interfacing with arduino

4. ESP8266 - Everything cool about it!

Language ain't any barrier!

Get Updates!

Enter your email address:


Get the latest projects delivered into your inbox, hot!