Temperature & Humidity From Arduino to Raspberry Pi

Introduction: Temperature & Humidity From Arduino to Raspberry Pi

Tracking temperature and humidity is important if you have a greenhouse, or have future plans to upgrade your greenhouse into a mini smart-farm.

For my first Instructable I will demonstrate how to create a prototype:

  • Connect a DHT11 temperature and humidity sensor to an Arduino Mega 2560
  • Program the Arduino in C to read the sensor data
  • Display temperature and humidity data on an LCD connected to the Arduino
  • Instruct the Arduino to send the sensor data to a Raspberry Pi 3 Model B+
  • Write code in Python to display the sensor data

Why use an RPi and Arduino together?

The Arduino and RPi connection can allow for great capabilities if you need I/O which the Arduino excels at and network communication/multithreading/visuals which the RPi is much better at.

In other words, we are going to use the Arduino for control intensive tasks and use the RPi for compute intensive tasks.

Ruggedized versions of Arduinos are available at Rugged-Circuits

Step 1: Getting Arduino & RPi Hardware

Arduino starter kits are readily available and allow you to experiment with different types of sensors and gadgets. Purchasing a starter kit works out cheaper instead of ordering various parts separately. I have supplied some affiliate links below pointing to Banggood and Amazon US.

Arduino Starter Kit (Banggood)

Arduino Starter Kit (Amazon US)

Element14 RPi 3 B+ Motherboard (Amazon US)

Raspberry Pi 3 B+ Case (Amazon US)

32GB Micro SD card (Amazon US).

Step 2: Connect the DHT11 & LCD to the Arduino

Step 3: Program the Arduino

#arduino-dht11-lcd2004
#Author: Vasoo Veerapen
#https://www.instructables.com/member/VasooV/
#Reads data from a DHT11 connected to an Arduino, displays on an LCD2004 and sends data over serial to Raspberry Pi
#include <dht.h>
#include <LiquidCrystal_I2C.h>
//LCD display is defined as device number 0x27 on the I2C bus
LiquidCrystal_I2C lcd(0x27, 20, 4);
//DHT11 is connected to pin 8
dht DHT;
#define sensorPin 8
//Raspberry Pi is connected to Serial 0
#define serialPi Serial
void setup() {
  lcd.begin(20, 4); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
  lcd.init();
  lcd.backlight();
  serialPi.begin(9600);  //Arduino to serial monitor
}
void loop() {
  //Read sensor data
  int sensorData = DHT.read11(sensorPin);
  float temperature = DHT.temperature;
  float humidity = DHT.humidity;
  //Print temperature
  lcd.setCursor(0, 0);
  lcd.print("Temperature ");
  lcd.print(temperature);
  lcd.print(" C");
  //Print humidity
  lcd.setCursor(0, 1);
  lcd.print("Humidity    ");
  lcd.print(humidity);
  lcd.print(" %");
  //Send temperature and humidity data to Raspberry Pi
  serialPi.print("<");
  serialPi.print(temperature);
  serialPi.print(",");
  serialPi.print(humidity);
  serialPi.println(">");
  //Wait for 10 seconds
  delay(10000);
}

Step 4: The Working Arduino, LCD & DHT11 Setup

Step 5: Connect the Raspberry Pi to the Arduino

Step 6: RPi Python Code to Read the USB Port Serial Data

#rpi-arduino-dht11
#Raspberry Pi reads temperature and humidity sensor data from Arduino
import serial, string, time
#In this example /dev/ttyUSB0 is used
#This may change in your case to /dev/ttyUSB1, /dev/ttyUSB2, etc.
ser = serial.Serial('/dev/ttyUSB0', 9600)
#The following block of code works like this:
#If serial data is present, read the line, decode the UTF8 data,
#...remove the trailing end of line characters
#...split the data into temperature and humidity
#...remove the starting and ending pointers (< >)
#...print the output
while True:
        if ser.in_waiting > 0:
            rawserial = ser.readline()
            cookedserial = rawserial.decode('utf-8').strip('\r\n')
            datasplit = cookedserial.split(',')
            temperature = datasplit[0].strip('<')
            humidity = datasplit[1].strip('>')
            print(temperature)
            print(humidity)

Be the First to Share

    Recommendations

    • Pocket-Sized Speed Challenge

      Pocket-Sized Speed Challenge
    • Audio Challenge 2020

      Audio Challenge 2020
    • Maps Challenge

      Maps Challenge

    Discussions

    0
    Yusuf BeenH
    Yusuf BeenH

    7 months ago on Step 6

    Can you please guide me to save the data in CSV file in column of temperature and humidity and time. Thank you