Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Jeff-Paredes
Published © MIT

Gyroscope With Arduino 101

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

BeginnerProtip1 hour9,589

Things used in this project

Hardware components

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

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematics

Code

GyroscopeSensor

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

int gxBrightness = 0;
int gxLed = 9;

int gyBrightness = 0;
int gyLed = 6;


int gzBrightness = 0;
int gzLed = 5;

void setup(){
  
  pinMode(gxLed, OUTPUT); 
  pinMode(gyLed, OUTPUT); 
  pinMode(gzLed, 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(gx>0)
    gxBrightness = gx/66.66;
  else
    gxBrightness=0;
  analogWrite(gxLed,gxBrightness);

  if(gy>0)
    gyBrightness = gy/66.66;
  else
    gyBrightness=0;
  analogWrite(gyLed,gyBrightness);
  

  
  if(gz>0)
    gzBrightness = gz/66.66;
  else
    gzBrightness=0;
  analogWrite(gzLed,gzBrightness);

}

Credits

Jeff-Paredes

Jeff-Paredes

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

Comments