Hello karabrs,
I don't know the sensor, but it seems to me that you are just trying to connect an I2C device to your nodemcu. This is done quite simple and you can use any pin you want for SDA and SCL (GPIO 0, 2, 4, 5, 12, 13, 14, 15, 16 - I wouldn't recommend to use GPIO 1, 3 because they are TxD and RxD for the Serial).
Here some example code to initialize I2C:
Code: Select all#include <Wire.h>
#define SCL_pin 0 // SCL at IO0 as example
#define SDA_pin 2 // SDA at IO2 as example
#define I2C_ADDR 0 // <- The address of your device goes here (see datasheet of the sensor).
void setup() {
delay (1000);
Wire.begin (SDA_pin, SCL_pin); // Init I²C
Wire.setClock (100000); // 100kHz (max 400kHz)
}
void writeSomeStuff() {
Wire.beginTransmission (I2C_ADDR); // Start writing
Wire.write (0); // Write whatever you need to write
Wire.write (0); // (Register addresses, data and stuff)
//...
Wire.endTransmission ();
}
byte readSomeStuff() {
Wire.beginTransmission (I2C_ADDR); // Start writing
Wire.write (0); // Write what you need to write to prepare the device to return data
Wire.endTransmission ();
Wire.requestFrom (I2C_ADDR, 1); // Start reading one byte
if (Wire.available () == 1)
return Wire.read (); // Read one byte
return 0; // something didn't work
}
Please don't forget the terminating resistors for SDA and SCL!
Please consider that the I2C address is not exactly like the most datasheets describe it. You need to totally ignore the "/WR" (LSB) bit and use the upper 7 bits of the datasheet as the lower 7 bits in your code.
The endTransmission also returns a value to show if the transmission has worked. Anything except 0 is bad - you might want to check that as well.