Go Down

Topic: Arduino as SPI Monitor (Read 637 times) previous topic - next topic



I want to use the Arduino (DUE) as a SPI Monitor that display data which is transmitted on the SPI Port on the Serial Monitor. I already tried this sketch here, but somehow the whole SPI communication stops as soon as I connect the Arduino to MOSI.

I hooked it up to my scope and saw that the Arduino is pulling the MOSI line low, but why? Normally it should act as slave and not touch the MOSI line and just listen to it.

Here is my code

Code: [Select]
// SPI slave for DUE, hack
//  master will lower CS and manage CLK
// MISO to MISO  MOSI to MOSI   CLK to CLK   CS to CS, common ground
//   http://forum.arduino.cc/index.php/topic,157203.0.html
// core hardware/arduino/sam/system/libsam/source/spi.c
// TODO:  C class, interrupt, FIFO/16-bit, DMA?

#include <SPI.h>

// assumes MSB
static BitOrder bitOrder = MSBFIRST;

void slaveBegin(uint8_t _pin) {
  REG_SPI0_CR = SPI_CR_SWRST;     // reset SPI
  REG_SPI0_CR = SPI_CR_SPIEN;     // enable SPI
  REG_SPI0_MR = SPI_MR_MODFDIS;     // slave and no modefault
  REG_SPI0_CSR = SPI_MODE0;    // DLYBCT=0, DLYBS=0, SCBR=0, 8 bit transfer

byte transfer(uint8_t _pin, uint8_t _data) {
    // Reverse bit order
    if (bitOrder == LSBFIRST)
        _data = __REV(__RBIT(_data));
    uint32_t d = _data;

    while ((REG_SPI0_SR & SPI_SR_TDRE) == 0) ;
    REG_SPI0_TDR = d;

    while ((REG_SPI0_SR & SPI_SR_RDRF) == 0) ;
    d = REG_SPI0_RDR;
    // Reverse bit order
    if (bitOrder == LSBFIRST)
        d = __REV(__RBIT(d));
    return d & 0xFF;

#define PRREG(x) Serial.print(#x" 0x"); Serial.println(x,HEX)

void prregs() {

#define SS 10
void setup() {
  prregs();  // debug

void loop() {
  byte in;
  static byte out=0x83;

  in = transfer(SS, out);
  out = in;


I am having the same problem with a Leonardo. The Serial Monitor seems to be incompatible with SPI. As soon as I open the serial port the SPI pins no longer respond. I could really use a solution to this. I am trying to communicate with the digital potentiometer AD5141.

Here is my code:

#include <SPI.h>

const int slaveselect = 10;
int n = 0;

void setup()
   while (!Serial)
     ; // wait for serial port to connect
 //Opening Serial Port makes SPI stop working...
 pinMode(slaveselect, OUTPUT);


byte digiPotWrite(byte control, byte data)
 digitalWrite(slaveselect, LOW);
 byte output = SPI.transfer(data);
 digitalWrite(slaveselect, HIGH);

 return output;

void loop()
 //Single Command
 digiPotWrite(B01110000, B0); //Insert desired command


Go Up