Problem about I2C Communication between Raspberry pi and Arudino

Dear All.

Below codes are sample code for I2C communication.

Raspberry pi is master and Arduino is slave.

The sample codes are echo cases. If I put # 3, arduino should send me back same number.

However, it always sends me '1'.

I do not know why..

Arudino Code

#include <Wire.h>

#define SLAVE_ADDRESS 0x04
int number = 0;
int state = 0;

void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600); // start serial for output
// initialize i2c as slave

// define callbacks for i2c communication


void loop() {

// callback for received data
void receiveData(int byteCount){

while(Wire.available()) {
number =;
Serial.print(“data received: “);
// callback for sending data
void sendData(){

Raspberry pi Code(Python)
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(“Enter 1 – 9: “)
if not var:

print “RPI: Hi Arduino, I sent you “, var

sleep one second


number = readNumber()
print “Arduino: Hey RPI, I received a digit “, number

I am no expert but I think that Number and number are two different integers, I have no doubt that if I have misread this then someone else will put me right.
The only thing I do not understand is why the result is not "0".

Perhaps you need to include the RPI details as well, maybe the mistake is there?