All posts by Mark Williams

BerryIMU Code for Teensy

Our BerryIMU GIT repository has been updated with code for the Teensy, specifically Teensy 3.6.
teensy accelerometer gyroscopeNow 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.

 

 

Teensy 3.6  Hookup

teensy accelerometer gyroscope

Connect SCL on the BerryIMU to PIN 19
Connect SDA on the BerryIMU to PIN 18
Use 3.3v pin on the Teensy to power the BerryIMU.

 

 

Code

Download the BerryIMU code from our GIT repository. The files you need are under the Teensy-BerryIMU folder.

TeenysDuino
We use the Arduino IDE to program the Teensy.  You will need to install Teensduino so that your Arduino IDE supports Teensy.

You can download it from this link;
https://www.pjrc.com/teensy/td_download.html

 

BerryIMU Raspberry Pi Gyroscope Accelerometer

 

Arduino Serial plotter

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.

Arduino 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.

  Serial.print(AccXangle);
  Serial.print("\t");
  Serial.print(AccYangle);

Continue reading BerryIMU Code for Teensy

Record Temperature and Pressure with ESP8266 and BerryIMU

In this guide, we will be using the ESP8266 and BerryIMU to record temperature and pressure and display the output onto a web page.

Google charts is used to display the data in an easy to read format.

ESP8266 Record temperature
ESP8266 Record temperature

Adafruit Feather Hazzuh! and BerryIMU
Adafruit Feather Hazzuh! and BerryIMU

Components used are;

Summary

  • NTP is used to get the correct time of day.
  • The free available RAM is calculated to work out how much data we can store,
  • Google charts is used to display chart data.
  • 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 .

Adafruit Huzzah IMU
Adafruit Huzzah and BerryIMU

 

Prepare Arduino IDE

The Arduino IDE needs to be updated with the board packages for the ESP8266.  This is very easy to do and both Sparkfun & Adafruit have detailed guides on how to do this;
Sparkfun Arduino IDE and ESP8266
Adafruit Arduino IDE and ESP8266

Continue reading Record Temperature and Pressure with ESP8266 and BerryIMU

GPS Data logger using a BerryGPS

This post explains how to log GPS data from a BerryGPS or a BerryGPS-IMU and then how to plot this data onto Google Maps and many other maps E.g. OpenStreet, WorldStreet, National Maps, etc..

Raspberry Pi GPS

1. Setup GPS

Follow the instructions on this page to setup your Raspberry Pi for a BerryGPS-IMU. Ensure GPSD is set to automatically start and confirm that you can see the NMEA sentences when using gpsipe;

pi@raspberrypi ~ $ gpspipe -r

 

2.  Automatically Capture Data on Boot.

We will be using gpspipe to capture the NMEA sentence from the BerryGPS and storing these into a file. The command to use is;

pi@raspberrypi ~ $ gpspipe -r -d -l -o /home/pi/`date +"%Y%m%d-%H-%M-%S"`.nmea

-r = Output raw NMEA sentences.
-d = Causes gpspipe to run as a daemon.
-l = Causes gpspipe to sleep for ten seconds before attempting to connect to gpsd.
-o = Output to file.

The date the file is created is also added to the name.

Now we need to force the above command to run at boot. This can be done by editing the rc.local file.

pi@raspberrypi ~ $ sudo nano /etc/rc.local

 

Just before the last line, which will be 'exit 0', paste in the below line;

gpspipe -r -d -l -o /home/pi/`date +"%Y%m%d-%H-%M-%S"`.nmea

Reboot and confirm that you can see a .nmea file in the home directory.

Continue reading GPS Data logger using a BerryGPS

New Products : BerryGPS and BerryGPS-IMU

We have released two new products:

BerryGPS - GPS for the Raspberry Pi

BerryGPS-IMU - GPS and IMU for the Raspberry Pi

Both GPS modules use the  M10478-A2 from Antenova, which is a high quality GPS module which is able to track 22 satellites and has an internal antenna. This means no external antenna is needed if the module has clear access to sky.  Both feature a SuperCap to store ephemeris data for up to four hours. This and many more features are included.

Raspberry Pi GPS
BerryGPS

 

 

 

Both have been specifically designed for the Raspberry Pi Zero, however they will work with any version of Raspberry Pi.

 

The BerryGPS-IMU also includes all the components found on the BerryIMU.  And is compatible with the existing code in our repository. The BerryGPS-IMU present a lot of sensors in a very, very small package.

 

 

