Arduino due spi problem and ide1.5.3 problem

Hello everybody,
yesterday i ran into 2 strange problems
Let me explain

I have 2 as5048a mag encoders which i have installed on my telescope and i use due to control them

seperetly they work fine.

So i deviced a small circuit that plug on the spi header (near the sam) and distributes the signals to and from the encoders

The problem is that the 1st encoder uses as cs pin 4 and the second pin10. when the cs lines are pluged in only the one encoder works(on pin 10)
the other is not. if i switch the other encoder to cs pin10 it works also but not together.

I think this has to do with the extented spi behaviour and the chip select
Can somebody point me to the right direction. below is a copy of my code.
its bad but its on testing yet


#include “SPI.h”

// 2013
# define SPI_NOP 0x0000
# define CLEAR_ERR_FG 0x0001
# define AGC 0x3ffd
# define MAG 0x3ffe
# define ANGLE 0x3fff
# define CMD_READ 0b1000000000000000
# define CMD_WRITE 0b1100000000000000

unsigned short agc1=0;
  unsigned short agc2=0;
  unsigned short agc=0;
  unsigned short mag1=0, mag2=0,mag=0,useless=0;
  unsigned short result2= 0;
  unsigned short result1 = 0,result3=0,result4=0;
  unsigned short  resultA = 0, resultB=0;
  float angA=0, angB=0;
void setup ()

[color=#CC6600]pinMode/color;//define ss pin as output
SPI.[color=#CC6600]begin/color;// initialize spi bus with pin4 as ss and clock divisor 8 so sclk is 84/8=10.5Mhz
[color=#CC6600]pinMode/color;//define ss pin as output
SPI.[color=#CC6600]begin/color;// initialize spi bus with pin4 as ss and clock divisor 8 so sclk is 84/8=10.5Mhz


void loop ()


void read_angle()

   result1 = SPI.transfer(10,(CMD_READ|ANGLE),SPI_CONTINUE);
   result2 = SPI.transfer(10,(CMD_READ|ANGLE),SPI_CONTINUE);
 result3 = SPI.transfer(4,(CMD_READ|ANGLE),SPI_CONTINUE);
   result4 = SPI.transfer(4,(CMD_READ|ANGLE),SPI_LAST); 
  resultA= result1|result2;//original resolution (FULL 14 bit resolution)


second problem is with the new ide 1.5.3.
My program compiles uploads but it doesnt do nothing. If i comment out the spi initialization at setup serial monitor works
I tried the nightly build but the problem persist.

When i use 1.5.2 it work

I have no idea what is wrong.

Hello alexandrosroidis,

I don't see that you are declaring pins 4 and 10 in your sketch. In order for Due to handle those pins as CS, you have to initialize them as follows:

void setup(){
  // initialize the bus for a device on pin 4
  // initialize the bus for a device on pin 10

For more information about how to set multiple SIP slaves using Arduino Due, please, read the Arduino extended SPI reference for Due. Here the link:

I recommend you to fix your SPI problem first and then try to solve your IDE 1.5.3 later on. Anyway, verify when using IDE 1.5.3 that your selected board is the Due and not a different one like the UNO. I happened to me that when I upload a new IDE, in my excitement, I use to forget to set the board.


1.5.3 had an issue with the SPI.begin(slaveSelectPin) function, the version with no argument worked fine.

The current nightly builds have this fixed or if you prefer you can add three lines to the begin function in libraries/SPI/arch/sam/SPI.cpp so it looks like this:

void SPIClass::begin(uint8_t _pin) {
    uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);

    SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);//2

    // Default speed set to 4Mhz
    setClockDivider(_pin, 21);
    setDataMode(_pin, SPI_MODE0);
    setBitOrder(_pin, MSBFIRST);

I solved the problem
It has to do with the predefined cs pins of the due . cs pin4 is the cs0.
so i switch 4 with 52 and everything works ok

I dont know why this is so , and actually i dont care as long as its working like it should
I for the spi extended reference i had already read it obviously to start using the multiple slave mode of the spi
Some people think they are very smart and others not

Anyway thanks for everything

In the nightly mac version donwloaded on 20.sept.13 extended SPI on all three pins (4, 10, 52) now works fine !

Even it took many hours with the logic analyzer to find this thread and fix it ;)

Thanks Arduino team :) michael


there is an other ugly bug in the SPI library.

Working with 3 different slaves on the predefined CS pins works fine, as long as only ONE of them is used in the sketch.
At the moment of the second SPI.begin(dPOTI_nCS) the DAC_nCS goes low !?! and keeps low,
the following writes to the DAC don’t happen, MOSI and CLK are not changed any more.
And during the setup the CS lines are going low too, and are just updated when the first SPI.transfer is initiated.

A bugfix would be great :slight_smile:

#define DAC_nCS 10
#define dPOTI_nCS 52

// DAC
SPI.setClockDivider(DAC_nCS, 32);
SPI.setDataMode(DAC_nCS, SPI_MODE1);

dacWrite(DAC_cmd_SetupDcenRefRegister, DAC_cannel_ALL, DAC_dcnrefmode_AlloneOn);
dacWrite(DAC_cmd_PowerDownUp, DAC_cannel_ALL, DAC_powermode_NormalOn);

// Digital Poti