















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















Data can be shifted into the slave device on either the rising or falling edge of the clock signal (called the clock phase ), and the SCLK default state can be set to either high or low (called the clock polarity ).
Because there are two options for the clock phase and two options for the clock polarity, you can configure the SPI bus in a total of four ways
Communication Scheme
Comparing SPI to I2C and UART
An accelerometer is one of three common positioning sensors that you might find in a modern smartphone (the other two being a gyroscope and a magnetometer).
An Arduino acts as the master device and the accelerometer as the slave
Wire the SPI pins from the breakout board to the Arduino’s pins that are listed in Table
Then, connect an RGB LED to pins 6, 5, and 3 (all PWM-capable, in case you want to control the brightness of the LED) via 220Ω current-limiting resistors.
Remember, this is a common-anode LED, so the common pin (the longest one) should connect to 5V from the Arduino.
Don’t forget to also connect the breakout board’s 5V and GND lines to the Arduino.
Accelerometer-based orientation sensor—orientation.ino // Uses the Z-Axis of an Accelerometer to detect orientation // Include Libraries // This library will, itself, include the SPI and Universal Sensor Libraries
**#include **
// Define the pins (the SPI hardware pins are used by default) const int RED_PIN = 6; const int GREEN_PIN = 5; const int CS_PIN = 10;
// Set the sensitivity of the accelerometer to +/-2G accel.setRange(LIS3DH_RANGE_2_G); // Set the LED cathode pins as outputs and turn them off // HIGH is off because this is a common anode LED
pinMode(RED_PIN, OUTPUT); digitalWrite(RED_PIN, HIGH); pinMode(GREEN_PIN, OUTPUT); digitalWrite(GREEN_PIN, HIGH); } void loop() { // Get X, Y, and Z accelerations accel.read(); // Print the Raw Z Reading Serial.print("Raw: "); Serial.print(accel.z);
// Map the Raw Z Reading G's based on +/-2G Range Serial.print("\tActual: "); Serial.print((float(accel.z)/32768.0)2.0); Serial.println("G");* // Check if we are upside-down if (accel.z < 0) { digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, HIGH); } else { digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, LOW); } // Get new data every 100ms delay(100); }