Chat freely about anything...

User avatar
By lampshade
#20136 Hey,

so i just wanted to initialize the i2c-Bus and write data to a known address.
In lua it was quite simple:

Code: Select allid=0
sda=3 --GPIO0
scl=4 --GPIO2
i2c.setup(id, sda, scl, i2c.SLOW)
i2c.start(id)
i2c.address(id, "address" ,i2c.TRANSMITTER)
i2c.write(id,"something")
i2c.stop(id)


I wanted to write a c-code to do the same, but i don't know how to address the slave correctly. I tried it several times with this basic code and some minor changes, so hopefully someone could help me out.

Code: Select alluser_init()
{
    i2c_master_gpio_init();
    i2c_master_init();
    i2c_master_start();
    ack=i2c_master_getAck();
    i2c_master_stop;
    i2c_master_start();
    i2c_master_writeByte("something");
    i2c_master_stop();
return;}


in i2c_master.h i uncomment this, because i wanted to use GPIO0 and GPIO2
Code: Select all#define I2C_MASTER_SDA_MUX PERIPHS_IO_MUX_GPIO2_U
#define I2C_MASTER_SCL_MUX PERIPHS_IO_MUX_GPIO0_U
#define I2C_MASTER_SDA_GPIO 2
#define I2C_MASTER_SCL_GPIO 0
#define I2C_MASTER_SDA_FUNC FUNC_GPIO2
#define I2C_MASTER_SCL_FUNC FUNC_GPIO0


and commented the define section before with GPIO14 and GPIO2.
Grateful for every piece of help ;)
User avatar
By tytower
#20200 Have a look in the wiki above(grey menu bar above on this page) under pin allocations and the readme on github ESP8266/Arduino . The SDA and SCL pins have defaults on pins 4 and 5 I think . It mentions a way to change them to alternate pins but I have not been able to as yet.

I'm sure you probably know all that above but just in case you don't.
Wire library currently supports master mode up to approximately 450KHz. Before using I2C, pins for SDA and SCL need to be set by calling Wire.begin(int sda, int scl), i.e. Wire.begin(0, 2); on ESP-01, else they default to pins 4(SDA) and 5(SCL).


I don't think you need to make any changes to the library if you are using the latest one