Skip to main content

Implementation of IoT with Raspberry Pi

  HOME             UNIT 1           UNIT 2          UNIT 3         UNIT 4           UNIT 5

 

 Implementation of IoT with Raspberry Pi


What is Raspberry Pi?


    Short for RPi, it is a credit-card-sized single-board computer built by the Raspberry Pi Foundation in association with Broadcom in the United Kingdom. It can do most of the things your typical desktop does, such as handling spreadsheets, word-processing, and games.

    Raspberry Pi also has the capacity to play videos in high-definition. It can run several versions of Linux and is used to teach kids worldwide how to program. In fact, that was one of the biggest reasons why the idea of Raspberry Pi was conceived. But more on that later.

    In addition, it has inputs and outputs for sensors so that you can attach all sorts of hardware to it. The General Purpose Input/Output (GPIO) pins on Raspberry Pi allow you to connect status lights, switches, analog signals, and more. You can use C++ or Python to control the board to sense or control devices attached to it.

    Invented to stimulate the teaching of basic computer science in schools, Raspberry Pi has since then been used in many applications, including hobbyist projects, hardware platforms for electronics design, embedded devices, and robotics.



Implementation of IoT with Raspberry Pi :

  • Creating an interactive environment
  • Network of devices connected together

Sensor

Electronic element 
  • Converts physical quantity into electrical signals. 
  • Can be analog or digital


Actuator

Mechanical/Electro-mechanical device 

  • Converts energy into motion
  • Mainly used to provide controlled motion to other components

System Overview

Sensor and actuator 

  • interfaced with Raspberry Pi      Read data from the sensor
  • Control the actuator according to the reading from the sensor
  • Connect the actuator to a device

Example:

Temperature Dependent Auto Cooling System

A Temperature Dependent Auto Cooling System using a Raspberry Pi can automate the process of cooling when the temperature reaches a certain threshold. The system can activate a fan (or another cooling device) when the temperature exceeds a specified limit.

Here's a step-by-step guide on how to create this system:

Components:

  1. Raspberry Pi (with Raspbian OS installed)
  2. DHT11 or DHT22 temperature sensor
  3. Relay module (to control the fan)
  4. Fan (or any cooling device)
  5. Breadboard and Jumper wires

Circuit Diagram:

  1. DHT11:

    • VCC to Raspberry Pi 3.3V
    • GND to Raspberry Pi GND
    • Data pin to a GPIO pin (e.g., GPIO 17)
  2. Relay Module:

    • VCC to Raspberry Pi 5V
    • GND to Raspberry Pi GND
    • IN to a GPIO pin (e.g., GPIO 27)
    • NO (Normally Open) pin to one wire of the fan
    • COM pin to the power source for the fan

Steps:

  1. Set up the Raspberry Pi:

    • Install Raspbian and set up the necessary tools.
    • Enable GPIO pins by running
      sudo raspi-config
    • Install the DHT library:
      sudo pip3 install Adafruit_DHT
  2. Write the Python Script: The following Python script will:

    • Read the temperature from the DHT11 sensor.
    • Activate the relay to turn on the fan when the temperature exceeds a set threshold (e.g., 30°C).
python code

import Adafruit_DHT import RPi.GPIO as GPIO import time # Set up the GPIO mode GPIO.setmode(GPIO.BCM) # Define the GPIO pin for the DHT11 sensor and the relay DHT_SENSOR = Adafruit_DHT.DHT11 DHT_PIN = 17 # GPIO pin connected to DHT11 RELAY_PIN = 27 # GPIO pin connected to the relay # Set up the relay pin as an output GPIO.setup(RELAY_PIN, GPIO.OUT) GPIO.output(RELAY_PIN, GPIO.HIGH) # Ensure fan is off at startup # Set temperature threshold in Celsius TEMP_THRESHOLD = 30 try: while True: # Read temperature and humidity from DHT11 humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN) if humidity is not None and temperature is not None: print(f"Temp: {temperature:.1f}°C Humidity: {humidity:.1f}%") # If temperature exceeds the threshold, turn on the fan if temperature > TEMP_THRESHOLD: GPIO.output(RELAY_PIN, GPIO.LOW) # Fan ON print("Fan ON (Cooling)") else: GPIO.output(RELAY_PIN, GPIO.HIGH) # Fan OFF print("Fan OFF") else: print("Failed to retrieve data from sensor") time.sleep(2) # Delay before next reading except KeyboardInterrupt: print("Program stopped") finally: GPIO.cleanup() # Clean up GPIO settings

Explanation:

  • Sensor Reading: The temperature and humidity are read from the DHT11 sensor using the Adafruit_DHT library.
  • Relay Control: The relay controls the fan. If the temperature is above the threshold (e.g., 30°C), the relay is triggered to power on the fan. When the temperature falls below the threshold, the fan is turned off.
  • GPIO Control: The GPIO.output(RELAY_PIN, GPIO.LOW) turns the fan ON, while GPIO.HIGH turns it OFF.

How to Run:

  1. Save the script (e.g., temp_auto_cooling.py).
  2. Run the script with:
  3. sudo python3 temp_auto_cooling.py

Optional Enhancements:

  1. Change the Threshold: You can modify the TEMP_THRESHOLD value in the script to adjust the temperature at which the fan turns on.
  2. Fan Speed Control: If you want to control the speed of the fan, you can use a PWM (Pulse Width Modulation) module to adjust fan speed based on temperature.
  3. Real-time Monitoring: Integrate an LCD display or use a web dashboard to monitor the temperature and fan status in real-time.

 

IoT uses of Raspberry Pi :

Raspberry Pi computers feature a set of General Purpose Input Output (GPIO) pins that provide connections to external electronic devices and therefore the development of IoT solutions. 

These GPIO pins can be connected to external sensors using either jumper wires or a ribbon cable. These subsequently connect the Pi to a breadboard (a solder-less, plug-and- play electronics board), enabling various application possibilities.

One such application is a Raspberry Pi IoT server. “With an in-built quadcore processor, Raspberry Pi can serve as the ‘Internet Gateway’ for IoT devices”, says Sanam Malhotra from Oodles AI.“Powered by a cloud network, Pi acts as a web server for uploading and transiting sensor data on   IoT platforms.

 Custom code, an operating system, a Python library, and a cloud network are all it takes to use Pi Computer as a web server.



Comments