I2C Slave.

Post Reply
keithsloan52
Posts: 30
Joined: Mon Dec 28, 2015 5:51 pm

I2C Slave.

Post by keithsloan52 » Thu Feb 18, 2016 11:52 am

I am trying to setup the chipKIT Pi as an I2C Slave to the Raspberry Pi

But when I load the following sketch

Code: Select all

/ Simple I2C protocol for Arduino
// Slave side program
// (c) 2014 Ignas Gramba
//
#include <Wire.h>
 
#define XSensorPin 2
#define YSensorPin 3
 
const byte SlaveDeviceId = 22;
byte LastMasterCommand = 0;
 
void setup(){
  Wire.begin(SlaveDeviceId);      // join i2c bus with Slave ID
  Wire.onReceive(receiveCommand); // register talk event
  Wire.onRequest(slavesRespond);  // register callback event
 
  pinMode(XSensorPin, INPUT);
  pinMode(YSensorPin, INPUT);
}
 
void loop(){
  delay(100);
}
 
void receiveCommand(int howMany){
  LastMasterCommand = Wire.read(); // 1 byte (maximum 256 commands)
}
 
void slavesRespond(){
 
  int returnValue = 0;
 
  switch(LastMasterCommand){
    case 0:   // No new command was received
      Wire.write("NA");
    break;
    
    case 1:   // Return X sensor value
      returnValue = GetXSensorValue();
    break;
 
    case 2:   // Return Y sensor value
      returnValue = GetYSensorValue();
    break;
    
  }
 
  byte buffer[2];                 // split int value into two bytes buffer
  buffer[0] = returnValue >> 8;
  buffer[1] = returnValue & 255;
  Wire.write(buffer, 2);          // return response to last command
  LastMasterCommand = 0;          // null last Master's command
}
 
int GetXSensorValue(){
  int val = analogRead(XSensorPin);
  return val;
}
 
int GetYSensorValue(){
  int val = analogRead(YSensorPin);
  return val;
}
And then run i2cdetect -y 1 on the Raspberry Pi I get

Code: Select all

pi@pi-three:~/chipKITPi $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
70: 70 71 72 73 74 75 76 77
Which I am advised means I probably have a wiring error.

I have SDA on the Pi going to TDO/RBP9/SDA1 pin 18 on the PICmx250f128B
and SCL on the Pi going to TCK/RBP8/SCL1 pin 17 on the PIC

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: I2C Slave.

Post by majenko » Thu Feb 18, 2016 12:02 pm

Do you have pullup resistors on the I2C lines?
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

keithsloan52
Posts: 30
Joined: Mon Dec 28, 2015 5:51 pm

Re: I2C Slave.

Post by keithsloan52 » Thu Feb 18, 2016 4:50 pm

majenko wrote:Do you have pullup resistors on the I2C lines?
The Raspberry Pi has pull up resistors on the SCL & SDA lines so I was assuming I did not need extra.

Did not need extra when I attached an SF08 ultrasonic distance detector to the same setup and that worked just fine.

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: I2C Slave.

Post by majenko » Thu Feb 18, 2016 5:00 pm

Ok. In general internal pullups are frowned upon for I2C since they are normally about 10x the resistance that I2C is supposed to use. Still, they work most of the time for simple 1:1 or 1:2 usage.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

keithsloan52
Posts: 30
Joined: Mon Dec 28, 2015 5:51 pm

Re: I2C Slave.

Post by keithsloan52 » Thu Feb 18, 2016 5:51 pm

majenko wrote:Ok. In general internal pullups are frowned upon for I2C since they are normally about 10x the resistance that I2C is supposed to use. Still, they work most of the time for simple 1:1 or 1:2 usage.
Don't believe they are internal, they are just already there on the Raspberry Pi PCB for SDA & SDL. I think they are both 1.8K from memory,

Post Reply