UnoATmega328PidBME280sensor0xD00x60SD card1SPI.beginTransaction(BME);2digitalWrite(CS, LOW);3SPI.transfer(0xD0);4id = SPI.transfer(0x00);5digitalWrite(CS, HIGH);// CS HIGH forgotten6SPI.endTransaction();1SPISettings BME(2 1000000, MSBFIRST,3 SPI_MODE0); SPI_MODE1);CS 10SCK 13MOSI 11MISO 12SD CS 9BMELOWHIGHHIGHLOW110100000000000001100000????????
0481216CSSD CSSCKMOSIMISOtime (µs)LOGIC ANALYSER
Back: the Uno's microcontroller, a BME280 sensor and an SD card on one SPI busFront: the driver code and its SPISettings; below: a logic analyser viewTokens: chip-select levels and the bits on MOSI and MISOAn Arduino Uno R3 reads a BME280 over SPI: SCK, MOSI and MISO are shared, and each chip has its own chip select. The driver pulls CS LOW, sends 0xD0 (read register 0xD0), sends a dummy 0x00 while 0x60 comes back, then raises CS. The logic analyser below shows the same transaction, the edges the mode reads on, and what two selected chips do to MISO.

The bus and the settings

  1. An Arduino Uno R3 reads a BME280 sensor over SPI. The Uno is the controller: it makes the clock. Four wires: SCK, the clock (pin 13); MOSI, data out (11); MISO, data in (12); and chip select, CS (10).

One transaction

Modes

Chip select with two chips

What happens

;; step 1/10 · bus
controller   Arduino Uno R3 (ATmega328P)
peripheral   BME280 breakout
SCK   pin 13   clock, made by the Uno
MOSI  pin 11   controller out, peripheral in (COPI)
MISO  pin 12   controller in, peripheral out (CIPO)
CS    pin 10   chip select, LOW = "talk to me"

The analyser view draws the two bytes back to back. On a real Uno the CPU needs a few instructions between bytes, so a short gap appears between them. 16 clocks at 1 MHz is 16 µs of clocking. MISO is drawn at half height where the sensor sends no data (nobody drives the line); a real analyser shows it floating or pulled to one level.