In this blog we study the behavior of the IMU (Inertial Measurement Unit) built into the Arduino Nano 33 IoT board. For the project we intend to see the possibility of using the IMU to estimate velocity, magnitude and direction, and to estimate the position of an object on which the IMU travels. To encourage the students we have proposed the creation of a simple game that, on the one hand, needs to calculate the estimates of velocity and position, understand the effect of friction and the sampling frequency and, on the other hand, analyze what type of screen we can use for our CO2 monitor and alternatives to visualize data that allow us to debug applications in a disconnected way.
It has been a week of a lot of coding and we have learned to use the libraries that will allow us to read the data from the IMU and present the data on OLED displays and TFT LCD displays. In a future blog we will describe the code of the game and the repository to download it.
Tracking System for Classroom Ventilation Routines
A STEM project for classrooms
the VenTTracker project - Blog #04 - Playing with the IMU
The Game
To get familiar with the LSM6DS6 IMU included in the Arduino NANO 33 IoT development board, we are going to implement a small maze game managed with the IMU.
The object of the game is to drive the ball through the randomly generated maze to eat all the balls. It is a game with time, if the player manages to eat all the cookies in time then he scores and goes to the next challenge where a new random maze is generated. If he does not manage to eat all the balls in time, he loses all the accumulated score and is offered to start over. The game requires coordination between the player's eyes and hands.
The device has no buttons and only communicates with the human with the accelerometer. The game uses the three-axis accelerometer for motion input.
As the game is a 2D-game and it is only interested in acceleration changes in X and Y axis to move the ball through the maze. The playfield offers resistance to the ball in the form of friction that slows it down.
The hardware 1.8 "SPI TFT LCD
- Arduino NANO 33 IoT board
- 1.8 "SPI TFT LCD Screen Module, 9 pins interface. The LCD drive ic is ST7735S. It's a 128 X 160 (resolution).
{gallery} The Maze Game |
---|
Maze Game Screenshot 01: Maze Game Screenshot 01 |
Maze Game Screenshot 02: Maze Game Screenshot 02 |
Maze Game Screenshot 03: Game Over |
How to play
- Tilt Forward – accelerates the big ball to the back of the playfield.
- Tilt Backward – accelerates the big ball to the front of the playfield.
- Tilt Right – accelerates the big ball to the left of the playfield.
- Tilt Left – accelerates the big ball to the right of the playfield.
Alternative hardware 0.91 inch OLED I2C Display 128 x 32 Pixels
- Arduino NANO 33 IoT board
- 0.91 inch OLED I2C Display 128 x 32 Pixels.This is a small display, only about 1" diagonal
Trying the game with 0.91 inch OLED I2C Display 128 x 32 Pixels
The board and the LSM6DS3 IMU
- The board's main processor is a low power Arm Cortex-M0 32-bit SAMD21.
- The WiFi and Bluetooth connectivity is performed with a module from u-blox, the NINA-W10, a low power chipset operating in the 2.4GHz range.
- On top of those, secure communication is ensured through the Microchip ECC608 crypto chip.
- And a 6 axis IMU, LSM6DS3
Product website: https://store.arduino.cc/arduino-nano-33-iot-with-headers
{gallery} The board and the LSM6DS3 IMU |
---|
ARDUINO NANO 33 IOT - TOP VIEW DRAWING: LSM6DS3 is situated near the reset button |
ARDUINO NANO 33 IOT - TOP VIEW: LSM6DS3 is situated near the reset button |
ARDUINO NANO 33 IOT - BOTTOM VIEW: ARDUINO NANO 33 IOT - BOTTOM VIEW |
ARDUINO NANO 33 IOT LSM6DS3 CLOSE UP: ARDUINO NANO 33 IOT LSM6DS3 CLOSE UP |
Arduino NANO 33 IoT - LSM6DS3 - Close Up 2: Arduino NANO 33 IoT - LSM6DS3 - Close Up 2 |
Arduino NANO 33 IoT - LSM6DS3 - Close Up 2: Arduino NANO 33 IoT - LSM6DS3 - Close Up 2 |
Orientation of the sensor on the board
LSM6DS3TRLSM6DS3TR in the Arduino NANO 33 IoT
Accelerometer
An acceleration sensor measures the acceleration applied to the device, including the force of gravity. The accelerometer returns acceleration force data for the three coordinate axes.
Motion sensors are useful for monitoring device movement, such as tilt, shake, rotation, or swing. The movement is usually a reflection of direct user input (for example, a user steering a car in a game or a user controlling a ball in a game), but it can also be a reflection of the physical environment in which the device is sitting.
Motion sensors by themselves are not typically used to monitor device position but we are going to try
The linear acceleration sensor always has an offset, which we need to remove. The simplest way to do this is to build a calibration step into our application. During calibration we can ask the user to set the device on a table, and then read the offsets for all three axes. We can then subtract that offset from the acceleration sensor's direct readings to get the actual linear acceleration.
LSM6DS3TRLSM6DS3TR
The LSM6DS3 is a system-in-package featuring a 3D digital accelerometer and a 3D digital gyroscope performing at 1.25 mA (up to 1.6 kHz ODR) in high-performance mode and enabling always-on low-power features.The LSM6DS3 has a full-scale acceleration range of ±2/±4/±8/±16 g and an angular rate range of ±125/±250/±500/±1000/±2000 dps.
- Power consumption: 0.9 mA in combo normal mode and 1.25 mA in combo high-performance mode up to 1.6 kHz.
- “Always-on” experience with low power consumption for both accelerometer and gyroscope
- Smart FIFO up to 8 kbyte based on features set
- Hard, soft ironing for external magnetic sensor corrections
- Analog supply voltage: 1.71 V to 3.6 V
- Independent IOs supply (1.62 V)
- Embedded temperature sensor
- Size: 2.5 mm x 3 mm x 0.83 mm
The LSM6DS3 is a system-in-package featuring a 3D digital accelerometer and a 3D digital gyroscope performing at 1.25 mA (up to 1.6 kHz ODR) in high-performance mode and enabling always-on low-power features.The LSM6DS3 has a full-scale acceleration range of ±2/±4/±8/±16 g and an angular rate range of ±125/±250/±500/±1000/±2000 dps.
INT1 and INT2 are not wired. In the diagram of the board we see that neither the INT1 nor the INT2 pin are connected, so we will not be able to use interruptions to wake up our device and we will have to continually poll it.
The IMU is a LSM6DS3, it is a 3-axis accelerometer and 3-axis gyroscope; it is connected to the microcontroller through I2C on the NANO board.
The LSM6DS3 on the Arduino Nano 33 IoT can be use easily through the I2C bus on the slave address 0x6A.
Arduino NANO 33 IoT Pinouts
For the Arduino Nano 33 IoT, the pins assignments are described in:
https://github.com/arduino/ArduinoCore-samd/blob/master/variants/nano_33_iot/variant.cpp
https://github.com/arduino/ArduinoCore-samd/blob/master/variants/nano_33_iot/variant.h
{gallery} LSM6DS3 Datasheet |
---|
IMU DATASHEET: |
IMU DATASHEET: |
IMU DATASHEET: |
Software Libraries for the LSM6DS3 IMU
We are going to analyze two libraries for the IMU LSM6DS3,
- the official Arduino library
- Sparkfun library
The official Arduino library is a very simple library that will allow us to maintain some compatibility with other IMUs placed on other Arduino boards.
The Sparkfun library allows finer control of IMU programming and access to all IMU registers.
LSM6DS3 Arduino Library
The library takes care of the sensor initialisation and sets its values as follows:
- Accelerometer range is set at -4 |+4 g with -/+0.122 mg resolution
- Gyroscope range is set at -2000 | +2000 dps with +/-70 mdps resolution
- Output data rate is fixed at 104 Hz
The LSM6DS3 is a system-in-package featuring a 3D digital accelerometer and a 3D digital gyroscope.
- Install the LSM6DS3 Arduino Library with the Library Manager available in the Arduino Software (IDE) .
- Tools / Manage Libraries
- For more info on the library go to https://www.arduino.cc/en/Reference/Arduino_LSM6DS3
- Lets try an example and check also serial port communication through the USB port
- Select the SimpleGyroscope sample
- Upload and open the Serial Monitor: Tools/Serial Monitor Ctrl-***-M
Reading data in no movement condition
Functions of the API used in the game
accelerationAvailable() : Query if new acceleration IMU data is available.
Syntax: IMU.accelerationAvailable()
Parameters: None
Returns: 0 if no new acceleration IMU data is available, 1 if new acceleration IMU data is available.
readAcceleration() : Query the IMU accelerometer and return the acceleration in g
Syntax: IMU.readAcceleration(x,y,z)
Parameters: x,y,z - the float variables where the acceleration values will be stored
Returns: 1 on success, 0 on failure
begin() : Initialize the IMU.
Syntax : IMU.begin()
Parameters: None
Returns : 1 on success, 0 on failure
end(): De-initialize the IMU.
Syntax: IMU.end()
Parameters: None
Returns: Nothing
accelerationSampleRate(): Return the accelerometer sample rate
Syntax: IMU.accelerationSampleRate()
Parameters: None
Returns: The Accelerometer sensor sample rate Hz
The Sparkfun LSM6DS3 library
To install the official library in the Arduino IDE, go in the menu Tools -> Manage Libraries... In the library manager, search for LSM6DS3 and install the SparkFun LSM6DS3 Breakout by SparkFun Electronics.
We have to change the I2C address to 0x6A (by default, the SparkFun module address is 0x6B). LSM6DS3 myIMU(I2C_MODE, 0x6A);
https://github.com/sparkfun/SparkFun_LSM6DS3_Arduino_Library
The Sparkfun library allows finer control of IMU programming and access to all IMU registers. We will use it in tests to see how we can take advantage of ML techniques in our project.
Selecting Displays
Our objective is to have a graphical user interface for:
- The environmental monitor
- Debugging when unplugged.
For the environmental monitor we need a display capable of showing data such as the image
And for debugging, we need a screen that is small and that allows us to update the data quickly.
0.91 inch OLED I2C Display 128 x 32 Pixels for debugging
The game uses a 0.91 inch OLED I2C Display 128 x 32 Pixels.This is a small display, only about 1" diagonal. It is made of 128x32 individual white OLED pixels, each one is turned on or off by the controller chip. No backlight is required. This reduces the power required to run the OLED.
The module we’re using here has only four pins and communicates with the Arduino using I2C communication protocol. This model has no RESET pin.
We just need to connect to the Arduino Nano: SDA (A4); SCL (A5);To control the OLED display you need the adafruit_SSD1306.h and the adafruit_GFX.h
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
{gallery} 0.91 inch OLED I2C Display 128 x 32 Pixels. |
---|
0.91 inch OLED I2C Display 128 x 32 Pixels: 0.91 inch OLED I2C Display 128 x 32 Pixels Top View |
0.91 inch OLED I2C Display 128 x 32 Pixels:0.91 inch OLED I2C Display 128 x 32 Pixels Bottom view |
0.91 inch OLED I2C Display 128 x 32 Pixels:Breadboard |
Power consumption
Power consumption with display | Power consumption without display |
---|---|
1.8 TFT SPI 128x160 v1.1 for environmental monitor
Feature
It is a 1.8 "SPI TFT LCD Screen Module, 9pins interface. Not just a LCD break but include SD card (2GB),
The LCD drive ic is ST7735S. It's a 128 X 160 (resolution). The LCD has a wide viewing angle, the contrast is also very suitable.
The display interface is serial, it just needs 5 wires (CS, RS, SCL, SDA, RST) for controlling.
SDcard use hardware SPI interface (CS / MOSI / MISO / SCK), Not solder pins.
Pinout
LED 3.3V IO and Power Supply pin
LED UTFT Support
SCL Serial clock input
SDA/SDI Serial data input
DC:Data/Command selection
RST:Reset Low level active
CS:Chip Selection,Low level active
GND:Ground
VDD33:3.3V Power Supply pin
ST7735S 132RGB x 162 dot 262K Color with Frame Memory Single-Chip TFT Controller/Driver
The ST7735S is a single-chip controller/driver for 262K-color, graphic type TFT-LCD. It consists of 396 source line and 162 gate line driving circuits.
This chip is capable of connecting directly to an external microprocessor, and accepts Serial Peripheral Interface (SPI), 8-bit/9-bit/16-bit/18-bit parallel interface.
Display data can be stored in the on-chip display data RAM of 132 x 162 x 18 bits
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 160 // OLED display height, in pixels
#define TFT_CS 10
#define TFT_RST -1 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC 8
Adafruit_ST7735 display(TFT_CS, TFT_DC, TFT_RST);
{gallery} 1.8 TFT SPI 128x160 |
---|
1.8 TFT SPI 128x160 : Top view |
1.8 TFT SPI 128x160 : Bottom view |
1.8 TFT SPI 128x160 : Dimensions |
1.8 TFT SPI 128x160 : Breadboard |
1.8 TFT SPI 128x160 : Connected to the Arduino Nano 33 IoT |
Summary and conclusions
The ST7735 TFT uses SPI to communicate. It is very slow for a graphics game.
We can't use double buffer technique. For an ST7735 (128 x 160 x 16 bit color) would require the transmission of 327,680 bits.
It has been very difficult to remove the flicker but we have learned to update the screen by small regions by creating and loading small bitmaps.
<< Previous VenTTracker Blog | Next VenTTracker Blog >> |
---|---|
VenTTracker #03 - Analyzing window types II | VenTTracker #05 - Sliding Window Mock-up |