Problem communication with I2C Line Following Sensor
Navigates to RoboticsConnection.com Home RoboticsConnection.com HomePage
RoboticsConnection User Forum
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        



Problem communication with I2C Line Following... Expand / Collapse
Author
Message
Posted Tuesday, March 25, 2008 4:36 AM


Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Wednesday, March 26, 2008 4:31 AM
Posts: 5, Visits: 9
Hi, Everybody

I'm trying to Interface I2C Line Following Sensor with my PIC16F877 and im use CCS compiler.It seems like it should be fairly straightforward. So what im trying to do as a beginnig is to read white line on the black ground.i made the following connection from the the Sensor:

Vcc: connected to the +5V.

Gnd: connected to the same ground of the PIC.

Sda: connected to the Pin_C3.

Scl: connected to the Pin_C4.

Im using built in pullup resistors for Scl and Sda. When i turned the power on, the  bi-color red/green LEDs were turned on, so the sensor is functioning perfectly.

I'm using the following statement in my Pic Headers:

#use I2C(MASTER,  SLOW,sda=PIN_C3,scl=PIN_C4)

Then I tried to read from the sensor by issuing the following code:

int read_color()
{
int8 data;

i2c_start();
i2c_write(0x50); // Address of the sensore (default address)
i2c_write(0x00); // I don't want to write any thing to the registers
i2c_stop();
i2c_start();
i2c_write(0x51); // Address plus 1 LSB for Read Mode
data = i2c_read();
return data;
}

I put the sensor on the black ground, and I run the program. I checked the value of the data and i got 255. i changed the position of the sensor and i put it on the white ground, the reading should be 0 but i got 255 instead of the 0 !! what its mean (255)? is it means there is no connection?

Any ideas about what I need to do to connect the sensor with the Pic? and is there any wrong with the code ?

Thanks

Post #221
Posted Tuesday, March 25, 2008 7:33 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Moderators
Last Login: Thursday, July 10, 2008 1:17 PM
Posts: 32, Visits: 47
Try this

i2c_read() should be i2c_read(0).

add the i2c_stop();

int read_color()
{
int8 data;

i2c_start();
i2c_write(0x51); // Address plus 1 LSB for Read Mode
data = i2c_read(0);

i2c_stop();
return data;
}



Ringo Davis
Post #222
Posted Tuesday, March 25, 2008 8:34 AM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Administrators
Last Login: Tuesday, September 02, 2008 9:05 AM
Posts: 270, Visits: 400
Please note that I moved this under the Robot Sensor Support forum.

Best Regards!

Jason Summerour
President,
Summerour Robotics Corporation
Microsoft MVP
www.roboticsconnection.com

Post #223
Posted Tuesday, March 25, 2008 8:49 AM


Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Wednesday, March 26, 2008 4:31 AM
Posts: 5, Visits: 9
Try this

i2c_read() should be i2c_read(0).

add the i2c_stop();

int read_color()
{
int8 data;

i2c_start();
i2c_write(0x51); // Address plus 1 LSB for Read Mode
data = i2c_read(0);

i2c_stop();
return data;
}

ringo thank you for your replay, i will try your code and i will tell you about the result

 

nad i will like to apology for posting the topic in wrong place, after i posted the topic i realized that its in wrong place

so im sorry

 

and thanks again for the replay

Post #224
Posted Tuesday, March 25, 2008 8:56 AM


Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Wednesday, March 26, 2008 4:31 AM
Posts: 5, Visits: 9
im sorry ringo, butt could you explain please do i need to use following statment
i2c_write(0x50); // Address of the sensore (default address)

Post #225
Posted Tuesday, March 25, 2008 9:03 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Moderators
Last Login: Thursday, July 10, 2008 1:17 PM
Posts: 32, Visits: 47
I do not use the first write. I don't remember why, I wrote the code a long time ago but it works without it.

Also, I noticed you said you use the internal pullups, those may not be enough. You should have something along the lines of 1.5K pullups per line.

Also, we use the slow i2c, so make sure that is in your statement where you set up the I2C like this

#use I2C(master, sda=PIN_c4, scl=PIN_c3,slow)

Ringo

Ringo Davis

Post #226
Posted Tuesday, March 25, 2008 11:31 AM