Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing

Similar documents
Raspberry Pi Class Ed 299. Mike Davis Truman College 5/26/2015

Send Raspberry Pi Data to COSM

บทท 4 ข นตอนการทดลอง

Adafruit's Raspberry Pi Lesson 6. Using SSH

micro:bit Lesson 2. Controlling LEDs on Breadboard

Measuring Light with a BeagleBone Black

Arduino Lesson 15. DC Motor Reversing

Adafruit's Raspberry Pi Lesson 1. Preparing an SD Card for your Raspberry Pi

Java_Embedded_Open_Onli...

Adafruit PiUART - USB Console and Power Add-on for Raspberry Pi

Rs-Pi 7 USB- Hub & I2C 1-Wire User Manual

TMP36 Temperature Sensor

COOKING WITH TEAM 279

Adding a Real Time Clock to Raspberry Pi

RaspiDigiHamClock. Raspberry Pi Amateur Radio Digital Clock. v WA4EFH R.Grokett

Adafruit Ultimate GPS on the Raspberry Pi

Adafruit's Raspberry Pi Lesson 1. Preparing an SD Card for your Raspberry Pi

DAQCplate Users Guide

Halloween Pumpkinusing. Wednesday, October 17, 12

Rs-Pi USB- 4 Hub & I2C RS232 User Manual

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

9 Output Devices: Buzzers

Adafruit Metro Mini. Created by lady ada. Last updated on :12:28 PM UTC

All rights reserved by Waveshare Electronics Co., Ltd. Not allow to modify, distribute, or copy without permission.

ECE471: Embedded Systems Homework 7 SPI, A/D and Temperature Probe. Due: Friday, 2 November 2018, 10:00am

Adafruit MAX98357 I2S Class-D Mono Amp

MacGyver IT: w/raspberry Pi 3 Kevin Capwell META Pat Zielke Viroqua

CamJam EduKit Sensors Worksheet Five. Equipment Required. The Parts. The Passive Infrared Sensor

Adafruit 1-Wire Thermocouple Amplifier - MAX31850K

Export: Copyright by Joy-IT - Published under CC BY-NC-SA 3.0

Installation guide. Step1. Install the OS for Raspberry Pi 3. Go to to download Raspbian to the PC.

Using the BMP085/180 with Raspberry Pi or Beaglebone Black

Adafruit PiOLED - 128x32 Mini OLED for Raspberry Pi

Spring 2017 Gabriel Kuri

Matrix and 7-Segment LED Backpack with the Raspberry Pi

ROBOTLINKING THE POWER SUPPLY LEARNING KIT TUTORIAL

Adafruit 1-Wire Thermocouple Amplifier - MAX31850K

Gooligum Electronics 2015

Raspberry Pi GPIO Zero Reaction Timer

Digital Circuits. Page 1 of 5. I. Before coming to lab. II. Learning Objectives. III. Materials

Raspberry Pi Setup Tutorial

User-space SPI TFT Python Library - ILI9341

Adafruit seesaw. Created by Dean Miller. Last updated on :30:23 AM UTC

AlaMode User Manual Revision

Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout

F28HS Hardware-Software Interface: Systems Programming

Raspberry Pi NTP Clock Setup Guide

BCS Raspberry Pi Launch Events Getting started with Raspberry Pi

Atmel 328-Processor for RaspberryPi

Raspberry PI 'How-To' Series

Grove - Solid State Relay

Zero2Go. User Manual (revision 1.03) Wide Input Range Power Supply for Your Raspberry Pi. Copyright 2017 UUGear s.r.o. All rights reserved.

High-Precision AD/DA Board User Manual

Pi-EzConnect. Pi-EzConnect. Key Features

Programmable Analog Input Node (PAIN) Master Semester Thesis, Nicolo D Anna.

System on Chip (SoC)

I, J, K. Ethernet.begin() method, 274. Future Technology Devices International (FTDI), 24

Adafruit Ultimate GPS on the Raspberry Pi

Stand-alone programming AVRs using CircuitPython

Security Camera. by maartenbrysbaert. The SeCam is a security camera that allows you to: - Livestream. - Rotate your camera 180 degrees

FruitBox Sequencer: Musically Delicious Step Pattern Generator

Adafruit WebIDE. Created by Tyler Cooper. Last updated on :29:47 PM UTC

Adafruit NFC/RFID on Raspberry Pi

