You are on page 1of 17

Arduino - Ultrasonic Sensor

The HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an


object just like the bats do. It offers excellent non-contact range detection with
high accuracy and stable readings in an easy-to-use package from 2 cm to 400
cm or 1” to 13 feet.
It comes complete with ultrasonic transmitter and receiver module.
Components Required
You will need the following components −

 1 × Breadboard

 1 × Arduino Uno R3

 1 × ULTRASONIC Sensor (HC-SR04)

Procedure
Follow the circuit diagram and make the connections as shown in the image
given below.

Sketch
Open the Arduino IDE software on your computer. Coding in the Arduino
language will control your circuit. Open a new sketch File by clicking New.

Arduino Code
const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor

const int echoPin = 6; // Echo Pin of Ultrasonic Sensor


void setup() {

Serial.begin(9600); // Starting Serial Terminal

void loop() {

long duration, inches, cm;

pinMode(pingPin, OUTPUT);

digitalWrite(pingPin, LOW);

delayMicroseconds(2);

digitalWrite(pingPin, HIGH);

delayMicroseconds(10);

digitalWrite(pingPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

inches = microsecondsToInches(duration);

cm = microsecondsToCentimeters(duration);

Serial.print(inches);

Serial.print("in, ");

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(100);

long microsecondsToInches(long microseconds) {

return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {

return microseconds / 29 / 2;

Code to Note
The Ultrasonic sensor has four terminals - +5V, Trigger, Echo, and GND
connected as follows −

 Connect the +5V pin to +5v on your Arduino board.

 Connect Trigger to digital pin 7 on your Arduino board.

 Connect Echo to digital pin 6 on your Arduino board.

 Connect GND with GND on Arduino.

In our program, we have displayed the distance measured by the sensor in


inches and cm via the serial port.

Result
You will see the distance measured by sensor in inches and cm on Arduino
serial monitor.
Audrino and PIR sensor

Pinout
 GND – connect to ground

 OUT – connect to an Arduino digital pin

 5V – connect to 5V

Parts required
 1x PIR Motion Sensor (HC-SR501)
 Arduino UNO – read Best Arduino Starter Kits
 1x LED
 Jumper Cables

CIRCUIT

SKETCH

/*  
    Arduino with PIR motion sensor
*/
 
int led = 13;             // the pin that the LED is atteched to
int sensor = 2;       // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;      // variable to store the sensor status (value)

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  Serial.begin(9600);        // initialize serial
}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds
  
    if (state == LOW) {
      Serial.println("Motion detected!");
      state = HIGH;       // update variable state to HIGH
  }
 }
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds
   
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
  }
 }
}
Arduino Motion Detector using PIR Sensor
Detecting motions or movements has always been important in most projects. With the help of the
PIR Sensor it has become very easy to detect human/animal movements. In this project we will
learn how we can interface a PIR Sensor with a microcontroller like Arduino. We will interface
an Arduino with PIR module and blink a LED and beep a Buzzer whenever a movement is
detected
Components Required
1. PIR Sensor Module
2. Arduino UNO (any version)
3. LED
4. Buzzer
5. Breadboard
6. Connecting Wires
7. 330 ohm resistor
Description:
PIR sensor:  
The PIR sensor stands for Passive Infrared sensor. It is a low cost sensor which can detect the
presence of Human beings or animals. There are two important materials present in the sensor one
is the pyroelectric crystal which can detect the heat signatures from a living organism
(humans/animals)   and the other is a Fresnel lenses which can widen the range of the sensor. 

the module work in two different modes. One is the “H” mode and the other is the “I” mode.
In “H” mode the output pin Dout will go high (3.3V) when a person is detected within range and goes
low after a particular time (time is set by potentiometer). In this mode the output pin will go high
irrespective of whether the person is still present inside the range or has left the area. We are using
our module in “H” mode in our project.
 
In “I” mode the output pin Dout will go high (3.3V) when  a person is detected within range and will
stay high as long as he/she stays within the limit of the Sensors range. Once the person has left the
area the pin will go low after the particular time
Components needed
HC-SR501 PIR Sensor Module
5V Relay Module
Arduino Board 
Breadboard and Jump Wires …
Cable, Plug, Socke

Circuit Diagram and Explanation:


The circuit Diagram for arduino motion detector project by interfacing Arduino with PIR module
and blinking an LED/Buzzer is shown in the below image.

We have powered the PIR sensor using he 5V Rail of the Arduino. The output pin of the PIR Sensor
is connected to the 2nd digital pin of Arduino. This pin will be the INPUT pin for Arduino. Then the
3rd pin of Arduino is connected to the LED and Buzzer. This pin will act as the output pin of the
Arduino. We will program the Arduino to trigger an Output on 3rd pin if an Input has been detected at
2nd pin. The complete Program is explained below.
Programming the Arduino:
The program for Arduino is pretty simple and straight forward. To connect Arduino PIR Sensor, we
have to assign the pin number 2 as input and pin number 3 as output. Then we have to produce a
discontinuous trigger whenever the pin 2 goes high. Each line is explained below.
In the void setup function shown below, we have to declare that the pin 2 connected to PIR output
will be used as input and the pin 3 connected to LED/Buzzer will be used as input.

Then we proceed to the loop() function. As we know the code in here gets executed as long as the
MCU is powered on. So we always check if the Pin 2 has gone high by using the below line inside
the loop() function.
If we find that the particular pin has gone high, it means that the PIR module has be triggered. So,
now we has make our output pin (pin 3) to go high. We turn this pin on and off with a delay of 100
milli second so that we can attain the flashing or buzzing output. The code to do the same is shown
below.
void setup() {
  pinMode(2, INPUT); //Pin 2 as INPUT
  pinMode(3, OUTPUT); //PIN 3 as OUTPUT
}
void loop() {
  if (digitalRead(2) == HIGH)
 {
  digitalWrite(3, HIGH);   // turn the LED/Buzz ON
  delay(100);                       // wait for 100 msecond 
  digitalWrite(3, LOW);   // turn the LED/Buzz OFF
  delay(100);                       // wait for 100 msecond 
 }
}

Now, power on the Arduino and wait for about 50-60 seconds for your PIR sensor to calibrate. Do
not be frustrated by the output that you get during this period. After that, try moving in front of the
PIR sensor and you LED/Buzzer should be triggered as shown in the video below
Arduino Soil Moisture Sensor
When you hear the word Smart Garden, one of the things that pop up to your
mind is the automatic measurement of the moisture content of the soil. If
you're building a Smart Garden that waters plants automatically and give you the
readings of the wetness of the soil, then you will definitely need a Soil Moisture
Sensor.
COMPONENTS REQUIRED

about the Soil Moisture Sensor


A typical Soil Moisture Sensor consist of two components. A two
legged Lead, that goes into the soil or anywhere else where water
content has to be measured. This has two header pins which connect
to an Amplifier/ A-D circuit which is in turn connected to the Arduino.
The Amplifier has a Vin, Gnd, Analog and Digital Data Pins. This
means that you can get the values in both Analog and Digital forms.
The soil Moisture sensor FC-28 has four pins

 VCC: For power


 A0: Analog output
 D0: Digital output
 GND: Ground

Circuit Diagram
The connections for connecting the soil moisture sensor FC-28 to the Arduino are as
follows.

 VCC of FC-28 to 5V of Arduino


 GND of FC-28 to GND of Arduino
 A0 of FC-28 to A0 of Arduino

int sensor_pin = A0;


int output_value ;

void setup() {

   Serial.begin(9600);

   Serial.println("Reading From the Sensor ...");

   delay(2000);

   }

void loop() {

   output_value= analogRead(sensor_pin);

   output_value = map(output_value,550,0,0,100);
   Serial.print("Mositure : ");

   Serial.print(output_value);

   Serial.println("%");

   delay(1000);

   }

How Does the Sensor Work

Most soil moisture sensors are designed to estimate soil volumetric


water content based on the dielectric constant (soil bulk
permittivity) of the soil. The dielectric constant can be thought of as
the soil's ability to transmit electricity. The dielectric constant of soil
increases as the water content of the soil increases. This response is
due to the fact that the dielectric constant of water is much larger than
the other soil components, including air. Thus, measurement of the
dielectric constant gives a predictable estimation of water
content.
Digital Mode – Interfacing Arduino and Soil Moisture
Sensor

Circuit Diagram
The connections for connecting the soil moisture sensor FC-28 to the Arduino in digital
mode are as follows.

 VCC of FC-28 to 5V of Arduino


 GND of FC-28 to GND of Arduino
 D0 of FC-28 to pin 12 of Arduino
 LED positive to pin 13 of Arduino
 LED negative to GND of Arduino

int led_pin =13;

int sensor_pin =8;


void setup() {

  pinMode(led_pin, OUTPUT);

  pinMode(sensor_pin, INPUT);

void loop() {

  if(digitalRead(sensor_pin) == HIGH){

    digitalWrite(led_pin, HIGH);

  } else {

    digitalWrite(led_pin, LOW);


    delay(1000);

  }

You might also like