Home RASPBERRY PI USB Temperature Sensor Raspberry Pi

USB Temperature Sensor Raspberry Pi

0
USB Temperature Sensor Raspberry Pi
USB Temperature Sensor Raspberry Pi

USB Temperature Sensor Raspberry Pi

DS18B20 temperature sensor is ideal for weather stations and home automation systems and other projects. Few sensors on the Raspberry Pi can be set. They are the same size as the transistor, using only one wire as the data signal. They are also very accurate and take measurements quickly. The only other component needed is a 4.7K ohm or 10K ohm resistor.

In this tutorial, I’ll show you how to connect the DS18B20 to a Raspberry Pi and display temperature readings on an SSH terminal or LCD display.

About the DS18B20

The DS18B20 communicates with the “One-Wire” communication protocol, a proprietary serial communication protocol that uses only one wire to send temperature readings to the microcontroller.

The DS18B20 can be referred to as parasitic power mode. Normally, the DS18B20 requires three lines to operate: Vcc, ground and data lines. In parasitic mode, only ground and data lines are used, and power is supplied through the data lines.

The DS18B20 also features an alarm function that can be configured as an output signal when the temperature exceeds the user-set high or low threshold.

64-bit ROM storage device unique serial code. This 64-bit address allows the microcontroller to receive temperature data from an almost infinite number of sensors on the same pin. The address tells the microcontroller which sensor has a specific temperature value from.

Technical specifications

-55 ° C to 125 ° C

3.0V to 5.0V operating voltage

750 ms sampling

0.5 ° C (9 digits); 0.25 ° C (10 bits); 0.125 ° C (11 digits); 0.0625 ° C

64-bit unique address

One-Wire communication protocol

Collect Hardware

Note: Please Buy Hardware from this link, and yes I will get very little percentage of commission.
Thank you So much for your support

 

Connect the DS18B20 to RASPBERRY PI

The DS18B20 has three separate pins for ground, data and Vcc:

USB Temperature Sensor Raspberry Pi

Wiring for SSH terminal output

Follow this wiring diagram to output the temperature to the SSH terminal:

Wiring for LCD output

Output the temperature reading to the LCD as shown below:

R1: 4.7K ohm or 10K ohm resistor

For more information on using LCDs on the Raspberry Pi, see our tutorial Raspberry Pi LCD Settings and Python Programming.

Enable single-wire interface

We need to enable the One-Wire interface before Pi can receive data from the sensor. After connecting the DS18B20, turn on the Pi and log in, then follow the steps below to enable the One-Wire interface:

  1. At a command prompt, type: sudo nano /boot/config.txt and add it to the bottom of the file:

dtoverlay = W1-GPIO

  1. Exit Nano and restart Pi (sudo reboot)
  2. Log in to Pi again and enter sudo modprobe w1-gpio at the command prompt
  3. Then type sudo modprobe w1-therm
  4. Change the directory to the / sys / bus / w1 / devices directory by entering the following command: cd / sys / bus / w1 / devices

Now enter LS Command to list the devices:

28-000006637696 w1_bus_master1 is shown in my case.

  1. Now enter cd 28-XXXXXXXXXXXX (change X to your own address)

For example, in my case, I would type: cd 28-000006637696

  1. Enter cat w1_slave to display the sensor’s original temperature readings:

The temperature reading here is t = 28625, which means a temperature of 28.625 degrees Celsius.

Enter cd to return to the root directory

This is all you need to set up a single wire interface. Now you can run the following program to output the temperature to an SSH terminal or an LCD …

Temperature output to the SSH terminal

This is a basic Python program that outputs temperature readings to your SSH terminal in Fahrenheit and Celsius

import os

import glob

import time

os.system(‘modprobe w1-gpio’)

os.system(‘modprobe w1-therm’)

 

base_dir = ‘/sys/bus/w1/devices/’

device_folder = glob.glob(base_dir + ’28*’)[0]

device_file = device_folder + ‘/w1_slave’

Temperature output to the LCD

We will use a Python library called RPLCD to drive the LCD. The RPLCD library can be installed from the Python Package Index or PIP. The PIP may already be installed on your Pi, but if not, enter it at the command prompt to install it:

sudo apt-get install python-pip

After installing PIP, enter the following command to install the RPLCD library:

sudo pip Install RPLCD

Once you have installed the library, you can run this program to output the temperature to the LCD monitor:

Python Source

import os

import glob

import time

from RPLCD import CharLCD

 

lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])

 

os.system(‘modprobe w1-gpio’)

os.system(‘modprobe w1-therm’)

 

base_dir = ‘/sys/bus/w1/devices/’

device_folder = glob.glob(base_dir + ’28*’)[0]

device_file = device_folder + ‘/w1_slave’

 

def read_temp_raw():

f = open(device_file, ‘r’)

lines = f.readlines()

f.close()

return lines

 

#CELSIUS CALCULATION

def read_temp_c():

lines = read_temp_raw()

while lines[0].strip()[-3:] != ‘YES’:

time.sleep(0.2)

lines = read_temp_raw()

equals_pos = lines[1].find(‘t=’)

if equals_pos != -1:

temp_string = lines[1][equals_pos+2:]

temp_c = int(temp_string) / 1000.0 # TEMP_STRING IS THE SENSOR OUTPUT, MAKE SURE IT’S AN INTEGER TO DO THE MATH

temp_c = str(round(temp_c, 1)) # ROUND THE RESULT TO 1 PLACE AFTER THE DECIMAL, THEN CONVERT IT TO A STRING

return temp_c

 

#FAHRENHEIT CALCULATION

def read_temp_f():

lines = read_temp_raw()

while lines[0].strip()[-3:] != ‘YES’:

time.sleep(0.2)

lines = read_temp_raw()

equals_pos = lines[1].find(‘t=’)

if equals_pos != -1:

temp_string = lines[1][equals_pos+2:]

temp_f = (int(temp_string) / 1000.0) * 9.0 / 5.0 + 32.0 # TEMP_STRING IS THE SENSOR OUTPUT, MAKE SURE IT’S AN INTEGER TO DO THE MATH

temp_f = str(round(temp_f, 1)) # ROUND THE RESULT TO 1 PLACE AFTER THE DECIMAL, THEN CONVERT IT TO A STRING

return temp_f

 

while True:

 

lcd.cursor_pos = (0, 0)

lcd.write_string(“Temp: ” + read_temp_c() + unichr(223) + “C”)

lcd.cursor_pos = (1, 0)

lcd.write_string(“Temp: ” + read_temp_f() + unichr(223) + “F”)

That should just about wrap it up! Hope you found it useful. Be sure to subscribe if you’d like to get an email each time we publish a new tutorial. Feel free to share it if you know someone else that would like it… And as always, let us know in the comments if you have any problems setting it up!

You may also like to read these articles

Top Best Cooling Case for Raspberry Pi 3 of 2018

Best Monitor for Raspberry Pi

Advertisements

LEAVE A REPLY

Please enter your comment!
Please enter your name here

one + 3 =