pir sensor arduino setup

How to Set Up a PIR Sensor with Arduino

By Hannah Clark
AD

Embarking on a journey into the world of Arduino and sensors? The DHT22, a popular humidity and temperature sensor, is a great place to start, and configuring it with Arduino is an intuitive process. In this guide, we'll walk you through the steps to set up a DHT22 sensor with an Arduino board, ensuring you're well-equipped to dive into your next IoT project.

The DHT22 sensor offers precise measurements and is user-friendly, making it an excellent choice for beginners and experienced enthusiasts alike. By the end of this article, you'll have a fully functional DHT22-Arduino setup, ready to collect and monitor environmental data.

Hardware Requirement and Connections

The DHT22 sensor and an Arduino board are the primary hardware components you'll need. For this guide, we'll use the Arduino Uno, a versatile and popular board.

AD
the instructions for how to use an arduino pir and motion sensor with arduino
the instructions for how to use an arduino pir and motion sensor with arduino
Arduino - Czujnik ruchu PIR - Tranzystor.pl - Vortal Elektroniczny
Arduino - Czujnik ruchu PIR - Tranzystor.pl - Vortal Elektroniczny
How to Use a PIR Sensor for Motion sensing using Arduino
How to Use a PIR Sensor for Motion sensing using Arduino

Let's establish the connections between the DHT22 and Arduino:

  • VCC (Pin 1 of DHT22) connects to 3.3V on the Arduino.
  • GND (Pin 2) connects to GND on the Arduino.
  • Data (Pin 3) connects to an analog or digital pin on the Arduino. For this guide, we'll use Pin 2.

Note on Pin Assignment

The DHT22 is sensitive and should be connected to a 3.3V pin on the Arduino. If your Arduino board does not offer 3.3V, you can use a voltage divider or a level shifter to adjust the signal to the appropriate level.

How to Setup Passive Infrared (PIR) Motion Sensors on the Arduino
How to Setup Passive Infrared (PIR) Motion Sensors on the Arduino
the instructions for how to make an automatic room light using arduino and pir sensor
the instructions for how to make an automatic room light using arduino and pir sensor
Arduino Projects: PIR Motion Sensor - Tutorial45
Arduino Projects: PIR Motion Sensor - Tutorial45

Moreover, connecting the DHT22 to an Analog pin (A0 to A5) can improve performance, as it allows the sensor to send more data.

Arduino Code and Library

To read data from the DHT22, the Arduino Adafruit DHT library simplifies the process. This library supports various DHT sensors, including DHT11 and DHT22.

AD

Let's install the library, write code to read temperature and humidity data, and display it in the serial monitor.

Trigger Sound Effects with Arduino & PIR Motion Sensor
Trigger Sound Effects with Arduino & PIR Motion Sensor
PIR Motion Sensor on Raspberry Pi Pico Using MicroPython
PIR Motion Sensor on Raspberry Pi Pico Using MicroPython
How PIR Sensor Works and How To Use It with Arduino
How PIR Sensor Works and How To Use It with Arduino
How to Wire a PIR Motion Sensor to Arduino: Wiring Diagram
How to Wire a PIR Motion Sensor to Arduino: Wiring Diagram
HC-SR505 Mini PIR Motion Sensor with Arduino
HC-SR505 Mini PIR Motion Sensor with Arduino
Arduino Basics: PIR Sensors
Arduino Basics: PIR Sensors

Installing Adafruit DHT Library

In the Arduino IDE, navigate to Sketch -> Include Library -> Manage Libraries. Search for 'Adafruit DHT' and click Install.

The library will be added to your system, and you can verify it by importing an example sketch from File -> Examples -> Adafruit DHT.

AD

Writing Arduino Code for DHT22

Now, let's write code to read temperature and humidity data using the Adafruit DHT library:

ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
HC-SR501 PIR Sensor interfacing with the Arduino
HC-SR501 PIR Sensor interfacing with the Arduino
Arduino with PIR Motion Sensor | Random Nerd Tutorials
Arduino with PIR Motion Sensor | Random Nerd Tutorials
How to Use Pir Sensor Without Arduino
How to Use Pir Sensor Without Arduino
Arduino - Mini alarm PIR HC-SR501 - Tranzystor.pl - Vortal Elektroniczny
Arduino - Mini alarm PIR HC-SR501 - Tranzystor.pl - Vortal Elektroniczny
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
PIR Sensor Arduino Alarm
PIR Sensor Arduino Alarm
Coming Soon
Coming Soon
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
a man wearing red glasses and holding a light up device in front of him with the caption arduro basics pir sensor
a man wearing red glasses and holding a light up device in front of him with the caption arduro basics pir sensor
Infrared Sensor Tutorial for Arduino, ESP8266 and ESP32
Infrared Sensor Tutorial for Arduino, ESP8266 and ESP32
PIR motion sensor tutorials | security system and motion sensor light
PIR motion sensor tutorials | security system and motion sensor light
Arduino Projects: PIR Motion Sensor - Tutorial45
Arduino Projects: PIR Motion Sensor - Tutorial45
  1. Include the necessary libraries:
    #include 
  2. Define the sensor type (DHT22) and data pin:
    #define DHTPIN 2
        #define DHTTYPE DHT22
        DHT dht(DHTPIN, DHTTYPE);
  3. Set up the sensor in the setup function:
    void setup() {
        dht.begin();
        }
  4. Read temperature and humidity in the loop function, then display it in the serial monitor:
    void loop() {
        delay(2000);
        float h = dht.readHumidity();
        float t = dht.readTemperature();
    
        if (isnan(h) || isnan(t)) {
          Serial.println("Failed to read from DHT sensor!");
          return;
        }
    
        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.print(" %\t");
    
        Serial.print("Temperature: ");
        Serial.print(t);
        Serial.println(" °C");
        }

Upload this sketch to your Arduino board, open the serial monitor (Ctrl+Shift+M), and watch the temperature and humidity data roll in.

With your DHT22 sensor set up with Arduino, you're now equipped to dive into more advanced projects, such as storing data in the cloud, creating self-watering plants, or monitoring environmental conditions in various settings. Happy coding!

AD
AD