Adam
Created September 18, 2019 © GPL3+

Raspberry Pi controlling Arduinos with I2C Connections

Using a Raspberry Pi to remotely control a system of Arduinos that are used to operate sets of relay controlled motors.

IntermediateFull instructions provided6 hours89
Raspberry Pi controlling Arduinos with I2C Connections

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
Any model Raspberry Pi should work for this project.
×1
Arduino UNO
Arduino UNO
×2
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Hook Up Wire Kit, 22 AWG
Hook Up Wire Kit, 22 AWG
×1
SparkFun Solder-able Breadboard - Mini
SparkFun Solder-able Breadboard - Mini
Any solder-able developmental board should work for this project.
×2
KUP-11D15-5 Relay
×4
Transistor
×4

Software apps and online services

Raspbian
Raspberry Pi Raspbian
Along with a Python IDE
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Solder Flux, Soldering
Solder Flux, Soldering

Story

Read more

Code

Arduino 1 Test

Python
My testing script for arduino 1.
# Arduino 1 script

from smbus import SMBus
import time

addr = 0x5
bus = SMBus(1)

# Time delay (Seconds)
timeup = float(5.0)
timedown = float(5.0)
timedelay = float(1.0)

# Main function
def main():
    # Tests the relays to make sure they are connected properly
    bus.write_byte(addr, 0x0)

    bus.write_byte(addr, 0x1)
    time.sleep(timedown)
    bus.write_byte(addr, 0x0)
    time.sleep(timedelay)
    bus.write_byte(addr, 0x2)
    time.sleep(timeup)
    bus.write_byte(addr, 0x0)
    time.sleep(timedelay)
        
def up():
    timeup = float(1.0)

    bus.write_byte(addr, 0x2)
    time.sleep(timeup)
    bus.write_byte(addr, 0x0)
    
def down():
    timedown = float(1.0)

    bus.write_byte(addr, 0x1)
    time.sleep(timedown)
    bus.write_byte(addr, 0x0)

Arduino 2 Test

Python
My testing script for arduino 2.
# Arduino 2 script

from smbus import SMBus
import time

addr2 = 0x6
bus = SMBus(1)

# Time delay (Seconds)
timeup = float(5.0)
timedown = float(5.0)
timedelay = float(1.0)

# Main function
def main():
    # Tests the relays to make sure they are connected properly
    bus.write_byte(addr2, 0x0)

    bus.write_byte(addr2, 0x2)
    time.sleep(timeup)
    bus.write_byte(addr2, 0x0)
    time.sleep(timedelay)
    bus.write_byte(addr2, 0x1)
    time.sleep(timedown)
    bus.write_byte(addr2, 0x0)
    time.sleep(timedelay)

def up():
    timeup = float(1.0)

    bus.write_byte(addr2, 0x2)
    time.sleep(timeup)
    bus.write_byte(addr2, 0x0)
    
def down():
    timedown = float(1.0)

    bus.write_byte(addr2, 0x1)
    time.sleep(timedown)
    bus.write_byte(addr2, 0x0)

Arduino Code

Arduino
// Wire slave receiver for relay control

#include <Wire.h>

#define forward 10
#define backward 11

void setup() {
  Wire.begin(0x5); // join i2c bus with address #5
  // Wire.begin(0x6); // join i2c bus with address #6 (2nd arduino)
  // Wire.begin(0x7); // join i2c bus with address #7 (3rd arduino)
  // Wire.begin(0xN); // join i2c bus with address #8 (Nth arduino)
  Wire.onReceive(receiveEvent); // register event
  pinMode(forward, OUTPUT);
  pinMode(backward, OUTPUT);
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
void receiveEvent(int howMany) {
  while (Wire.available()) { // if wire connection is available
    char c = Wire.read(); // receive byte as a character
    
    if (c == 1 && c != 2){
      digitalWrite(forward, c); // sets the forward relay to close when input is 1
    }
    else if (c == 2 && c != 1){
      digitalWrite(backward, c); // sets the backward relay to close when input is 2
    }
    else{
      digitalWrite(forward, c); // resets both relays to remain open when input is 0
      digitalWrite(backward, c);
    }
  }
}

Credits

Adam
7 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.