Hello everyone, i am a student doing this Project where i have to use I2C Protocoll to use my SHT31 Sensor. Now the Prototype Shield that i have for my ATMega 2560 does not have any SDA and SCL Pins configured on it, so i researched and found out that a library like Softwire could do the job to set the SDA and SCL Pins on any other Pins(rather than 20 and 21) as i would like to.
So i decided to work first with the Arduino's Wire Library and do the same thing with my Softwire Library, as both the libraries have similar Functions. While working with the Wire Library and default SDA and SCL Pins it works perfectly but when i change the Pins and use the SoftWire Library, it doesn't.
Here is the Code with my Wire library after i got helped from a YT Video:
Can you show a photo with the Arduino board, the prototype shield, the sensor module and the wiring ?
Can you give a link to your prototype shield ? Is it a shield for an Arduino Uno without the SDA and SCL pins near the AREF pin ?
Instead of trying to communicate with the sensor, it is easier to run a I2C Scanner.
Can you try this:
// Forum: https://forum.arduino.cc/t/i2c-communication-with-softwire-library/1237721
// This Wokwi project: https://wokwi.com/projects/392870739462268929
//
// Question: Does the SoftWire library work on a Arduino Mega ?
// Answer: Yes, I think it does.
#include <SoftWire.h>
// PINS for Custom SDA ans SCL
#define SDA_PIN 2
#define SCL_PIN 3
// Instantiate SoftWire with custom pins
SoftWire mywire = SoftWire(SDA_PIN, SCL_PIN);
void setup()
{
Serial.begin(9600);
mywire.begin();
Serial.print("Scan: ");
for(int address=1; address<=127; address++)
{
mywire.beginTransmission(address);
int error = mywire.endTransmission();
if(error==0)
{
Serial.print("0x");
Serial.print(address,HEX);
Serial.print(" ");
}
}
Serial.println(" --> Finished");
}
void loop()
{
delay(10);
}
You could copy the declaration and initialization of the buffers to your code and give it a try... (of course change the name from "sw." to "mywire." ... )
Hi @Koepel, these are the photos of my Shield and the wiring i made. I just wanted to check first with the connections i made in Breadboard before i use the Shield. The Shield is custom made and provided to me by my Professor and told me that he doesnt have any Documentation . So here it is.
I also tested the code you provided for the i2c scanner. It worked.
Hello @ec2021, yes that is the library i am using. Sorry i didnt post the link. Those Rx and Tx are responsible for the receiving and transmitting data? I will give it a try. Thanks.
OMG, @ec2021 Thanks mate, it worked. Although i dont get it what are those chars for Rx and Tx. What do they do ? Could you please explain it to me? Thank you very much, appreciate it.
In my post #4, I have connected UNO as Slave just to check the functionality of the soft I2C bus. You will go with your sensors being connected as Slaves.
If you "dive" a little bit into the library you'll find this
// The Wire compatibility functions require RX and TX buffers. The same address space may be used for both
// as long as the user does not call receiveFrom between startTransmission and endTransmission.
inline void setRxBuffer(void *rxBuffer, uint8_t rxBufferSize) {
_rxBuffer = (uint8_t*)rxBuffer;
_rxBufferSize = rxBufferSize;
_rxBufferIndex = 0;
_rxBufferBytesRead = 0;
}
It are just buffers for reception/transmission due to Wire compatibility.
As the comment says the same buffer would be sufficient for both purposes if the function receiveFrom() is not called between startTransmission() and endTransmission(). That might save some valuable memory space if required.