# Trouble with i2c and bmp280 sensors

**URL:** <https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859>\
**Category:** Hardware Interfacing Questions\
**Created:** [February 10, 2022, 6:54pm UTC](https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859 "2022-02-10T18:54:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kennethwmartin](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/kennethwmartin/32/3411_2.png) [@kennethwmartin](https://forum.xod.io/u/kennethwmartin)\
**Post date:** [February 10, 2022, 6:54pm UTC](https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859/1 "2022-02-10T18:54:51Z")

</div>

I have a project where I need to measure atmospheric pressure in 6 different locations simultaneously. I have selected the BMP280 for this job and because they all have the same 76h address I am using AJ-Burton’s I2C XOD control nodes. When I install a single BMP280 in the XOD design I get a good answer but when I have two sensors, one of them gives very wrong numbers. Re-isolating the sensor that gave the wrong answer then gives the correct results. I must be missing something in how to put multiple BMP280s along with AJ-Burton’s Nodes. Can someone construct an example for me so I can move forward.

---

<div class="post-metadata">

**Author:** ![xodballxod](https://avatars.discourse-cdn.com/v4/letter/x/f475e1/32.png) [@xodballxod](https://forum.xod.io/u/xodballxod)\
**Post date:** [February 12, 2022, 5:42pm UTC](https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859/2 "2022-02-12T17:42:52Z")

</div>

You can only talk to one I2C at a time. If you’re going to use the same address, then you’ve got to put a multiplexer between them. To make it interesting, I’ll send you to this version that is also controlled by I2C (but digital pin versions may be easier to program if you’re very new to XOD, since you’ll only deal with one I2C address).

> **[Adafruit TCA9548A 1-to-8 I2C Multiplexer Breakout](https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout)**
>
> You just found the perfect I2C sensor, and you want to wire up two or three or more of them to your Arduino when you realize "Uh oh, this chip has a fixed I2C address, and from what I know about I2C, you cannot have two devices with the same address...

However, your device appears to also be controlled by SPI, if it’s like this one

> **[Adafruit BMP280 I2C or SPI Barometric Pressure & Altitude Sensor](https://www.adafruit.com/product/2651)**
>
> Bosch has stepped up their game with their new BMP280 sensor, an environmental sensor with temperature, barometric pressure that is the next generation upgrade to the BMP085/BMP180/BMP183. ...

It says “It has the same specifications but can use either I2C or SPI. For simple easy wiring, go with I2C. If you want to connect a bunch of sensors without worrying about I2C address collisions, go with SPI.”

That’s really your easiest solution, to use SPI (though the multiplex solution is pretty similar, logically)… Each Chip Select from each device will sit on a different Digital Pin on the Aruino, and they all share the other MISO, MOSI, SCK of SPI

You still have to time it so you talk to only one Chip Select at a time (usually pulling to ground to select). I use Output Pulse in XOD for that type of function, and Defer Node back to the DO input pulse of the I2C or SPI.

> [@MCP4725 Odd Behavior - does not update with number directly](https://forum.xod.io/t/mcp4725-odd-behavior-does-not-update-with-number-directly/5393):
>
> I’m using the e/MCP4725 library, and it works fine in 2 scenarios, and doesn’t work in another (direct number wire). Basically, can work with deprecated triangle wave, but not the updated triangle wave. I couldn’t get it to update with any kind of number wired directly, even when changing the number to trigger the pulse-on-change inside the mcp4725 node. Works using the example node with the library, with the deprecated triangle-wave node, directly wired to the mcp4725 Works using updated …

~xbx~

---

<div class="post-metadata">

**Author:** ![wayland](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/wayland/32/1590_2.png) [@wayland](https://forum.xod.io/u/wayland)\
**Post date:** [February 13, 2022, 3:50pm UTC](https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859/3 "2022-02-13T15:50:54Z")

</div>

Hi @kennethwmartin,

If you were trying to use my BMP280 library ([wayland/bmp280-barometer — XOD](https://xod.io/libs/wayland/bmp280-barometer/)) with the TCA9548A I²C multiplexer, you will have run into the problem of not being able to control when the BMP280 devices are initialized. To setup each BMP280, we have to do the following:

1. open the appropriate channel on the multiplexer
2. run an initialization routine

I’ve now modified my [BMP280 library](https://xod.io/libs/wayland/bmp280-barometer/) to include an initialization node. I’ve also created a very simple library for the TCA9548A ([wayland/tca9548a-i2c-mux — XOD](https://xod.io/libs/wayland/tca9548a-i2c-mux/)). It has only one node, [select-channel](https://xod.io/libs/wayland/tca9548a-i2c-mux/select-channel/), which can be used to open one channel on the TCA9548A at a time. Included in the tca9548a-i2c-mux library is the following example patch:

 ![Screenshot 2022-02-13 at 15.01.44](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/7/73a2f98c3499a07a96340d07569fb0a1535b2e57.png)

The above patch is for a hardware configuration where we have two BMP280 devices, one on channel 0 of the multiplexer and the other on channel 3.

AJ-Burton’s library ([aj-burton/tca9548a — XOD](https://xod.io/libs/aj-burton/tca9548a/)) also works well and provides much richer functionality. The **select-channel** node in the above patch could be replaced with the **open-channel** node in AJ-Burton’s library, however the latter node requires slightly more memory.

_N.B._ the I²C address of my BMP280 devices is **77h**. You will obviously need to set the **ADDR** pin on the init node to **76h** for your BMP280s.

---

<div class="post-metadata">

**Author:** ![kennethwmartin](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/kennethwmartin/32/3411_2.png) [@kennethwmartin](https://forum.xod.io/u/kennethwmartin)\
**Post date:** [February 13, 2022, 8:03pm UTC](https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859/4 "2022-02-13T20:03:19Z")

</div>

Thank so very much for this example and the new data for me. I will try this today.

I will let you know how it goes.

---

<div class="post-metadata">

**Author:** ![kennethwmartin](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/kennethwmartin/32/3411_2.png) [@kennethwmartin](https://forum.xod.io/u/kennethwmartin)\
**Post date:** [February 14, 2022, 11:24pm UTC](https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859/5 "2022-02-14T23:24:42Z")

</div>

Wayland,

It works perfectly! Well, once I dug out my errors. I am having trouble understanding the logic though. Let me know if I am stating it correctly.

Upon start of the program, each channel of the TCA9548a must be initialized with its own i2c circuit one after the other. Once that is completed each of the i2c circuits will have their channel pulsed for data. Once that is done the first time the clock keeps pulsing the i2c circuits. hmmm?

---

<div class="post-metadata">

**Author:** ![wayland](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/wayland/32/1590_2.png) [@wayland](https://forum.xod.io/u/wayland)\
**Post date:** [February 16, 2022, 11:49am UTC](https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859/6 "2022-02-16T11:49:34Z")

</div>

It is actually the BMP280 devices we need to initialize, rather than the TCA9548A. Let’s start by looking at the block of nodes on the left hand side of the patch - this portion of the program runs only once, at start-up.

1. At the moment the Arduino starts up, it can see only one device on the I²C bus, the TCA9548 multiplexer.
2. The first select-channel node instructs the TCA9548 to open channel 0.
3. The first BMP280 is now visible to the Arduino and can be initialized using the init node.
4. We send our bmp280-device into a [to-bus](https://xod.io/docs/guide/buses/) to avoid criss-crossing wires. The alternative would be to connect the bmp280-device node directly to the read-pressure and read-temperature nodes.
5. The second select-channel node instructs the TCA9548 to open channel 3 and close all other channels.
6. The second BMP280 is now visible to the Arduino and can be initialized.
7. Once initialization of both BMP280s is complete, the output of the flip-flop changes from false to true. This boolean value is passed to the block of nodes on the right.

The block of nodes on the right handle measurement of temperature and pressure.

1. When INTD changes to true, the clock is activated. The clock is set to generate one pulse per second.
2. A select-channel node opens channel 0 (and closes all other channels) so that the arduino can read the current temperature and pressure measured by the first BMP280.
3. Next, another select-channel node opens channel 3 (and closes all other channels) so that the arduino can get the latest temperature and pressure measurements from the second BMP280.

Hope this helps. Let me know if you have any questions.

---

<div class="post-metadata">

**Author:** ![kennethwmartin](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/kennethwmartin/32/3411_2.png) [@kennethwmartin](https://forum.xod.io/u/kennethwmartin)\
**Post date:** [February 18, 2022, 9:17pm UTC](https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859/7 "2022-02-18T21:17:45Z")

</div>

OK, that was very helpful.

It certainly gives me enough to do some experimenting around. Thanks again.

Kenneth

---

<div class="post-metadata">

**Author:** ![jh295](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@jh295](https://forum.xod.io/u/jh295)\
**Post date:** [February 24, 2022, 6:57pm UTC](https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859/8 "2022-02-24T18:57:04Z")

</div>

Thanks Matt,  
The TCA9548A I²C multiplexer library works well with the M5Stack Pa.HUB device:

 ![PaHUB_device1](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/3/32526c765f9281c32764472183fc03b1fab648a0.png)  
(With some help from Matt) it can also be used to communicate with multiple LCD displays - here talking to 3 different 4004 LCD displays sitting on the same I2C address, but plugged into a M5Stack Pa.HUB device. Matt created a variant of the LCD library: [wayland/text-lcd-i2c@0.0.2](https://xod.io/libs/wayland/text-lcd-i2c/) that exposes an addressable init function, to allow initialisation of the different displays before writing to the screens.

Thanks again, Matt!

![3xI2C_displays2](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/9/92b5e6150052eae229e699a6f23bf519f108f355.gif)

 ![3xI2C_Displays](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/5/5fa28f04593966f86ec6765ce8b75bb73999dd67.png)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/eb7348baa04192c2ef932d1db54d44f09f8a7888.png) [@system](https://forum.xod.io/u/system)\
**Post date:** [March 26, 2022, 6:57pm UTC](https://forum.xod.io/t/trouble-with-i2c-and-bmp280-sensors/5859/9 "2022-03-26T18:57:59Z")

</div>

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