# SPI - cannot communicate with multiple peripherals

**URL:** <https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345>\
**Category:** Networking, Protocols, and Devices\
**Created:** [April 26, 2024, 8:55am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345 "2024-04-26T08:55:13Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![honza1r](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@honza1r](https://forum.arduino.cc/u/honza1r)\
**Post date:** [April 26, 2024, 8:55am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/1 "2024-04-26T08:55:13Z")

</div>

Hello,  
I am simply trying to get my master device (raspberry pi pico) to communicate with multiple peripherals. As it is with spi, MOSI, MISO, and CLK lines are common, and each device has its own CS pin.

When I setup my code to only communicate with one peripheral, the peripheral receives all bytes without a problem and everything works perfectly. The code is below (only SPI related stuff included)

```arduino
void setup(){
  SPI1.setRX(12);
  SPI1.setCS(motCS);
  SPI1.setSCK(10);
  SPI1.setTX(11);
  SPI1.begin(true);
}

void sendCommand(){
  bufferOut[0] = uint8_t(controlWord>>8);
  bufferOut[1] = uint8_t(controlWord);
  for (int i = 0; i < 4; i++){
    bufferOut[i+2] = controlValue.bytes[i];
  }

  SPI1.beginTransaction(spi1settings);
  for (int i = 0; i < 6; i++){
    SPI1.transfer(bufferOut[i]);
    delayMicroseconds(comDelay);//found the communciation is more reliable with slight delay
  }
  SPI1.endTransaction();
}

```

But when I initialize and control the CS pin manually, since that is how every single tutorial does it, the data is just not going through. The code for that is the following:

```arduino
void setup(){
  SPI1.setRX(12);
  SPI1.setSCK(10);
  SPI1.setTX(11);
  SPI1.begin(true);
  pinMode(motCS,OUTPUT);
  digitalWrite(motCS,HIGH);
}
void sendCommand(){
  bufferOut[0] = uint8_t(controlWord>>8);
  bufferOut[1] = uint8_t(controlWord);
  for (int i = 0; i < 4; i++){
    bufferOut[i+2] = controlValue.bytes[i];
  }
  digitalWrite(motCS,LOW);
  SPI1.beginTransaction(spi1settings);
  for (int i = 0; i < 6; i++){
    SPI1.transfer(bufferOut[i]);//send command to RP2040 motor
    delayMicroseconds(comDelay);
  }
  SPI1.endTransaction();
  digitalWrite(motCS,HIGH);
}

```

I tried differnet order of commands, including slight delays, everything I could think of, but in neither case the data is transfered from the master to the slave.

Since it works without a problem in the first case, I know hardware is not the problem.

If anybody's got any ideas, I'd be very grateful.

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [April 26, 2024, 9:34am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/2 "2024-04-26T09:34:00Z")

</div>

use `SPI1.begin()` without `true`

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [April 26, 2024, 9:39am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/3 "2024-04-26T09:39:25Z")

</div>

> [@honza1r](#):
>
> ```arduino
> SPI1.setCS(motCS);
> 
> ```

That means to me that the RasPi class will handle a single client device and do the CS itself. So I see two possible solutions:

1. a special value tells the SPI class not to select a client
2. setCS() must be called before a transaction

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [April 26, 2024, 9:39am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/4 "2024-04-26T09:39:33Z")

</div>

> [@honza1r](#):
>
> my master device (raspberry pi pico)

What is board oackage do you use for Pico?

---

<div class="post-metadata">

**Author:** ![honza1r](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@honza1r](https://forum.arduino.cc/u/honza1r)\
**Post date:** [April 26, 2024, 10:08am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/5 "2024-04-26T10:08:52Z")

</div>

tried it, did't make a difference. Actually, it made the first working code not work

---

<div class="post-metadata">

**Author:** ![honza1r](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@honza1r](https://forum.arduino.cc/u/honza1r)\
**Post date:** [April 26, 2024, 10:12am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/6 "2024-04-26T10:12:00Z")

</div>

It's a custom board so I am just programming the RP2040. But in arduino IDE I use the Raspberry Pi pico build by Earl Philhower

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [April 26, 2024, 10:24am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/7 "2024-04-26T10:24:00Z")

</div>

> [@honza1r](#):
>
> Actually, it made the first working code not work

Indeed

The flag in `SPI.begin()` controls, whether the library use hardware CS pin to interface with slave or not.  
Therefore, you need `...begin(true)` in your first code and `begin(false)` or just `begin()` in the second - it allow you to use software controlled CS pins for multiple slaves.

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [April 26, 2024, 10:46am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/8 "2024-04-26T10:46:22Z")

</div>

> [@honza1r](#):
>
> it made the first working code not work

there it is need. in the other it interferes.  
you didn't look up the meaning of the parameter?

---

<div class="post-metadata">

**Author:** ![honza1r](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@honza1r](https://forum.arduino.cc/u/honza1r)\
**Post date:** [April 26, 2024, 4:12pm UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/9 "2024-04-26T16:12:58Z")

</div>

I tried calling setCS() with the different CS pins before each communication and it didn't help unfortunately. I also couldn't find anything useful on handling CS pins within the SPI calss

---

<div class="post-metadata">

**Author:** ![honza1r](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@honza1r](https://forum.arduino.cc/u/honza1r)\
**Post date:** [April 26, 2024, 4:14pm UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/10 "2024-04-26T16:14:54Z")

</div>

I tried it and it didn't help. Doesn't matter if I manually pull the CS pins high/low or try to call setCS() with the right pin each time. The moment I dont call setCS() before SPI1.begin(true) and try to do anything else, I can't get the communciation to work

---

<div class="post-metadata">

**Author:** ![honza1r](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@honza1r](https://forum.arduino.cc/u/honza1r)\
**Post date:** [April 27, 2024, 11:18am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/11 "2024-04-27T11:18:19Z")

</div>

Ok I found the solution.  
The problem was with sending multiple bytes one by one. Before each SPI.transfer() the CS pin needs to pulled low and after each SPI.transfer the CS pin needs to be pulled high. SO the working code looks the follwoing:

```arduino
void setup() {
  SPI1.setRX(12);
  SPI1.setSCK(10);
  SPI1.setTX(11);
  SPI1.begin();
  pinMode(motCS,OUTPUT);
  digitalWrite(motCS, HIGH);
  delay(200);
}

void sendCommand(){
  bufferOut[0] = uint8_t(controlWord>>8);
  bufferOut[1] = uint8_t(controlWord);
  for (int i = 0; i < 4; i++){
    bufferOut[i+2] = controlValue.bytes[i];
  }
  digitalWrite(motCS,LOW);
  SPI1.beginTransaction(spi1settings);
  for (int i = 0; i < 6; i++){
    if(i>0) digitalWrite(motCS,LOW);
    SPI1.transfer(bufferOut[i]);//send command to RP2040 motor
    if(i<5) digitalWrite(motCS,HIGH);
    delayMicroseconds(comDelay);
  }
  SPI1.endTransaction();
  digitalWrite(motCS,HIGH);
}

```

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [April 27, 2024, 12:15pm UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/12 "2024-04-27T12:15:33Z")

</div>

> [@honza1r](#):
>
> Before each SPI.transfer() the CS pin needs to pulled low and after each SPI.transfer the CS pin needs to be pulled high.

no, it is incorrect

---

<div class="post-metadata">

**Author:** ![honza1r](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@honza1r](https://forum.arduino.cc/u/honza1r)\
**Post date:** [April 27, 2024, 12:54pm UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/13 "2024-04-27T12:54:15Z")

</div>

alright, well regardless the solution code I povided solves my problem so I am sticking with it

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [April 27, 2024, 12:58pm UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/14 "2024-04-27T12:58:28Z")

</div>

Well, if you provided the whole code for context, perhaps someone could illuminate why you both may be correct. As it is, your snippet is worthless.  
Loop() is missing, as are variable declarations that are needed to understand, so 🤷‍♂️.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [May 3, 2024, 6:37am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/15 "2024-05-03T06:37:13Z")

</div>

Without the full code the solution mark should be removed, the post #11 is not the solution

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 30, 2024, 6:37am UTC](https://forum.arduino.cc/t/spi-cannot-communicate-with-multiple-peripherals/1252345/16 "2024-10-30T06:37:45Z")

</div>

Closed on October 30, 2024, 6:37am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