Raspberry Pi GPS
BerryGPS-IMU

 

 

 

Raspberry Pi GPS
BerryGPS-IMU on a Raspberry Pi Zero

 

BerryGPS-IMU
BerryGPS-IMU on Raspberry Pi 2

berrygps-skull

ESP8266 and a BerryIMU - simple web server

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 BerryIMU
Sparkfun Thing and BerryIMU
Adafruit Feather Hazzuh! and BerryIMU
Adafruit Feather Hazzuh! and BerryIMU

Hook Up

The below diagrams show how to connect a BerryIMU to an ESP8266 microcontroller, in this case the  Adafruit Feather Huzzah and the Sparkfun Thing Dev board.

 

Adafruit Huzzah IMU
Adafruit Huzzah and BerryIMU
Sparkfun Thing IMU
Sparkfun Thing Dev and BerryIMU

 

Prepare Arduino IDE

The Arduino IDE needs to be updated with the board packages for the ESP8266.  This is very easy to do and both Sparkfun & Adafruit have detailed guides on how to do this;
Sparkfun Arduino IDE and ESP8266
Adafruit Arduino IDE and ESP8266

BerryIMU Raspberry Pi Gyroscope Accelerometer

The Code

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.

const char* ssid = "******";
const char* password = "***************";

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

[wp_ad_camp_3]

Boot Raspberry Pi from USB - in Beta

The Raspberry Pi 3 can now be booted from a USB drive or from over the Network.  It is still in beta and somewhat complicated to setup.

Raspberry Pi Network Boot

 

However, once the Raspberry Foundation has ironed out some of the bugs and have made it easier to configure, I think these two features will be used more frequently, specifically booting from USB.

 

Raspberry Pi USB

 

 

Info here;
Raspberry Pi boot modes

And below;

https://www.raspberrypi.org/blog/pi-3-booting-part-i-usb-mass-storage-boot/

 

[wp_ad_camp_3]

Testing points on a Raspberry Pi

Below is a list of test points which can be found on Raspberry Pi 2, 3 and some are also on b+.

With the use of a multimeter, these test points can help with troubleshooting hardware issues.

I have yet to find any formal documentation about these test points. However, I do know they exist.

PP3 GND
PP4 GND
PP5 GND
PP6 GND
PP7 5V after polyfuse
PP8 3V3
PP9 1V8
PP10 Goes from 3V3 to 2V on brownout
PP11 DAC_2V5 (for composite video DAC)
PP12 AUD_2V5 (for PWM audio drivers)
PP13 Goes from 3V3 to 2V on ACT activity
PP14 SD_CLK
PP15 SD_CMD
PP16 SD_DAT0
PP17 SD_DAT1
PP18 SD_DAT2
PP19 SD_DAT13
PP20 H5V
PP21 RUN signal (reset)
PP22 Goes from 3V3 to 2V on activity of green (link) ethernet jack LED
PP23 Goes from 3V3 to 2V on activity of yellow (speed) ethernet jack LED
PP24 COMPVID
PP25 AUDIO_L
PP26 AUDI_R
PP27 VBUS (USB 5V power)
PP28 ETH_CLK (25.000 MHz)
PP29 VC_TMS
PP30 VC_TRST_N
PP31 VC_CLK
PP32 VC_TDI
PP33 VC_TDO
PP34 GND
PP35 GPIO6 of LAN9514
PP36 GPIO7 of LAN9514
PP37 CAM_GPIO0
PP38 CAM_GPIO1
PP39 SCL0
PP40 SDA0

 

Below is an example of how you can use PP9 to confirm that  the regulator is supplying 1.8v correctly.

Ground can be sourced from the SD card slot;Pi3TestPointsProbs-1000

And the exact location of PP9;

Raspberry Pi test point 9

 

Adafruit has some great information covering the Raspberry Pi power circuitry. link

 

 

How to Check the Software and Hardware Version of a Raspberry Pi

There are a number of commands which can be used to check the hardware and software versions on a Raspberry Pi.

Version of Debian;

cat /etc/debian_version can be used to see what version of Debian you are running.

pi@raspberrypi ~ $ cat /etc/debian_version
7.8

2015-05-05-raspbian-wheezy

pi@raspberrypi ~ $ cat /etc/debian_version
8.0

2016-02-03-raspbian-jessie

OS Release Notes;

cat /etc/os-release can be used to see OS release notes

pi@raspberrypi ~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 7 (wheezy)"
NAME="Raspbian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
ID=raspbian
ID_LIKE=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

