In this post I explore and describe the connections, libraries and code required to connect grove accelerometers to our Arduino Unos.
We are using a Seed Studio’s Grove 3-axis digital accelerometer (LIS3DHTR). There are a couple of little details that might slow you down at first, but once you have things sorted you should find this an easy sensor to interface.
Accelerometers measure the acceleration (motion) of the sensor body along the X,Y,Z-axis. Acceleration is the rate of change of velocity (speed) of a moving body. At this point it is acceptable to think of it as movement.
All kinds of playful interfaces become possible when you add acceleration to sensory capacity of a controller or object.
Parts required
- Arduino Uno
- breadboard
- accelerometer
- grove to header pin connector
- hookup wire (for power)
Serial not Voltage
This grove sensor is unique in our kits. It uses a serial protocol to send sensor readings into the Arduino. The data sent over serial uses a specified protocol called I2C (said eye-squared-see — tho I often incorrectly say eye-two-see ; learned it wrong, can’t shake it).
This course is not about serial protocols, so I will be light on details. I2C is serial — and in this way similar to the Serial object and port we have been using in many of the tangible videos and in class. I2C is an example of synchronous serial. This means data transmission is regulated by a clock. The basic command Serial.println() and similar, that we use all the time, is a-synchronous — there is not clock — just a baud rate.
Once up and running, the sensor sends a data stream to the Arduino (over I2C). We can unpack that data with the help of a library. This is very different than the simple sensors such as buttons, photocells and pots we have been using in class. The simpler sensors create changes in voltage that are sent to the Arduino and we read these changing voltages directly.
Video
Get the Library
As indicate above, in addition to hardware, you will need to install a free library that will simplify the use of the sensor.
To do this, in Arduino software, navigate to Arduino-> Sketch-> Include Library-> Manage Libraries
A Library Manager window will open. In the search box (upper right) type LIS3DHTR
This should result in the GROVE library for the LIS3DHTR sensor being displayed. Select and install the latest version (1.2.4 at time of writing). Make SURE you get the grove version (not the adafruit one).
Once the library installs you can close the library manager window.
Add Some Code
When the code below is uploaded you should see X,Y,Z values returned in the serial monitor or plotter. But on first try you may get gibberish in the serial monitor. This is because the provided sample code uses a fast baud rate for communication between your Arduino and programming computer.
Set Baud
You need to match the baud rate set in the serial monitor and/or plotter with the baud rate set in code. In the setup(), of this example the baud (communication rate between Arduino and your computer) is set to 115200 (not the default 9600 we generally use).
Serial.begin(115200);
You need to use the BAUD drop down in the serial plotter/ monitor to match that code setting.
The image below shows drop down for baud setting in serial monitor.
The image below shows drop down for baud setting in serial plotter.
Now, upload the following code, open plotter or monitor and tilt the sensor in various directions to see how the values change.
// Simplified example -- see video for details.
// This example shows the 3 axis acceleration.
#include "LIS3DHTR.h"
#include <Wire.h>
LIS3DHTR<TwoWire> LIS; //IIC
#define WIRE Wire
void setup() {
Serial.begin(115200);
while (!Serial) {};
LIS.begin(WIRE, 0x19); //IIC init
delay(100); // give it a moment to start
LIS.setFullScaleRange(LIS3DHTR_RANGE_2G); //sensitivity
LIS.setOutputDataRate(LIS3DHTR_DATARATE_50HZ); // data rate
LIS.setHighSolution(true); //High solution enable
}
void loop() {
if (!LIS) {
Serial.println("LIS3DHTR didn't connect.");
while (1);
return;
}
//3 axis
Serial.print("x:"); Serial.print(LIS.getAccelerationX()); Serial.print(" ");
Serial.print("y:"); Serial.print(LIS.getAccelerationY()); Serial.print(" ");
Serial.print("z:"); Serial.println(LIS.getAccelerationZ());
delay(50);
}
Going Further
Learn about thresholds and edges in analog sensors.