I2C Communication between Arduino and Raspberry Pi

I2C Communication between Arduino and Raspberry Pi © GPL3+

Have a successful connection between Arduino and Raspberry Pi using I2C Communication Protocol.

  • 26,148 views
  • 2 comments
  • 20 respects

Components and supplies

Apps and online services

About this project

Hi Everyone!

In today's world, every electronics hobbyist works with Arduino and Raspberry Pi to do his/her projects. With the introduction of Windows 10 IOT Core, Microsoft is also into the embedded world. Today Internet Of Things is a buzzword, but for basic things we need an Arduino to communicate with a Raspberry Pi. In this project I will explain how to communicate the Arduino with the Pi using an I2C bus and Windows 10 IOT Core.

Getting Started

First diving into coding: discussing the basics.

Arduino Uno

The image above shows an Arduino Uno in which we use the analog pin 0 for reading LDR value. I have added one resistor so that we get accurate results because Arduino reads the voltage difference. If we connect only LDR (Photocell) then it will have same voltage drop. Analog pin 4 and 5 are used as SDA and SCL lines which are required to have I2C Communication and these pins need not be initiated. By default they will be acting as SDA and SCL.

Arduino sends data read from LDR to Pi using the function:

Wire.write(response)

Raspberry Pi

With the Raspberry, I have used windows 10 IOT core and have developed a Windows UWP app which reads data from Arduino and displays it. You can use Windows IOT Remote Client to remotely connect to the Raspberry Pi. First extract the code and open .sln file and dump the code in Raspberry Pi.

Code

Arduino CodeC/C++
This code is should be dumped to arduino which will read analog value from ldr on A0 pin and send it to Pi when it request
#include <Wire.h>  // Library which contains functions to have I2C Communication
#define SLAVE_ADDRESS 0x40 // Define the I2C address to Communicate to Uno

byte response[2]; // this data is sent to PI
volatile short LDR_value; // Global Declaration
const int LDR_pin=A0; //pin to which LDR is connected A0 is analog A0 pin  
void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  Wire.begin(SLAVE_ADDRESS); // this will begin I2C Connection with 0x40 address
  Wire.onRequest(sendData); // sendData is funtion called when Pi requests data
  pinMode(LDR_pin,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  
delay(1000);
}
void sendData(){
  LDR_value=analogRead(LDR_pin);
  // Arduino returns 10bit data but we need to convert it to 8bit 
  LDR_value=map(LDR_value,0,1023,0,255);
  response[0]=(byte)LDR_value;
  Wire.write(response,2); // return data to PI
}
Xaml UiC#
change your MainPage.xaml content with this
<Page
    x:Class="I2CComm.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:I2CComm"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel Margin="20">
            <TextBlock Text="LDR Value from Arduino " FontSize="25"></TextBlock>
            <TextBlock Name="ldr_val" Text="This will change to value return" ></TextBlock>
        </StackPanel>
    </Grid>
</Page>
Raspberry Pi Code
Open .sln file and dump it to Raspberry

Schematics

Circiut Diagram
This circuit helps to connect components.
ArduinoComm.fzz

Comments

Similar projects you might like

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 297,272 views
  • 98 comments
  • 706 respects

Arduino I2C communication with Raspi 2 WIOT

Project tutorial by Christiano Faig

  • 35,435 views
  • 25 comments
  • 112 respects

Bridging the Raspberry Pi and the Arduino

by Daniel Porrey

  • 12,400 views
  • 10 comments
  • 38 respects

Home Automation system using Raspberry Pi

Project tutorial by Christian Kratky

  • 127,597 views
  • 34 comments
  • 404 respects

DHT Tiny Breakout for the Raspberry Pi

Project tutorial by Daniel Porrey

  • 6,002 views
  • 5 comments
  • 15 respects

Chromotherapy with Raspberry and Arduino

Project tutorial by Davide Longo

  • 6,070 views
  • 0 comments
  • 40 respects
Add projectSign up / Login