Main Content

Tap Detection with ADXL345 Accelerometer Chip Using NI USB-8452 Controller

This example shows how to write and read data from an Analog Devices® ADXL345 I2C-enabled accelerometer chip using an NI™ USB-8452 controller. The accelerometer is configured to detect a double tap and return a message that indicates this.

Requirements

This example requires Microsoft® Windows® and the NI-845x driver. Make sure that the NI Measurement and Automation Explorer (NI MAX) recognizes the NI-8452 controller. You must close NI MAX prior to connecting to the controller from MATLAB®.

The example uses the Analog Devices ADXL345 accelerometer. The accelerometer has I2C bus lines that can be connected to the controller's I2C bus line inputs. Connect the VCC and GND pins of the ADXL345 to the 5 V source and GND pins of the NI USB-8452 controller, as demonstrated in this figure.

Detect NI USB-8452 Controller

Search for NI USB-8452 hardware connected to your machine using ni845xlist.

Multiple NI USB-8452 controllers can be connected to a machine. Select the appropriate controller by comparing the SerialNumber returned by ni845xlist with the serial number printed on the controller board.

controllerList = ni845xlist
controllerList=1×2 table
             Model        SerialNumber
         _____________    ____________

    1    "NI USB-8452"     "01F26E0A" 

Connect to Accelerometer and Set Bus Speed

Connect to the ADXL345 accelerometer chip using the serial number returned by ni845xlist. In this case, the first SerialNumber corresponds to the desired controller.

ni845Obj = ni845x(controllerList.SerialNumber(1))
ni845Obj = 
  NI845x with properties:

                   Model: "NI USB-8452"
            SerialNumber: "01F26E0A"
    AvailableDigitalPins: ["P0.0"    "P0.1"    "P0.2"    "P0.3"    "P0.4"    "P0.5"    "P0.6"    "P0.7"]

  Show all properties, functions

Determine the address of the ADXL345 accelerometer connected to the controller by using the scanI2CBus function.

accelerometerAddress = scanI2CBus(ni845Obj)
accelerometerAddress = 1×2 string
    "0x48"    "0x53"

Connect to the ADXL345 accelerometer using the I2C address returned by scanI2CBus. In this example, the accelerometer address is the second value, as two I2C peripheral devices are connected to the controller. Set the bit rate of the accelerometer chip to 100 kHz.

adxlDevice = device(ni845Obj,I2CAddress=accelerometerAddress(2))
adxlDevice = 
  I2CDevice with properties:

      Protocol: "I2C"
    I2CAddress: 83
       BitRate: 100000
     ByteOrder: "little-endian"

  Show all functions

adxlDevice.BitRate = 100000;

Set Tap Threshold, Second Tap Latency, Second Tap Window, and Tap Duration Register Values

Set the register values according to the datasheet of the device. Read the value back from the device to confirm that the value is indeed set.

threshTapRegisterAddress = 0x1D;
valueToWrite = 0x50;
writeRegister(adxlDevice, threshTapRegisterAddress, valueToWrite, "uint8");
disp("The write value of the THRESH_TAP register is: " + num2str(valueToWrite));
The write value of the THRESH_TAP register is: 80
registerValue = readRegister(adxlDevice, threshTapRegisterAddress, 1, "uint8"); 
disp("The read value of the THRESH_TAP register is: " + num2str(registerValue));
The read value of the THRESH_TAP register is: 80
latentRegisterAddress = 0x22;
valueToWrite = 0x5;
writeRegister(adxlDevice, latentRegisterAddress, valueToWrite, "uint8");
disp("The write value of the LATENT register is: " + num2str(valueToWrite));
The write value of the LATENT register is: 5
registerValue = readRegister(adxlDevice, latentRegisterAddress, 1, "uint8"); 
disp("The read value of the LATENT register is: " + num2str(registerValue));
The read value of the LATENT register is: 5
windowRegisterAddress = 0x23;
valueToWrite = 0xFF;
writeRegister(adxlDevice, windowRegisterAddress, valueToWrite, "uint8");
disp("The write value of the WINDOW register is: " + num2str(valueToWrite));
The write value of the WINDOW register is: 255
registerValue = readRegister(adxlDevice, windowRegisterAddress, 1, 'uint8'); 
disp("The read value of the WINDOW register is: " + num2str(registerValue));
The read value of the WINDOW register is: 255
durationRegisterAddress = 0x21;
valueToWrite = 0x10;
writeRegister(adxlDevice, durationRegisterAddress, valueToWrite, "uint8");
disp("The write value of the DUR register is: " + num2str(valueToWrite));
The write value of the DUR register is: 16
registerValue = readRegister(adxlDevice, durationRegisterAddress, 1, 'uint8'); 
disp("The read value of the DUR register is: " + num2str(registerValue));
The read value of the DUR register is: 16
tapAxesRegisterAddress = 0x2A;
valueToWrite = 0b00000111;
writeRegister(adxlDevice, tapAxesRegisterAddress, valueToWrite, "uint8");
disp("The write value of the TAP_AXES register is: " + num2str(valueToWrite));
The write value of the TAP_AXES register is: 7
registerValue = readRegister(adxlDevice, tapAxesRegisterAddress, 1, "uint8");
disp("The read value of the TAP_AXES register is: " + num2str(registerValue));
The read value of the TAP_AXES register is: 7
interruptEnableRegisterAddress = 0x2E;
valueToWrite = 0b01100000;
writeRegister(adxlDevice, interruptEnableRegisterAddress, valueToWrite, "uint8");
disp("The write value of the INT_ENABLE register is: " + num2str(valueToWrite));
The write value of the INT_ENABLE register is: 96
registerValue = readRegister(adxlDevice, interruptEnableRegisterAddress, 1, "uint8"); 
disp("The read value of the INT_ENABLE register is: " + num2str(registerValue));
The read value of the INT_ENABLE register is: 96

Enable Operation by Writing to POWER_CTL Register

Based on the datasheet, write to the POWER_CTL register to change the chip from standby mode to normal operation mode.

powerControlRegisterAddress = 0x2D;
valueToWrite = 0b00001000;
writeRegister(adxlDevice, powerControlRegisterAddress, valueToWrite, "uint8");
disp("The write value of the POWER_CTL register is: " + num2str(valueToWrite));
The write value of the POWER_CTL register is: 8
registerValue = readRegister(adxlDevice, powerControlRegisterAddress, 1, "uint8"); 
disp("The read value of the POWER_CTL register is: " + num2str(registerValue));
The read value of the POWER_CTL register is: 8

Poll Interrupt Register

The interrupt source register contains bits that correspond to interrupt flags generated by specific sources. Check to see that the double tap interrupt is generated.

interruptSourceRegisterAddress = 0x30;
disp("Waiting for double tap...");
Waiting for double tap...
while(1)
    interruptValues = readRegister(adxlDevice, interruptSourceRegisterAddress, 1, "uint8");
    tapInterrupt = bitand(interruptValues, 0b00100000);
    if tapInterrupt
        disp("Double tap detected!");
        break
    end
end
Double tap detected!

See Also

| | | | |

Related Topics