Reads a byte (8 bits) of data one bit at a time from a data pin. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. For each bit, the clock pin is pulled high, the next bit is read from the data line, and then the clock pin is taken low.
Note: If you’re interfacing with a device that’s clocked by rising edges, you’ll need to make sure that the clock pin is low before the first call to shiftIn(), e.g. with a call to digitalWrite(clockPin, LOW).
Note: this is a software implementation; Arduino also provides an SPI library that uses the hardware implementation, which is faster but only works on specific pins.
Use the following function to read serial data synchronously:
byte incoming = shiftIn(dataPin, clockPin, bitOrder)
The function admits the following parameters:
dataPin: the pin where the data is read from. Allowed data types: int.clockPin: the pin that provides the timing signal.bitOrder: which order to shift in the bits; either MSBFIRST or LSBFIRST. (Most Significant Bit First, or, Least Significant Bit First).The function returns a byte (0 - 255) representing the 8 bits read from the device. Data type: byte.
#define DATA_PIN 4 // Pin connected to serial data output
#define CLOCK_PIN 5 // Pin connected to clock input (CLK)
void setup() {
pinMode(DATA_PIN, INPUT);
pinMode(CLOCK_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read 8 bits from the data line using shiftIn()
byte dataByte = shiftIn(DATA_PIN, CLOCK_PIN, MSBFIRST);
// Print the byte in binary format
Serial.print("Received Byte: ");
Serial.println(dataByte, BIN);
delay(1000); // Wait before next read
}