Connecting to PADI via SPI

Connecting to PADI via SPI

Daniil Zhuk
Modern microcontrollers and single-chip systems have many interfaces for communicating with peripheral devices. One of them, the easiest to implement - SPI or Serial Peripheral Interface - allows you to transfer data in two directions simultaneously over a single clock signal, that is, it is full-duplex. About him and his implementation in the PADI IoT Stamp and will be discussed!

SPI has been used for a long time and is used both for connecting devices on the board and for connecting relatively remote devices.

A total of 4 wires are used:
  • MISO - Master In, Slave Out - transmission from the master device on the peripheral bus
  • MOSI - Master Out, Slave In - transfer from peripheral to main
  • SCK - Serial Clock - data synchronization during transfer
  • SS - Slave Select - select the active device on the bus

PADI There are 2 hardware interfaces that are handled in semi-automatic mode - you only need to configure it, load the byte, and then the periphery will deal with the transmission or reception without loading the processor.

But it is not always convenient to connect the device to the pins of the hardware interface, because, as we have said, there are many peripherals and few pins for it. Therefore, sometimes you have to organize the interface yourself, using direct writing to the pins. Do not worry, the Arduino environment provides ready-made functions for transferring data in both directions programmatically.

In some cases, when the device does not require a response (as, for example, from displays or too simple actuators), some connection lines are not used (most often, MISO is not used, and SS is connected directly to ground if the device is on the bus one), which saves space and pins of the controller.

As an example, we will connect just such a simple device - the shift register 74HC595. It allows you to simply and quickly increase the number of pins of the controller by a number that is a multiple of 8, while using at least 2 and, in normal mode, 4 wires. Connect the same they can be an unlimited number of pieces.

Due to the lack of a chip in a DIP-package, we made such an adapter for using an SMD-chip with a breadboard, but working with chips in different cases will not differ.

Pinout 74HC595 of datasheet

All Q pins are directly on the microcircuit outputs, and it is controlled by pins 10 to 14. To connect, you need to pull the 13th pin to the ground with a 10 kΩ resistor or connect it directly, and 10 to the power plus.

The 11th contact is a clock signal, the 12th is a “latch” (when the voltage on it changes from LOW to HIGH, the values ​​from the buffer go to the register pins), the 14th is the data input. The interface will be involved in the 14th and 11th output, and the 12th will have to manage manually.

We will organize the program interface using the shiftOut() function.

Its syntax is:

shiftOut(data_pin, clock_pin, order, value)
The first two arguments are the pin numbers of the data transfer and the clock signal, respectively. The third parameter is the order of bits in the transmitted byte (from the oldest to the youngest, or vice versa), you will see its importance later. The fourth parameter is the directly transmitted value.

Let's start a simple sketch, having previously connected everything according to the above scheme:

#define CLOCK_PIN 10
#define LATCH_PIN 11
#define DATA_PIN 12

void setup() {

void loop() {

digitalWrite(LATCH_PIN, LOW);
digitalWrite(LATCH_PIN, HIGH);

If you connect LEDs to the register pins, then all 8 should light up. Do not forget to use current limiting resistors!

You can experiment with the transmitted value. If you send not FF, then the LEDs will display the binary form of this number. For example, the number 0xAA:

And if you change the transfer order from the MSBFIRST (Most Significant Bit First - the most significant bit first) to LSBFIRST (the least significant bit first), you get the following picture:

You see that the order has changed and the data is output in the opposite direction. In fact, in one case, the device received 0x55 (0b01010101), and in the other, 0xAA (0b10101010), so it is very important to take this parameter into account when transmitting.

Thus, devices using SPI protocol are connected to PADI and other controllers, and we will describe further protocols later. Experiment with a shift register and try to implement a running light of 8 LEDs!

Add a comment

* Comments must be approved before being displayed.