Lego ultrasonic sensor with Teensy 3.0 and wire library

Status
Not open for further replies.

yan.kun0567

New member
Hi,

I got my Teensy 3.0 a few days ago, so far it is fantastic! But now I'm stucked at following problem. I'm having a Lego ultrasonic sensor, which I would like to control via the I2C bus.

I know that the sensor has some quirks regarding I2C compability, but on the web you can find a few advices how to work around them. But somehow I fail to get a respones from my device.

In the past it was impossible to get it to work with the wire library because it was missing the ability to send a repeated start signal. So all examples I found using the I2CMaster Library. But since the wire library is now also supporting repeated start, I thought I could give it a try.

Here is my code:

Code:
#include <Wire.h>

int sensorAddress = 0x01;
int clockPin = 10;
byte buffer[9];

void setup() {
  Wire.begin();
  Serial.begin(9600);
}

void loop() {
  //Read product id, should return LEGO.
  printCommandToSerial(0x08);
  
  delay(1000); 
}

void printCommandToSerial(byte cmd) {
  delay(1000);

  byte stat = 0x31;
  int receivedStat = 0;

  Serial.print("Sensor Address is: ");
  Serial.println(sensorAddress);

  pinMode(clockPin, INPUT);
  digitalWrite(clockPin, HIGH);

  Serial.print("Writing Command: ");
  Serial.println(cmd);

  Wire.beginTransmission(sensorAddress);
  Wire.write(cmd);
  stat = Wire.endTransmission();

  Serial.print("Result Transmission: ");
  Serial.println(stat);
  
  jiggleClockLine();

  receivedStat = Wire.requestFrom(sensorAddress, 8, 0);

  Serial.print("Result Received: ");
  Serial.println(receivedStat);

  while(Wire.available()) {
    Serial.print(Wire.receive());
  }

  Serial.println("");

}

void jiggleClockLine() {
  delayMicroseconds(50);
  pinMode(clockPin, OUTPUT);
  digitalWrite(clockPin, LOW);
  delayMicroseconds(50);
  pinMode(clockPin, INPUT);
  digitalWrite(clockPin, HIGH);
  delayMicroseconds(50);
}

The sensor is connected directly to the two I2C pins on 19, 18 without any pull up resistors as they are build in in the sensor. I've connect an additional 9V battery to the white wire and connected one of the two ground signals to the battery (already tried switching them back and forth).

Could anybody guid me what else I could try? Thanks in advance!
 
Status
Not open for further replies.
Back
Top