DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. IoT
  4. Arduino and Raspberry Pi Working Together (Part 2): Now With I2C

Arduino and Raspberry Pi Working Together (Part 2): Now With I2C

While you can use a USB cable to connect communications between an Arduino and a Raspberry Pi, I2C is an easier-to-use solution for the boards.

Gonzalo Ayuso user avatar by
Gonzalo Ayuso
·
May. 23, 17 · Tutorial
Like (3)
Save
Tweet
Share
23.68K Views

Join the DZone community and get the full member experience.

Join For Free

The easiest way to connect our Arduino board to our Raspberry Pi is using a USB cable, but sometimes this communication is a nightmare, especially because there isn’t any clock signal to synchronize our devices and we must rely on the bitrate. There are different ways to connect our Arduino and our Raspberry Pi, such as I2C, SPI, and serial over GPIO. Today, we’re going to speak about I2C, especially because it’s pretty straightforward if we take care of a couple of things. Let’s start.

I2C uses two lines — SDA (data) and SCL (clock) — in addition to GND (ground). SDA is bidirectional, so we need to ensure one way or another who is sending data (master or slave). With I2C, only the master can start communications. The master also controls the clock signal. Each device has a 7-bit direction, so we can connect 128 devices to the same bus.

If we want to connect an Arduino board and a Raspberry Pi we must ensure that the Raspberry Pi is the master. That’s because the Arduino works with 5V while the Raspberry Pi works with 3.3V. That means that we need to use pull-up resistors if we don’t want to destroy our Raspberry Pi. But the Raspberry Pi has 1k8 ohms resistors to the 3.3-volt power rail, so we can connect both devices (if we connect other I2C devices to the bus, they must have their pull-up resistors removed)

That's all we need to connect our Raspberry Pi to our Arduino board.

  • RPi SDA to Arduino analog 4
  • RPi SCL to Arduino analog 5
  • RPi GND to Arduino GND

Now we are going to build a simple prototype. The Raspberry Pi will blink one LED (GPIO17) each second while also sending a message (via I2C) to the Arduino to blink another LED. That’s the Python part:

import RPi.GPIO as gpio
import smbus
import time
import sys

bus = smbus.SMBus(1)
address = 0x04

def main():
    gpio.setmode(gpio.BCM)
    gpio.setup(17, gpio.OUT)
    status = False
    while 1:
        gpio.output(17, status)
        status = not status
        bus.write_byte(address, 1 if status else 0)
        print "Arduino answer to RPI:", bus.read_byte(address)
        time.sleep(1)


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print 'Interrupted'
        gpio.cleanup()
        sys.exit(0)


And finally, the Arduino program. Arduino also answers to the Raspberry Pi with the value that it has been sent, and the Raspberry Pi will log the answer within the console.

#include <Wire.h>

#define SLAVE_ADDRESS 0x04
#define LED  13

int number = 0;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Wire.begin(SLAVE_ADDRESS);
  Wire.onReceive(receiveData);
  Wire.onRequest(sendData);

  Serial.println("Ready!");
}

void loop() {
  delay(100);
}

void receiveData(int byteCount) {
  Serial.print("receiveData");
  while (Wire.available()) {
    number = Wire.read();
    Serial.print("data received: ");
    Serial.println(number);

    if (number == 1) {
      Serial.println(" LED ON");
      digitalWrite(LED, HIGH);
    } else {
      Serial.println(" LED OFF");
      digitalWrite(LED, LOW);
    }
  }
}

void sendData() {
  Wire.write(number);
}


Hardware:

  • Arduino UNO
  • Raspberry Pi
  • Two LEDs and two resistors


Code is available on my GitHub.

raspberry pi arduino

Published at DZone with permission of Gonzalo Ayuso, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Beginner’s Guide To Styling CSS Forms
  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything
  • 19 Most Common OpenSSL Commands for 2023
  • Implementing PEG in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: