<p>I successfully obtained data from my I2C device in Arduino:</p>
<pre><code>#include &lt;Wire.h&gt;
...
Wire.begin();
... // read function:
Wire.beginTransmission(address);
Wire.write(startAddress &gt;&gt; 8);
Wire.write(startAddress &amp; 0xFF);
uint16_t numberOfBytesToRead = bytesRemaining;

Wire.requestFrom((uint8_t)address, numberOfBytesToRead);
if (Wire.available()) {
  for (uint16_t x = 0 ; x &lt; numberOfBytesToRead / 2; x++) {
    data[dataSpot] = Wire.read() &lt;&lt; 8; //MSB    // uint16_t *data
    data[dataSpot] |= Wire.read(); //LSB
    dataSpot++;
  }
}
</code></pre>
<p>Now I need to do the same in STM32F411 FC:</p>
<ol>
<li>obtain this data in Betaflight loop, along with other sensors data</li>
<li>send it to OSD, along with other sensors data</li>
</ol>
<p>Please help me with specific code examples or suggest which files and functions should I edit? I'm still very new to Betaflight project structure </p>

CC BY-SA 4.0
2022-09-10T06:23:46.863Z
Welcome to the forum!

This is quite a complicated process that would involve a significant amount of learning on your part, based on the fact that you're presenting us with Arduino code (*on which I in no way intend to be bashing*). Converting Arduino code to the low-level C used in the Betaflight source is not really an easy feat and we'd likely have to write a multi-page answer to adequately describe the process.

Your question is a good one, which is why I don't want to close it, but just know that you aren't likely to get a satisfying answer. Sorry to be the bearer of bad news! :)
ifconfig
CC BY-SA 4.0
2022-09-11T16:31:10.780Z
@ifconfig I've looked through Betaflight [source code](https://github.com/betaflight/betaflight). There is code that launches tasks: \src\main\scheduler\scheduler.c. Also, there is a list of sensors in \src\main\sensors. Can I just add my custom sensor there and implement functions? Then read its data as it is done for other sensors. Please suggest me which specific steps do I need to take to achieve this. I've already built custom Betaflight, I know C/C++ too. But I'm stuck with this specific problem
user255936
CC BY-SA 4.0
2022-09-12T18:19:36.147Z
Yes, that is likely to be the correct approach. Driver integration of your custom code into the Betaflight build system and into the OSD codebase might be a significant chore, though. Also keep in mind that you'll have no option to configure the new OSD element from the configurator, as it relies on the pre-existing element set in Betaflight.
ifconfig
CC BY-SA 4.0
2023-06-14T23:58:42.093Z
We're you able to successfully add a custom Sensor and produce Text from it on the OSD ? If so, can you share your success details?
user5555
<quadcopters><flight-controller><betaflight><sensor><telemetry>
How to add custom betaflight code to obtain data from I2C device and pass to OSD?
user255936
# How to add custom betaflight code to obtain data from I2C device and pass to OSD?

(Asked by: username_0 on 2022-09-04)

I successfully obtained data from my I2C device in Arduino:
#include 
...
Wire.begin();
... // read function:
Wire.beginTransmission(address);
Wire.write(startAddress >> 8);
Wire.write(startAddress & 0xFF);
uint16_t numberOfBytesToRead = bytesRemaining;

Wire.requestFrom((uint8_t)address, numberOfBytesToRead);
if (Wire.available()) {
  for (uint16_t x = 0 ; x < numberOfBytesToRead / 2; x++) {
    data[dataSpot] = Wire.read() << 8; //MSB    // uint16_t *data
    data[dataSpot] |= Wire.read(); //LSB
    dataSpot++;
  }
}

Now I need to do the same in STM32F411 FC:

obtain this data in Betaflight loop, along with other sensors data
send it to OSD, along with other sensors data

Please help me with specific code examples or suggest which files and functions should I edit? I'm still very new to Betaflight project structure

**Tags:** quadcopters, flight-controller, betaflight, sensor, telemetry
> **Comments:**
> **username_1**: Welcome to the forum!  This is quite a complicated process that would involve a significant amount of learning on your part, based on the fact that you're presenting us with Arduino code (*on which I in no way intend to be bashing*). Converting Arduino code to the low-level C used in the Betaflight source is not really an easy feat and we'd likely have to write a multi-page answer to adequately describe the process.  Your question is a good one, which is why I don't want to close it, but just know that you aren't likely to get a satisfying answer. Sorry to be the bearer of bad news! :)  
> **username_0**: @ifconfig I've looked through Betaflight [source code](https://github.com/betaflight/betaflight). There is code that launches tasks: \src\main\scheduler\scheduler.c. Also, there is a list of sensors in \src\main\sensors. Can I just add my custom sensor there and implement functions? Then read its data as it is done for other sensors. Please suggest me which specific steps do I need to take to achieve this. I've already built custom Betaflight, I know C/C++ too. But I'm stuck with this specific problem  
> **username_1**: Yes, that is likely to be the correct approach. Driver integration of your custom code into the Betaflight build system and into the OSD codebase might be a significant chore, though. Also keep in mind that you'll have no option to configure the new OSD element from the configurator, as it relies on the pre-existing element set in Betaflight.  
> **username_2**: We're you able to successfully add a custom Sensor and produce Text from it on the OSD ? If so, can you share your success details?  

---

## 0 answers

