# Usage of i2c devices

**URL:** https://forum.xod.io/t/usage-of-i2c-devices/5406
**Category:** XOD Programming Questions
**Created:** [August 20, 2021, 10:06am UTC](https://forum.xod.io/t/usage-of-i2c-devices/5406 "2021-08-20T10:06:12Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![kullanan](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@kullanan](https://forum.xod.io/u/kullanan)
#### Post date: [August 20, 2021, 10:06am UTC](https://forum.xod.io/t/usage-of-i2c-devices/5406/1 "2021-08-20T10:06:12Z")

</div>

Hello,  
I am pretty new to XOD and microcontrollers in general and I was trying to use data from 3 different sensors which use i2c to produce an air quality index(and maybe actuate the results with some LEDs). I have already finished this mini-project using the Arduino IDE with some downloaded libraries but I can’t quite figure out how to implement the same project using XOD since I have very limited knowledge of I2C communication. Is it possible that I can access some readily written library without diving into all the details and specifications of I2C?  
-The sensors I am trying to use are,  
–Wuhan Cubic CM1109 CO2 Sensor  
– Sensirion SHT31  
– Sensirion SGP30

---

<div class="post-metadata">

### Author: ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)
#### Post date: [August 20, 2021, 3:11pm UTC](https://forum.xod.io/t/usage-of-i2c-devices/5406/2 "2021-08-20T15:11:06Z")

</div>

I’m not seeing any nodes or libraries specifically for those devices, but there are i2c nodes in xod/i2c in the project browser that implement basic i2c functions and can be chained together to implement i2c communication for specific devices. This would basically require converting IDE code into the appropriate i2c nodes. This will probably not be trivial, but once completed, you will have your own library for those devices that you could then share with others.

---

<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: [August 20, 2021, 10:45pm UTC](https://forum.xod.io/t/usage-of-i2c-devices/5406/3 "2021-08-20T22:45:52Z")

</div>

In General: I just got multiple different I2C devices on the same BUS… Check out the solution to this recent post. The key is not crash the device communications into each other. There are several ways to do this. The simplest is to make a continuous daisy chain…

> [@MCP4725 Odd Behavior - does not update with number directly](https://forum.xod.io/t/mcp4725-odd-behavior-does-not-update-with-number-directly/5393/3):
>
> Seams like a potential safety issue with the i2c communication. The clocks can’t match, or one dominates the i2c bus. In my case, the ADS1115 appears to dominate. If you choose different clock speeds, then the speeds have to be out of phase… and never match on moment to UPD, I’m not sure the best way to get that… One option is to use prime numbers, but I’m not sure if they could overlap ever… [image] A Flip-Flop could be used for just 2 i2c devices, assuming you want them sampling at the same…

Now, you need a library in XOD… You’ve got to make your own, or maybe one of the XODERS can adapt it for you. You gotta start with the Arduino IDE Library from GitHub… (Assuming you can’t or don’t want to do what qweimer was talking about, which is fairly complex)

Here’s a recent example, that’s also fairly complex, using I2C, that Wayland made (bless his soul)…

> [@ADS1115 Not working](https://forum.xod.io/t/ads1115-not-working/5386):
>
> I’ve struggled every which way with the Adafruit ADS1115. I’ve even tried alternative libraries. I’ve edited the c++ code. Everything! Is anyone getting this thing to work?

In general, there’s some info on adapting libraries from Arduino libraries  
[https://xod.io/docs/guide/wrapping-arduino-libraries/](https://xod.io/docs/guide/wrapping-arduino-libraries/)

Once you know what you’re doing, then it’s trivial to adapt a library. However, I actually haven’t mastered it… even after working on it for at least a week and reviewing waylands work.

I would recommend linked web addresses to 1) the libraries you need on GitHub, 2) the Devices you’re using… If somebody can do it, then they might. Especially, if your equipment could be very useful to other XODERS.

Alternatively, look at other devices that already have XOD libraries  
[https://xod.io/libs/](https://xod.io/libs/)

---

<div class="post-metadata">

### Author: ![kullanan](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@kullanan](https://forum.xod.io/u/kullanan)
#### Post date: [September 2, 2021, 8:24am UTC](https://forum.xod.io/t/usage-of-i2c-devices/5406/4 "2021-09-02T08:24:58Z")

</div>

Thanks for the advice! At first, I was quite stumbled with the absence of an XOD library since I do not have any programming background either aside from some C programming that I had done in the past. I tried to figure out adapting the Arduino library for XOD using C++, but it turns out the i2c functions in XOD are quite simple! I was able to finish my project in 2 days without any background in basically anything 😃  
P.S. Sorry for the late reply, I was trying to figure out how far I can go 😅  
Edit: The basic instructions in the XOD documentation helped a lot.

> **[I²C Communication Basics](https://xod.io/docs/guide/i2c/)**
>
> How to use XOD nodes from the standard i2c library to talk to I2C/TWI devices

Here are some patches I created:  
–Wuhan Cubic CM1109 CO2 Sensor

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/2/2b6e14e57780e3fd77d6c7c41705a4e684f4c6d8.png)

-Sensirion SHT31

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/4/4106709539ec0a051fcd5602940933c1a15a55b8.png)

-Sensirion SGP30

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/1/14970cc187753792c0b2e7cd8dcfecbb7d9a150f.png)

They still miss many features that the sensors are capable of(calibration, setting baselines etc.) but they got the job done when it comes to reading and outputting measurements. Any help on my code’s optimization is still much appreciated 😃

---

<div class="post-metadata">

### Author: ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)
#### Post date: [September 2, 2021, 4:30pm UTC](https://forum.xod.io/t/usage-of-i2c-devices/5406/5 "2021-09-02T16:30:30Z")

</div>

Using SEND signal to toggle flip-flop to start continuously performing an action is probably not a good idea. Usually this would be an UPD pin to run the write/read process one time. Setting the UPD pin to LOOP would basically do what you have coded (but might run too often). Dropping the clock/flip-flop would be even more important if you were trying to create nodes to share with others; you can always add a clock node to continuously pulse the UPD pin outside this node.

---

<div class="post-metadata">

### Author: ![kullanan](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@kullanan](https://forum.xod.io/u/kullanan)
#### Post date: [September 3, 2021, 1:06pm UTC](https://forum.xod.io/t/usage-of-i2c-devices/5406/6 "2021-09-03T13:06:50Z")

</div>

I see. Can you specify which nodes I can use in this context that has a UPD pin? (I don’t really remember running into such a pin in the nodes I’ve tried using). Also, I can see intuitively why the flip-flop/clock combination may not be a good practice but I don’t get the exact reason(speed, applicability, storage usage, readability etc.)  
Thanks again for the feedback.

---

<div class="post-metadata">

### Author: ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)
#### Post date: [September 3, 2021, 2:47pm UTC](https://forum.xod.io/t/usage-of-i2c-devices/5406/7 "2021-09-03T14:47:16Z")

</div>

UPD is just a different label. Your SEND pin is not really triggering a send as much as it is triggering an update of the output pins for the node; except as you have it programmed it is actually a “start updating output” pin. Part of the problem with this is that you have 2 devices on your I2C bus. If you have nodes auto-updating both of them, there is no way to prevent an attempt to communicate to both devices at the same time. If you just rename your pulse SEND pin to UPD, then tie it directly to begin-transmission-SEND and delete the clock & flip-flop, it will behave more like existing sensor nodes. If you really want it to update every second, you can put a clock node outside your sensor node & connect it to the UPD pin, but it might be better if you added a DONE (pulse) pin to your node and tie it to the last read-byte-DONE. This would allow you to UPD your sensor, and tie the DONE pin to the UPD pin of the next sensor so you know only one sensor is being updated at a time (similar to how you have read-byte daisy-chained in your node so you know each read finishes before you start the next one).

---

<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: [September 5, 2021, 2:48pm UTC](https://forum.xod.io/t/usage-of-i2c-devices/5406/8 "2021-09-05T14:48:06Z")

</div>

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