Sending data using wire.h

Hello, i need somehelp with i2C, i'm using wire.h with uno r4 and it keeps giving me error 5 for timeout on endtransmission(). i think its because it's not sending start and stop right.
i tried many things but have the same problem. iput in comment of code what type of data the sensor needs ;

 // send START :
  //    1st BYTE [7 bit adress + 1 bit LSB] +ACK
  //         0x02=read mode and 0x03=Write mode
  // Send MSGTYPE : 0X4D for configuration of Sensor +ACK
  //                0x56 for asking the sensor to send 4Bytes of data
  // send STOP
  // send START :
  // if 0x4D was sent, send 1 byte.
  // if 0x56 was sent, send 4bytes : [byte]+ACK;[byte]+ACK;[byte]+ACK;[byte]+NACK
  //SEND STOP
#include "wire.h"

#define WIRE Wire

void setup()
{
  Serial.begin(115200);
  WIRE.begin();
  Serial.println("\nI2C Scanner");
}

void loop()
{
  // send START :
  //    1st BYTE [7 bit adress + 1 bit LSB] +ACK
  //         0x02=read mode and 0x03=Write mode
  // Send MSGTYPE : 0X4D for configuration of Sensor +ACK
  //                0x56 for asking the sensor to send 4Bytes of data
  // send STOP
  // send START :
  // if 0x4D was sent, send 1 byte.
  // if 0x56 was sent, send 4bytes : [byte]+ACK;[byte]+ACK;[byte]+ACK;[byte]+NACK
  //SEND STOP
  byte erreur, address;
  erreur = 101;

  Serial.println(".......");
  address = 0x01;
  // address = address & 0xFE;
  Serial.print("\tl'adress est : ");
  Serial.print(address);
  WIRE.beginTransmission(address);
  WIRE.write(0X56);
  erreur = WIRE.endTransmission();
  uint32_t DATA= WIRE.read();
  Serial.print("    DATA : ");
  Serial.println(DATA,HEX);

  // erreur = WIRE.endTransmission();
  Serial.print("\tl'erreur est : ");
  Serial.println(erreur);
}

and here's the type of signal that i will need for it to work when asking for data to the sensor. THEORICALLY

and here's what i'm getting :
image

What is your I2C Slave -- is it another Arduino or a Sensor? If senor, what type of sensor it is?

it's a pcb that's using different sensors and there is a microcontroller for i2C on it, all of it works fine with a another circuit that's meant to works as master.

After Wire.begin() the SDA and SCL should be near VCC (near 5V on a Arduino Uno).

Are you using the right pins ? Do you need pullup resistors ? Can you show a schematic ?
Please answer our questions (what type of sensor is it) and please give us a lot more information.

A I2C Scanner sketch can find the I2C devices.
Don't do a Wire.read() on its own, that is not allowed. See my alternative explanation of the functions of the Wire library.

Please do this step-by-step. If the SDA and SCL pin are near zero, then you have no I2C bus. Fix that first. If that is fixed, then run a I2C Scanner sketch.

So I'm using arduino uno 4 wifi and on the scl and sda pins(female header) it's not high but on jst connecter it's high on those pins. Its a program just with wire.begin in setup. What should i do?

I did this on platformio, i can set those puns high with digital write so pins are ok. Ill try to put pullup resistance to see what it gives or maybe try with arduino ide

Arduino UNO R4 WiFi has two I2C buses:

  • The JST header is the Qwiic connector, that is a 3.3V I2C bus.
  • The normal pins near the reset button are another I2C bus.

Can you read this: https://docs.arduino.cc/tutorials/uno-r4-wifi/cheat-sheet/
because I'm confused. I don't understand which is "Wire" and which is "Wire1".

I think that i tried both wire and wire1 but I'm not sure, I'll try it in like an hour.

The pins SDA and SCL near the reset button might not have a pullup resistor.
Some boards need to run initialization code for the hardware on the board. I'm not sure that PlatformIO does that.

so it was the pull up resistances on the SCL and SDA pins of arduino uno R4 wifi. turns out it doesn't have builtin pullup like in uno R3. now i want to reduce the frequency of i2C do you know how to do it. And if i use uno R4 wifi as slave can it run at frequency lower then 100kHz? cause i need to run it on 50kHz and another master will control the SCL.

You wanted to connect a unknown I2C Sensor to a Arduino UNO R4 WiFi board. But now you want to run a UNO R4 WiFi as a I2C Slave ?

What is going on ? Does the sensor work ? Do you want the Arduino UNO R4 WiFi to be in the middle of a unknown Master and a unknown Sensor and behave like the sensor ?

A I2C Slave device does not have a clock speed. The Master provides the clock signal.

Do you know if the Arduino UNO R4 WiFi can run as a I2C Slave ? If it can, do you if that is reliable ? Are there projects running with a UNO R4 WiFi as I2C Slave ?

here is the little diagram to show what i'm trying to do :
image

What is Master Arduino -- is it UNOR4WiFi?

What are your Slave Arduinos -- are they Arduino UNOR3s?

it's uno R4 wifi and there's no reason for it to not work on uno r3. and other devices arn't arduinos, but other microcontrollers

Step 1: Learn about the I2C bus.
Step 2: Buy a Logic Analyzer.

There are also projects to turn a Arduino or Raspberry Pi Pico into a Logic Analyzer or I2C Sniffer.

With the I2C bus, the Master communicates with the Slave. That communication is going in both directions. It is not possible to connect to that communication with another I2C Slave.
Maybe the UNO R4 WiFi does not work as a I2C Slave. Maybe the Master does not support I2C clock stretching. Maybe the voltage levels on the I2C do not match. You forgot to draw the GND wires in the picture.

What I am trying to say:
Sorry, but it is not possible what you want to do.
Suppose that your remove the Slave Board from the I2C bus and try to use the Arduino as a Slave to communicate with the Master board, that is probably also not possible.
Suppose that you remove the Master board and use the Arduino board as Master for the Slave board, that might be possible, but you give us so little information, that it is better to assume at this moment that it will not work.

What is it for ? Can you give us a broader view of the project ? This could be a XY-problem: https://xyproblem.info/
Is it legal what you are trying to do ?

i have done many projects using esp32 and arduino with them always in master mode and communicate with adafruit sensors but not something like this. i have the program of the other devices and its little complicated to understand cause its very long. i'm trying to understand the program so i can modify it to send the value from the master to the Arduino. but that program is hard to understand cause there are many fonctions that are being used and i can't understand that what fonctions are being called for what, some fonctions have simillar names and there arn't any comments too and not to mention it's more then 60 files and more then 10k lines of code. the arduino is just used to recive the data of slave board(slave board have different sensors) and show it on the screen, means that master can recive the data of slave board and then send it to Arduino. And it's totally legal.

Which processor is the Master ? What does it run ? Perhaps Mbed or FreeRTOS ? Does it use hardware I2C or software I2C ? What processor is on the Slave board ? Can you write code and compile it for the Slave board as well ? Can you show a photo of the project ?

That is about 1% of what we would like to know.
The Serial/UART bus has a baudrate, stopbits, voltage levels. That's about it. The I2C bus has many more things that can cause trouble.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.