<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Raspberry pi with Arduino using i2c - Raspberry Pi				            </title>
            <link>https://forum.dronebotworkshop.com/raspberry-pi/raspberry-pi-with-arduino-using-i2c/</link>
            <description>Discussion board for Robotics, Arduino, Raspberry Pi and other DIY electronics and modules. Join us today!</description>
            <language>en-US</language>
            <lastBuildDate>Thu, 12 Nov 2026 19:50:39 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>RE: Raspberry pi with Arduino using i2c</title>
                        <link>https://forum.dronebotworkshop.com/raspberry-pi/raspberry-pi-with-arduino-using-i2c/#post-29141</link>
                        <pubDate>Thu, 05 May 2022 11:20:26 +0000</pubDate>
                        <description><![CDATA[Posted by: @mohamedmym 
Hi,
I&#039;m trying to connect raspberry pi with Arduino using I2C communication protocol, But I&#039;m have a problem.
when I&#039;m trying to write data to Arduino (as it&#039;s the...]]></description>
                        <content:encoded><![CDATA[<blockquote data-userid="4716" data-postid="29009" data-mention="mohamedmym">
<div class="wpforo-post-quote-author"><strong> Posted by: @mohamedmym </strong></div>
<p>Hi,</p>
<p>I'm trying to connect raspberry pi with Arduino using I2C communication protocol, But I'm have a problem.</p>
<p>when I'm trying to write data to Arduino (as it's the slave device) from the pi (the master device),</p>
<p>the data arrives perfectly.</p>
<p>but when I'm trying to read data from the Arduino I always get 0.</p>
<p>here's my code plz some one help me.</p>
<p>//Arduino Code:</p>
<pre contenteditable="false">#include &lt;Wire.h&gt;
int dataSize;
char data;
char response;
char Buffer;
String answer = "Hi from Arduino";

void setup() {
    pinMode(13, OUTPUT);
    digitalWrite(13, 0);
    Serial.begin(9600);
    Wire.begin(0x8);

    Wire.onReceive(receiveEvent);
    Wire.onRequest(requestEvent);
    delay(500);
    Serial.println("Welcome To I2c test.");
    for (byte i = 0; i &lt; 32; i++) {
        data = 0x00;
    }
    for (byte i = 0; i &lt; 32; i++) {
        response = 0x00;
    }

}

void loop() {
    if (Serial.available()) {
        answer = Serial.readString();
    }
    delay(10);
}

int counter = 0;
char x;

void receiveEvent(int howMany) {
    dataSize = Wire.read();

 

    while (Wire.available()) {
        x = Wire.read();
        data = x;
        counter++;
    }

    Serial.print("Data Size: ");
    Serial.println(dataSize);
    Serial.print("Data Received: ");
    Serial.println(data);
    Serial.println(data);
    counter = 0;

    for (int i = 0; i &lt; 30; i++)
        data = 0;
    }

    void requestEvent(){

    char response;

    if (sizeof(answer) &lt; 32) {
        for (byte i = 0; i &lt; 32; i++) {
            if (answer.charAt(i) != 0)
                response = answer.charAt(i);
            else
                response = 0xff;
        }
    }    

    dataSize = Wire.read();
    Wire.write(response);
    sprintf(Buffer, "Data: \n"
, response, response, response, response, response, response, response, response
, response, response, response, response, response, response, response, response
, response, response, response, response, response, response, response, response
, response, response, response, response, response, response, response, response);

    Serial.println("Request Event");
    Serial .println(Buffer);

    for (byte i = 0; i &lt; 32; i++) {
        response = 0xff;
    }
    for (byte i = 0; i &lt; 32; i++) {
        data = 0xff;
    }

}</pre>
<p> </p>
<p>/***************************************************/</p>
<p>#Raspberry Pi Code (Python):</p>
<pre contenteditable="false">from smbus import SMBus
import time
name = "Welcome to my test"

data = []
bus = SMBus(1)

time.sleep(0.5)
data1 = 48
for i in name:
    data.append(i)

for i in range(len(data)):
    try:
        data  = ord(data)
    except:
        data = 0

time.sleep(0.1)
print (data)

print(len(data), "Bytes")
bus.write_byte_data(0x8,len(data),data1+2)
print ("data sent")
time.sleep(1)

rec = bus.read_i2c_block_data(0x8,32,32);
print(rec)</pre>
<p>##################################</p>
<p>I've tried the read_i2c_block_data () function, and the read_byte_data() function but still the same.</p>
<p> </p>
</blockquote>
<p> </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/raspberry-pi/">Raspberry Pi</category>                        <dc:creator>MohamedMYM</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/raspberry-pi/raspberry-pi-with-arduino-using-i2c/#post-29141</guid>
                    </item>
				                    <item>
                        <title>RE: Raspberry pi with Arduino using i2c</title>
                        <link>https://forum.dronebotworkshop.com/raspberry-pi/raspberry-pi-with-arduino-using-i2c/#post-29020</link>
                        <pubDate>Wed, 04 May 2022 14:13:47 +0000</pubDate>
                        <description><![CDATA[Please read the help/faq on how to post code, and before posting use Auto Format. Thank you.]]></description>
                        <content:encoded><![CDATA[<p>Please read the help/faq on how to post code, and before posting use Auto Format. Thank you.</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/raspberry-pi/">Raspberry Pi</category>                        <dc:creator>Ron</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/raspberry-pi/raspberry-pi-with-arduino-using-i2c/#post-29020</guid>
                    </item>
				                    <item>
                        <title>Raspberry pi with Arduino using i2c</title>
                        <link>https://forum.dronebotworkshop.com/raspberry-pi/raspberry-pi-with-arduino-using-i2c/#post-29009</link>
                        <pubDate>Wed, 04 May 2022 07:06:06 +0000</pubDate>
                        <description><![CDATA[Hi,
I&#039;m trying to connect raspberry pi with Arduino using I2C communication protocol, But I&#039;m have a problem.
when I&#039;m trying to write data to Arduino (as it&#039;s the slave device) from the p...]]></description>
                        <content:encoded><![CDATA[<p>Hi,</p>
<p>I'm trying to connect raspberry pi with Arduino using I2C communication protocol, But I'm have a problem.</p>
<p>when I'm trying to write data to Arduino (as it's the slave device) from the pi (the master device),</p>
<p>the data arrives perfectly.</p>
<p>but when I'm trying to read data from the Arduino I always get 0.</p>
<p>here's my code plz some one help me.</p>
<p>//Arduino Code:</p>
<p>#include &lt;Wire.h&gt;<br />int dataSize;<br />char data;<br />char response;<br />char Buffer;<br />String answer = "Hi from Arduino";</p>
<p>void setup() {<br />    pinMode(13, OUTPUT);<br />    digitalWrite(13, 0);<br />    Serial.begin(9600);<br />    Wire.begin(0x8);</p>
<p>    Wire.onReceive(receiveEvent);<br />    Wire.onRequest(requestEvent);<br />    delay(500);<br />    Serial.println("Welcome To I2c test.");<br />    for (byte i = 0; i &lt; 32; i++) {<br />        data = 0x00;<br />    }<br />    for (byte i = 0; i &lt; 32; i++) {<br />        response = 0x00;<br />    }</p>
<p>}</p>
<p>void loop() {<br />    if (Serial.available()) {<br />        answer = Serial.readString();<br />    }<br />    delay(10);<br />}</p>
<p>int counter = 0;<br />char x;</p>
<p>void receiveEvent(int howMany) {<br />    dataSize = Wire.read();</p>
<p> </p>
<p>    while (Wire.available()) {<br />        x = Wire.read();<br />        data = x;<br />        counter++;<br />    }</p>
<p>    Serial.print("Data Size: ");<br />    Serial.println(dataSize);<br />    Serial.print("Data Received: ");<br />    Serial.println(data);<br />    Serial.println(data);<br />    counter = 0;</p>
<p><br />    for (int i = 0; i &lt; 30; i++)<br />        data = 0;<br />    }</p>
<p>    void requestEvent(){</p>
<p><br />    char response;</p>
<p>    if (sizeof(answer) &lt; 32) {<br />        for (byte i = 0; i &lt; 32; i++) {<br />            if (answer.charAt(i) != 0)<br />                response = answer.charAt(i);<br />            else<br />                response = 0xff;<br />        }<br />    }    </p>
<p>    dataSize = Wire.read();<br />    Wire.write(response);<br />    sprintf(Buffer, "Data: \n"<br />, response, response, response, response, response, response, response, response<br />, response, response, response, response, response, response, response, response<br />, response, response, response, response, response, response, response, response<br />, response, response, response, response, response, response, response, response);</p>
<p>    Serial.println("Request Event");<br />    Serial .println(Buffer);</p>
<p>    for (byte i = 0; i &lt; 32; i++) {<br />        response = 0xff;<br />    }<br />    for (byte i = 0; i &lt; 32; i++) {<br />        data = 0xff;<br />    }</p>
<p>}</p>
<p> </p>
<p>/***************************************************/</p>
<p>#Raspberry Pi Code (Python):</p>
<p>from smbus import SMBus<br />import time<br />name = "Welcome to my test"</p>
<p>data = []<br />bus = SMBus(1)</p>
<p>time.sleep(0.5)<br />data1 = 48<br />for i in name:<br />    data.append(i)</p>
<p>for i in range(len(data)):<br />    try:<br />        data  = ord(data)<br />    except:<br />        data = 0<br /><br />time.sleep(0.1)<br />#print (data)</p>
<p>#print(len(data), "Bytes")<br />#bus.write_byte_data(0x8,len(data),data1+2)<br />#print ("data sent")<br />time.sleep(1)<br />#received = bus.read_i2c_block_data(0x8,20,20)<br />#print(received)e<br />rec = bus.read_i2c_block_data(0x8,32,32);<br />print(rec)</p>
<p>##################################</p>
<p>I've tried the read_i2c_block_data () function, and the read_byte_data() function but still the same.</p>
<p> </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/raspberry-pi/">Raspberry Pi</category>                        <dc:creator>MohamedMYM</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/raspberry-pi/raspberry-pi-with-arduino-using-i2c/#post-29009</guid>
                    </item>
							        </channel>
        </rss>
		