Hi friends!
I have a question. I am trying to send a data float between an Arduino nano 33 BLE sense and Arduino nano 33 IoT. I don't know why but only is possible to send data in one way. In other words, I can send perfectly, for example, the float 27.9. But when I receive for example the value 200 I receive strange numbers. However, between two Arduino nano 33 IoT I don't have any problems.
Also, I have connected the GND ground.
I2C only can work between the same Arduino model?
Thanks you!!
There is no reason why it will not work between different Arduinos. Can you please post your code for each Arduinos. And to make sure, please between [code] and [/code]
//Edit
Changed the closing tag
Thanks you for your fast answer.
Sender:
[code] #include "Wire.h"
const byte I2C_SLAVE_ADDR = 0x20;
double data = 25.9;
long response = 0;
void setup()
{
Serial.begin(115200);
Wire.begin();
}
void loop()
{
sendToSlave();
requestToSlave();
delay(2000);
}
void sendToSlave()
{
Wire.beginTransmission(I2C_SLAVE_ADDR);
Wire.write((byte*)&data, sizeof(data));
Wire.endTransmission();
}
void requestToSlave()
{
response = 0;
Wire.requestFrom(I2C_SLAVE_ADDR, sizeof(response));
uint8_t index = 0;
byte* pointer = (byte*)&response;
while (Wire.available())
{
*(pointer + index) = (byte)Wire.read();
index++;
}
Serial.println(response);
} [\code]
Receiver:
[code]#include "Wire.h"
const byte I2C_SLAVE_ADDR = 0x20;
void setup()
{
Serial.begin(115200);
Wire.begin(I2C_SLAVE_ADDR);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
}
double data = 0;
long response = 200;
void receiveEvent(int bytes)
{
data = 0;
uint8_t index = 0;
while (Wire.available())
{
byte* pointer = (byte*)&data;
*(pointer + index) = (byte)Wire.read();
index++;
}
}
void requestEvent()
{
Wire.write((byte*)&response, sizeof(response));
}
void loop() {
if (data != 0)
{
Serial.println(data);
data = 0;
}
} [\code]
I've updated the instructions; there was a mistake in the closing tag.
Sender code
#include "Wire.h"
const byte I2C_SLAVE_ADDR = 0x20;
double data = 25.9;
long response = 0;
void setup()
{
Serial.begin(115200);
Wire.begin();
}
void loop()
{
sendToSlave();
requestToSlave();
delay(2000);
}
void sendToSlave()
{
Wire.beginTransmission(I2C_SLAVE_ADDR);
Wire.write((byte*)&data, sizeof(data));
Wire.endTransmission();
}
void requestToSlave()
{
response = 0;
Wire.requestFrom(I2C_SLAVE_ADDR, sizeof(response));
uint8_t index = 0;
byte* pointer = (byte*)&response;
while (Wire.available())
{
*(pointer + index) = (byte)Wire.read();
index++;
}
Serial.println(response);
}
And the receiver code
#include "Wire.h"
const byte I2C_SLAVE_ADDR = 0x20;
void setup()
{
Serial.begin(115200);
Wire.begin(I2C_SLAVE_ADDR);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
}
double data = 0;
long response = 200;
void receiveEvent(int bytes)
{
data = 0;
uint8_t index = 0;
while (Wire.available())
{
byte* pointer = (byte*)&data;
*(pointer + index) = (byte)Wire.read();
index++;
}
}
void requestEvent()
{
Wire.write((byte*)&response, sizeof(response));
}
void loop() {
if (data != 0)
{
Serial.println(data);
data = 0;
}
}
Thank you! I don't know if is the code. But I have problems with the communication between different Arduino models. Also, I have tried between Arduino Mega and UNO. Always with the same voltage.
Hi Friends,
I have changed the last code and I have tried the code that Arduino gives in its webpage: https://www.arduino.cc/en/Tutorial/LibraryExamples/MasterReader. I noted that the communication between different Arduinos models, not work well:
Sender/Slave:
[code] #include <Wire.h>
void setup() {
Wire.begin(0x60); // join i2c bus with address
Wire.setClock(100000);
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
} [\code]
Receiver code/reader:
[code]#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Wire.setClock(100000);
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(0x60, 6); // request 6 bytes from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
I am receiving incorrectly the word "hello" --> @o hello @o hello @o hello @o hello @o hello @o hello .... ¿some idea?
Thanks!!
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.