We have released a new product, the BerryIMU-320G, which is an inertial measurement unit, or IMU, that measures and reports on velocity, orientation, shock events and gravitational forces using a combination of two accelerometers, a gyroscope, a magnetometer and a barometric/altitude sensor.
At the heart of BerryIMU-320G is ST’s LSM6DSV320X, a smart 6-axis IMU that integrates one gyroscope and two separate accelerometers on the same die:
a low-g accelerometer for precise everyday motion sensing (tilt, orientation, vibration), and
a high-g accelerometer capable of measuring extreme impacts up to ±320 g.
This means BerryIMU-320G can capture fine movements and violent shocks with one device, without needing a separate high-g sensor. The low-g channel provides high resolution and low noise for normal motion, while the high-g channel is reserved for crashes, drops and other intense events. Because they are independent, saturating the high-g accelerometer does not disturb the low-g readings, so you don’t lose detail on everyday motion while still being able to catch rare extreme impacts.
BerryIMU-320G is also fitted with a Bosch BMP581 barometric pressure sensor which can be used to calculate altitude & temperature , and a LIS3MDL magnetometer for heading/compass measurements. Temperature readings are available from the on-board sensors.
In this guide we will show how to get the a BerryIMUv3 working with a Raspberry Pi Pico, using MicroPython. This code example supports I2C and SPI.
Hook up Guide
The two images below show how to hook up the BerryIMUv3 via I2C or SPI. (If using SPI, you will need to place a solder “blob” on JP7 on the BerryIMUv3 to complete the SPI connection, as shown here)
Raspberry Pi Pico and BerryIMv3 via SPI
Raspberry Pi Pico and BerryIMv3 via I2C
Thonny and the MicroPython code
The MicroPython code can be downloaded from our GitHub repository. The code for this example can be found under the PicoMicroPython directory.
We will be using Thonny to program the Raspberry Pi Pico.
The sample code supports both I2C and SPI communications. Comment out the protocol which will not be used as shown below. Below we have commented out I2C as we will be using SPI
import utime
import math
from LSM6DSL import *
import machine
#Comment out one of the below lines
import IMU_SPI as IMU
#import IMU_I2C as IMU
The accelerometer and gyroscope on the BerryIMUv3 can output data at a rate of 6,664 times a second! I2C is too slow to read the output at this rate, this is where SPI comes in.
Buy default, BerryIMUv3 is setup to use I2C. You can complete the SPI interface by placing a solder blob on jumper 7 (JP7).
Blob on jumper 7
SPI uses 4 pins, and depending on what device you are using these pins could be named differently, which causes confusion.
The most common pin names are;
MOSI (Master out Slave In)
MISO (Master In Slave Out
SCLK (Serial Clock)
CS (chip select) This is CE0 or CE1 on the Raspberry PI.
These pins have been highlighted below
Raspberry Pi 40 pin header
On the BerryIMUv3, they are called;
SDI (Slave Data In)
SDO (Slave Data Out)
SPC (Serial Port Clock)
CS (Chip Select)
When we connect a BerryIMUv3 to a Raspberry Pi using SPI, the Raspberry Pi will be acting as a master and the BerryIMUv3 will be acting as a slave. This is how they are connected logically.
BerryIMU and Raspberry Pi SPI
Here is the physical wiring
Raspberry Pi SPI and BerryIMUv3
You can enable SPI on the Raspberry Pi using raspi-config
pi@raspberrypi ~ $ sudo raspi-config
Go into “Interfacing Options”
Then select “SPI”
When asked if you want to enable SPI, select “yes”
The accelerometer(LSM6DSL) on the BerryIMUv3 has built in double tap detection, which makes it very easy to detect double taps without the need for any fancy code.
When the LSM6DSL detects a double tap, it can fire an interrupt pin on the BerryIMUv3. We will use a Raspberry Pi to monitor the interrupt pin and turn a LED off and on when a double-tap is detected.
Double-Tap event recognition has special registers which control tap recognition functionality, these are the tap threshold and the Shock, Quiet and Duration time windows
Double-tap event recognition
The Raspberry Pi will configure the BerryIMUv3 for double tap recognition. It will also monitor for double taps, which will be used to turn a LED on and off.
INT1 On the BerryIMUv3 will go high when a double tap is detected.
GPIO18 (physical pin 12) on the Raspberry Pi will be used to monitor INT1 , using an interrupt.
GPIO20 (physical pin 28) will be used to drive the LED.
The resister below is 330 Ohms
Here is the hock up diagrams
BerryIMU double-tap using QWIIC cableBerryIMU double-tap
import signal
from LSM6DSL import *
import sys
import RPi.GPIO as GPIO
import smbus
bus = smbus.SMBus(1)
LED_ON = 0 #Used to track of the current state of the LED
INTERRUPT_PIN = 12 #The interrupt pin which will be connected to the IMU
LED_PIN = 38 #The pin which will be driving the LED
#Used to clean up when Ctrl-c is pressed
def signal_handler(sig, frame):
GPIO.cleanup()
sys.exit(0)
#Used to write to the IMU
def writeByte(device_address,register,value):
bus.write_byte_data(device_address, register, value)
def LEDnotification(channel):
global LED_ON
if LED_ON:
GPIO.output(LED_PIN,0)
LED_ON = 0
else:
GPIO.output(LED_PIN,1)
LED_ON = 1
writeByte(LSM6DSL_ADDRESS,LSM6DSL_CTRL1_XL,0b01100000) #ODR_XL = 416 Hz, FS_XL = +/- 2 g
writeByte(LSM6DSL_ADDRESS,LSM6DSL_TAP_CFG,0b10001110) #Enable interrupts and tap detection on X, Y, Z-axis
writeByte(LSM6DSL_ADDRESS,LSM6DSL_TAP_THS_6D,0b10001100) #Set tap threshold
writeByte(LSM6DSL_ADDRESS,LSM6DSL_INT_DUR2,0b01111111) #Set Duration, Quiet and Shock time windows
writeByte(LSM6DSL_ADDRESS,LSM6DSL_WAKE_UP_THS,0b10000000) #Double-tap enabled
writeByte(LSM6DSL_ADDRESS,LSM6DSL_MD1_CFG,0b00001000) #Double-tap interrupt driven to INT1 pin
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(INTERRUPT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, 0)
GPIO.add_event_detect(INTERRUPT_PIN, GPIO.RISING, callback=LEDnotification, bouncetime=300)
while True:
signal.signal(signal.SIGINT, signal_handler)
signal.pause()
We will cover specific code which relates to double-tap recognition.
Line 37, LSM6DSL_TAP_CFG is used to enable tap recognition on the X, Y, Z directions. It is also used to enable the interrupt function for double-tap recognition.
Line 38, LSM6DSL_TAP_THS_6D is used to set the tap thresholds. a lower value will result in softer taps being detected.
Line 39, LSM6DS_INT_DUR2 is used to set the duration, quiet and shock time window. A larger duration will result in a longer time between 1st and 2nd tap.
Line 40, LSM6DSL_WAKE_UP_THS. Set the left most bit to enable double tap recognition.
Line 41, LSM6DSL_MD1_CFG is used to set which interrupt pin ont he BerryIMUv3 is used. In this instance, it is set to INT1.
BerryIMUv3 has been updated with the latest sensors, resulting in lower noise and a faster output rate (up to 6,664 times a second!).
We have also included level shifters for 5V MCUs. Which means you can safely connect the BerryIMUv3 directly to an Arduino.
BerryIMUv3 is compatible with the SparkFun QWIIC echo system.
We sell a QWIIC connector and cable for the Raspberry here. This does away with the need to solder headers onto the BerryIMUv3 when connecting to a Raspberry Pi.
Some of the features.
Gyroscope and accelerometer output rates of 6.7KHz (6,664 times a second!)
Detect tilt, tap and double tap
Pedometer, step detector and step counter
Interrupt pins
Read temperature
supports both 3.3V and 5V
I2C and SPI
“Always-on” experience with low power consumption for both accelerometer and gyroscope
In this post we will show you how to geotag and capture the “attitude” of photos taken with the Raspberry Pi camera and record these values within the photo itself using EXIF metadata
We used a modified (hacked?) cap to take the images in this post. The cap took photos, geo-tagged and recorded attitude as we walked around Sydney Harbour.
The BerryGPS-IMU was used to capture the GPS coordinates as well as “attitude”. No external antenna was needed as the BerryGPS-IMU includes an internal antenna.
The “attitude” would include values such as pitch, roll, direction. Some of this data you can see annotate in the image below.
Other programs can use some of this data to plot the image on a map and even show the direction of the camera at the time the image was taken. A good example of this is seen in GeoSetter
The Cap
The cap has the BerryGPS-IMU sitting on top of the visor, with the Raspberry Pi sitting under the viso. Some holes where made in the visor to allow connectivity between the BerryGPS-IMU and Raspberry Pi. We also created a basic camera mount out of 3mm laser cut acrylic. M2.5 Nylon screws were used to hold everything in place.
BerryIMU also works great with Windows IoT Core on the Raspberry Pi.
Our Git repository contains the source files needed to get the BerryIMU up and running on Windows IoT.
The code will print out the following values to the screen;
Raw values from the gyroscope, accelerometer and magnetometer.
Accelerometer calculated angles.
Gyro tracked angles.
Fused X and Y angles.
Heading.
Tilt compensated heading.
Connecting BerryIMU to a Raspberry Pi
BrryIMU can connect via the jumper cables to the Raspberry Pi as shown below;
Or BerryIMU can sit right on top of the GPIO pins on a Raspberry Pi. The first 6 GPIOs are used as shown below.
Get the Code
Download the BerryIMU code for Windows IoT from our GIT repository. The files you need are under the WindowsIoT-BerryIMU folder.
You will need to download the entire git repository as GIT doesn’t allow downloading individual folders.
Once downloaded, double-click the file WindowsIoT-BerryIMU.sln to open up the project in Visual Studio.
About the code
The project code outputs all of the needed values to the screen and a complementary filter is used to fuse the accelerometer and gyroscope angles.
We have a number of guides already documented on how to get the BerryIMU working with the Raspberry Pi. https://ozzmaker.com/berryimu/ These are based on Raspbian, however the principals and math are the same for Windows Iot.
The final values which should be used are the fused X &Y angles and the tilt compensated heading.
The sensor on the BerryIMU is the LSM9DS0 and all the I2C registers for this sensor can be found in LSM9DS0.cs
The main code can be found in MainPage.xaml.cs
Complementary Filter
A complementary filter is used to fuse the angles. Is summary, the complementary filter trusts the gyroscope for short periods and trusts the accelerometer for longer periods;
Changing how much trust is given for each of the sensors can be changed by modify the complementary filter constant at the start of the code.
const float AA = 0.03f; // Complementary filter constant
Loop Speed
The loop speed is important as we need to know how much time has past to calculate the rotational degrees per second on the gyroscope. A time delta is set at the start of the code.
const int DT = 100; //DT is the loop delta in milliseconds.
This is then used to specify a new timer method.
periodicTimer = new Timer(this.TimerCallback, null, 0,DT);
Here you can see where DT is used to keep track of the gyroscope angle. You can also see it in the above calculation for the complementary filter.
//Calculate the angles from the gyro
gyroXangle += rate_gyr_x * DT / 1000;
gyroYangle += rate_gyr_y * DT / 1000;
gyroZangle += rate_gyr_z * DT / 1000;
BerryIMU orientation
The calculations in the code are based on how the BerryIMU is orientated. If BerryIMU is upside down, then some of the angles need to be reversed. It is upside down when the skull logo is facing up(or to the sky). If it is upside down, set the below value to true. Otherwise, set it to false.
Our BerryIMU GIT repository has been updated with code for the Teensy, specifically Teensy 3.6. Now you can have access to an accelerometer, gyroscope, compass, temperature and pressure sensor on your Teensy.
Here you can see the angles displayed using the Serial Plotter in the Arduino IDE which is connected to a Teensy 3.6.
The latest version of Arduino IDE includes a Serial Plotter. This is great to show angles in a sliding graph.
The below image shows how to access the Serial Plotter.
The Serial Monitor will have to be closed as both cannot be opened at the same time.
The Serial Plotter expects the values to be separated with a space. To show the X and Y angles on the plotter, comment out the print statements in the existing code and insert the code below.
4 web pages are created: “/” – home page which is used to display the gauges. “/chart.html” – Is used to display chart. “/table.html” – Is used to display the data in a table. “/data.json“- Is used by the home page to update the the gauge values.
Hook Up
The below diagram shows how to connect a BerryIMU to an ESP8266 microcontroller, in this case it is the Adafruit Feather Huzzah .
The ESP8266 is another good microcontroller which can be used with the BerryIMU.
The ESP8266 is small and includes Wifi.
In this guide we will setup of the ESP8266 to provide a web page which we can then use to read the accelerometer, gyroscope and compass values from the BerryIMU. We will also force this webpage to refresh every 1 seconds.
Accessing the ESP8266 from an iPhone
The ESP8266 Arduino core to program our ESP8266. This allows you to use the Arduino IDE to program and upload to the ESP8266.
We have used the Adafruit Feather Huzzah and the Sparkfun Thing Dev board in this guide. Both are excellent boards with included USB to serial converters. Just plug in and upload. This guide can also be used with other ESP8266 boards, just take note of the pins used.
Sparkfun Thing and BerryIMUAdafruit Feather Hazzuh! and BerryIMU
The code can be found here . Download the entire Git repo. The code for this guide can be found under the directory ESP8266-BerryIMU/BerryIMU_ESP8266_simple_web/ The file you load into the Arduino IDE is BerryIMU_ESP8266_simple_web.ino.
This guide will only cover the specific to the ESP8266. There is another guide here https://ozzmaker.com/berryimu/ which covers the code used to calculate the angles and heading from the BerryIMU.
The first thing to do is update the code with your wireless network settings.
Further down you can see where we define the web server and what port to listen on
ESP8266WebServer server(80);
There is then a function called handleroot(). This is what builds the web page and sends it to the client when the client requests it E.g. When a web browser requests for a page. Looking at the line which contains the meta tag, you can see where the refresh timer is set to 1 seconds.
I have also hilighted the variables which store the angles and heading from the BerryIMU.
void handleroot()
{
//Create webpage with BerryIMU data which is updated every 1 seconds
server.sendContent("HTTP/1.1 200 OK\r\n"); //send new p\r\nage
server.sendContent("Content-Type: text/html\r\n");
server.sendContent("\r\n");
server.sendContent
("<html><head><meta http-equiv='refresh' content='1'</meta>"
"<h3 style=text-align:center;font-size:200%;color:RED;>BerryIMU and ESP8266</h3>"
"<h3 style=text-align:center;font-size:100%;>accelerometer, gyroscope, magnetometer</h3>"
"<h3 style=text-align:center;font-family:courier new;><a href=https://ozzmaker.com/ target=_blank>https://ozzmaker.com</a></h3><hr>");
server.sendContent
("<h2 style=text-align:center;> Filtered X angle= " + String(<strong><span style="color: #ff0000;">CFangleX</span></strong>));
server.sendContent
("<h2 style=text-align:center;> Filtered Y angle= " + String(<span style="color: #ff0000;"><strong>CFangleY</strong></span>));
server.sendContent
("</h2><h2 style=text-align:center;> Heading = " + String(<span style="color: #ff0000;"><strong>heading</strong></span>));
server.sendContent
("</h2><h2 style=text-align:center;> Tilt compensated heading = " + String(<strong><span style="color: #ff0000;">headingComp</span></strong>));
}
Within setup(), we define what pins are used for I2C to communicated with the BerryIMU.
Wire.begin(4,5);
The first value is the SDA pin and the second specifies the SCL pin. Any pin on the ESP8266 can be used for I2C.
Wireless is then enabled and then we try and connect to the wireless network. The IP address is then printed to the serial console.
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
//Print IP to console
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(3);
And finally, the web server is started and match on root of the web server and then run handleroot().
server.on("/", handleroot);
You need to add the below line in the main loop to handle web requests.
server.handleClient(); //Handler for client connections