Jump to content

MDM331AID/Experiment 1: Difference between revisions

From cognifront
CogWikiEditor (talk | contribs)
Created page with "= 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 (SPPU) ---- == 1. Aim == To develop an application for interfacing sensors with Arduino UNO and Raspberry Pi, acquire sensor data, process the readings and display the output through a..."
 
CogWikiEditor (talk | contribs)
No edit summary
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
'''Program:''' TE Artificial Intelligence and Data Science Engineering
'''Program:''' TE Artificial Intelligence and Data Science Engineering


'''University:''' Savitribai Phule Pune University (SPPU)
'''University:''' Savitribai Phule Pune University


----
----
Line 11: Line 11:
== 1. Aim ==
== 1. Aim ==


To develop an application for interfacing sensors with Arduino UNO and Raspberry Pi, acquire sensor data, process the readings and display the output through a serial monitor or terminal.
To develop applications for interfacing sensors with Arduino UNO and Raspberry Pi and to acquire, process and display sensor data.


== 2. Objectives ==
== 2. Objectives ==


* Understand sensor interfacing with Arduino UNO and Raspberry Pi.
* Understand sensor interfacing with Arduino UNO and Raspberry Pi.
* Interface an LDR sensor with Arduino UNO and acquire analog sensor data.
* Interface a DHT11 temperature and humidity sensor with Arduino UNO.
* Interface a DHT11 sensor with Raspberry Pi to acquire temperature and humidity readings.
* Interface a DHT11 temperature and humidity sensor with Raspberry Pi.
* Develop programs to read, process and display sensor data.
* Develop programs to acquire and display sensor readings.
* Understand the differences between analog and digital sensor interfacing.
* Understand the differences between microcontroller-based and single-board-computer-based sensor interfacing.


== 3. Required Components ==
== 3. Required Components ==
Line 41: Line 41:
|-
|-
| 3
| 3
| LDR Sensor
| 1
| COG-U1
|-
| 4
| DHT11 Temperature and Humidity Sensor
| DHT11 Temperature and Humidity Sensor
| 1
| 1
| COG-MSBA
| COG-MSBA
|-
|-
| 5
| 4
| Breadboard (if required)
| 1
| Additional
|-
| 6
| 10 kΩ resistor for LDR voltage divider (if using bare LDR)
| 1
| Additional
|-
| 7
| USB Data Cable
| USB Data Cable
| 1
| 1
| COG-U1
| COG-U1
|-
|-
| 8
| 5
| Raspberry Pi power supply
| Raspberry Pi Power Supply
| 1
| 1
| COG-RPI4
| COG-RPI4
|-
|-
| 9
| 6
| Patch cords
| Patch Cords
| As required
| As required
| Available kits
| Available kits
Line 84: Line 69:
* Adafruit Blinka library
* Adafruit Blinka library


