Jump to content

MDM331AID/Experiment 1

From cognifront

Experiment 1: Develop an Application for the Connectivity of the Arduino UNO/Raspberry Pi Circuit with Sensors

Course: MDM331AID – Internet of Things

Program: TE Artificial Intelligence and Data Science Engineering

University: Savitribai Phule Pune University


1. Aim

To develop applications for interfacing sensors with Arduino UNO and Raspberry Pi and to acquire, process and display sensor data.

2. Objectives

  • Understand sensor interfacing with Arduino UNO and Raspberry Pi.
  • Interface a DHT11 temperature and humidity sensor with Arduino UNO.
  • Interface a DHT11 temperature and humidity sensor with Raspberry Pi.
  • Develop programs to acquire and display sensor readings.
  • Understand the differences between microcontroller-based and single-board-computer-based sensor interfacing.

3. Required Components

Sr. No. Component Quantity Kit
1 Arduino UNO 1 COG-U1
2 Raspberry Pi 4 (4 GB) 1 COG-RPI4
3 DHT11 Temperature and Humidity Sensor 1 COG-MSBA
4 USB Data Cable 1 COG-U1
5 Raspberry Pi Power Supply 1 COG-RPI4
6 Patch Cords As required Available kits

Software Requirements

  • Arduino IDE
  • Python 3
  • Raspberry Pi OS
  • Adafruit CircuitPython DHT library
  • Adafruit Blinka library

Note: The DHT11 sensor is connected to one development board at a time. The sensor module must be compatible with the supply voltage and logic levels of the selected board.

4. Theory

4.1 Sensor Interfacing

Sensor interfacing is the process of connecting sensors to a microcontroller or computer to acquire physical parameters from the surrounding environment.

Sensors convert physical quantities such as temperature, humidity, light intensity and distance into electrical signals that can be processed by electronic systems.

4.2 DHT11 Temperature and Humidity Sensor

The DHT11 is a digital sensor used to measure temperature and relative humidity.

Features:

  • Measures temperature in degrees Celsius.
  • Measures relative humidity in percentage.
  • Uses a single-wire digital communication protocol.
  • Provides digital readings to a microcontroller or compatible GPIO interface.

4.3 Arduino UNO

Arduino UNO is a microcontroller development board based on the ATmega328P. It provides digital input/output pins and analog input channels.

It can communicate with digital sensors using GPIO pins and execute embedded programs written using the Arduino IDE.

4.4 Raspberry Pi

Raspberry Pi is a single-board computer that runs a Linux operating system. It supports Python programming and provides GPIO pins for connecting digital sensors and external circuits.

Unlike Arduino UNO, Raspberry Pi does not have built-in analog input channels. However, it can interface directly with compatible digital sensors such as the DHT11.

4.5 Sensor Communication

The DHT11 communicates using a single-wire digital protocol. The controller initiates a request and the sensor responds with digital data containing temperature and humidity information.

Both Arduino UNO and Raspberry Pi can read the sensor data using suitable software libraries or communication routines.

5. Pin Connections

5.1 DHT11 with Arduino UNO

DHT11 Pin Arduino UNO Pin Description
VCC 5V Power supply (for a 5V-compatible module)
DATA Digital Pin 2 Digital communication
GND GND Common ground

5.2 DHT11 with Raspberry Pi

DHT11 Pin Raspberry Pi Pin Physical Pin Number
VCC 3.3V 1
DATA GPIO4 (BCM) 7
GND GND 6

Important:

  • Verify the DHT11 module's pin labels before making connections.
  • Use 3.3V-compatible data signaling for Raspberry Pi.
  • If the module has an onboard pull-up resistor, an additional pull-up may not be necessary.
  • Never apply a 5V signal directly to a Raspberry Pi GPIO pin.

6. Connection Diagram

6.1 Arduino UNO with DHT11

      +----------------------+
      |      Arduino UNO     |
      |                      |
      |  5V  o---------------+---- VCC
      |                      |
      |  D2  o-------------------- DATA
      |                      |
      | GND  o-------------------- GND
      |                      |
      +----------------------+
                       DHT11

6.2 Raspberry Pi with DHT11

      +----------------------+
      |    Raspberry Pi 4    |
      |                      |
      | 3.3V o-------------------- VCC
      |                      |
      | GPIO4 o------------------- DATA
      |                      |
      | GND  o-------------------- GND
      |                      |
      +----------------------+
                       DHT11

7. Program Code

7.1 Arduino UNO Program

Library Requirement: Install the DHT sensor library by Adafruit and its required dependencies through the Arduino IDE Library Manager.

#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
    Serial.begin(9600);
    dht.begin();

    Serial.println("DHT11 Sensor Monitoring");
}

void loop()
{
    float temperature = dht.readTemperature();
    float humidity = dht.readHumidity();

    if (isnan(temperature) || isnan(humidity))
    {
        Serial.println("Sensor reading failed");
    }
    else
    {
        Serial.print("Temperature: ");
        Serial.print(temperature);
        Serial.println(" C");

        Serial.print("Humidity: ");
        Serial.print(humidity);
        Serial.println(" %");

        Serial.println("-------------------");
    }

    delay(2000);
}

