PIC18F I2C Communication with Arduino


Closed Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    May 2016
    Posts
    33

    Default PIC18F I2C Communication with Arduino

    Hello all,

    I read all the I2C related posts but still can't fully understand how to send dada from PIC18F to an Arduino.

    I set the address of arduino to 0x26 and try to do following in PicBasic but I don't get any data:


    Code:
    TRISC.0 = 0
    TRISB = 000000
    
    LED VAR PORTC.0   ' Assign name "LED" to PORTC.0
    SCL VAR PORTB.3    ' Clock pin
    SDA VAR PORTB.1    ' Data pin
    
    ADDR VAR WORD
    
    ADDR = $26 ' I2C address
    
    mainloop:
       HIGH LED        ' Turn on LED connected to PORTB.0
       Pause 1000       ' Delay for 1 seconds
       
       I2CWrite SDA,SCL,ADDR,[0]
       
       LOW LED          ' Turn off LED connected to PORTB.0
       Pause 1000       ' Delay for 1 seconds
       Goto mainloop   ' Go back to loop and blink LED forever

    Any idea?

  2. #2
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    Im no expert, but don't you have to send data. All the examples I've seen, and use send and receive a variable after the address, eg to send the value of a word variable

    Code:
    I2CWRITE SDApin,SCLpin,i2cWriteAddress,i2cControl,[variable.lowbyte,variable.highbyte]
    I can't see that in your code

    Also I think you need to set one device as a slave and another as master, so maybe posting your arduino code could also help those more qualified to comment to asses whats going on

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,519


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    The data being sent is the value 0, that looks alright to me though I've literally never used I2CWrite.
    Check the pins you're using for multiplexed functions, specifically analog such as ADC and comparators and make sure the pins are set to digital.

    Then I'd check the pins with a scope or logic analyzer to verify if data is actually coming out or not - if you don't KNOW, for sure, that the Arduino code is working.

    /Henrik.

  4. #4
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    I've literally never used I2CWrite.
    Really Henrick? Wow, I use it all the time to write to serial eeprom's, a/d's and i/o expanders, etc. You should really design more hardware..
    Dave Purola,
    N8NTA
    EN82fn

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    Can you inform us which 18F pic you are using?

    Most of the 18F PICs (28 pin and 40 pin) have the hardware I2C pins on PORT C not PORTB. Whilst I'm sure PBP can define any pins, it might be "better" to use the actual pins associated with the hardware module on the PIC, eg

    Code:
    SCLpin             var PORTC.3          'clk
    SDApin             var PORTC.4          'data

  6. #6
    Join Date
    May 2016
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    I am using PIC18F2450 which doesn't have hardware I2C pins.Isn't I2CWRITE just a software solution and not using actual hardware?

    here is arduino code:

    Code:
    #include <Wire.h>
    
    void setup() {
      Wire.begin(0x26);                // join i2c bus with address
      Wire.onReceive(receiveEvent); 
      Serial.begin(9600);          
      
    }
    
    void loop() {
     delay(1000);
    
    }
    
    
    void receiveEvent(int howMany) {
    
      while (1 < Wire.available()) { 
        char c = Wire.read(); 
        Serial.print(c);    
      }
      int x = Wire.read();   
      Serial.println(x);
    }

  7. #7
    Join Date
    May 2016
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    I am using 2 2.2K pull up resistors on pins.

    Correct arduino code:

    Code:
    #include <Wire.h>
    
    void setup() {
      Wire.begin(0x26);                // join i2c bus with address
      Wire.onReceive(receiveEvent); 
      Serial.begin(9600);          
      
    }
    
    void loop() {
     delay(1000);
    
    }
    void receiveEvent(int howMany) {
    
    while(Wire.available() > 0){  
      int x = Wire.read();    
     Serial.println(x); 
      }
    }

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,519


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    Dave,
    I do design hardware from time to time but yeah, I'd love to do more. I tend to use SPI as I think it's both easier and faster and all the functions you mention are available in chips with SPI-interface as well as I2C so I've never actually had the need TO use it - that's it really.

    Scampy,
    I2C/Read/Write are not "hardware assisted" commands, they do not use the (M)SSP module in the PIC so any pins can be used.

    picmilan,
    I'll repeat what I said earlier: Check if the pins you're using has analog functions and make sure they're turned off.

    /Henrik.

  9. #9
    Join Date
    May 2016
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    Quote Originally Posted by HenrikOlsson View Post
    picmilan,
    I'll repeat what I said earlier: Check if the pins you're using has analog functions and make sure they're turned off.

    /Henrik.
    Yeah,You are right! Looking at datasheet those pins are analog by default..

    Now Control Address and write address is what confuses me.Control Address is 0x26 as defined in arduino code,right? so what is write address?

    Is SPI easier? I don't have many pins available but if I2C doesn't work I guess I don't have any other option.

  10. #10
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    Henrik, I thought as much, which is why I added the comment about PBP using any pin. I was simply suggesting he tried other pins, and use those designated for such communications if the pic has them. Hopefully if the OP disables any analogue config his issue will be resolved

  11. #11
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    try it like this
    Arduino i2c address' are 7bit
    pbp uses 8bit


    ADDR = $26 ' 7bit Arduino I2C address
    addr=addr<<1 ;shift to 8 bitformat



    I2CWrite SDA,SCL,ADDR,[0]
    Warning I'm not a teacher

  12. #12
    Join Date
    May 2016
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    Quote Originally Posted by richard View Post
    try it like this
    Arduino i2c address' are 7bit
    pbp uses 8bit


    ADDR = $26 ' 7bit Arduino I2C address
    addr=addr<<1 ;shift to 8 bitformat



    I2CWrite SDA,SCL,ADDR,[0]
    It worked! Thanks alot!

Similar Threads

  1. Interfacing with Arduino I2C LCD
    By norohs in forum Documentation
    Replies: 47
    Last Post: - 30th May 2017, 18:53
  2. I2C lcd ( arduino ) with PICBASIC, help
    By iw2fvo in forum mel PIC BASIC Pro
    Replies: 92
    Last Post: - 10th September 2014, 18:00
  3. EEprom and MCP9801 I2C communication problems
    By aajgss in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 3rd September 2011, 21:45
  4. How do I operate an Arduino I2C LCD
    By menta in forum General
    Replies: 8
    Last Post: - 13th July 2011, 02:28
  5. Replies: 2
    Last Post: - 10th June 2005, 02:34

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts