# shiftIn() and shiftOut() in Arduino

ArduinoHardwareSoftware & Coding

shiftIn() and shiftOut() commands in Arduino are, very loosely speaking, software implementations of SPI. Of course, SPI is much faster, but SPI can work only on some specific pins of Arduino. shiftIn() and shiftOut() can use any two GPIOs of Arduino (not some specific pins like SPI).

Both shiftIn() and shiftOut() require two digital pins, one dataPin and one clockPin.The dataPin will shift in or shift out a byte (8 bits) of data, 1 bit at a time. The clockPin synchronizes the data transfer. It is generally kept low, and for each bit transfer, it goes HIGH and then back to LOW. The rising edge (LOW to HIGH) is when the transfer of the bit takes place.

## Syntax

The syntax of shiftOut is −

shiftOut(dataPin, clockPin, bitOrder, value)

where dataPin and clockPin represent the two pins to be used for the shift operation.bitOrder can be MSBFIRST or LSBFIRST, indicating where to start the transfer from. The value is the byte (8-bit) data that needs to be shifted out.

Similarly, the syntax for shiftIn is −

byte incoming = shiftIn(dataPin, clockPin, bitOrder)

Here, we are reading a bit, instead of sending. Therefore, it only takes in 3 arguments, and outputs a byte.

Note that the dataPin has to be defined as OUTPUT for shiftOut() and INPUT for shiftIn(). The clockPin has to be defined as OUTPUT in both cases. When interfacing with a device that’s clocked by rising edges, make sure that the clock pin is low (digitalWrite(clockPin,LOW)) before making the call to shiftOut().

## Applications

As described above, shiftIn() and shiftOut() are like software implementations of SPI. They help reduce the number of pins for data transfer. Instead of sending 8-bits in parallel over 8 lines, you send the bits one by one serially over one line, with the operation being synchronized by the clock line.

One popular application is the interface with 74HC595 shift resistor, which is used for Serial to Parallel conversion. A good example can be found here.

Published on 30-Jul-2021 12:50:05