- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
shiftIn() and shiftOut() in Arduino
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.
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().
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.
- Bitwise AND and OR in Arduino
- Logical AND and OR in Arduino
- pulseIn() and pulseInLong() in Arduino
- tone() and noTone() in Arduino
- For and While loops in Arduino
- Square and Square root in Arduino
- Enable and disable interrupts in Arduino
- Reference and dereference operator in Arduino
- Print new line and tab in Arduino
- Difference between float and double in Arduino
- Semaphore and Mutex in FreeRTOS using Arduino
- Difference between #define and const in Arduino
- Arduino Uno vs Arduino Mega
- Arduino Uno vs Arduino Nano
- Arduino Uno vs Arduino Due