# Obtaining orientation from an IMU

**URL:** <https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564>\
**Category:** Uncategorized\
**Created:** [21 November 2023 03:09 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564 "2023-11-21T03:09:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitchell258024](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Mitchell258024](https://forum.core-electronics.com.au/u/Mitchell258024)\
**Post date:** [21 November 2023 03:09 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/1 "2023-11-21T03:09:38Z")

</div>

I’m wanting to obtain the real time orientation of an object. How do I obtain this?

Currently I have the Wifi Uno Rev 2 with the LSM6DS3TR IMU. Though I can obtain other equipment if needed.

---

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [21 November 2023 03:17 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/2 "2023-11-21T03:17:22Z")

</div>

Cool!  
Couple of quick questions.

What is the object that we are tracking?  
Will the object be stationary in space while it’s rotating or is carried around?  
Does the object have room on the inside which could house the LSM6DS3TR IMU?  
🙂

---

<div class="post-metadata">

**Author:** ![Liam](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/liam/32/3562_2.png) [@Liam](https://forum.core-electronics.com.au/u/Liam)\
**Post date:** [21 November 2023 03:25 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/3 "2023-11-21T03:25:50Z")

</div>

Hi Mitchell,

Another couple of questions.

What do you mean by real-time orientation? Is it just the angle that the object is being held at or also a distance from where it was picked up?

A bit more general information about your project will help a ton!  
Liam

---

<div class="post-metadata">

**Author:** ![Mitchell258024](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Mitchell258024](https://forum.core-electronics.com.au/u/Mitchell258024)\
**Post date:** [21 November 2023 08:30 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/4 "2023-11-21T08:30:09Z")

</div>

Thanks Mixmusix

The thigh of a person while walking, so the object will move in space as well as rotating. The IMU would be on the persons thigh so not technically inside

---

<div class="post-metadata">

**Author:** ![Mitchell258024](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Mitchell258024](https://forum.core-electronics.com.au/u/Mitchell258024)\
**Post date:** [21 November 2023 08:32 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/5 "2023-11-21T08:32:22Z")

</div>

Just the angle will be sufficient. I want to assess the orientation of someone’s thigh in real time (flexion/extension, abduction/adduction, internal rotation/external rotation.

---

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [22 November 2023 00:30 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/6 "2023-11-22T00:30:11Z")

</div>

> [@Mitchell258024](#):
>
> Just the angle will be sufficient.

[Have a look at this library by adafruit that interfaces between the arduino and the lsm6ds3trc](https://github.com/adafruit/Adafruit_LSM6DS)

#### You’ll also need some code to calculate the 3d distance travel between each loop. I’d recommend a crude linear algebra approach.

```cpp

/* UNTESTED CODE!!!
THIS CODE IS INCOMPLETE AND FOR INSTRUCTIONAL PURPOSES ONLY */
struct GVec {
   float x;
   float y;
   float z;

  float dist(GVec v) {
    float dx = x - v.x;
    float dy = y - v.y;
    float dz = z - v.z;
    return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
  }
}

//Init a global vector.
GVec cacheVector;
  
/* SETUP REQUIRED! See Library Documentation */
void setup{// CODE GOES HERE!}

void loop() {
  // Get a new normalized sensor event
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  lsm6ds3trc.getEvent(&accel, &gyro, &temp);

  /* Calculate the distance traveled form last loop in radians */
  GVec vzero;
  vzero.x = gyro.gyro.x;
  vzero.y = gyro.gyro.y;
  vzero.z = gyro.gyro.z;
  Serial.println(vzero.dist(cacheVector));
  cacheVector = vzero;
  delay(1000);
}

```

---

<div class="post-metadata">

**Author:** ![Liam](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/liam/32/3562_2.png) [@Liam](https://forum.core-electronics.com.au/u/Liam)\
**Post date:** [22 November 2023 00:36 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/7 "2023-11-22T00:36:31Z")

</div>

Hi Mitchell,

An IMU seems like the intuitive answer but a poteniometer is also an option: [Core Electronics on Instagram: "We caught Liam trying to turn himself into a cyborg after playing too much Cyberpunk 2077. Only a few pots, a Pico and some Feetech servos and he's already claiming he can talk to the toaster. Head the link in bio to grab yourself some servos and get in on the craze."](https://www.instagram.com/reel/Cq6XKi9s9Ti/?hl=en)

There is definitely a scale of accuracy and timelyness when using an IMU, the method PIx used is very fast to compute but doesnt account for gyro drift.  
Using the accelerometer is also fast but cannot easily reject fast movements or vibrations.

Sensor fusion through the use of a Kalman filter will be a bit slower to compute (still close enough to real time for the movements of a human) and help with the issues above.

Liam

---

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [22 November 2023 00:43 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/8 "2023-11-22T00:43:01Z")

</div>

> [@Liam](#):
>
> the method PIx used is very fast to compute but doesnt account for gyro drift.

Ooooo yeah good point.

---

<div class="post-metadata">

**Author:** ![Richard83832](https://avatars.discourse-cdn.com/v4/letter/r/22d042/32.png) [@Richard83832](https://forum.core-electronics.com.au/u/Richard83832)\
**Post date:** [27 November 2023 09:59 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/9 "2023-11-27T09:59:13Z")

</div>

Hi Mitchell  
Maybe I’m missing something here, but if you want the orientation of an object in real time without drift error then a 3D magnetometer sensor would seem to be the most appropriate. Core sell various magnetometers, some with onboard accelerometers, gyroscopes and onboard processing included.  
Richard

---

<div class="post-metadata">

**Author:** ![Jack](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jack/32/9287_2.png) [@Jack](https://forum.core-electronics.com.au/u/Jack)\
**Post date:** [27 November 2023 23:05 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/10 "2023-11-27T23:05:53Z")

</div>

Here’s an example of an IMU that does the fusion itself and has a visualiser.  
I’ve linked our store page and Adafruit’s page. There is a comprehensive setup guide in there for a few different processors.

> **[Adafruit 9-DOF Absolute Orientation IMU Fusion Breakout - BNO055](https://core-electronics.com.au/adafruit-9-dof-absolute-orientation-imu-fusion-breakout-bno055.html)**
>
> If you've ever ordered and wire up a 9-DOF sensor, chances are you've also realized the challenge of turning the sensor data from an accelerometer, gyroscope and...

> **[Adafruit BNO055 Absolute Orientation Sensor](https://learn.adafruit.com/adafruit-bno055-absolute-orientation-sensor/overview)**
>
> The BNO055 is everything you've always wanted for AHRS or orientation data in a single chip. On chip sensor fusion algorithms, quaternion, euler and vector output, and "just works" data output. Stop meddling with mind-numbing fusion algorithms, and...

---

<div class="post-metadata">

**Author:** ![Mitchell258024](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Mitchell258024](https://forum.core-electronics.com.au/u/Mitchell258024)\
**Post date:** [28 November 2023 07:46 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/11 "2023-11-28T07:46:53Z")

</div>

How do I apply the Kalman Filter in the code. What would be an estimate of the delay using the Kalman Filter?

---

<div class="post-metadata">

**Author:** ![Mitchell258024](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Mitchell258024](https://forum.core-electronics.com.au/u/Mitchell258024)\
**Post date:** [28 November 2023 08:35 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/12 "2023-11-28T08:35:05Z")

</div>

> [@Jack](#):
>
> here is a comprehensive setup guide in there for a few different processors.

Great, thanks. An IMU that does the fusion itself will be the best option.

There are some discussion posts about drift over time causing significantly inaccurate measures with this sensor. Are there other options that are more accurate?

---

<div class="post-metadata">

**Author:** ![Jack](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jack/32/9287_2.png) [@Jack](https://forum.core-electronics.com.au/u/Jack)\
**Post date:** [29 November 2023 03:04 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/13 "2023-11-29T03:04:41Z")

</div>

There will always be some element of drift that creeps into sensors. Long term the goal is to minimise it.  
Some things to consider, in no particular order, would be:

- Testing the sensors regularly
- Keeping the environment they’re in while in use consistent (This is one that may cause you issues)
- Redundancy can help pick up on drift while still providing some source of data
- Implementing filters in your code (I can see references to a Kalman filter, there are libraries for these readily available online, I recommend plotting your raw and filtered data together while you tune it)
- The effect of batteries on the magnetometers magnetic field should also be considered, I can see this is where some people are having difficulties with drift. If you are tracking some ones thigh, if they place the battery in their pocket you shouldn’t have any issues.

Another thing to consider is if you are tracking movement long enough for drift to be an issue. This is where testing is needed.

I am pretty sure I have given you some more questions than answers here but hopefully this helps.

---

<div class="post-metadata">

**Author:** ![Sally261693](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Sally261693](https://forum.core-electronics.com.au/u/Sally261693)\
**Post date:** [1 December 2023 14:42 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/14 "2023-12-01T14:42:00Z")

</div>

> [@Richard83832](#):
>
> Core sell various magnetometers, some with onboard accelerometers, gyroscopes and onboard processing included.  
> Richard

Is there a product that you would recommend?

---

<div class="post-metadata">

**Author:** ![Bryce](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/bryce/32/3631_2.png) [@Bryce](https://forum.core-electronics.com.au/u/Bryce)\
**Post date:** [5 December 2023 01:34 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/15 "2023-12-05T01:34:26Z")

</div>

Hey Sally,

It depends on the specs that you need, and what microcontroller/s you’re intending on using it with. Although the MPU-6050’s and similar breakouts are often the ubiquitous generic option:

> **[MPU-6050 Module 3 Axis Gyroscope + Accelerometer](https://core-electronics.com.au/mpu-6050-module-3-axis-gyroscope-acce-lerometer.html)**
>
> A 3-axis gyroscope and 3-axis accelerometer housed on a development board ready for prototyping.

As for a magnetometer, something like the LIS3MDL is a popular generic choice:

> **[LIS3MDL 3-Axis Magnetometer Carrier with Voltage Regulator](https://core-electronics.com.au/lis3mdl-3-axis-magnetometer-carrier-with-voltage-regulator.html)**
>
> This module is a carrier/breakout board for the ST LIS3MDL three-axis magnetometer. The sensor provides magnetic field strength measurements with a...

Otherwise, if you’re using a MicroBit, RPi, or Pico you can set up the PiicoDev range that we design, manufacture, and prep code for here:

> **[PiicoDev Magnetometer QMC6310](https://core-electronics.com.au/piicodev-magnetometer-qmc6310.html)**
>
> Measures invisible magnetic fields - use as a compass or to detect nearby magnets.

> **[PiicoDev 3-Axis Accelerometer LIS3DH](https://core-electronics.com.au/piicodev-3-axis-accelerometer-lis3dh.html)**
>
> Measure motion with the PiicoDev 3-Axis Accelerometer!

---

<div class="post-metadata">

**Author:** ![Richard83832](https://avatars.discourse-cdn.com/v4/letter/r/22d042/32.png) [@Richard83832](https://forum.core-electronics.com.au/u/Richard83832)\
**Post date:** [7 December 2023 11:24 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/16 "2023-12-07T11:24:19Z")

</div>

It’s difficult to recommend the best sensor without knowing all the facts. It seems a BNO055/BNO085/BNO086 might be a good start if size is not an issue. And it may be necessary to trial a few sensors before finding the optimum solution. Drift should not be an issue from my experience. However, as Jack mentioned, there may be some distortion of output if there is any iron nearby the sensor, but your other components should be designed to minimise such distortion.

---

<div class="post-metadata">

**Author:** ![Bryce](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/bryce/32/3631_2.png) [@Bryce](https://forum.core-electronics.com.au/u/Bryce)\
**Post date:** [14 December 2023 01:25 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/17 "2023-12-14T01:25:42Z")

</div>

> [@Richard83832](#):
>
> It’s difficult to recommend the best sensor without knowing all the facts. It seems a BNO055/BNO085/BNO086 might be a good start if size is not an issue. And it may be necessary to trial a few sensors before finding the optimum solution

100% agree here, completely depends on use case and the problem-space.

This includes the level of precision required, what you’re intending to interface it with, available power and any battery life requirements, price, how comfortable you are with abstraction rather than dealing with and likely filtering or handling raw readings, etc.

If you’re putting together a prototype to get a better understanding of how to approach a solution, an all-in-one IMU such as the BNO Jack linked earlier would be a good place to start, should have plenty of docs online too.

Start generic and after some experimentation and experience with the setup you should have a much better idea of what hardware and specs are needed.

---

<div class="post-metadata">

**Author:** ![Mitchell258024](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Mitchell258024](https://forum.core-electronics.com.au/u/Mitchell258024)\
**Post date:** [7 April 2024 12:14 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/18 "2024-04-07T12:14:42Z")

</div>

Is there a way to power the device without/minimally effecting the magnetometer while still having the power source and hardware together? As a small, all inclusive device would be best as it is a wearable… Also, what is the smallest set up with the BN085 powered and transmitting data via bluetooth to a pico W. Currently using a pico W with the BN085.

---

<div class="post-metadata">

**Author:** ![Samuel](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/samuel/32/11763_2.png) [@Samuel](https://forum.core-electronics.com.au/u/Samuel)\
**Post date:** [9 April 2024 05:35 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/19 "2024-04-09T05:35:06Z")

</div>

Hey Mitchell,

Avoiding electromagnetic interference entirely might prove challenging. Maybe have a look into EMF shielding sheets? Separating the components as best as you can with EMF shielding may be your best bet although it can only do so much as you probably wont be able to completely enclose your batteries.

as a general rule electromagnetic interference will grow alongside the current drawn from the batteries so if you could minimise that as much as possible you will get the best results.

If you can the more you can space out the batteries and the sensor the better. use EMF shielding if you need and keep the current draw as low as possible.

The smallest setup that you could easily setup for this would probably be a small battery, the esp32 and the BN085 on one end and another esp32 device on the other for it to talk to.

Hope this helps!  
Sam

---

<div class="post-metadata">

**Author:** ![Robert93820](https://avatars.discourse-cdn.com/v4/letter/r/ac91a4/32.png) [@Robert93820](https://forum.core-electronics.com.au/u/Robert93820)\
**Post date:** [9 April 2024 05:54 UTC](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564/20 "2024-04-09T05:54:11Z")

</div>

Hi Mitchell  
As Sam has pointed out EMF shielding can be a pain. The one thing he has not mentioned that is the most effective material for a shield is steel. Aluminium is just about useless. This makes it a real pain in the you know what.

As most of your static interfering field should be in the battery wires there is one thing you can do which will be really effective. Have the positive and negative wires the same length. They probably would normally be anyway. Twist them together as a twisted pair. This does not have to be too tight as long as they are kept in close proximity to each other. As the currents in the 2 wires are going in opposite directions the magnetic fields will cancel out so you should end up with a near zero result. There is no need for any shielding but don’t have them wandering all over the place. Keep as short as practical and you will have no trouble from this source.  
Cheers Bob

[Next page](https://forum.core-electronics.com.au/t/obtaining-orientation-from-an-imu/18564.md?page=2)