'''Note:''' The experiment uses two independent implementations. The Arduino UNO implementation reads the LDR, while the Raspberry Pi implementation reads the DHT11. An external ADC such as MCP3008 is additionally required if the LDR is to be read directly by the Raspberry Pi.
'''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. Theory ==
Line 90: Line 75:
=== 4.1 Sensor Interfacing ===
=== 4.1 Sensor Interfacing ===


A sensor is an electronic device that detects physical parameters such as temperature, humidity, light intensity, pressure or motion and converts them into an electrical signal.
Sensor interfacing is the process of connecting sensors to a microcontroller or computer to acquire physical parameters from the surrounding environment.


Sensor interfacing is the process of connecting sensors to a microcontroller or single-board computer to acquire and process environmental data.
Sensors convert physical quantities such as temperature, humidity, light intensity and distance into electrical signals that can be processed by electronic systems.


=== 4.2 LDR Sensor ===
=== 4.2 DHT11 Temperature and Humidity Sensor ===


An LDR (Light Dependent Resistor) is a passive sensor whose resistance changes with the intensity of incident light.
The DHT11 is a digital sensor used to measure temperature and relative humidity.


* In bright light, its resistance decreases.
'''Features:'''
* In darkness, its resistance increases.


By connecting an LDR in a voltage divider circuit, the Arduino UNO can measure the resulting voltage through an analog input pin.
* 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 DHT11 Sensor ===
=== 4.3 Arduino UNO ===


The DHT11 is a digital sensor used to measure temperature and relative humidity.
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.


It communicates with a controller using a single-wire digital communication protocol.
=== 4.4 Raspberry Pi ===


The sensor provides:
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.
* Temperature in degrees Celsius.
* Relative humidity in percentage.


=== 4.4 Arduino UNO and Raspberry Pi ===
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.


Arduino UNO is a microcontroller development board with built-in analog input channels and digital GPIO pins.
=== 4.5 Sensor Communication ===


Raspberry Pi is a single-board computer that runs a Linux operating system and supports programming in Python, C and other languages.
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.


Unlike the Arduino UNO, Raspberry Pi does not have built-in analog input pins. Digital sensors such as DHT11 can be interfaced directly with suitable GPIO pins.
Both Arduino UNO and Raspberry Pi can read the sensor data using suitable software libraries or communication routines.


== 5. Pin Connections ==
== 5. Pin Connections ==


=== 5.1 LDR Sensor with Arduino UNO ===
=== 5.1 DHT11 with Arduino UNO ===


{| class="wikitable"
{| class="wikitable"
|-
|-
! LDR Circuit Point
! DHT11 Pin
! Arduino UNO Pin
! Arduino UNO Pin
! Description
! Description
|-
|-
| Voltage divider output
| VCC
| A0
| Analog sensor input
|-
| One end of LDR
| 5V
| 5V
| Supply connection
| Power supply (for a 5V-compatible module)
|-
|-
| Other end of LDR
| DATA
| A0
| Digital Pin 2
| Voltage divider junction
| Digital communication
|-
|-
| 10 kΩ resistor lower end
| GND
| GND
| Ground connection
| GND
| Common ground
|}
|}


'''Circuit arrangement:'''
=== 5.2 DHT11 with Raspberry Pi ===
 
<pre>
        Arduino UNO 5V
              |
            LDR
              |
              +---------- A0
              |
            10 kΩ
              |
            GND
</pre>
 
This arrangement produces a higher analog voltage in brighter conditions, assuming the LDR is connected to 5V and the resistor to GND.
 
=== 5.2 DHT11 Sensor with Raspberry Pi ===
 
The following connections assume a DHT11 module with an onboard pull-up resistor.


{| class="wikitable"
{| class="wikitable"
Line 183: Line 148:
|-
|-
| GND
| GND
| Ground
| GND
| 6
| 6
|}
|}


'''Important:''' Do not connect a 5V pull-up signal directly to a Raspberry Pi GPIO. Raspberry Pi GPIO pins are 3.3V tolerant only. If using a bare DHT11 sensor, a suitable pull-up resistor is required between DATA and 3.3V.
'''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. Connection Diagram ==


=== 6.1 Arduino UNO with LDR ===
=== 6.1 Arduino UNO with DHT11 ===


<pre>
<pre>
      +----------------------+
      +----------------------+
      |     Arduino UNO     |
      |     Arduino UNO     |
      |                      |
      |                      |
      | 5V o-----------------+
      | 5V o---------------+---- VCC
      |                      |
      |                      |
      | A0 o-------+        |
      | D2  o-------------------- DATA
      |           |        |
      |                     |
      | GND o---+  |         |
      | GND o-------------------- GND
      +---------|--|---------+
      |                     |
                |  |
      +----------------------+
              10kΩ  LDR
                      DHT11
                |  |
                GND +5V
</pre>
</pre>


Line 212: Line 180:


<pre>
<pre>
      +----------------------+
      +----------------------+
      |    Raspberry Pi 4    |
      |    Raspberry Pi 4    |
      |                      |
      |                      |
      | Pin 1 (3.3V) o-------+------ VCC
      | 3.3V o-------------------- VCC
      |                      |
      |                      |
      | Pin 7 (GPIO4) o------------- DATA
      | GPIO4 o------------------- DATA
      |                      |
      |                      |
      | Pin 6 (GND) o---------------- GND
      | GND o-------------------- GND
      +----------------------+
      |                      |
 
      +----------------------+
                DHT11
                      DHT11
</pre>
</pre>


== 7. Program Code ==
== 7. Program Code ==


=== 7.1 Arduino UNO Program for LDR Sensor ===
=== 7.1 Arduino UNO Program ===
 
'''Library Requirement:''' Install the DHT sensor library by Adafruit and its required dependencies through the Arduino IDE Library Manager.


<pre>
<pre>
const int LDR_PIN = A0;
#include <DHT.h>
 
#define DHTPIN 2
#define DHTTYPE DHT11
 
DHT dht(DHTPIN, DHTTYPE);