7.2 Raspberry Pi Program

Step 1: Install the required libraries.

python3 -m venv --system-site-packages dht_env
source dht_env/bin/activate
pip install adafruit-blinka adafruit-circuitpython-dht

Step 2: Create a Python program named dht11_sensor.py.

import time
import board
import adafruit_dht

dht = adafruit_dht.DHT11(board.D4)

print("DHT11 Sensor Monitoring")

try:
    while True:
        try:
            temperature = dht.temperature
            humidity = dht.humidity

            if temperature is not None and humidity is not None:
                print("Temperature: {:.1f} C".format(temperature))
                print("Humidity: {:.1f} %".format(humidity))
                print("-------------------")

        except RuntimeError as error:
            print("Reading error:", error)

        time.sleep(2)

except KeyboardInterrupt:
    print("Monitoring stopped")

finally:
    dht.exit()

Step 3: Execute the program.

python dht11_sensor.py

8. Code Explanation

8.1 Arduino UNO Program

  • `#include <DHT.h>` includes the DHT sensor library.
  • `#define DHTPIN 2` assigns digital pin 2 to the sensor data line.
  • `#define DHTTYPE DHT11` selects the DHT11 sensor.
  • `DHT dht(DHTPIN, DHTTYPE)` creates a sensor object.
  • `dht.begin()` initializes the sensor.
  • `Serial.begin(9600)` initializes serial communication.
  • `dht.readTemperature()` reads the temperature.
  • `dht.readHumidity()` reads relative humidity.
  • `isnan()` checks whether the sensor readings are invalid.
  • `delay(2000)` introduces a two-second interval between readings.

8.2 Raspberry Pi Program

  • `import board` provides the Raspberry Pi pin definitions.
  • `import adafruit_dht` imports the DHT sensor library.
  • `adafruit_dht.DHT11(board.D4)` initializes the sensor using BCM GPIO4.
  • `dht.temperature` reads the temperature.
  • `dht.humidity` reads the relative humidity.
  • `try` and `except` handle communication errors.
  • `time.sleep(2)` provides an interval between readings.
  • `dht.exit()` releases the sensor resources when the program terminates.

9. Procedure

9.1 Arduino UNO

  1. Connect the DHT11 sensor to Arduino UNO using patch cords according to the pin connection table.
  2. Connect the Arduino UNO to the computer using the USB data cable.
  3. Open Arduino IDE.
  4. Install the required DHT sensor library.
  5. Select Arduino UNO as the development board and choose the correct COM port.
  6. Enter the Arduino program and compile it.
  7. Upload the program to the Arduino UNO.
  8. Open the Serial Monitor and set the baud rate to 9600.
  9. Observe the temperature and humidity readings.

9.2 Raspberry Pi

  1. Ensure Raspberry Pi OS is installed and the Raspberry Pi is powered.
  2. Connect the DHT11 sensor to the Raspberry Pi using patch cords according to the pin connection table.
  3. Open the terminal.
  4. Create and activate the Python virtual environment.
  5. Install the required Python libraries.
  6. Create the Python program.
  7. Execute the program using the terminal.
  8. Observe the temperature and humidity readings.
  9. Compare the readings with the Arduino UNO implementation.

10. Observation and Output

10.1 Observation Table

Sr. No. Board Temperature (°C) Humidity (%) Remarks
1 Arduino UNO
2 Arduino UNO
3 Raspberry Pi
4 Raspberry Pi

10.2 Expected Output

Arduino UNO Serial Monitor

DHT11 Sensor Monitoring
Temperature: 27.0 C
Humidity: 58.0 %
-------------------
Temperature: 27.0 C
Humidity: 59.0 %
-------------------

Raspberry Pi Terminal

DHT11 Sensor Monitoring
Temperature: 27.0 C
Humidity: 58.0 %
-------------------
Temperature: 27.0 C
Humidity: 59.0 %
-------------------

Note: The above values are illustrative examples. Actual readings depend on the surrounding environmental conditions and the sensor.

11. Result

The DHT11 temperature and humidity sensor was interfaced with Arduino UNO and Raspberry Pi using their respective hardware interfaces and programming environments.

Both implementations demonstrate sensor data acquisition and displaying temperature and humidity readings.

12. Conclusion

This experiment demonstrates sensor connectivity and data acquisition using Arduino UNO and Raspberry Pi. It provides practical knowledge of digital sensor interfacing, GPIO communication, embedded programming and Python-based sensor monitoring.

13. Viva Questions

  1. What is sensor interfacing?
  2. What is the working principle of the DHT11 sensor?
  3. Which parameters are measured by DHT11?
  4. What is the difference between analog and digital sensors?
  5. What is GPIO?
  6. What is the difference between Arduino UNO and Raspberry Pi?
  7. Why is a suitable pull-up resistor required for some DHT11 modules?
  8. Why are Raspberry Pi GPIO pins limited to 3.3V?
  9. What is the purpose of the DHT library?
  10. Why is a delay used between successive DHT11 readings?
  11. What is the purpose of serial communication in Arduino?
  12. What are the advantages of using Raspberry Pi for IoT applications?