Go Down

Topic: Arduino Nano with Lego Accelerometer (Read 876 times) previous topic - next topic

Nai

I don't know how many people have actually done this, but my day was spent getting this to work.
So to start with, you need to cut a lego cable
Soldering/tinning the ends works best ;)

Then, with a breadboard most likely, connect the pins 1 and 4 to 5v, 2 and 3 to ground, 5 and 6 to I2C clock and data respectively.
Then the code:
Code: [Select]

#include "Wire.h"
void setup()
{
 Wire.begin();
 Serial.begin(19200);           // start serial for output
}

void loop()
{
 Wire.beginTransmission(1); // transmit to device #1(lego accelerometer
 Wire.send(0x42);           // sends Read first 8 bits of accelerometer
 Wire.endTransmission();
 delay(700);
 Wire.requestFrom(1, 8);
 while(Wire.available())
 {
   char c = Wire.receive();
   Serial.println(c,HEX);    //output the data
 }
 Serial.print("Done \n");
 delay(500);
}

Fairly simple, but I had trouble googling it, so I did it myself
The rest of the request bytes can be found on the hitechnic site

Always glad to help
~Nai

Casey Gross

#1
Jun 08, 2010, 10:11 pm Last Edit: Jun 08, 2010, 10:12 pm by caseygross Reason: 1
You might have to take a look at the datasheet on the accelerometer, for the I2C connection. Are you receiving any type of data back? If so, will you post it.

Nai

Nope, got it working. One of the wires was broken inside the connector.
Also, for anyone else doing this here's the code:
Code: [Select]

#include "Wire.h"
void setup()
{
 Wire.begin();
 Serial.begin(19200);
}

void loop()
{
 Wire.beginTransmission(1);
 Wire.send(0x42);
 Wire.endTransmission();
 delay(20);
 Wire.requestFrom(1, 1);
 char b;
 while(Wire.available())
 {
   b = Wire.receive();
 }
 delay(50);
 Wire.send(0x45);  
 Wire.endTransmission();
 delay(20);
 Wire.requestFrom(1, 1);
 while(Wire.available())
 {
   char c = Wire.receive();
   int i = (c & 3) + (b << 2);
   Serial.println(i,DEC);
 }
 delay(500);
}

Go Up