Mustang65
Posts: 10
Joined: Fri Dec 06, 2013 2:20 am

IndexError: list index out of range

Sat Mar 30, 2019 11:03 pm

Well, I decided to try my luck at using a Pi to work with my DS18B20 temperature sensors. I am inching along but hit a snag. I am getting a "IndexError: list index out of range" error message. Has to be something dumb that I am or am not doing. Would appreciate any assistance with getting past this error.
Thanks
Don

ERROR:
pi@EAG1 /ds18b20 $ sudo python temperatures2.py
Traceback (most recent call last):
File "temperatures2.py", line 9, in <module>
device_folder = glob.glob(base_dir + '28*')[0]
IndexError: list index out of range

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)
        

plugwash
Forum Moderator
Forum Moderator
Posts: 3614
Joined: Wed Dec 28, 2011 11:45 pm

Re: IndexError: list index out of range

Sun Mar 31, 2019 12:35 am

Your error means it didn't find anything that matched /sys/bus/w1/devices/28*

Mustang65
Posts: 10
Joined: Fri Dec 06, 2013 2:20 am

Re: IndexError: list index out of range

Sun Mar 31, 2019 1:13 am

Thank you,
I will check into that.
Don

Andyroo

Re: IndexError: list index out of range

Sun Mar 31, 2019 1:36 am

You can check if any devices exist from the command line by using:

Code: Select all

ls /sys/bus/w1/devices/
and see if anything shows up.

It would be worth checking the cabling and pull up resistor - these are the normal reason why the chip does not show up. Feel free to post any pictures if you get stuck.

I would also look to turn on I2C using

Code: Select all

sudo raspi-config
and remove

Code: Select all

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
from the code - it cuts out any issues that are not trapped by your program.

DirkS
Posts: 10362
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: IndexError: list index out of range

Sun Mar 31, 2019 1:51 am

Andyroo wrote:
Sun Mar 31, 2019 1:36 am
I would also look to turn on I2C using

Code: Select all

sudo raspi-config
That should be 1-wire, not i2c

And the modprobes should definitely be removed from the code

Andyroo

Re: IndexError: list index out of range

Sun Mar 31, 2019 2:12 am

DirkS wrote:
Sun Mar 31, 2019 1:51 am
Andyroo wrote:
Sun Mar 31, 2019 1:36 am
I would also look to turn on I2C using

Code: Select all

sudo raspi-config
That should be 1-wire, not i2c

And the modprobes should definitely be removed from the code
I’m off to bed :lol: :oops:

Mustang65
Posts: 10
Joined: Fri Dec 06, 2013 2:20 am

Re: IndexError: list index out of range

Mon Apr 01, 2019 12:42 am

Found the problem. The jumper wire that I was using for data was open. I replaced the jumper and low and behold there was the 28-xxxxxx DS18B20 number. Sorry about that. I checked the voltages and they were ok, did not think that I would have a bad jumper on the data connection.
Thanks again
Do

Return to “Python”