Shop OBEX P1 Docs P2 Docs Learn Events
I2C??? — Parallax Forums

I2C???

JhhanJhhan Posts: 3
edited 2008-02-08 05:39 in General Discussion
import stamp.core.*;
import stamp.peripheral.io.I2C;

public class SRF02{

· static I2C i2cBus = new I2C(CPU.pin0, CPU.pin1);

· static int Distance=0;
· static int Light=0;

· static int DataHigh = 0;
· static int DataLow = 0;

· public static void main() {

··· while (true) {

····· Distance = getSRF08Data ();

····· System.out.print("Distance(cm) is ");
····· System.out.println(Distance + "\n");

····· CPU.delay(20000);
··· }
· }

· public static int getSRF08Data(){

··· System.out.print("Reading SRF08 Data ");
··· System.out.print(".....");

··· i2cBus.start();
··· i2cBus.write(0x0);
··· CPU.delay(1);
··· i2cBus.write(0x51);
··· DataHigh = i2cBus.read(0);
··· DataLow = i2cBus.read(1);
··· i2cBus.stop();

··· System.out.println("Done");
··· return(DataHigh+DataLow);
· }
}

Hi,

I used the following code to make an I2C connection between the Javelin and an ultrasonic sensor.

However, it seems to return 510 all the time. I'm not sure if something is wrong with the commands.. (There's no compile-time error).

Can someone help me out?

The list of commands are found in: http://www.robot-electronics.co.uk/htm/srf02tech.htm

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-02-07 10:30
    It looks you are omitting the device address (default 0xE0),
    so try this

    ··· i2cBus.start();
    ··· i2cBus.write(0xE0); //device address
    ··· i2cBus.write(0x00);·//register
    ··· i2cBus.write(0x51); //range cm
    ··· CPU.delay(700); //wait 65 msec
    ··· DataHigh = i2cBus.read(2);
    ··· DataLow = i2cBus.read(3);
    ··· i2cBus.stop();

    regards peter
  • JhhanJhhan Posts: 3
    edited 2008-02-08 02:44
    Peter Verkaik said...
    It looks you are omitting the device address (default 0xE0),
    so try this

    ··· i2cBus.start();
    ··· i2cBus.write(0xE0); //device address
    ··· i2cBus.write(0x00);·//register
    ··· i2cBus.write(0x51); //range cm
    ··· CPU.delay(700); //wait 65 msec
    ··· DataHigh = i2cBus.read(2);
    ··· DataLow = i2cBus.read(3);
    ··· i2cBus.stop();

    regards peter


    ·Hi, Thank you for your response, but I don't seem to get the correct range... I'm still getting 510.

    Is there something wrong with either my sensor or my Javelin???
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-02-08 05:39
    I just noted your class is called SRF02,
    whereas your read method is called getSRF08Data().

    So which device do you have, because the SRF02 has both a serial and I2C interface.
    If you have the SRF02 make sure the I2C interface is selected.
    Also,·do you have 4.7k pullup resistors on javelin I/O pins 0 and 1?

    regards peter
Sign In or Register to comment.