I2c line following sensor


I was trying to interface with an i2c line following sensor and having some trouble and was hoping someone could help me out.

The line following sensor is as follows: http://shop.gadgetsngizmosetc.com/Line-following-Sensor-Line-Sensor.htm

psuedocode(found at the bottom of the link):

i2c w 80 1 2 // set to analog mode

i2c r 80 7 // read 7 bytes

Here is the section of my code that implements it however it doesn’t work.

      while (Wire.available() != 7);
      for (int i=0; i<7; i++){
        lineFollowing[i] = Wire.receive();
        Serial3.print(" ");
      Serial3.println(" ");

I apologize I had more to say.

This is just a section of the code. I know that this by itself will not work. When it run it get to the point in the code where it waits for 7 bytes of data. It never receives 7 bytes of data. I emailed the distributor and they do not respond. Any help would greatly be appreciated.

I am wondering if I have to send all of this together as in some type of an array. My next option is to trying using the serial communications.

Thank you Casey

Try removing the Wire.available() statement and just read the data that is returned.

I actually started the code without the Wire.available(). The problem with that is that it doesn’t wait for the line following sensor to collect the data. All it sends me is a bunch of garbage. I found something on the internet that was very helpful.


I implemented his code on my machine and it works. I however want to see the analog values. So I have come to the conclusion that the send sequence is what isn’t working. If anyone has got any suggestion it would be super.

  //declare array for line following sensor
  byte lineFollowing;
  Serial3.println("enter m to exit test");
  if (device == 1){
    Serial3.println("testing i2c line following sensor");
    while(mode != 'm'){
      if (Serial3.available() > 0)
        mode = Serial3.read();
      while (Wire.available() < 0);
      lineFollowing = Wire.receive();
      Serial3.println(lineFollowing, BIN);

Thank you

Well that articles helps a lot. The address you are using, 80, is actually the 8 bit address when in fact you need to use the 7 bit address for Arduino. Change the 80 to 40 (80 bit shifted to the right by one or just simply the 8 bit number divided by two).

As for the Wire.available(), the only thing that function is really good for is telling you how many bytes you received. It's not like Serial.available() that will update as more bytes come in. Before your code gets to Wire.available() all of the bytes have already been transferred and stored in the internal array.