Click here to Skip to main content
14,921,505 members
Articles / Internet of Things
Posted 11 May 2016


5 bookmarked

I2C Between Raspberry and Pic24

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
11 May 2016CPOL2 min read
RaspberryPi2 master transmits to and receives from Pic24FJ64GB002 slave through I2C

Default I2C bus frequency: 100kHz. RaspberryPi2 has I2C module activated. Pic24 Is mounted on Microstick II.

Pic24: Ra0, Ra1 and Ra2 are outputs for 3 Leds.

3 I2C wires between RaspberryPi2 and Pic24: Gpio3->Microchip18; Gpio5->Microchip17; Gpio9->Microchip27. The length of 3 wires is 20 cm.

I2Cms Random

I trimmed the delay code at the head of _SI2C1Interrupt in Pic24. Too long and it slows everything down and also makes errors.

Using the code: Raspberry fills a table with 256 random numbers [0, 255], sends it to Pic24. Pic24 receives and sends back the table. Raspberry compares local and received table.

Load I2Cs6.X on Pic24 and I2Cm5 on Raspberry. Type sudo ./I2Cm or sudo ./I2Cm xxx . xxx must be a positive number: number of cycles desired: 800 for example.

Image 1

Image 2


I2Cms Ranges

I trimmed the delay code at the head of _SI2C1Interrupt in Pic24. This time, I used some more delay.

Using the code: Raspberry sends Pic24 3 parameters: start number, number of iterations, increment. Pic24 increments start number for number of iterations and sends back all numbers calculated. These numbers are compared with local copies in Raspberry.

Load I2Cs7.X on Pic24 and I2Cm6 on Raspberry. Type sudo ./I2Cm xxx yyy zzz. xxx, yyy, zzz must be numbers in the range [0,255]. All parameters are needed. Now Raspberry can send commands to Pic24. Heap on Pic24 wasn't strictly necessary, but it helps. There isn't a 1 to 1 dependency between dimension of array and heap needed. In this case, my max dimension would be 255 elements, but Pic24 wants more or less 500. I don't intervene in the overflow that Rx and Tx registers of Pic24 have for I2C: they correctly manage only 8 bits. The digits displayed by PuTTY are correct.

Image 3

Image 4

I use VisualGDB to program Raspberry from laptop ASUS with Windows10; MPLAB X IDE v 3.20 for Pic24.

Points of Interest

  • I2C


Below, there is a bunch of attempts I've made learning some I2C. You can also find some useful links.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

Comments and Discussions

-- There are no messages in this forum --