element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Adding a temperature sensor to my Pi 2
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Former Member
  • Date Created: 17 Mar 2015 9:43 AM Date Created
  • Views 926 views
  • Likes 5 likes
  • Comments 4 comments
  • pi
  • rpibeginner
  • ds18b20
  • raspberry_pi_projects
  • temperature_sensor
Related
Recommended

Adding a temperature sensor to my Pi 2

Former Member
Former Member
17 Mar 2015

I decided to add my temperature sensor to my new Pi2. This is fairly straightforward - you need the following

 

  • 1 DS18B20 DS18B20 temperature sensor
  • 1 4.7K resistor4.7K resistor

 

Wiring it up

 

The DS18B20 sensor has three pins which are connected as follows. Note that the sensor has a domed side and a flat side - make sure you have it the right way round (i.e. flat side towards the pi in the breadboard setup shown below).

  • Pin 1 connects to GND
  • Pin 2 connects to GPIO4 on the Raspberry Pi
  • Pin 3 connects to the 3.3V supply (3V3) from the Pi

image

 

This is connected to the Pi as follows. Do this with the Pi turned off!

 

image

 

Coding

If you are using one of the 'new' Pis (i.e. the Pi 2), you will need to add the following line to file /boot/config.txt. If you don't do this then the temperature sensor wont be detected by the Pi.

 

  • Open the file for editing

 

sudo nano /boot/config.txt

 

  • Add the following to the end of the file

 

# Added to get temperature sensor working
dtoverlay=w1-gpio

 

  • Use Ctrl-X to save the file

 

Log on to the Pi and enter the following commands

 

sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
ls -l

 

The final command above lists the content of directory /sys/bus/w1devices - you will see something similar to the following.

 

image

Note that there is a directory consisting of numbers - i.e. 28-00000521fad0 in the example above. This is the unique directory for the temperature sensor and is the serial number of the sensor you have used. Make a note of this as you will need tit when accessing the sensor.

 

Reading the temperature

Cd to the directory that you noted above and then run command cat w1_slave. This will interrogate the sensor and display the output - e.g.

image

Note that the temperature is displays in Celcius to 3 decimal places - e.g. in the example above t=23500, so temperature is 23.5 Celcius.

  • Sign in to reply
  • Former Member
    Former Member over 9 years ago

    I meant temperature sensor, I want to display temperature from sensor for example as a widget on raspbian desktop or something like this, codebug also maybe the variant.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 9 years ago in reply to Former Member

    Yes If you want to measure the voltage, to display with a program. In fact the only thing you need is a codebug on serial port to Pi.

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago

    Is there any way to read sensor data with a human way, like visual program, or to show temp on codebug?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • troush118
    troush118 over 10 years ago

    I am new to Raspberry Pi and using sensors with it. This project was a cool way to "dip my toes" into sensors.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube