In this project, we start building an accelerometer application that will record each time the CREATOR Kit is tapped. As you'll see in future projects, this will open the door for some interesting games and devices.

In previously projects, we've mostly been using the accelerometer to measure tilt — which is one function of the accelerometer. But, what the accelerometer really does is measure forces in 3 directions. When you knock on the top of the lid of the CREATOR Kit, the accelerometer feels the force of your knock by sensing the up/down movement and vibration of the kit resulting from the knock. In fact, because it's so sensitive, even a relatively gentle knock will provide enough force to be measured by the accelerometer.

For this and the following projects, we'll only need the accelerometer wired up and the speaker attached. Here is what your breadboard should look like:

To register a knock with the accelerometer, the force we'll be trying to measure is an up and down motion — that is, a change in the z-axis force. In this first project, we'll start simple and record this force to see when it goes over some threshold that we define.

When the accelerometer sits in the breadboard, even though it may look like it is sitting perfectly flat, it is likely that it is off by a few degrees. So, before we can do a good job of measuring the up-and-down force on the z-axis, we'll need to calibrate the sensor. Calibrating the sensor means to figure out how far off from flat the sensor is really sitting, so that we'll be able to adjust our calculations later given this information.

Here is our first set of code that calibrates the sensor, and then records a few seconds of user taps.

Now, let's take a look at what our code is doing in more detail:

Give it a try. After the start beep, gently tap a short rhythm on the CREATOR Kit lid (don't tap the accelerometer directly — we don't want to break it!). Stop tapping after the second beep.

One you're done tapping, the output will show you the number of times that the taps occurred. The numbers that are printed are the time through the for loop that the tap occurred. For each tap, you might see a bunch of numbers close together in value. The reason is that when the tap occurs, the z-force will go wildly up and down for a few moments as the kit vibrates.

We'll fix that in the next project...

  1. What happens if you hold the kit off of the table while it is being calibrated? Why?

  2. The taps were recorded for about 2 seconds. Can you double the length of time that taps get recorded?

  3. The z-force threshold was arbitrarily chosen to be 0.1. Can you change this threshold to be more or less sensitive to taps? If the threshold is too low, then you'll record taps even when no one tapped. What is the smallest threshold you can use that doesn't record any taps if no one taps?