Jeff-Paredes
Published © MIT

Accelerometer Using Arduino 101

Learn how to read Accelerometer Sensor on Arduino 101 and show tilt status on LED.

BeginnerProtip1 hour9,948

Things used in this project

Hardware components

LED (generic)
LED (generic)
×1
Resistor 221 ohm
Resistor 221 ohm
×1
Breadboard (generic)
Breadboard (generic)
×1
Arduino 101
Arduino 101
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Create 3 LED in Arduino Circuit

Create 3 LED in Arduino Circuit

Code

ReadSensorsLed.ino

C/C++
Reads accelerometer and gyro sensor
#include "CurieIMU.h"
int ax, ay, az;         // accelerometer values
int gx, gy, gz;         // gyrometer values

int axBrightness = 0;
int axLed = 9;

int ayBrightness = 0;
int ayLed = 6;


int azBrightness = 0;
int azLed = 5;

void setup(){
  
  pinMode(axLed, OUTPUT); 
  pinMode(ayLed, OUTPUT); 
  Serial.begin(9600); // initialize Serial communication
  CurieIMU.begin();
  delay(5000); // Allow the user to set everything down
  CurieIMU.autoCalibrateGyroOffset();
  CurieIMU.autoCalibrateAccelerometerOffset(X_AXIS, 0);
  CurieIMU.autoCalibrateAccelerometerOffset(Y_AXIS, 0);
  CurieIMU.autoCalibrateAccelerometerOffset(Z_AXIS, 0);
}

String jsonEncodeValue(String key, float keyVal){
  return "\"" + key + "\":" + String(keyVal) + "";
}

String assembleJson(String keysAndVals){
  return "{" + keysAndVals + "}";
}

void loop(){
  // read raw accel/gyro measurements from device
  CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz);

  // display JSON formatted accel/gyro x/y/z values
  String keyVals = jsonEncodeValue("ax", ax) + ",";
  keyVals += jsonEncodeValue("ay", ay) + ",";
  keyVals += jsonEncodeValue("az", az) + ",";
  keyVals += jsonEncodeValue("gx", gx) + ",";
  keyVals += jsonEncodeValue("gy", gy) + ",";
  keyVals += jsonEncodeValue("gz", gz);
  
  if(Serial){
    Serial.println(keyVals);
  }
  delay(100);

  if(ax>0)
    axBrightness = ax/66.66;
  else
    axBrightness=0;
  analogWrite(axLed,axBrightness);

  if(ay>0)
    ayBrightness = ay/66.66;
  else
    ayBrightness=0;
  analogWrite(ayLed,ayBrightness);
  
  if(az>0)
    azBrightness = az/66.66;
  else
    azBrightness=0;
  analogWrite(azLed,azBrightness);
}

Credits

Jeff-Paredes

Jeff-Paredes

1 project • 126 followers
Programming is my love and engineering is my third party :)

Comments