Configuring i2c on the nodemcu.

Status
Not open for further replies.

CringeKid

Newbie
Joined
Apr 23, 2021
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
41
So i have 2 sensors connected to the same pins of the nodemcu connecting SDA and SCL of both to the i2c pins of the nodemcu and i need to retrieve data from these sensors. Is it possible to simultaneously retrieve the data from them or do i have to stop 1 and take from the other and vice versa?
If yes, how do i do it?
 

Hi,

since I2C is a (serial) bus you can´t do simultaneous communication on two salves.

They need to have different device addresses and need to be accessed one after the other. Straight forward.
(This is the main idea behind a (serial) bus.)

Klaus
 

Hey, thank you for the reply. Do you have any idea on how to do it or where i could learn?
 

Hi,

What do you mean?

I2C access ingeneral? Read documantation, tutorials, ... Many thousands of available informations in the internet.
Two device access? It´s the same as one device access....

Klaus
 

I'm running an LCD, real time clock, two ADCs and two 8-bit ports on I2C, fed from a nodeMCU right now and it all works fine. I'll be dropping the nodeMCU and using a bare ESP8266 in the final production. All you need to do is provide a different address for each I2C device. All the devices monitor the SDA and SCL lines continuously, looking for start and stop conditions. When a start is detected, the next byte is the address and if it matches the one for the particular device, it wakes up and communicates, all the other devices stay idle.

Brian.
 

Is it possible to simultaneously retrieve the data from them
You will realize that most I2C devices have pins used to determine address offset, so that even devices of the same kind can be at the same bus
 

Okay so i can use the i2c scanner library to find the addresses of the devices right? So once i find them, i initiate communication with that particular device with wire.beginTransmission and then do i have to take the data from the internal registers of the device or can i use other libraries on the internet to get the data other than from its raw form?
 

You normally get the device addresses from the data sheet. Most I2C devices have addresses you can change by wiring pins to VDD, VSS or sometimes another pin. This allows you to connect more than one of the same device but see them as having different addresses.

In Arduino IDE, yes, to write to an I2C device use:
Wire.beginTransmission(I2CAddress); // sends start bit sequence and the address to the i2C bus
Wire.write(I2CDataByte); // this can be repeated to send more data
Wire.endTransmission(); // send stop bit sequence.

To read a device use:
Wire.requestFrom(I2CAddress,numberOfBytes); // last parameter is the number of bytes you expect the device to return
IncomingByte = Wire.read();

Other libraries may use the same underlying functions but return the data in a different format. For example reading a time/date device might convert the data transferred by the commands above into a readable form like day of the week, month name, 12 or 24 hour time or an alarm setting.

Brian.
 

I only plan on reading data and i have a temperature sensor and a heart rate-oxygen saturation level sensor. I just need the data from them in real time.
I have the code for getting data from each of them in a readable format individually. But I'm confused as to how to get them while using i2c.
The commands you gave above for reading is to get the data in raw form right? Is it possible to use the wire.requestfrom() function to ask the sensor for the data and then use the other code to get the data in the readable format?
If not, how do i convert the raw data to a readable value after using your above mentioned commands?
 

@OP:

Readable or not isn't a question of I2C...it is a general task in programming.
Usually no numeric variable is human readable unless transformed to an ASCII string.
(There are other human readable conversions than ASCII, like 7 segment ...)

So showing the result of a simple multiplication like "150 x 10" needs some human readable conversion.

Klaus
 

If not, how do i convert the raw data to a readable value after using your above mentioned commands?
That depends on what the original format is. For example, if the device returned a value which should be interpreted as 1234, but it is stored as 8-bit values, the I2C interface would have to read at least two bytes because a single byte can only hold values up to 255. '1234' might be stored as BCD '12' and '34' in which case you multiply the 12 by 100 and add the 34. Or, it might be stored in binary as 0x4d2 being '0x04' and '0xd2' in which case you shift the 0x04 eight places to the left and add the 0xd2 to get the value back then convert it to decimal. Some device specific libraries do this for you but they only replicate the basic instructions above.

As FvM explained, printf() and sprintf() are your friends because they convert many different number types into a more usable or printable format.

Brian.
 

So how do i take two different datas from the same sensor, that is heart rate and oxygen saturation in blood?
 

So how do i take two different datas from the same sensor
You can get the answer for this from previous replies.
Review general I2C specs and device specific specs, and you'll undestand how each internal variable is mapped into the address range.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…