Introduction
This tutorial will teach you how to retrieve and display sensor readings from the onboard inertial measurement unit (IMU) of the new Arduino Uno WiFi Rev2 development board. A basic understanding of electronics and programming is expected along with some familiarity with the Arduino platform. If you are new to the Arduino platform or would just like to refresh your knowledge, please see our Blink: Making An LED Blink On An Arduino Uno tutorial before proceeding with this one.
What Is Needed
- Linux, macOS, Or Windows Based Computer With A USB Port
- Arduino IDE
- Arduino Uno WiFi Rev2 (available on Arduino and SparkFun) With Compatible USB Cable
Background Information
The recently released Arduino Uno WiFi Rev2 development board has an onboard IMU with a 3-axis accelerometer, a 3-axis gyroscope, and an embedded temperature sensor. This particular IMU is the LSM6DS3 device provided by ST Microelectronics (datasheet) accessed over the SPI serial bus interface.
My development system consists of the Arduino Uno WiFi Rev2 development board connected to a macOS based computer running the Mojave operating system with the desktop Arduino IDE. If you are using a different computer setup, the vast majority of this tutorial should still apply, however, some minor changes may be necessary.
Installing The IMU Library
The getting started guide for the Arduino Uno WiFi Rev2 states that the onboard IMU is “already supported by many libraries; you can choose between the available ones in our library manager using LSM6DS keyword”. I found this was not necessarily the case for me. I researched both the available libraries within the Library Manager along with general internet searches for an LSM6DS3 based library that supports the SPI interface and only found two. Both were available within the Library Manager.
The Accelerometer And Gyroscope LSM6DS3 by Seeed Studio library is a derivative of the SparkFun LSM6DS3 Breakout by SparkFun Electronics library with only the default I2C address being different. The two libraries also have different accompanying example sketches with the SparkFun library offering a few more. I decided to use the SparkFun library as it is the base library.
To install the SparkFun library, open the Library Manager (Tools > Manage Libraries…) and enter LSM6DS3 in the search field. Find and install the SparkFun LSM6DS3 Breakout by SparkFun Electronics library from the listing.
Writing The Software
Once the library is installed, open the MinimalistExample library example sketch (File > Examples > SparkFun LSM6DS3 Breakout > MinimalistExample). This sketch is currently configured to work with the IMU over an I2C serial bus interface so we need to change that to use the SPI interface instead. Change line 41 from
LSM6DS3 myIMU; //Default constructor is I2C, addr 0x6B
to
LSM6DS3 myIMU(SPI_MODE, SPIIMU_SS); // SPI Chip Select
That’s all it takes. Now let’s watch it in action. Open the Serial Monitor window (Tools > Serial Monitor) so that we can see the program’s output. Upload (Sketch > Upload) the sketch and watch the readings being presented in the Serial Monitor. My readings are shown below.
Processor came out of reset.
Accelerometer:
X = 0.0200
Y = 0.0156
Z = 1.0189
Gyroscope:
X = 0.0000
Y = -4.7600
Z = -3.8500
Thermometer:
Degrees C = 23.8125
Degrees F = 74.8625
Summary
In this tutorial, we learned how to access the onboard IMU of the new Arduino Uno WiFi Rev2 development board. We installed the relevant library and were able to see acceleration, rotation, and temperature sensor readings being displayed upon loading and modifying an example sketch.
Thank you for joining me in this journey and I hope you enjoyed the experience. Please feel free to share your thoughts in the comments section below.
Dear John,
I m trying using this library to get the orientation using gyro values. I am getting values for Gyroscope:
X = 0.0000
Y = -4.7600
Z = -3.8500.
When I change the orientation of the board the values change and again angles come out as above. Since, I am a beginner. Can you help me, in getting the values of gyro angles at different orientations of the board. Thanks
The values for the gyroscope will only change while the device is in motion. Once motion stops, the gyroscope values will go back to being close to zero. The orientation of the device is registered with the accelerometer whose values will remain different at different board orientations.
Thanks for the response.
Can you share me the code in which I can get angles of the board at different orientations?
I used the exact same code as the example except for the one line change as described in the tutorial. After installing the SparkFun LSM6DS3 Breakout by SparkFun Electronics library, the example code can be retrieved by selecting File > Examples > SparkFun LSM6DS3 Breakout > MinimalistExample from the Arduino IDE main menu. It can also be retrieved from GitHub with the https://github.com/sparkfun/SparkFun_LSM6DS3_Arduino_Library/blob/master/examples/MinimalistExample/MinimalistExample.ino link. The values for the Accelerometer readings are in g and the readings for the Gyroscope (angles) are in degrees/second.
I think I misunderstood your question earlier. I believe you are looking to obtain inclination angles showing the tilt of the board. You can use the following to calculate those values. angleX is the angle around the X axis and angleY is the angle around the Y axis. It is my understanding that angleZ is a much more involved calculation needing additional sensor data and I don’t know how to calculate that one at this time.
angleX = atan(imu.readFloatAccelY() / sqrt(sq(imu.readFloatAccelX()) + sq(imu.readFloatAccelZ()))) * 180 / PI;
angleY = atan(imu.readFloatAccelX() / sqrt(sq(imu.readFloatAccelY()) + sq(imu.readFloatAccelZ()))) * 180 / PI;
I hope this answers your question.
Thanks a lot.
Dear John,
Currently, I am using the MPU 6050 Library to get stable data from Gyroscope, but stop getting values from gyroscope after some time (via Serial Monitor). If I try to use this new Arduino version with onboard IMU, Can I get the stable data (without yaw drift)?
I’m afraid you are already beyond my expertise concerning yaw capabilities. You might want to search for “yaw drift” within the Arduino Forum. There are already some discussions on this topic.
Can I get any solution with Arduino Uno WiFi Rev2? If I compare on board and MPU 6050 module which one is best? Can I get proper data with LSM6DS3 (without noise and stable data)?
I’m not sure I understand your question. Here are the datasheets for the LSM6DS3 and MPU-6050 components. They should contain the noise information you are trying to find.
Can you post any sample data screenshot, it will help us to select and finalize. Because you know very well, how best will get the output from the device. If the dates are proper then it’s great… I can move further.
I am not an expert in this area; I just post information that I research as part of my own learning in case others may find it helpful. If you are trying to decide which IMU is best for your project, I would compare your requirements to the datasheets and ask some other folks across various forums who have much more experience than I for their thoughts.
By the way, I created a new article (Using The Arduino_LSM6DS3 Library To Access The Arduino Uno WiFi Rev2 IMU) for accessing the IMU using the official Arduino_LSM6DS3 library that even includes creating roll, pitch, and yaw angles.
I followed exactly the stteps but i got this error.. can you help? thank you
Arduino: 1.8.16 (Mac OS X), Board: “Arduino Uno WiFi Rev2, None (ATMEGA4809)”
/Users/tensor/Documents/Arduino/libraries/SparkFun_LSM6DS3_Breakout/src/SparkFunLSM6DS3.cpp: In member function ‘status_t LSM6DS3Core::beginCore()’:
/Users/tensor/Documents/Arduino/libraries/SparkFun_LSM6DS3_Breakout/src/SparkFunLSM6DS3.cpp:78:47: error: ‘MSB_FIRST’ was not declared in this scope
mySpiSettings = SPISettings(spiPortSpeed, MSB_FIRST, SPI_MODE1);
^~~~~~~~~
/Users/tensor/Documents/Arduino/libraries/SparkFun_LSM6DS3_Breakout/src/SparkFunLSM6DS3.cpp:78:47: note: suggested alternative: ‘MSBFIRST’
mySpiSettings = SPISettings(spiPortSpeed, MSB_FIRST, SPI_MODE1);
^~~~~~~~~
MSBFIRST
In file included from /Users/tensor/Documents/Arduino/libraries/SparkFun_LSM6DS3_Breakout/src/SparkFunLSM6DS3.h:31:0,
from /Users/tensor/Documents/Arduino/libraries/SparkFun_LSM6DS3_Breakout/src/SparkFunLSM6DS3.cpp:30:
/Users/tensor/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.7/libraries/Wire/src/Wire.h: In member function ‘status_t LSM6DS3Core::readRegisterRegion(uint8_t*, uint8_t, uint8_t)’:
/Users/tensor/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.7/libraries/Wire/src/Wire.h:63:12: note: candidate 1: size_t TwoWire::requestFrom(int, int)
size_t requestFrom(int, int);
^~~~~~~~~~~
/Users/tensor/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.7/libraries/Wire/src/Wire.h:61:12: note: candidate 2: virtual size_t TwoWire::requestFrom(uint8_t, size_t)
size_t requestFrom(uint8_t, size_t);
^~~~~~~~~~~
/Users/tensor/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.7/libraries/Wire/src/Wire.h: In member function ‘status_t LSM6DS3Core::readRegister(uint8_t*, uint8_t)’:
/Users/tensor/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.7/libraries/Wire/src/Wire.h:63:12: note: candidate 1: size_t TwoWire::requestFrom(int, int)
size_t requestFrom(int, int);
^~~~~~~~~~~
/Users/tensor/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.7/libraries/Wire/src/Wire.h:61:12: note: candidate 2: virtual size_t TwoWire::requestFrom(uint8_t, size_t)
size_t requestFrom(uint8_t, size_t);
^~~~~~~~~~~
exit status 1
Error compiling for board Arduino Uno WiFi Rev2.
I am also getting the same errors as you are seeing. It looks like SparkFun upgraded the library and is causing those issues. Downgrade the SparkFun LSM6DS3 Breakout library to 1.0 and you should get a clean compile. You could also check out the Using The Arduino_LSM6DS3 Library To Access The Arduino Uno WiFi Rev2 IMU tutorial which uses the newer official Arduino library.
Thank you this works!