Grand Central Soundboard in Ten Minutes Created by Mike Barela. Last updated on :11:24 PM UTC

PiCAN2 DUO ISO USER GUIDE V1.1. PiCAN 2 DUO ISO Rev B V1.1. PiCAN2 DUO CAN-Bus ISO Board for Raspberry Pi

Networked Thermal Printer using Raspberry Pi and CUPS

Grove - 80cm Infrared Proximity Sensor

Grove - Vibration Motor

Raspberry Pi Activity 2: My Binary Addiction...Reloaded

This tutorial will show you how to take temperature readings using the Freetronics temperature sensor and an Arduino Uno.

Here's how the 4 channel receiver attaches to a Raspberry Pi B+, A+ or Pi 2.

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Adafruit NFC/RFID on Raspberry Pi

R-DAS Tiny User s Manual

How to receive codes from a wireless remote on your Raspberry Pi.

Lab 2.2 Ohm s Law and Introduction to Arduinos

ECE 2036 Lab 4 Setup and Test mbed I/O Hardware Check-Off Deadline: Thursday, March 17, Name:

XLink Kai Raspberry Pi Beginners Tutorial

Button Code Kit. Assembly Instructions and User Guide. Single Button Code Entry System

A Slice of Raspberry Pi

Project 15 - Reading a keypad with the Raspberry Pi

Installation of RedHawk 6.5-r24.2 on the Jetson TX1 Development Board Release Notes. September 19 th, 2017

Adafruit 1-Wire GPIO Breakout - DS2413

Theo. Miles. Olivia. Joseph. Nina. Kishan. The Richard Pate School, Cheltenham

TWO PLAYER REACTION GAME

Basic Electronics and Raspberry Pi IO Programming

INTRODUCTION HARDWARE

This is the Arduino Uno: This is the Arduino motor shield: Digital pins (0-13) Ground Rail

Adafruit I2S MEMS Microphone Breakout

Introducting Itsy Bitsy 32u4

Raspberry Pi based MP3 jukebox with USB display board 2014 Russell Kramer

RPi General Purpose IO (GPIO) Pins

Adafruit DPI Display Kippah

BehringerMods.com. Instructions for modification of Behringer SRC analog inputs and outputs

Adafruit PowerBoost Charger

J. La Favre Calibrating Sharp Distance Sensors July 18, 2018

Connecting LEDs to the ADB I/O

Adafruit PowerBoost Charger

Gravity: I2C ADS Bit ADC Module(Arduino & Raspberry Pi Compatible) SKU: DFR0553

2.2" TFT Display. Created by lady ada. Last updated on :19:15 PM UTC

Transcription:

Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing Created by Simon Monk Last updated on 2016-11-03 12:19:05 AM UTC

Guide Contents Guide Contents Overview Other Code Libraries Parts Hardware DS18B20 Add OneWire support Software Configure and Test Adding more sensors 2 3 3 4 7 11 11 13 15 15 Page 2 of 15

Overview The Occidentalis Linux distribution for Raspberry Pi (and Raspbian as of Dec 2012) includes support for the DS18B20 1-wire temperature sensor. These sensors come in a small three pin package like a transistor and are accurate digital devices. In this lesson, you will learn how to use a DS18B20 with the Raspberry Pi to take temperature readings. Since the Raspberry Pi has no ADC (Analog to Digital Converter), it cannot directly use an analog temperature sensor like the TMP36, making the DS18B20 a good choice for temperature sensing. Other Code Libraries If you are a python-fancier, there's also a nice DS18B20 library that works with raspberry pi over at https://github.com/timofurrer/ds18b20 (http://adafru.it/dpk) (written by Timo!) Page 3 of 15

Parts To build the project described in this lesson, you will need the following parts. Raspberry Pi Page 4 of 15

DS18B20 Digital temperature sensor + extras, or waterproof or high temperature versions (see products sidebar) You'll also need a 4.7K or 10K ohm resistor, it comes with the sensor when purchased from Adafruit Half-sized breadboard Page 5 of 15

Jumper wire pack Pi Cobbler Page 6 of 15

Hardware The breadboard layout for just the basic DS18B20 is shown below. The DS18B20 "1-wire" sensors can be connected in parallel - unlike nearly any other sensor sold! All sensors should share the same pins, but you only need one 4.7K resistor for all of them The resistor is used as a 'pullup' for the data-line, and is required to keep the data transfer stable and happy Be careful to get the DS18B20 the right way around. The curved edge should be to the left as shown in the figure below. If you put it the wrong way around, it will get hot and then break. Despite both being temperature sensors, the DS18B20+ is totally different than the TMP36. You cannot use a TMP36 for this tutorial! Page 7 of 15

If you are using the waterproof version of the DS18B20 then the device has three leads, red, black and yellow. The bare copper screening lead that does not need to be connected. If you are using the "high temperature" version of the DS18B20 we sell, connect Orange Stripe to 3.3V, White connects to ground and Blue Stripe is data, pin #4. You still need a ~4.7K-10K resistor from data to 3.3V Page 8 of 15

Page 9 of 15

Page 10 of 15

DS18B20 Although the DS18B20 just looks like a regular transistor, there is actually quite a lot going on inside. The chip includes the special 1-wire serial interface as well as control logic and the temperature sensor itself. Its output pin sends digital messages and Raspbian/Occidentalis includes an interface to read those messages. You can experiment with the device from the command line or over SSH (see Lesson 6 (http://adafru.it/awc)), before we run the full program. Add OneWire support Start by adding the following line to /boot/config.txt You can edit that file with nano by running sudo nano /boot/config.txt and then scrolling to the bottom and typing it there dtoverlay=w1-gpio Then reboot with sudo reboot. When the Pi is back up and you're logged in again, type the commands you see below into a terminal window. When you are in the 'devices' directory, the directory starting '28-' may have a different name, so cd to the name of whatever directory is there. sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices ls cd 28-xxxx (change this to match what serial number pops up) cat w1_slave The interface is a little unreliable, but fortunately it tells us if there is a valid temperature to read. It's like a file, so Page 11 of 15

all we have to do is read The response will either have YES or NO at the end of the first line. If it is yes, then the temperature will be at the end of the second line, in 1/000 degrees C. So, in the example above, the temperature is actually read as 20.687 and then 26.125 degrees C. If you have more than one Sensor connected, you'll see multiple 28-xxx files. Each one will have the unique serial number so you may want to plug one in at a time, look at what file is created, and label the sensor! Page 12 of 15

Software The Python program deals with any failed messages and reports the temperature in degrees C and F every second. 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' def read_temp_raw(): f = open(device_file, 'r') lines = f.readlines() f.close() return lines def read_temp(): 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 = float(temp_string) / 1000.0 temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c, temp_f while True: print(read_temp()) time.sleep(1) The program starts by issuing the 'modprobe' commands that are needed to start the interface running. The next three lines, find the file from which the messages can be read. A problem has been reported with occasional hangs when reading the temperature file when using Raspbian. If you find you have the same problem, try replacing the function read_temp_raw with the code below. You will also need to add a line at the top of the file 'import subprocess'. def read_temp_raw(): catdata = subprocess.popen(['cat',device_file], stdout=subprocess.pipe, stderr=subprocess.pipe) out,err = catdata.communicate() out_decode = out.decode('utf-8') lines = out_decode.split('\n') return lines Reading the temperature takes place in two functions, read_temp_raw just fetches the two lines of the message from the interface. The read_temp function wraps this up checking for bad messages and retrying until it gets a message with 'YES' on end of the first line. The function returns two values, the first being the temperature in degrees C and the second in degree F. Page 13 of 15

You could if you wished separate these two as shown in the example below: deg_c, deg_f = read_temp() The main loop of the program simply loops, reading the temperature and printing it, before sleeping for a second. To upload the program onto your Raspberry Pi, you can use SSH to connect to the Pi, start an editor window using the line: nano thermometer.py and then paste the code above, before saving the file with CTRL-x and Y. Page 14 of 15

Configure and Test The program must be run as super user, so type the following command into the terminal to start it: sudo python thermometer.py If all is well, you will see a series of readings like this: Try putting your finger over the sensor to warm it up. Adding more sensors You can add additional DS18B20 sensors in parallel - connect all the sensors' VCC, data and ground pins together. Use a single 4.7K resistor. You will see multiple /sys/bus/w1/devices/28-nnnnn directories, each one having the unique serial number as the directory name. The python example code only works for one sensor right now so you will have to adapt it if you want it to read from different sensors at once Adafruit Industries Last Updated: 2016-11-03 12:19:04 AM UTC Page 15 of 15