Will19
Posts: 5
Joined: Sun May 05, 2019 7:21 pm
Location: uk

Newbie problem.. tryinng to use GPIOZero routines to interface a DS18b20 sensor.

Sun May 05, 2019 7:35 pm

Hi all,

Previously done a bit of programming with the Arduino , but Totally new to RPi ,Python and Linux and seem to have fallen at the first hurdle.

Thought Python3 was the way to connect things with the GPIOZero routine and have done the basic led and button examples ok in Idle.

However when looking to read in something a bit more complex like a DS18b20 temp , seems you have to run them from the command line /Shell.

So despite reading around what I cannot see, yet, is how do the two methods come together as a complete project or do I just have to do everything in the Shell, which seems to negate the use of Idle for anything other than very simple control ?

User avatar
MrYsLab
Posts: 314
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Newbie problem..

Mon May 06, 2019 12:05 am

I may be wrong here, but I don't think Gpiozero provides an interface to the DS18b20. If you are willing to try pigpio http://abyz.me.uk/rpi/pigpio/index.html, you could the example code for that device: http://abyz.me.uk/rpi/pigpio/code/DS18B20-1_py.zip

You can use pigpio with Gpiozero https://gpiozero.readthedocs.io/en/stab ... t-gpiozero

PhatFil
Posts: 1197
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Newbie problem..

Mon May 06, 2019 2:13 am

you got the wrong end of the proverbial stick i think ;)

ds18b20 1 wire sensors are supported by raspbian itself if you connect one you can tell the raspbian system and it will be read as part of the normal system operation just like a usb stick or sd card is read when connected.
The pi system will read the sensor as part of its administration cycle and write its current reading to its file,

So your python script written in a text editor or ide like idle will access the h/w file for the sensor as the old unix mantra goes "Everythng is a file..." rather than access/import a driver module to do the work that the pi system has already done..

https://thepihut.com/blogs/raspberry-pi ... he-ds18b20

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

Re: Newbie problem..

Mon May 06, 2019 4:45 am

PhatFil wrote:
Mon May 06, 2019 2:13 am
ds18b20 1 wire sensors are supported by raspbian itself if you connect one you can tell the raspbian system and it will be read as part of the normal system operation just like a usb stick or sd card is read when connected.
The pi system will read the sensor as part of its administration cycle and write its current reading to its file,
Close, but the last part isn't quite right. Raspbian normally only periodically checks the sensor is present. It doesn't continuously ask for readings that will probably not be needed.

When you read the file a command is sent to the sensor to take and return a reading. The response from the command is formatted and returned as the content of the "file". The file itself is virtual and contains nothing. It is just an interface to the driver.
Signature is on holiday.

Will19
Posts: 5
Joined: Sun May 05, 2019 7:21 pm
Location: uk

Re: Newbie problem..

Mon May 06, 2019 9:32 am

Thanks all, think its slowly coming together for this OAP , though a long way to go ! :D

That Pi Hut link and tutorials also answered some of my other concerns about how to reboot and restart a monitoring program.

pcmanbob
Posts: 5942
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Newbie problem..

Mon May 06, 2019 11:24 am

You don't need any gpio driver be it RPi.GPIO or Gpiozero to read a DS18B20 using python.

You simply need to enable the 1-wire interface using raspi-config and then this python code will read the DS18B20 device and print out the temperature reading.

Code: Select all


import glob
import time


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)
	
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PhatFil
Posts: 1197
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Newbie problem..

Mon May 06, 2019 1:45 pm

rpdom wrote:
Mon May 06, 2019 4:45 am
PhatFil wrote:
Mon May 06, 2019 2:13 am
ds18b20 1 wire sensors are supported by raspbian itself if you connect one you can tell the raspbian system and it will be read as part of the normal system operation just like a usb stick or sd card is read when connected.
The pi system will read the sensor as part of its administration cycle and write its current reading to its file,
Close, but the last part isn't quite right. Raspbian normally only periodically checks the sensor is present. It doesn't continuously ask for readings that will probably not be needed.

When you read the file a command is sent to the sensor to take and return a reading. The response from the command is formatted and returned as the content of the "file". The file itself is virtual and contains nothing. It is just an interface to the driver.
Thanks for clarifying,

Will19
Posts: 5
Joined: Sun May 05, 2019 7:21 pm
Location: uk

Re: Newbie problem..

Mon May 06, 2019 3:37 pm

pcmanbob wrote:
Mon May 06, 2019 11:24 am
You don't need any gpio driver be it RPi.GPIO or Gpiozero to read a DS18B20 using python.

You simply need to enable the 1-wire interface using raspi-config and then this python code will read the DS18B20 device and print out the temperature reading.
Your code works fine, as you say without any need for the gpio driver.

still a lot to learn though ! :D

thanks

Return to “Python”