2

I have connected a Raspberry Pi 2 model B to an Arduino Uno via Bi-Directional Level shifter, from this BLOG:

Raspberry pi    GND    ----------   GND     Arduino
                3.3v   ----------   5v
                SCL    ----------   A5
                SDA    ----------   A4

I hope my I2C connection is correct.

And my Arduino is connected to an 8-Channel relay board.

Now I have written code in which I can control the relay board with the Raspberry Pi. For example, if I Press '1', relay 1 goes high.

Now I want to send data back from the Arduino to the Raspberry Pi in order to cross-check if relay 1 is high or not. If relay 1 is high then it should send some data back to the Raspberry Pi.

My Raspberry Pi code is:

import smbus
import time
# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)

# This is the address we setup in the Arduino Program
address = 0x04

def writeNumber(value):
    bus.write_byte(address, value)
    # bus.write_byte_data(address, 0, value)
    return -1

def readNumber():
    number = bus.read_byte(address)
    # number = bus.read_byte_data(address, 1)
    return number

while True:
    var = input("")
    if not var:
        continue
    writeNumber(var)
    number = readNumber()

My Arduino code:

#include <Wire.h>

#define SLAVE_ADDRESS 0x04
#define RELAY1 9

int number = 0;
int state = 0;

void setup() {
    pinMode(RELAY1, OUTPUT);
    Serial.begin(9600); // start serial for output
    // initialize i2c as slave
    Wire.begin(SLAVE_ADDRESS);
    // define callbacks for i2c communication
    Wire.onReceive(receiveData);
    Wire.onRequest(sendData);
    Serial.println("Ready!");
}

void loop() {
    delay(100);
}

// callback for received data
void receiveData(int byteCount){
    while(Wire.available()) {
       number = Wire.read();
       Serial.print("data received: ");
       Serial.println(number);
        if (number == 1) {
            if (state == 0){
                digitalWrite(RELAY1, HIGH); // set the LED on
                state = 1;
            } else {
                digitalWrite(RELAY1, LOW); // set the LED off
                state = 0;
            }
        }
    }
}

// callback for sending data
void sendData(){
    Wire.write(number);
}

Now if I type 1, and due to some loose connection relay 1 doesn't go high, I want the Arduino to take data from the relay board and send it to the Raspberry Pi every time.

It will be great if someone can explain also that how it works.

Hope I was able to explain the problem. I have done lots of research but was not able to find some answer.

I am a beginner in Python so please help me.

Thanks in advance.

  • 4
    Character '1' is not number 1. Try "number == '1'. – Mikael Patel Mar 9 '16 at 11:34
  • That is not the question, The question is how to pass the value from digital pin of Arduino to rapsberry pi. – shivam Mar 10 '16 at 6:12
  • 2
    See my answer here about connecting I2C between a 3.3V device and a 5V device. – Nick Gammon Mar 10 '16 at 20:52
1

You might want to graduate to the pigpio library for the Pi side of this.

Here is where you can find examples of how it works.

here is a fun one that is like Wireshark - will show you the I2C traffic:

https://github.com/joan2937/pigpio/tree/master/EXAMPLES/C/I2C_SNIFFER

Here is the FAQ about I2C

0

Character '1' is not number 1. Try "number == '1'. – Mikael Patel Mar 9 '16 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.