Python and Raspberry Pi Temperature Sensor

It's been quite a long time, when I’d first posted this RPi sample. Time passed, I'd gathered some feedback form the community, took into account the comments, and this sample is ready to be published at Instructables for the public interested in microcontrollers and the Internet of Things.

Python and Raspberry Pi Temperature Sensor

What does this example show? How easy, in just a few hours, you can build your first IoT fun project. Building a remote temperature sensor and a remotely controlled LED can be a very good starting point.

It involves replacing LED with power relay and controlling light in your room, adding humidity sensors and, say, light sensors to better monitor the environment and report everything on your mobile device.

This sample can work on virtually any embedded linux board that has Python and 1-wire kernel drivers. It’s possible to connect a number of 1-wire devices to the same line.

You can check the video and follow the 1-6 steps instructions.

Step 1: You Will Need

  • Rasbperry Pi Rev. B (http://raspberrypi.org) or any other inexpensive Embedded Linux development board, like gumstix, or beaglebone.
  • Dallas 1-wire temperature sensor: DS18B20 – available everywhere :-).
  • Any LED
  • 510 ohm resistor for LED and 4.7K ohm (or so) resistor for DS18B20
  • Breadboard

The schematic is as follows (pic.1), the sensor will be powered from RasPi’s Pin 2. No external power needed.

Step 2: Configuring Raspberry Pi

  1. Download SD card image of Raspbian and expand it onto an SD card.
  2. Find a reliable power supply – Micro-USB smartphone chargers are usually a good choice. It should be capable of supplying 5V up to 1A (if you want to use a wifi module with it).
  3. You don’t have to use HDMI display and USB keyboard – you can connect it to your laptop or network using Ethernet and let RasPi obtain the DHCP address and connect to it using ssh using pi/raspberry as login/password.
  4. Connect LED/Sensor as shown on Figure 1 above.
  5. Load 1-wire kernel modules that come pre-installed but not loaded: sudo modprobe w1-gpio and then sudo modprobe w1_therm
  6. Add lines w1-gpio and w1_therm into /etc/modules using sudo nano /etc/modules so they get loaded automatically the next time you restart it.
  7. Find your sensor: ls /sys/bus/w1/devices/ it should look like 28-00000393268a
  8. Test the sensor by printing its output: cat /sys/bus/w1/devices/28-00000393268a/w1_slave

Step 3: Installing DeviceHive on Raspberry Pi

Python and Raspberry Pi Temperature Sensor Schemetic

  1. Download the latest version of DeviceHive.
  2. Connect to your RasPi using ssh. Create a folder for devicehive sources:mkdir ~/devicehive
  3. Using scp, copy the Python source code from your local machine to RasPi (for example: scp –r ~/Downloads/devicehive/python/device/Source/* [email protected]:~/devicehive). This will copy all libraries and samples.
  4. Go to devicehive.com/playground and create an account for yourself for a playground if you don’t already have one.

 

For more detail: Python and Raspberry Pi Temperature Sensor


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top