2015-05-05-raspbian-wheezy

pi@raspberrypi ~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

2016-02-03-raspbian-jessie

Kernel Version;

uname -a can be used to see what kernel version is running

pi@raspberrypi ~ $ uname -a
Linux raspberrypi 3.18.11-v7+ #781 SMP PREEMPT Tue Apr 21 18:07:59 BST 2015 armv7l GNU/Linux

2015-05-05-raspbian-wheezy

pi@raspberrypi ~ $ uname -a
Linux raspberrypi 4.1.19-v7+ #858 SMP Tue Mar 15 15:56:00 GMT 2016 armv7l GNU/Linux

2016-02-03-raspbian-jessie

 

BerryIMU Raspberry Pi Gyroscope Accelerometer

To check the hardware version;

cat /proc/cpuinfo can be used to see what hardware you are using. Take note of the revision number in the second last line and then refer to the table below. The output below is from a Pi 2

pi@raspberrypi ~ $ cat /proc/cpuinfo
processor : 0
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5processor : 1
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5processor : 2
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5processor : 3
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5Hardware : BCM2709
Revision : a21041
Serial : 00000000c15e9432
pi@raspberrypi ~ $

 

 

Model and Pi Revision256MBHardware Revision Code from cpuinfo
Model B Revision 1.0256MB0002
Model B Revision 1.0 + ECN0001 (no fuses, D14 removed)256MB0003
Model B Revision 2.0
Mounting holes
256MB0004
0005
0006
Model A
Mounting holes
256MB0007
0008
0009
Model B Revision 2.0
Mounting holes
512MB000d
000e
000f
Model B+512MB0010
Compute Module512MB0011
Model A+256MB0012
Pi 2 Model B1GBa01041 (Sony, UK)
a21041 (Embest, China)
PiZero512MB900092(no camera connector)
900093(camera connector)
Pi 3 Model B1GBa02082 (Sony, UK)
a22082 (Embest, China)
PiZero W512MB9000c1

##UPDATED##
The completed list can now be found here  https://elinux.org/RPi_HardwareHistory

Converting values from an Accelerometer to G

In this post I will show how to convert the raw values read from an accelerometer to 'Gs'.

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone https://github.com/ozzmaker/BerryIMU.git

The code for this guide can be found under the python-BerryIMU-measure-G directory.

 

An accelerometer measures proper acceleration, which is the acceleration it experiences relative to freefall. This is most commonly called "G-Force" (G)

For example, an accelerometer at resting on a table would measure 1G ( 9.81 m/s2) straight upwards. By contrast, accelerometers in free fall and accelerating due to the gravity of Earth will measure zero.

The accelerometer used by the BerryIMU is a MEMS sensors(LSM9DS0), which outputs the raw readings as mg/LSB.
Most MEMS accelerometers use this output format.

mg = milli-G's (just like milliliters)
1mG = 0.001 G's of acceleration, so 1000mG = 1G.
LSB = Least Significant bit, which is the last bit on the right.

BerryIMU Raspberry Pi Gyroscope Accelerometer

The LSM9DS0 outputs a 16 bit value for the accelerometer readings.

If you look at the characteristics of the LSM9DS0 in the datasheet, you can see the sensitivity levels for the accelerometer highlighted in red below and the corresponding values for the LSB, which are highlighted in blue. You can download the datasheet here;

The raw values from the accelerometer are  multiplied by the sensitive level to get the value in G.

LSM9DS1 BerryIMU

Let's use FS ±2 g as an example sensitivity level.  As the range is -2 to +2, this would be a total of 4g.  Or 4,000 Milli-Gs.
The output is 16 bits. 16 bits equals 65,535.   This means we can get 65,535 different readings for the range  between -2 and +2. (or -2,000 MilliGs and +2,000 MilliGs)

 4,000 MilliGs / 65,535 = 0.061

Each time the LSB changes by one, the value changes by 0.061, which is the value highlighted in blue in the table above.

For FS ±8 g, the range would be -8 to +8, which is a total of 16,000 MilliGs.
 16,000 MilliGs / 65,535 = 0.244

Example when using ±2g sensitivity
In the table below, every time the raw values increments by one, the final calculated value(which is MilliG) increments by 0.061

RAW  	BINARY	LSB value for +/-2G	Calc MilliG
16	10000		0.061		0.976
17	10001		0.061		1.037
18	10010		0.061		1.098

