marian2704
Posts: 10
Joined: Mon Jun 15, 2015 1:40 pm

SPIDEV code problem

Mon Jun 15, 2015 1:55 pm

Hi guys,
This code has been giving me some problems:

Code: Select all

import spidev
import time
spi = spidev.SpiDev()
spi.open(0,0)

def readadc(adcnum):
    if ((adcnum > 7) or (adcnum < 0)):
        return -1
    r = spi.xfer2([1,(8+adcnum)<<4,0])
    adcout = ((r[1]&3) << 8) + r[2]
    return adcout

while True:
    for adcInput in range(0,8):
        value = readadc(adcInput)
        voltage = value * 3.3
        voltage /= 1024.0
        tempCelsius = (voltage-0.5)*100
        print "---------------------------"
        print "ADC(", adcInput,")= ", value
        print "---------------------------"
        print "Voltage: ", voltage
        print "---------------------------"
        print "Temp: ", tempCelsius
    time.sleep(1)
I keep getting the error message:
"Traceback (most recent call last):
File temp.py, line 4 in <module>
spi.open(0,0)
IOError: [Errno 2] No such file or directory"
What exacly is wrong? Could it be something wrong with the sensor? Should I get a new one? How can I alter the code I do for it to print 3 to 5 values (with some 10-15 seconds between them) and stop there?
Last edited by marian2704 on Mon Jun 15, 2015 4:15 pm, edited 1 time in total.

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: code

Mon Jun 15, 2015 2:34 pm

Hi and welcome to the forum. I have moved this post to the python forum where is may get more attention.
However I suggest you re-post it here using the CODE tabs, as the formatting has been lost - something important for python listings......
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: code

Mon Jun 15, 2015 3:41 pm

Hello,

welcome in the python forum !
Most possibly you need to enable the spi driver. This is best done using
sudo raspi-config
Look into '8, advanced', 'A6, SPI' and enable this. Could be you need to reboot then.

Regards,
Gerhard

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: code

Mon Jun 15, 2015 3:50 pm

marian2704 wrote:Hi guys,
I keep getting the error message:
"Traceback (most recent call last):
File temp.py, line 4 in <module>
spi.open(0,0)
IOError: [Errno 2] No such file or directory"
Do you always get the error or does it sometimes work ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

marian2704
Posts: 10
Joined: Mon Jun 15, 2015 1:40 pm

Re: code

Mon Jun 15, 2015 4:03 pm

ghp wrote:Hello,

welcome in the python forum !
Most possibly you need to enable the spi driver. This is best done using
sudo raspi-config
Look into '8, advanced', 'A6, SPI' and enable this. Could be you need to reboot then.

Regards,
Gerhard
Thank you very much! It slipped my mind to activate it :lol: . I thought my sensor was fried. Can you help me with my other issue? I only need it to print 4-5 results with a few seconds between each result. Right now I get an endless list which moves too fast for me to even catch a glimpse on. I also plan to use other brick sensors (humidity, light, pressure, bending and a brick button) and I want it to print only the voltage. What modifications do I need to do? Are there any specific formulas for each sensor type? :?: This whole thing is part of my diploma project and I'm a bit stuck.

marian2704
Posts: 10
Joined: Mon Jun 15, 2015 1:40 pm

Re: code

Mon Jun 15, 2015 4:06 pm

PeterO wrote:
marian2704 wrote:Hi guys,
I keep getting the error message:
"Traceback (most recent call last):
File temp.py, line 4 in <module>
spi.open(0,0)
IOError: [Errno 2] No such file or directory"
Do you always get the error or does it sometimes work ?
PeterO

Just at this one. The first code worked without problems. This happened at the second one and I couldn't understand why. The first part was the same at both codes.

marian2704
Posts: 10
Joined: Mon Jun 15, 2015 1:40 pm

Re: code

Mon Jun 15, 2015 4:19 pm

texy wrote:Hi and welcome to the forum. I have moved this post to the python forum where is may get more attention.
However I suggest you re-post it here using the CODE tabs, as the formatting has been lost - something important for python listings......
Texy
Thank you! I've done as you asked. I learned that formatting is important the hard way, but I'd rather learn Python instead of C anytime. Is there any chance I'd might get some help with my other issue?

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: code

Mon Jun 15, 2015 4:23 pm

How does this work for you?
Will print out the data from all 8 channels 5 times with a second gap between each, wait 10 seconds then print out the results again.

Code: Select all

while True:
    for i in range(5):
        for adcInput in range(0,8):
            value = readadc(adcInput)
            voltage = value * 3.3
            voltage /= 1024.0
            tempCelsius = (voltage-0.5)*100
            print "---------------------------"
            print "ADC(", adcInput,")= ", value
            print "---------------------------"
            print "Voltage: ", voltage
            print "---------------------------"
            print "Temp: ", tempCelsius
            time.sleep(1)
        time.sleep(10)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

marian2704
Posts: 10
Joined: Mon Jun 15, 2015 1:40 pm

Re: code

Mon Jun 15, 2015 4:41 pm

scotty101 wrote:How does this work for you?
Will print out the data from all 8 channels 5 times with a second gap between each, wait 10 seconds then print out the results again.

Code: Select all

while True:
    for i in range(5):
        for adcInput in range(0,8):
            value = readadc(adcInput)
            voltage = value * 3.3
            voltage /= 1024.0
            tempCelsius = (voltage-0.5)*100
            print "---------------------------"
            print "ADC(", adcInput,")= ", value
            print "---------------------------"
            print "Voltage: ", voltage
            print "---------------------------"
            print "Temp: ", tempCelsius
            time.sleep(1)
        time.sleep(10)
Sweet! I like it! Thank you! But it gives me a syntax error at "for i in range (5)" You don't happen to know how can I find the conversion rate for sensors so I can modify this code and use it for the other sensors in my project, do you?

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: code

Mon Jun 15, 2015 5:32 pm

Try this instead

Code: Select all

for i in range(1,5):
I don't know what you mean by conversion rate.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

marian2704
Posts: 10
Joined: Mon Jun 15, 2015 1:40 pm

Re: code

Tue Jun 16, 2015 5:06 am

Still sees it as an invalid syntax.I was reffering to this part of the code:

Code: Select all

value = readadc(adcInput)
  voltage = value * 3.3
  voltage /= 1024.0
Which converts the input from the sensor into voltage and that voltage is printed out later. I was wondering how and where can I find the information I need in order to write similar codes for other sensors. I have a brick sensor for humidity, one for light, one for pressure and one for bending plus a brick button. I want it print the values a few times and stop.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: SPIDEV code problem

Tue Jun 16, 2015 9:18 am

I can't replicate the error you are seeing. The code runs fine for me. Check the indentation of your code.

On the topic of the 'conversion rate', you need to look at the data sheets for each of the sensors. This will tell you how the voltage it outputs relates to the physical quantity it is reading.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

marian2704
Posts: 10
Joined: Mon Jun 15, 2015 1:40 pm

Re: SPIDEV code problem

Tue Jun 16, 2015 10:28 am

scotty101 wrote:I can't replicate the error you are seeing. The code runs fine for me. Check the indentation of your code.

On the topic of the 'conversion rate', you need to look at the data sheets for each of the sensors. This will tell you how the voltage it outputs relates to the physical quantity it is reading.
It must be a software glitch. I'll look into it or rewrite the code in a different file. Thank you for all the help.

Return to “Python”