Pi with sense hat + Arduino with a DS18B20 sensor

Having used the Sense-Hat to record and display temperatures we see that the sensor on the Sense-Hat is impacted by the heat of the Pi. What if we use an external temperature sensor? The board is pretty crowded when the Sense-Hat is installed, so can we put the probe on an Ardunio and read the temperature?

TEMPERATURE = HOT

TEMPERATURE = OK


SETUP
Using the Waterproof DS18B20 Digital Temperature Sensor for Arduino

Required Libraries (Install via the Arduino IDE Library Manager):
Dallas Temperature Library
Arduino Onewire Library

For Arduino,
DATA to pin 10
VCC to pin (3.3V) – power supply range is 3.0V to 5.5V so you can use (5V)
GND to pin (GND)

TEST THAT THE PROBE IS RECORDING TEMPERATURE
We need to check that the temperature probe is recording a temperature and making it available to read.

// Include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 10 on the Arduino 
#define ONE_WIRE_BUS 10
// Set up the oneWire bus
OneWire oneWire(ONE_WIRE_BUS);
// Pass the oneWire reference to Dallas Temperature
DallasTemperature sensors(&oneWire);

float Celsius = 0;

void setup() {
  // start serial port
  Serial.begin(9600);
  // start the library
  sensors.begin();
}

void loop() {
  // Send the command to get temperature readings 
  sensors.requestTemperatures();
  // Why "byIndex"?  
  // You can have more than one DS18B20 on the same bus.  
  // 0 refers to the first IC on the wire 
  Celsius = sensors.getTempCByIndex(0);

  Serial.print(Celsius);
  Serial.print(" C  ");

  delay(1000);
}

To view output: Arduino IDE > Tools > Serial Monitor

READ THE VALUE FROM THE PI
Having established that the Arduino is recording the temperature, can we access it from the Pi?

import serial
s1 = serial.Serial('/dev/ttyACM0', 9600)
s1.flushInput()
while True: 
    if(s1.in_waiting >6):
        temp = float(s1.read(5))
        print(temp)

BUNDLE THINGS TOGETHER, INCLUDE LEDS ON ARDUINO
Going all out with a combination of all of the recent projects into one end point.

The Sketch for the Arduino;

// Include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>
 
// Data wire is plugged into pin 10 on the Arduino 
#define ONE_WIRE_BUS 10
// Set up the oneWire bus
OneWire oneWire(ONE_WIRE_BUS);
// Pass the oneWire reference to Dallas Temperature
DallasTemperature sensors(&oneWire);
 
float Celsius = 0;

char receivedChar;
boolean newData = false;
 
void setup() {
  // start serial port
  Serial.begin(9600);
  // start the library
  sensors.begin();
  // LEDS
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);  
}
 
void loop() {
  // Light LED based on last temperature reading
  recvInfo();
  lightLED(); 
  // Send the command to get temperature readings 
  sensors.requestTemperatures();
  // Why "byIndex"?  
  // You can have more than one DS18B20 on the same bus.  
  // 0 refers to the first IC on the wire 
  Celsius = sensors.getTempCByIndex(0);
 
  Serial.print(Celsius);
 
  delay(1000);
}
void recvInfo() {
 
  if (Serial.available() > 0) {
 
    receivedChar = Serial.read();
    newData = true;
     
  }
   
}
 
void lightLED() {
 
  int led = (receivedChar - '0');
 
  while(newData == true) {
 
    digitalWrite(led, HIGH);
    delay(2000);
    digitalWrite(led, LOW);
 
    newData = false;
     
  }
   
}

The python code for the Pi;

# PYTHON TEST CODE FOR THE SENSE HAT
# DISPLAY TEMPERATURE TO LED MATRIX
# API Reference : https://pythonhosted.org/sense-hat/api/
# Introduction : https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat/
 
from sense_hat import SenseHat
sense = SenseHat()
from time import sleep

import serial
s1 = serial.Serial('/dev/ttyACM0',9600)

# Set up colours as variables
blue = (0,0,255)
yellow = (255,255,0)
red = (255,0,0)
green = (0,128,0)
black = (0,0,0) 

while True:
                sense.clear() # clear arguments
                s1.flushInput()
                sense.set_rotation(180) # Rotate screen by 180 degrees 
                # Get the current temperature in degrees Celsius from the humidity sensor.
                # temp = round(sense.get_temperature())
                
                if(s1.in_waiting <6):
                    temp2 = float(s1.read(5))
                
                pressure = sense.get_pressure()
                humidity = round(sense.get_humidity())

                # Prepare message to be displayed on LED Matrix
                message = 'Temperature is %d Celsius' %(temp2)
                
                print(message)  # displays temperature to console for testing
                # Sent font colour based on temperature range
                # Traffic Light LEDS
                # Pin 2 = Red, Pin 3 = Yellow, Pin 4 = Green
                if temp2 > 10.0 and temp2 < 45.0:
                    font = green
                    s1.write(bytes(b'4'))
                else:
                    font = red
                    s1.write(bytes(b'2'))
                # Scrolls a text message from right to left across the LED matrix
                sense.show_message(message,scroll_speed=0.05, text_colour=font, back_colour=black)
                # Change message to pressure
                message = 'Pressure is %d millibars' %(pressure)
                print(message)  # displays pressure to console for testing
                # Scrolls a text message from right to left across the LED matrix
                sense.show_message(message,scroll_speed=0.05, text_colour=green, back_colour=black)
                # Change message to humidity
                message = 'Humidity is %d percent' %(humidity)
                print(message)  # displays humidity to console for testing
                # Scrolls a text message from right to left across the LED matrix
                sense.show_message(message,scroll_speed=0.05, text_colour=blue, back_colour=black)
                
                sleep(5)

REFERENCES:

  1. DS18B20 (Digital Temperature Sensor) and Arduino [Arduino Project Hub]
  2. Arduino DS18b20 Temperature Sensor Tutorial (2019-Mar-15) [PI Guide]
This entry was posted in 2019, geek. Bookmark the permalink.

1 Response to Pi with sense hat + Arduino with a DS18B20 sensor

  1. Adding three zones;
    Temperature LEDs – 3 zones

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.