Pattern creations by sensing body motion 4

GYROSCOPE 

Gyro L3G4200D detects or measure rotation on 3 axis: x, y and z. It has three selectable scales: 250 , 500 or 2000 deg/sec. Gyros are not affected by gravity (like accelerometers) and can give you exact information how object is orientated. Gyros and accelerometers are often used together because of great complement to each other. Three rotation of axis are referenced as x or roll, y or pitch and z or yaw.

Hooking it up:

Gyros can be connected in two different ways: I2C or SPI

1-  I2C (2 wire serial connection)

SDA (data) and SCL (clock) are connected to analog pins A4 and A5. VCC to 3.3V (can be also 5V),  SDO to power and CS needs to be high too. Many suggested connection on internet don’t have CS connected to power, so gyro is not working.  With this connection the I2C address in the Arduino code  is 105. If you use other connections also 104 is applicable.

 

Screen Shot 2014-07-07 at 7.56.01 PM

GYROconn

Arduino code:

#include <Wire.h>

#define CTRL_REG1 0×20
#define CTRL_REG2 0×21
#define CTRL_REG3 0×22
#define CTRL_REG4 0×23

int Addr = 105; // I2C address of gyro
int x, y, z;

void setup(){
Wire.begin();
Serial.begin(9600);
writeI2C(CTRL_REG1, 0x1F); // Turn on all axes, disable power down
writeI2C(CTRL_REG3, 0×08); // Enable control ready signal
writeI2C(CTRL_REG4, 0×80); // Set scale (500 deg/sec)
delay(100); // Wait to synchronize
}

void loop(){
getGyroValues(); // Get new values
// In following Dividing by 114 reduces noise
Serial.print(x / 114);
Serial.print(” ,”); Serial.print(y / 114);
Serial.print(” ,”); Serial.println(z / 114);
delay(500); // Short delay between reads
}

void getGyroValues () {
byte MSB, LSB;

MSB = readI2C(0×29);
LSB = readI2C(0×28);
x = ((MSB << 8) | LSB);

MSB = readI2C(0x2B);
LSB = readI2C(0x2A);
y = ((MSB << 8) | LSB);

MSB = readI2C(0x2D);
LSB = readI2C(0x2C);
z = ((MSB << 8) | LSB);
}

int readI2C (byte regAddr) {
Wire.beginTransmission(Addr);
Wire.write(regAddr); // Register address to read
Wire.endTransmission(); // Terminate request
Wire.requestFrom(Addr, 1); // Read a byte
while(!Wire.available()) { }; // Wait for receipt
return(Wire.read()); // Get result
}

void writeI2C (byte regAddr, byte val) {
Wire.beginTransmission(Addr);
Wire.write(regAddr);
Wire.write(val);
Wire.endTransmission();
}

Screen Shot 2014-07-17 at 1.45.35 PM

The idea is that every second cube rotates the same way (second, fourth .. in first row and first, third … in second row and so on).

Processing code

work in progress

 

Leave a Reply