About Us Knowledge Base Community Forum Electronics Toolbox FAQ Shipping & Delivery

Community Forum

Share your projects and post your questions

Search
Register or Sign In
Forum

DS18B20 on IO ports

897 Views - Created 27/06/2018

27/06/2018

Posted by:
lucky93

Last edited: 27/06/2018

Users Avatar

Location:
Denamark

Hey, I am a beginner to python and circuts in general.

My issue is i want to use the IO ports of the expander pi to read the DS18B20. I found a tourtorial for the PI's GPIO ports that worked. 

http://www.circuitbasics.com/raspberry-pi-ds18b20-temperature-sensor-tutorial/

But i cannot see what i have to change, to get it to use the IO ports instead.

Code from the tutorial:

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
        return temp_c
    
while True:
    print(read_temp())    
    time.sleep(1)

 

And the code i tried to write:

#!/usr/bin/env python

import time

#Imports the ExpanderPi's IO port libary
try:
    import ExpanderPi
except ImportError:
    print("Failed to import ExpanderPi from python system path")
    print("Importing from parent folder instead")
    try:
        import sys
        sys.path.append('..')
        import ExpanderPi
    except ImportError:
        raise ImportError(
            "Failed to import library from parent folder")


SleepTimeH = 2 #delay for the program before continueing

#Activates the IO port 0 (pin 1-8) and activates the relay
iobus = ExpanderPi.IO()

iobus.set_port_direction(0, 255)


while True:
    iobus.read_pin(3)
    print(iobus.read_pin(3))
    time.sleep(SleepTimeH)

I know my code just prints the reading from IO port. Which is 0 or 1

The math is easy to implement, but I cant get the right numbers out. 

 

27/06/2018

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

Hi

Unfortunatly due to the way the 1 Wire protocol works the IO port on the Expander Pi is not suitable for communicating with a DS8B20 sensor.  What you will need is a dedicated 1 Wire interface like our 1 Wire Pi Plus.

AB Electronics UK Tech Support

27/06/2018

Posted by:
lucky93

Users Avatar

Location:
Denamark

Hi Andrew

I think, I will just use the GPIO ports instead, since I already have some working code. Just need to expand it for more sensors and writing to a text file :) 

Thanks for the fast reply.

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view, download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.