Arduino notes five Three-Axis Gyroscope l00004200d

Source: Internet
Author: User

Lda-4200d is a MEMS motion sensor released by Italian law (ST) semiconductor company: Three-axis digital output gyroscope. Optional;-250 ~ 250,-500 ~ 500,-2000-2000dps

Development Environment:
System: XP
Board: Arduino Leonardo
Platform: arduino-1.0.1

Objective: To read the raw data of the Three-Axis Gyroscope and display it through the serial port

I. Hardware Introduction

The schematic diagram of the Three-Axis Gyroscope l00004200d module is as follows:

It is only used to connect to the corresponding interfaces of Arduino by using the respective methods of SCL, SDA, vcc_3.3v, And Gnd. Arduino Leonardo can be directly attached with SDA and SCL, and other Arduino can be connected based on their own board.

2. Compile the test code

The current version of Arduino is high, and all the routines found on the Internet are compiled and compiled only after a lower version is changed. You can refer to the above Code https://github.com/pololu/L3G4200D/tree/66f1448d7f6767e12d0fe0c5c50d4e037aedc27c/L3G4200D to find the two files l00004200d. cpp l00004200d. h, but the file does not seem to be able to directly, the code is pasted on the web page, directly copy down. Then you need to create a new l3g4200d directory under the arduino-1.0.1-windows \ arduino-1.0.1 \ libraries, copy l3g4200d. cpp l3g4200d. H to the just created l3g4200d, you can use l3g4200d class in Android.

File l00004200d. cpp

 

#include <L3G4200D.h>#include <Wire.h>#include <math.h>// Defines ////////////////////////////////////////////////////////////////// The Arduino two-wire interface uses a 7-bit number for the address, // and sets the last bit correctly based on reads and writes#define GYR_ADDRESS (0xD2 >> 1)// Public Methods //////////////////////////////////////////////////////////////// Turns on the L3G4200D's gyro and places it in normal mode.void L3G4200D::enableDefault(void){// 0x0F = 0b00001111// Normal power mode, all axes enabledwriteReg(L3G4200D_CTRL_REG1, 0x0F);}// Writes a gyro registervoid L3G4200D::writeReg(byte reg, byte value){Wire.beginTransmission(GYR_ADDRESS);Wire.write(reg);Wire.write(value);Wire.endTransmission();}// Reads a gyro registerbyte L3G4200D::readReg(byte reg){byte value;Wire.beginTransmission(GYR_ADDRESS);Wire.write(reg);Wire.endTransmission();Wire.requestFrom(GYR_ADDRESS, 1);value = Wire.read();Wire.endTransmission();return value;}// Reads the 3 gyro channels and stores them in vector gvoid L3G4200D::read(){Wire.beginTransmission(GYR_ADDRESS);// assert the MSB of the address to get the gyro // to do slave-transmit subaddress updating.Wire.write(L3G4200D_OUT_X_L | (1 << 7)); Wire.endTransmission();Wire.requestFrom(GYR_ADDRESS, 6);while (Wire.available() < 6);uint8_t xla = Wire.read();uint8_t xha = Wire.read();uint8_t yla = Wire.read();uint8_t yha = Wire.read();uint8_t zla = Wire.read();uint8_t zha = Wire.read();g.x = xha << 8 | xla;g.y = yha << 8 | yla;g.z = zha << 8 | zla;}void L3G4200D::vector_cross(const vector *a,const vector *b, vector *out){  out->x = a->y*b->z - a->z*b->y;  out->y = a->z*b->x - a->x*b->z;  out->z = a->x*b->y - a->y*b->x;}float L3G4200D::vector_dot(const vector *a,const vector *b){  return a->x*b->x+a->y*b->y+a->z*b->z;}void L3G4200D::vector_normalize(vector *a){  float mag = sqrt(vector_dot(a,a));  a->x /= mag;  a->y /= mag;  a->z /= mag;}

File l00004200d. h:

 

 

#ifndef L3G4200D_h#define L3G4200D_h#include <Arduino.h> // for byte data type// register addresses#define L3G4200D_WHO_AM_I      0x0F#define L3G4200D_CTRL_REG1     0x20#define L3G4200D_CTRL_REG2     0x21#define L3G4200D_CTRL_REG3     0x22#define L3G4200D_CTRL_REG4     0x23#define L3G4200D_CTRL_REG5     0x24#define L3G4200D_REFERENCE     0x25#define L3G4200D_OUT_TEMP      0x26#define L3G4200D_STATUS_REG    0x27#define L3G4200D_OUT_X_L       0x28#define L3G4200D_OUT_X_H       0x29#define L3G4200D_OUT_Y_L       0x2A#define L3G4200D_OUT_Y_H       0x2B#define L3G4200D_OUT_Z_L       0x2C#define L3G4200D_OUT_Z_H       0x2D#define L3G4200D_FIFO_CTRL_REG 0x2E#define L3G4200D_FIFO_SRC_REG  0x2F#define L3G4200D_INT1_CFG      0x30#define L3G4200D_INT1_SRC      0x31#define L3G4200D_INT1_THS_XH   0x32#define L3G4200D_INT1_THS_XL   0x33#define L3G4200D_INT1_THS_YH   0x34#define L3G4200D_INT1_THS_YL   0x35#define L3G4200D_INT1_THS_ZH   0x36#define L3G4200D_INT1_THS_ZL   0x37#define L3G4200D_INT1_DURATION 0x38class L3G4200D{public:typedef struct vector{float x, y, z;} vector;vector g; // gyro angular velocity readingsvoid enableDefault(void);void writeReg(byte reg, byte value);byte readReg(byte reg);void read(void);// vector functionsstatic void vector_cross(const vector *a, const vector *b, vector *out);static float vector_dot(const vector *a,const vector *b);static void vector_normalize(vector *a);};#endif

File l00004200d. Ino

 

 

#include <Wire.h>#include <L3G4200D.h>L3G4200D gyro;void setup() {  Serial.begin(9600);  Wire.begin();  gyro.enableDefault();}void loop() {  gyro.read();  Serial.print("G ");  Serial.print("X: ");  Serial.print((int)gyro.g.x);  Serial.print(" Y: ");  Serial.print((int)gyro.g.y);  Serial.print(" Z: ");  Serial.println((int)gyro.g.z);  delay(100);}

Iii. Compile and Test

 

Arduino is very easy to operate. After selecting the board and reference, you can directly click "check" to start compilation. Compilation is okay. Click "->" and start the upload program, the upload progress bar is complete.


Start tools/serial monitor to display the following information:

This is the result of horizontal placement. The tilt module will see the value change.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.