Go Down

Topic: SPI Library and UNO problems? (Read 1 time) previous topic - next topic


Are there any known issues with the SPI Library and UNO?  I can't seem to get it to work, but I'm new to both of these and the chip I'm trying to control, (MCP2515).

I've had SPI working with a Sanguino and a Pro Mini along with a SCP1000 chip, but without using the library.  So I know I can do it.  But right now I don't seem to be getting anything out of the MOSI/MISO/SCK pins.  Using CS on pin 10 I can get that to go low and then back to high, but MOSI (pin 11) just stays high.  SPI.transfer doesn't even seem to be doing anything.

I'm using 0021 and have the Uno selected.  I have written a small sketch that simply toggles MOSI and CS with 10ms delay in the main loop.  I can see this on my scope to confirm it works.  When I add SPI.begin() into the setup function MOSI goes high and stays high even in the loop.  Is this expected behavior?

I've hacked about with this so much yesterday that I'm lost.  I'm going to try and start afresh with a new sketch and see where that gets me.  I'm also going to try it on my Sanguino.  And I happen to have an SCP1000 that I'll try with the example code from the web site, although I'm not sure if the chip is good or not.

Any advice for troubleshooting steps or what I might be missing would be welcome!


There have been some bugs found in the new SPI library. For example:


Maybe try to go back to what you had working and wait for the next release?

The Quick Shield: breakout all 28 pins to quick-connect terminals


OK, I think I'm tracking down my problem.  Thanks to RC I realized that the clock divider bug in the library wasn't my problem, it was that I wasn't actually setting the divider to begin with!  Now with the library fixed and the divder set to 128 I can see my signals on MOSI are correct.

Next step is to see if I can read back from the MCP2515.  If I get success I think I'll put this one down to both me and the UNO going too fast. :D


Well this is frustrating.  I got it all working by setting the divider to 128.  Ran through a couple of tests writing to one of the registers and then reading back from it, and displaying it in the Serial Monitor.  All worked well.

I then started trying different dividers trying to see where it would fail.  Once the return value stopped being correct I went right back to clock divider of 128, and it still wouldn't work.  Went and cooked a pizza, came back and it was working again.  (All my code is in the setup function and I was pushing the Reset button.)

Again I started changing the divider values, and again it eventually stopped working.  I found that if I left a few seconds between pressing the Reset button sometimes it would start working again.  I finally set it back to a divider of 128 but couldn't get it working after that until I closed down the IDE, left it for a while, reopened with a blank sketch, and then when I opened the Serial Monitor it was working.  Pressed the Reset button, not working.

This is highly unreliable to say the least.  What could be going wrong here?


Are you resetting the MCP2515 when you press the reset button? If not, then a bad SPI clock setting (too high) can put it in some kind of mode that requires you to reset it and/or pulse the chip select line.

The Quick Shield: breakout all 28 pins to quick-connect terminals


The first thing I do is issue a software reset command in my code.  I'm thinking this might be unreliable.  I'm going to try adding a small delay after this command, or pulse the CS line like you suggest.  And if that fails a hardware reset.


Thanks!  A hardware reset using a spare pin does the job.  Reliable down to clock divider of 2!!

Now for the rest of the code.  ::)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131