Monthly Archive:April 2017


Connecting DS18B20 Temperature Sensor to Raspberry Pi

The Parts Lists for the program shown below .

1)DS18B20 Temperature Sensor
2)Raspberry Pi
3)solderless breadboard
4)several jumper wires
Wiring the DS18B20 Temperature sensor to the RPi

It is important to double check that you don’t confuse Pin 1 and Pin 3 on the device otherwise the power will be applied the wrong way round!Once you have connected everything together you can power up your Raspberry Pi.

Temperature code:

Before we make the Python script we first need to setup the Pi so it can actually read data from the sensor. To do this we need to add OneWire support.

  1. To add support, we first need to open up the boot config file, this can be done by running the following command:
    sudo nano /boot/config.txt
  2. At the bottom of this file enter the following.
  3. Once done save & exit by pressing ctrl x and then y. Now reboot the Pi by running the following command.
    sudo reboot
  4. You can skip to downloading the code onto the Pi or follow the next few steps to check that the sensor is actually working.
  5. Once the Raspberry Pi has booted back up we need to run modprobe so we can load the correct modules.
    sudo modprobe w1-gpio
    sudo modprobe w1-therm
  6. Now change into the devices directory and use ls to see the folder/files in this directory.
    cd /sys/bus/w1/devices
  7. Now run the following command, change the numbering after cd to what has appeared in your directory by using the ls command. (If you have multiple sensors there will be more than one directory)
    cd 28-000007602ffa
  8. Now run the following command.
    cat w1_slave
  9. This should output data but as you will notice it is not very user friendly. The first line should have a YES or NO at the end of it. If it is YES, then a second line with the temperature should appear. This would look similar to something like t=12323, you will need to do a bit of math to make this a useable temperature that we can understand easily. For example, Celsius you simply divide by 1000.
    temperature output

Now it’s time to move onto the python script. Input following command.

git clone

To begin the python script, we import 3 packages, OS, Glob and time.

Next we run the modprobe commands these are the same as what we used before.

We then declare 3 different variables that will point to the location of our sensor data. You shouldn’t have to change any of these.

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'

In the read_temp_raw function we open up the file that contains our temperature output. We read all the lines from this and then return it so the code that has called this function can use it. In this case the read_temp function calls this function.

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    return lines

In the read_temp function we process the data from the read_temp_raw function. We first make sure that the first line contains YES. This means there will be a line with a temperature in it.

If there is a temperature we then find the line with t= by using lines[1].find('t='). Lines[1] means we’re looking at the 2nd element in the array, in this case the 2nd line. Once we have the line we simply get all the numbers that are after the t= this is done here lines[1][equals_pos+2:]. Equals_pos is the start position of the temperature (t) and we add 2 to the position so we only get the actual temperature numbers.

We then convert the number into both a Celsius and Fahrenheit temperature. We return both of these to the code that called this function. This is the print function located in the while loop.

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        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

The while loop is always true so it will run forever until the program is interrupted by an error or the user cancelling the script. It simply calls the read_temp within the print function. The print function allows us to see the output on our screen. The script is then put to sleep for 1 second every time it has read the sensor.

while True:

Once you have either downloaded or finished writing up the code and you also have setup the circuit correctly we can now call the Python script. To call the python script simply run the following command.

sudo python

You should now have an output of temperatures in both Fahrenheit and Celsius. You can alter this to just display your preferred temperature scale.


Connecting a Push Switch to Raspberry Pi

The programmable switch doesn’t required a lot of  electronic components and is quite easy to build. The Parts Lists for the programmable switch is shown below .

1)tactile pushbutton switch

2)Raspberry Pi

3)solderless breadboard

4)several jumper wires

Installing the RPi.GPIO Library

In order to read the tactile pushbutton switch status, the RPi.GPIO library needs to be installed on the Raspberry Pi. The RPi.GPIO  is a software module that conveniently allows the Raspberry Pi GPIO pins to be manipulated for electronic controls applications. To install the RPi.GPIO library onto the Raspberry Pi, open the LXTerminal and type the following linux installation command after the prompt:

pi@raspberrypi ~ $ sudo apt-get install python-dev python~rpi.gpio

After the linux installation command is entered, you will see a series of RPi.GPIO bulld-installation file sequences being displayed on the monitor as shown below.

Wiring the Tactile Pushbutton Switch to the RPi

Attaching a tactile pushbutton switch to the RPi is quite easy to do. An important item to remember is the RPi’s GPIO pins are +3.3VDC compliant. Applying voltages greater than +3.3VDC will damage the RPi. The electrical wiring diagram for attaching the tactile pushbutton switch is shown next.


Although, the circuit wiring to the RPi is quite simple, recheck the wiring before programming the RPi. This important verification step will assure you project execution success when the python code is installed and running on the RPi.

The Programmable Switch Python Script

The next phase of the project build is to provide a python script for reading a tactile pushbutton switch wired to a RPi GPIO pin. The python script for reading a tactile pushbutton switch is shown next.

import  RPi.GPIO as GPIO
import time

GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_UP)

while True:
    inputValue = GPIO.input(18)
    if (inputValue == False):
        print("Button press ")

You can entered this script using either the LXTerminal’s nano editor or with the Python’s IDLE (Integrated Development Enviroment). Save the script as in the home/pi directory of the RPi. Next, type the following linux command to run the script on the RPi into the LXTerminal as shown next.

Next, press the tactile pushbutton switch. If the script was typed correctly, you will see the message “button press” displayed on the monitor’s screen. Congratulations on building a programmable pushbutton switch!

pi@raspberrypi ~ $ sudo python

The tactile pushbutton switch can easily be programmed to provide a variety of output messages and switching responses. Try changing the “Button Press” message to display your name or a whimiscal word when activating the switch. Record your results in a laboratory notebook.