{"id":1340,"date":"2018-02-03T11:21:02","date_gmt":"2018-02-03T19:21:02","guid":{"rendered":"http:\/\/www.donluc.com\/?p=1340"},"modified":"2018-02-03T11:21:02","modified_gmt":"2018-02-03T19:21:02","slug":"programming-tri-axis-gyro-l3g4200d-arduino","status":"publish","type":"post","link":"https:\/\/www.donluc.com\/?p=1340","title":{"rendered":"Programming: Tri-Axis Gyro \u2013 L3G4200D \u2013 Arduino"},"content":{"rendered":"<p>DonLuc1802Mk03.ino<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"DonLuc1802Mk03.ino\">\/\/ ***** Don Luc *****\r\n\/\/ Software Version Information\r\n\/\/ DonLuc1802Mk03 1.0\r\n\r\n#include &lt;Wire.h&gt;\r\n\r\n#define CTRL_REG1 0x20\r\n#define CTRL_REG2 0x21\r\n#define CTRL_REG3 0x22\r\n#define CTRL_REG4 0x23\r\n#define CTRL_REG5 0x24\r\n\r\nint L3G4200D_Address = 105; \/\/I2C address of the L3G4200D\r\n\r\nint x;\r\nint y;\r\nint z;\r\n\r\nvoid setup(){\r\n\r\n  Wire.begin();\r\n  Serial.begin(9600);\r\n\r\n  Serial.println(&quot;starting up L3G4200D&quot;);\r\n  setupL3G4200D(2000); \/\/ Configure L3G4200  - 250, 500 or 2000 deg\/sec\r\n\r\n  delay(1500); \/\/wait for the sensor to be ready \r\n  \r\n}\r\n\r\nvoid loop(){\r\n  \r\n   getGyroValues();  \/\/ This will update x, y, and z with new values\r\n\r\n  Serial.print(&quot;X:&quot;);\r\n  Serial.print(x);\r\n\r\n  Serial.print(&quot; Y:&quot;);\r\n  Serial.print(y);\r\n\r\n  Serial.print(&quot; Z:&quot;);\r\n  Serial.println(z);\r\n\r\n  delay(100); \/\/Just here to slow down the serial to make it more readable\r\n  \r\n}\r\n\r\nvoid getGyroValues(){\r\n\r\n  byte xMSB = readRegister(L3G4200D_Address, 0x29);\r\n  byte xLSB = readRegister(L3G4200D_Address, 0x28);\r\n  x = ((xMSB &lt;&lt; 8) | xLSB);\r\n\r\n  byte yMSB = readRegister(L3G4200D_Address, 0x2B);\r\n  byte yLSB = readRegister(L3G4200D_Address, 0x2A);\r\n  y = ((yMSB &lt;&lt; 8) | yLSB);\r\n\r\n  byte zMSB = readRegister(L3G4200D_Address, 0x2D);\r\n  byte zLSB = readRegister(L3G4200D_Address, 0x2C);\r\n  z = ((zMSB &lt;&lt; 8) | zLSB);\r\n  \r\n}\r\n\r\nint setupL3G4200D(int scale){\r\n\r\n  \/\/ Enable x, y, z and turn off power down:\r\n  writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);\r\n\r\n  \/\/ If you&#039;d like to adjust\/use the HPF, you can edit the line below to configure CTRL_REG2:\r\n  writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);\r\n\r\n  \/\/ Configure CTRL_REG3 to generate data ready interrupt on INT2\r\n  \/\/ No interrupts used on INT1, if you&#039;d like to configure INT1\r\n  \/\/ or INT2 otherwise, consult the datasheet:\r\n  writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);\r\n\r\n  \/\/ CTRL_REG4 controls the full-scale range, among other things:\r\n\r\n  if(scale == 250){\r\n    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);\r\n  }else if(scale == 500){\r\n    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);\r\n  }else{\r\n    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);\r\n  }\r\n\r\n  \/\/ CTRL_REG5 controls high-pass filtering of outputs, use it\r\n  \/\/ if you&#039;d like:\r\n  writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);\r\n  \r\n}\r\n\r\nvoid writeRegister(int deviceAddress, byte address, byte val) {\r\n  \r\n    Wire.beginTransmission(deviceAddress); \/\/ start transmission to device \r\n    Wire.write(address);       \/\/ send register address\r\n    Wire.write(val);         \/\/ send value to write\r\n    Wire.endTransmission();     \/\/ end transmission\r\n    \r\n}\r\n\r\nint readRegister(int deviceAddress, byte address){\r\n\r\n    int v;\r\n    \r\n    Wire.beginTransmission(deviceAddress);\r\n    Wire.write(address); \/\/ register to read\r\n    Wire.endTransmission();\r\n\r\n    Wire.requestFrom(deviceAddress, 1); \/\/ read a byte\r\n\r\n    while(!Wire.available()) {\r\n      \r\n        \/\/ waiting\r\n        \r\n    }\r\n\r\n    v = Wire.read();\r\n    return v;\r\n    \r\n}<\/pre>\n<p><strong>Don Luc<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>DonLuc1802Mk03.ino \/\/ ***** Don Luc ***** \/\/ Software Version Information \/\/ DonLuc1802Mk03 1.0 #include &lt;Wire.h&gt; #define CTRL_REG1 0x20 #define CTRL_REG2 0x21 #define CTRL_REG3 0x22 #define CTRL_REG4 0x23 #define CTRL_REG5 0x24 int L3G4200D_Address = 105; \/\/I2C address of the L3G4200D int x; int y; int z; void setup(){ Wire.begin(); Serial.begin(9600); Serial.println(&quot;starting up L3G4200D&quot;); setupL3G4200D(2000); \/\/ Configure &#8230; <a title=\"Programming: Tri-Axis Gyro \u2013 L3G4200D \u2013 Arduino\" class=\"read-more\" href=\"https:\/\/www.donluc.com\/?p=1340\" aria-label=\"Read more about Programming: Tri-Axis Gyro \u2013 L3G4200D \u2013 Arduino\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,38,5,58,43],"tags":[],"class_list":["post-1340","post","type-post","status-publish","format-standard","hentry","category-arduino","category-digitalelectronics","category-microcontrollers","category-arduino-programming","category-sparkfun"],"_links":{"self":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/1340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1340"}],"version-history":[{"count":2,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/1340\/revisions"}],"predecessor-version":[{"id":1342,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/1340\/revisions\/1342"}],"wp:attachment":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}