Skip navigation

STEM Academy

3 Posts authored by: ntewinkel Top Member

Hi all,


In this blog post, I show how to read the temperature from a DS18B20 temperature sensor using Python on the Raspberry Pi.

This sensor is nice not only in that it's inexpensive, but it's also a digital sensor. Given that the Raspberry Pi doesn't easily handle analog inputs, the DS18B20 is a great choice.


In case you need it, here are my previous posts to help you get started with Python programming and IO pins for the Raspberry Pi:

Raspberry Pi - HelloWorld

Raspberry Pi - Blink


For this tutorial, I am using:

1x Raspberry Pi Model B+ running Raspbian (other models should work fine too)

1x breadboard to make the connections

1x DS18B20 temperature sensor - I'm using a waterproof one with a foot long cable containing red, black, and yellow leads

3x M-F jumper wires

1x 10KΩ resistor


Edit: I tried this on a new RaspberryPi and it didn't work. Turns out I missed this step:

- Add the following line to /boot/config.txt


- You can edit that file with the leafpad editor by running "sudo leafpad /boot/config.txt" and then adding that line at the very bottom.

- Save, and reboot.


The hardware connections:

- put the 3 leads of the DS18B20 into the breadboard. (I found this worked ok enough, but ideally it's better to solder some good tips onto them for stronger connections.)

- connect a jumper wire from the yellow (data) wire to pin 7 (GPIO4) on the Raspberry Pi

- connect a jumper wire from the red (power) wire to pin 1 (3.3v) on the Raspberry Pi

- connect a jumper wire from the black (ground) wire to pin 9 (GND) on the Raspberry Pi

- add a 10KΩ resistor between data and power


So that's the hard part.

This is what is looks like:


(Check out my duct tape Pi case!)


Now open up the attached example ( and run it, and you should see temperature numbers showing up on the Python console.

If you see error messages, it is most likely due to a loose wire. In my case the DS18B20 leads are a little short and one popped out, which caused some scary looking error messages when I tried to run the script!

The other thing I forgot at first was that 10K resistor - it is vitally important too.


This is what the output should look like, with numbers depending on your temperature, of course. Try putting your fingers on the sensor to see how that affects the readings.



For more details, also check out this Adafruit tutorial, which I used as a guide to get started.

The attached Python code is also derived from their example.


Edit: This has been tested and works on both the RPi model B+ and the RPi 2 model B.





Raspberry Pi - Blink

Posted by ntewinkel Top Member Nov 19, 2014

Hi all,


In this blog post, I go through the basic steps required to blink an LED on a Raspberry Pi - the basic microprocessor way of saying "Hello World!"


In case you missed it and need help getting started on the basics of writing and running a Python program on the Raspberry Pi, my previous blog post goes into a lot more detail of those first steps: Raspberry Pi - HelloWorld


For this tutorial, I am using:

1x Raspberry Pi Model B+ running Raspbian (other models should work fine too)

1x breadboard to make the connections

1x LED

1x 470Ω resistor (the precise value is not important)

2x M-F jumper wires


One of the great features of the Raspberry Pi is that it includes IO pins which, like an Arduino, allow you to connect it to other bits of hardware.

But, unfortunately, the default installation does not allow access to the IO pins, so the first step here is to change things so that we do have access.


Thankfully this can be done very easily by just changing the settings of the "IDLE 3" desktop icon.

To do this, simply right-click on the IDLE 3 icon, then choose "Open With...", toggle to see Accessories, and then select Leafpad and click OK.

This pops up an editor showing several lines of text.

For the line that starts with "Exec", after the = sign add "sudo " (just the part within the quotation marks, including the space)

This will run the Python IDE as "root", giving it full access to the machine.

Now save and exit, and you are ready to blink the LED!



To connect the LED:

- put the LED into the breadboard

- connect one side of the 470Ω resistor to the short lead of the LED

- connect a wire from the long lead of the LED to pin 11 (GPIO17) on the Raspberry Pi

- connect a wire from the other side of the 470Ω resistor to pin 9 (GND) on the Raspberry Pi.


Now the fun begins!

You can open up a new window and try this yourself, or just download the attached example and open it.


To use the GPIO pins, you will need to import the correct library:

import RPi.GPIO as GPIO


Then you need to get the GPIO system set up:



And the output pin has to be set up as output:

GPIO.setup(11, GPIO.OUT)


After that basic setup you can then send a high or low value to the pin:

GPIO.output(11, GPIO.HIGH)


GPIO.output(11, GPIO.LOW)


When the script finishes, remember to clean up:



In my example I use a little loop and a timer to turn the LED on and off 10 times.



It's actually quite easy to use once you know the basics


ps, I've given a bit less detail in the steps than in my last post, as that would make this a very long post, but please feel free to ask questions if any of it is unclear.


edit: Here is a picture of how the IO pins are arranged for the B+


Next: Raspberry Pi - DS18B20 Temperature Sensor


Edit: This has been tested and works on both the RPi model B+ and the RPi 2 model B.




Hi All,


In this blog post I go through the very first basic steps of programming a Raspberry Pi using Python, to run the familiar starting program "Hello World"


Note that this is geared for people who are complete beginners with Raspberry Pi.


My first thought was that this must have been done a zillion times before, but when I started looking I couldn't find anything that summed it up in easy steps, and I found myself having to dig a little just to get the first steps figured out. Not too difficult, but a bit annoying. So I thought I may as well track what I'm doing and make this blog post, in the hopes that it might make someone else's life a bit easier.


First of all, I'm assuming that your Raspberry Pi is already set up, and that you are using the Graphical User Interface. I'm using Raspbian here, and all my steps will be based on that. I'm also using the version of Raspbian that came on a NOOBS SD card, circa October 2014. So if your version is older it may not have all the tools installed in the same way. In that case, please refer to the many other resources out there that can do a much better job than I of explaining how to update and/or install the software.

If all else fails, it might be easiest just to get a new SD card and install the latest version of Raspbian on it.


I am doing this on a Raspberry Pi Model B+, but everything should work the same way on other models.


The tool we will use is "IDLE 3", which is a handy little Python editor. It comes pre-installed with Raspbian.


The first step is to just double-click the IDLE 3 icon to open the "shell" - basically a line by line Python interpreter.

Now you can try your very first line of Python: at the >>> prompt, type this line:

print("Hello World!")

then press the enter key. The shell should respond by printing "Hello World!" on the next line and then giving you a new >>> prompt.


The shell provides a nice and easy way to try single line Python commands, but most programs require more than one line. To accomplish this, you need to either open a file, or create a new file.

Let's create a new file: from the file menu of the Python Shell window, choose "New Window".

A new window titled "Untitled" will pop up on your desktop, and it'll be a blank slate. We'll just ignore the obvious self-contradiction of the title of the window. But this is where you can now start typing in your program.


Keeping with the super-easy theme, let's also make it say "Hello World!", by typing the same line as before, but this time type it into the Untitled window.

You'll notice that it won't automatically try to run it. That's the point - it gives you time to type out a more complex program before it tries to run it.


To run it, we'll need to save the file first. The usual ctrl-S or File-Save will do the trick. Put it somewhere where you'll be able to find it back later, of course. I prefer to put it under "Documents", as I'm not using this Raspberry Pi computer for anything else.


Now from the file menu, select Run and Run Module to run your new Hello World program.


You'll notice that it switches the window selection to the Python Shell, and then prints a long line that says "RESTART", shows a prompt, then shows your "Hello World!" line, and then another prompt.




That's it! That's all there's too it.


Now the next time you open IDLE 3, you can simply use "File-Open..." to open the existing file and run it again.


I hope that helps out. My next blog post will go on to show how to blink an LED - the basic microprocessor version of "Hello World"


Edit: This has been tested and works on both the RPi model B+ and the RPi 2 model B.