void setup()
void setup()
{
{
     Serial.begin(9600);
     Serial.begin(9600);
     Serial.println("LDR Sensor Monitoring");
    dht.begin();
 
     Serial.println("DHT11 Sensor Monitoring");
}
}


void loop()
void loop()
{
{
     int ldrValue = analogRead(LDR_PIN);
     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.print("LDR Value: ");
        Serial.println("-------------------");
     Serial.println(ldrValue);
     }


     delay(1000);
     delay(2000);
}
}
</pre>
</pre>


=== 7.2 Raspberry Pi Program for DHT11 Sensor ===
=== 7.2 Raspberry Pi Program ===


'''Step 1:''' Install the required libraries in the Python environment.
'''Step 1: Install the required libraries.'''


<pre>
<pre>
Line 259: Line 251:
</pre>
</pre>


Install the system GPIO library dependency if required by your Raspberry Pi OS version, following the Adafruit CircuitPython DHT installation instructions.
'''Step 2: Create a Python program named dht11_sensor.py.'''
 
'''Step 2:''' Create a Python program named `dht11_sensor.py`.


<pre>
<pre>
Line 281: Line 271:
                 print("Temperature: {:.1f} C".format(temperature))
                 print("Temperature: {:.1f} C".format(temperature))
                 print("Humidity: {:.1f} %".format(humidity))
                 print("Humidity: {:.1f} %".format(humidity))
                 print("----------------------")
                 print("-------------------")


         except RuntimeError as error:
         except RuntimeError as error:
Line 295: Line 285:
</pre>
</pre>


'''Step 3:''' Execute the program.
'''Step 3: Execute the program.'''


<pre>
<pre>
Line 303: Line 293:
== 8. Code Explanation ==
== 8. Code Explanation ==


=== 8.1 Arduino UNO Code ===
=== 8.1 Arduino UNO Program ===


* `const int LDR_PIN = A0;` defines the analog input pin used for the LDR.
* `#include <DHT.h>` includes the DHT sensor library.
* `Serial.begin(9600);` initializes serial communication at 9600 baud.
* `#define DHTPIN 2` assigns digital pin 2 to the sensor data line.
* `analogRead(LDR_PIN);` reads the analog voltage and returns a value between 0 and 1023.
* `#define DHTTYPE DHT11` selects the DHT11 sensor.
* `Serial.print()` displays the sensor reading.
* `DHT dht(DHTPIN, DHTTYPE)` creates a sensor object.
* `delay(1000);` introduces a one-second delay between readings.
* `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 Code ===
=== 8.2 Raspberry Pi Program ===


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


== 9. Procedure ==
== 9. Procedure ==


=== 9.1 Arduino UNO with LDR ===
=== 9.1 Arduino UNO ===


# Connect the LDR and resistor in a voltage divider arrangement.
# Connect the DHT11 sensor to Arduino UNO using patch cords according to the pin connection table.
# Connect the voltage divider output to Arduino analog pin A0.
# Connect the Arduino UNO to the computer using the USB data cable.
# Connect the Arduino UNO to the computer using the USB data cable.
# Open Arduino IDE and select the appropriate Arduino UNO board and COM port.
# Open Arduino IDE.
# Enter the Arduino program.
# Install the required DHT sensor library.
# Compile and upload the program.
# Select Arduino UNO as the development board and choose the correct COM port.
# Open the Serial Monitor and select 9600 baud.
# Enter the Arduino program and compile it.
# Observe the sensor readings under different light conditions.
# Upload the program to the Arduino UNO.
# Open the Serial Monitor and set the baud rate to 9600.
# Observe the temperature and humidity readings.


=== 9.2 Raspberry Pi with DHT11 ===
=== 9.2 Raspberry Pi ===


# Ensure Raspberry Pi OS is installed and the Raspberry Pi is powered.
# Ensure Raspberry Pi OS is installed and the Raspberry Pi is powered.
# Connect the DHT11 module to the Raspberry Pi according to the pin connection table.
# Connect the DHT11 sensor to the Raspberry Pi using patch cords according to the pin connection table.
# Open the terminal.
# Create and activate the Python virtual environment.
# Create and activate the Python virtual environment.
# Install the required Python libraries.
# Install the required Python libraries.
Line 344: Line 341:
# Execute the program using the terminal.
# Execute the program using the terminal.
# Observe the temperature and humidity readings.
# Observe the temperature and humidity readings.
# Compare the readings under different environmental conditions.
# Compare the readings with the Arduino UNO implementation.


== 10. Observation and Output ==
== 10. Observation and Output ==


=== 10.1 LDR Sensor Observation ===
=== 10.1 Observation Table ===


{| class="wikitable"
{| class="wikitable"
|-
|-
! Sr. No.
! Sr. No.
! Light Condition
! Board
! LDR Analog Reading
! Temperature (°C)
! Observation
! Humidity (%)
! Remarks
|-
|-
| 1
| 1
| Bright light
| Arduino UNO
|
|  
|  
|  
|  
|-
|-
| 2
| 2
| Normal room light
| Arduino UNO
|
|  
|  
|  
|  
|-
|-
| 3
| 3
| Dim light
| Raspberry Pi
|
|  
|  
|  
|  
|-
|-
| 4
| 4
| Dark condition
| Raspberry Pi
|
|  
|  
|  
|  
|}
|}


The analog readings should generally increase with increasing light intensity in the given voltage divider arrangement. Actual readings depend on the LDR and ambient lighting conditions.
=== 10.2 Expected Output ===


=== 10.2 DHT11 Sensor Observation ===
'''Arduino UNO Serial Monitor'''


{| class="wikitable"
<pre>
|-
DHT11 Sensor Monitoring
! Sr. No.
Temperature: 27.0 C
! Environmental Condition
Humidity: 58.0 %
! Temperature (°C)
-------------------
! Humidity (%)
Temperature: 27.0 C
|-
Humidity: 59.0 %
| 1
-------------------
| Normal room condition
</pre>
|
|
|-
| 2
| Warmer condition
|
|
|-
| 3
| Different humidity condition
|
|
|}


'''Expected Output (Illustrative)'''
'''Raspberry Pi Terminal'''


<pre>
<pre>
Line 411: Line 400:
Temperature: 27.0 C
Temperature: 27.0 C
Humidity: 58.0 %
Humidity: 58.0 %
----------------------
-------------------
Temperature: 27.0 C
Temperature: 27.0 C
Humidity: 59.0 %
Humidity: 59.0 %
----------------------
-------------------
</pre>
</pre>


The values shown are illustrative only and are not measured experimental results.
'''Note:''' The above values are illustrative examples. Actual readings depend on the surrounding environmental conditions and the sensor.


== 11. Result ==
== 11. Result ==


The interfacing of an LDR sensor with Arduino UNO and a DHT11 sensor with Raspberry Pi was demonstrated through separate sensor-monitoring applications.
The DHT11 temperature and humidity sensor was interfaced with Arduino UNO and Raspberry Pi using their respective hardware interfaces and programming environments.


The Arduino UNO acquired analog light sensor data, while the Raspberry Pi acquired digital temperature and humidity data using Python.
Both implementations demonstrate sensor data acquisition and displaying temperature and humidity readings.


== 12. Conclusion ==
== 12. Conclusion ==


This experiment demonstrates the fundamental process of interfacing sensors with Arduino UNO and Raspberry Pi. It provides practical understanding of analog and digital sensor communication, GPIO interfacing, data acquisition and displaying sensor measurements.
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 ==
== 13. Viva Questions ==


# What is sensor interfacing?
# What is sensor interfacing?
# What is the operating principle of an LDR?
# What is the working principle of the DHT11 sensor?
# Why is a voltage divider used with an LDR?
# Which parameters are measured by DHT11?
# What is the resolution of the Arduino UNO ADC?
# Why does Raspberry Pi require an external ADC to read an LDR?
# What parameters can be measured using DHT11?
# What is the difference between analog and digital sensors?
# What is the difference between analog and digital sensors?
# What is GPIO?
# What is the difference between Arduino UNO and Raspberry Pi?
# Why is a suitable pull-up resistor required for some DHT11 modules?
# Why are Raspberry Pi GPIO pins limited to 3.3V?
# Why are Raspberry Pi GPIO pins limited to 3.3V?
# What is the purpose of a pull-up resistor?
# What is the purpose of the DHT library?
# Why is a delay introduced between DHT11 readings?
# Why is a delay used between successive DHT11 readings?
# What is the difference between Arduino UNO and Raspberry Pi?
# What is the purpose of serial communication in Arduino?
# What are the advantages of connecting sensors to IoT devices?
# What are the advantages of using Raspberry Pi for IoT applications?

Latest revision as of 14:23, 2 October 2026

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?