davidanddiesel
Posts: 15
Joined: Sun Jan 18, 2015 4:03 am

DS1820 Temp sensor on different pin?

Thu Jan 29, 2015 1:37 am

I am using a DS1820 temperature sensor with my raspberry pi. It only works if it is connected to GPIO BCM pin 4, however, in my project, GPIO pin 4 is going to be used by the Adafruit LCD Pi Plate display. Here is my python code:

Code: Select all

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)

User avatar
rpdom
Posts: 17172
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: DS1820 Temp sensor on different pin?

Thu Jan 29, 2015 7:39 am

From what I've read, you can change the pin used for the one-wire bus with an entry in /boot/cmdline.txt.

To use, for example, GPIO 27 you would add this in the line in cmdline.txt

Code: Select all

bcm2708.w1_gpio_pin=27
This method will be obsolete soon when the kernel upgrades to 3.18 (which you get if you do an rpi-update now), when Device Tree will take over. Then you may have to make a custom device tree overlay with the pin number in (unless it can be set as an option in config.txt - I'm not sure yet). Don't worry if this sounds tricky, it's not too hard to do. I may look into trying it on one of my Pis later.
[edit]
No, custom overlay will not be required. To use one-wire with 3.18 kernel and a custom pin you need to put

Code: Select all

dtoverlay=w1-gpio,gpiopin=27
into config.txt. Change 27 to the pin you want to use.

Return to “Troubleshooting”