OKbgreat wrote:Please post a drawing of how you have MCP3004 connected and your code. Unless there is a hardware failure, the issue is either wiring or software. With the additional information, it will be possible to help you determine the cause.
Enjoy!
Bill

Code: Select all
# Python Code to communicate with MCP3004
#Import SpiDev wrapper to enable hardware SPI
import spidev
#Establish SPI connection with Bus 0, Device 0
spi = spidev.SpiDev()
spi.open(0,0)
def get_adc(channel):
#Check valid channel
if((channel > 3) or (channel < 0)):
return -1
#Perform SPI transaction and store returned bits in 'r'
r = spi.xfer([1, (8+channel)<<4, 0])
#Filter data bits from returned bits
adcout = ((r[1]&3) << 8) + r[2]
#Return value from 0-1023
return adcout
while True:
print get_adc(0)
Thanks very muchbgreat wrote:Sorry for the delay, but I have been rather busy. Nothing obvious in the wiring. I will take a closer look at this tonight on my Raspberry Pi and post back.
Enjoy!
Bill
xfer not xfer2.Code: Select all
sudo rmmod spi_bcm2708
sudo modprobe spi_bcm2708
I'm afraid nothing on this post made it work, the spiConfig function didn't say anything was wrong, I rebooted multiple times and tried that rmmod and modprobe commands and nothing. Is there a way simply to uninstall and reinstall the SPI drivers or something. Maybe it thinks it's working but doesn't know which pin is the chip select or something?bgreat wrote:It almost seems that the SPI is not enabled for the GPIO lines. I recall an earlier thread that discussed an issue with using the SPI for GPIO and then attempting to use it for SPI required a reboot. Can you confirm that no other application has used the same GPIO pins for IO before you run your program?
If you have not added any code to the system start up, this is as simple as rebooting the Raspberry Pi and testing your program before doing any other action.
Another option is to simply unload and reload the spidev module:You could also try using my spiConfig() function from this SPI display thread to make certain the configuration settings are correct.Code: Select all
sudo rmmod spi_bcm2708 sudo modprobe spi_bcm2708
Enjoy!
Bill
Well I did the tests and they work fine. The pins work as both inputs and outputs perfectly fine. I left the LEDs hooked up to the pins and reenabled the SPI module and ran the SPI program and I can see that it's sending data and the clock is running as the LEDs light up dimly, but both chip select lines stay high.bgreat wrote:The rmmod/modprobe commands basically remove the SPI driver from memory and then restart it. If the driver is corrupted or an incompatible version you should receive a message indicating a problem when the modprobe command is executed.
Barring an issue with your wiring or ADC, it is possible you have have a hardware issue with the Raspberry Pi GPIO. I would recommend wiring the SPI pins to LEDs with resistors and use a test program to confirm they function as GPIO outputs. Then test the same GPIO pins as inputs. If these tests work, I am at a loss as to where to look next without direct access to your system and connections.
Enjoy!
Bill
You can verify this by emulating the Chip Select line on another gpio that you control from your program.frasmacon wrote: So now the problem has been narrowed down to: SPI chip select lines don't go low.
Code: Select all
gcc -o spidev_test spidev_test.c
./spidev_test --device /dev/spidev0.0
I never thought about that...notro wrote:You can verify this by emulating the Chip Select line on another gpio that you control from your program.frasmacon wrote: So now the problem has been narrowed down to: SPI chip select lines don't go low.
Set it initially HIGH
Set it LOW
Send data with SPI
Set it HIGH
Have you tried the loopback test, to see if send/receive works?
It's in the kernel source: https://github.com/raspberrypi/linux/bl ... dev_test.c
Connect a wire between MISO and MOSI.It will not test the Chip Select signals.Code: Select all
gcc -o spidev_test spidev_test.c ./spidev_test --device /dev/spidev0.0
I'm sure it's getting enough power as the 3v3 is one of the recommended voltages for the chip and I have used it before. I think I could have captured the CS line go low with the LED as it's low more than it's high and even with the function where it doesn't put the CS line high again, the LED stays on max brightness. I will try various ways of getting it work. Worst case scenario just bitbang it I guess without trying another language.devindunseith wrote:Are you definitely giving the ADC enough power? I had a similar problem with an AD7908 ADC because I was trying to drive it off the Pi's 5V supply (which actually sits at ~4.7 V). The Pi's supply lines and ground are also a bit noisy which can cause errors in the ADC.
After a few days of it not working today I finally powered the ADC off an DC power supply, and connected the supply's ground to the ADC ground and it worked first time. It's possible your SPI chip select is working fine but you can't see it go low with an LED. I spent a lot of time trying to work out what was wrong with my RPi and code only to realise it was just that the chip didn't have enough power.
If you're not having any joy getting the MCP3004/8 chip to work with SPI, it should actually be pretty easy to run it directly through the GPIO pins using the RPi.GPIO python libraries (I can send you my code which I used to do just this with the AD7908 - it's on my work computer so I'll have to get it tomorrow).
Unfortunately I have already tried the SPI equivalent with no joy. I am going to wipe the SD card and try with a fresh distro. If I could have some code to do what I said further up the post in C or something incase it's the Python wrapper.Wholetthesmokeout wrote:I don't know if this will help, but I was having issues with the I2C bus (http://www.raspberrypi.org/phpBB3/viewt ... 44&t=39925) and I think what solved the issue was setting the I2C speed to a whopping 1 Hz and then back to 32KHz. It's just a suggestion akin to 'turn it off and back on again'.
Did you check that the CE line is going low when you did this? If you're wiggling it manually you can put in a sleep() or input() when it's low, to make sure you don't miss it.frasmacon wrote:I tried doing the chip select line manually, turning it low before doing the transfer function then turning it back on and it didn't seem to work. Is there any way it could accidentally use the wrong pins for the proper SPI?
Yes I checked. I can manually make the chip select lines go high and low but that still makes a problem which makes me think the may be something wrong with the code but I don't know what...IanH2 wrote:Did you check that the CE line is going low when you did this? If you're wiggling it manually you can put in a sleep() or input() when it's low, to make sure you don't miss it.frasmacon wrote:I tried doing the chip select line manually, turning it low before doing the transfer function then turning it back on and it didn't seem to work. Is there any way it could accidentally use the wrong pins for the proper SPI?
If you can't set GPIO 7 or 8 low from Python, perhaps the hardware is broken.
Ian
In theory it almost defiantly should work that way but I don't know what it's doing...trouch wrote:/dev/spidev0.0 => Chip Enable 0 (CE0 / Pi header pin 24 / GPIO 8)
/dev/spidev0.1 => Chip Enable 1 (CE1 / Pi header pin 26 / GPIO 7)