Using Arduino Serial Monitor

Since this project uses the accelerometer, let's start by exploring how it works and behaves. You can read some technical details in the Lesson #0 Guide. And for reference, here is where the accelerometer is located on the Circuit Playground.

There is an example sketch included with the Circuit Playground library that we can use to play around with the accelerometer. It can be found in the following location:

File -> Examples -> Adafruit Circuit Playground -> Hello_CircuitPlayground -> Hello_Accelerometer

With this sketch loaded and running on the Circuit Playground, open the Serial Monitor.

Tools -> Serial Monitor

The current values from the accelerometer will be printed once a second.

Try rotating the Circuit Playground around in various orientations and watch how the values change. The values are in units of m/s2 (meters per second squared). If you are doing this on planet Earth, you will see a value of 9.8 m/s2 showing up when an axis is aligned vertically. This value will be there even if the Circuit Playground is held still and does not move. More on this later.

Another way to watch the values of the accelerometer is to use the Serial Plotter. To do this, let's first modify the code slightly. The code below changes the format of the output to work with the Serial Plotter and increases the rate at which the values are displayed.

#include <Adafruit_CircuitPlayground.h>
#include <Wire.h>
#include <SPI.h>
  
float X, Y, Z;

void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

void loop() {
  X = CircuitPlayground.motionX();
  Y = CircuitPlayground.motionY();
  Z = CircuitPlayground.motionZ();

  Serial.print(X);
  Serial.print(",");
  Serial.print(Y);
  Serial.print(",");
  Serial.println(Z);

  delay(100);
}

With this sketch loaded and running on the Circuit Playground, open the Serial Plotter.

Tools -> Serial Plotter

The accelerometer values will now be plotted like a strip chart as shown below. There should be 3 lines, one each for the X, Y, and Z values. Again, play around with rotating the Circuit Playground and watch the values change.

In the example below, the Circuit Playground was slowly rotated so that each axis aligned with the vertical, held there for a bit, and then rotated again. Finally, it was given a bit of a shake.

Using CircuitPython REPL

If you want to do the same thing using CircuitPython, here is a simple program to do that:

CircuitPython only works on the Circuit Playground Express.
# Circuit Playground Express Accelerometer
import time
from adafruit_circuitplayground.express import cpx

# Loop forever
while True:
    x,y,z = cpx.acceleration
    print("X={:6.2f}\tY={:6.2f}\tZ={:6.2f}".format(x,y,z))
    time.sleep(1)

Save the above to a file named main.py on your Circuit Playground Express. Then read the following guide section for how to setup and access the REPL:

With the above program running on the Circuit Playground Express, you should see output like this in your REPL:

Try shaking and rotating your Circuit Playground Express around to watch how the values change.

This guide was first published on Oct 25, 2016. It was last updated on Mar 08, 2024.

This page (Hello Accelerometer) was last updated on Mar 08, 2024.

Text editor powered by tinymce.