The above values of 16,17 and 18 above a very low and only used for illustration.
If your accelerometer is horizontal and resting and at rest when using a sensitive level of ±2g, the raw value for Z should hover  around 16,500.
16,500 X 0.061 = 1006 MilliGs or 1G

Example when using ±8g sensitivity
In the table below, every time the raw values increments by one, the final calculated value(which is MilliG) increments by 0.244

RAW  	BINARY	LSB value for +/-2G	Calc MilliG
16	10000		0.244		3.904
17	10001		0.244		4.148
18	10010		0.244		4.392

If you accelerometer is horizontal and at rest, when using a sensitive level of ±8g, the raw value for Z should hover  around 4,475.

4,175
 X 0.244 = 1018.7 MilliGs or 1G

 

The Code

The two above examples are easy to implement in python;
±8g Sensitivity

writeACC(CTRL_REG2_XM, 0b00010000) #+/- 8G full scale
print("G Value for Z axis %f G" % ((ACCz * 0.244)/1000))

The first line above is used to initialise the accelerometer with a sensitivity level of ±2g.
The second line prints the calculated value as G using the raw values from the accelerometer.

±2g Sensitivity

writeACC(CTRL_REG2_XM, 0b00000000) #+/- 2G full scale
print("G Value for Z axis %f G" % ((ACCz * 0.061)/1000))

The first line above is used to initialise the accelerometer with a sensitivity level of ±2g.
The second line prints the calculated value as G uses using raw values from the accelerometer.
Below is a snippet from the main program;

import smbus
import time
import math
from LSM9DS0 import *
import datetime
bus = smbus.SMBus(1)


def writeACC(register,value):
        bus.write_byte_data(ACC_ADDRESS , register, value)
        return -1


def readACCx():
        acc_l = bus.read_byte_data(ACC_ADDRESS, OUT_X_L_A)
        acc_h = bus.read_byte_data(ACC_ADDRESS, OUT_X_H_A)
	acc_combined = (acc_l | acc_h <<8)
	return acc_combined  if acc_combined < 32768 else acc_combined - 65536

def readACCy():
        acc_l = bus.read_byte_data(ACC_ADDRESS, OUT_Y_L_A)
        acc_h = bus.read_byte_data(ACC_ADDRESS, OUT_Y_H_A)
	acc_combined = (acc_l | acc_h <<8)
	return acc_combined  if acc_combined < 32768 else acc_combined - 65536

def readACCz():
        acc_l = bus.read_byte_data(ACC_ADDRESS, OUT_Z_L_A)
        acc_h = bus.read_byte_data(ACC_ADDRESS, OUT_Z_H_A)
	acc_combined = (acc_l | acc_h <<8)
	return acc_combined  if acc_combined < 32768 else acc_combined - 65536


	
#initialise the accelerometer
writeACC(CTRL_REG1_XM, 0b01100111) #z,y,x axis enabled, continuos update,  100Hz data rate
writeACC(CTRL_REG2_XM, 0b00011000) #+/- 8G full scale

while True:
	
	
	#Read the accelerometer,gyroscope and magnetometer values
	ACCx = readACCx()
	ACCy = readACCy()
	ACCz = readACCz()
	print("##### X = %f G  #####" % ((ACCx * 0.244)/1000)),
	print(" Y =   %fG  #####" % ((ACCy * 0.244)/1000)),
	print(" Z =  %fG  #####" % ((ACCz * 0.244)/1000))
	
	
	#slow program down a bit, makes the output more readable
	time.sleep(0.03)

 

Guides and Tutorials

Raspberry Pi Digital Spirit Level

 

In this post we show how to create a Digital Spirit Level using a Raspberry Pi and python.

The code moves that bubbles on the display in relation to the angle read from the IMU.
Parts used in this project;

Any IMU or TFT can be used, however the code would need to be updated to accommodate the different devices. It is best to use a 480x320 TFT as the images are scaled to fit this resolution.

This guide assumes that some basic understanding of an IMU(Accelerometer and Gyroscope)  is already known. And you have one already working with your Raspberry Pi.

If you don't, we do have some guides which covers this.

BerryIMU Raspberry Pi Gyroscope Accelerometer

 

We have used our existing python code to read the values from the IMU, however we have removed the code related to the magnetometer as it isn't needed for this project.

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone http://github.com/ozzmaker/BerryIMU.git

 

Placement of IMU

The IMU can be attached anywhere, however it is best to place it in the same orientation as shown below. If you do change the orientation, you will need to update the code accordingly.

spiritLevel1-600

Continue reading Raspberry Pi Digital Spirit Level