Friday, 1 February 2019

Send Temperature & Humidity Sensor Readings from Arduino to Raspberry Pi

  Will demonstrate how to:
  • 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. You can also get ruggedized versions of Arduinos at Rugged-Circuits

Procure 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.

Connect the DHT11 & LCD to the Arduino

Picture of Connect the DHT11 & LCD to the Arduino

Arduino Code to Read DHT11 Sensor

#Author: Vasoo Veerapen
#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
  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(" C");
  //Print humidity
  lcd.setCursor(0, 1);
  lcd.print("Humidity    ");
  lcd.print(" %");
  //Send temperature and humidity data to Raspberry Pi
  //Wait for 10 seconds

Python Code to Read RPi USB Serial

#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('>')

No comments:

Post a comment