You can use the sketch below to observe the behavior of computing the Circuit Playground tilt angle via the two different methods.

// Circuit Playground Slouch Detector Angle Demo
// Compute current angle using accelerometer.
// Compare asin() to atan2().
// Author: Carter Nelson
// MIT License (

#include <Adafruit_CircuitPlayground.h>

#define GRAVITY             9.80665   // standard gravity (m/s^s)
#define RAD2DEG             52.29578  // convert radians to degrees

float currentAngle1;
float currentAngle2;

void setup() {

  // Initialize Circuit Playground

void loop() {
  // Compute current angle
  currentAngle1 = RAD2DEG * asin(-CircuitPlayground.motionZ() / GRAVITY);
  currentAngle2 = RAD2DEG * atan2(-CircuitPlayground.motionZ() , 
                                   CircuitPlayground.motionX() );
  // Print current angle

  // But not too fast

Copy and paste this code into the Arduino IDE and upload it to the Circuit Playground. Then open the serial plotter:

Tools -> Serial Plotter

First, try holding the Circuit Playground with the micro USB connector pointed up. This makes the X axis point up as shown in the figures in the previous section. Now tilt the Circuit Playground a little and watch the two lines. They should follow each other pretty closely. Now rotate the Circuit Playground so the micro USB connector is pointed to the side. Tilt the Circuit Playground again and you should see the lines behaving very differently. Something like what is shown in the figure below.

The blue line is from the asin() function and continues to behave correctly. However, the orange line, which comes from the atan2() function only works when the X axis is pointing up. For our slouch detector, we can't assume this to be true. Therefore, we will use the asin() equation to compute the value for currentAngle.

This guide was first published on Dec 21, 2016. It was last updated on Mar 08, 2024.

This page (Arctan Vs. Arcsine) was last updated on Dec 15, 2016.

Text editor powered by tinymce.