ESP8266 accelerometer and gyroscope

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
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 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");
  ("<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= target=_blank></a></h3><hr>");
  ("<h2 style=text-align:center;> Filtered X angle= " + String(<strong><span style="color: #ff0000;">CFangleX</span></strong>));
  ("<h2 style=text-align:center;> Filtered Y angle= " + String(<span style="color: #ff0000;"><strong>CFangleY</strong></span>));
  ("</h2><h2 style=text-align:center;> Heading = " + String(<span style="color: #ff0000;"><strong>heading</strong></span>));
  ("</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.


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)
  Serial.print("Connected to ");
  //Print IP to console
  Serial.print("IP address: ");

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


2 thoughts on “ESP8266 and a BerryIMU – simple web server”

  1. I’m having a hell of a time using the BerryIMUv3 with an ESP-12F. (ACEIRMC ESP8266 ESP-12 ESP-12F NodeMcu Mini D1)

    I have it connected properly via pins 4 & 5, webgui loads & everything seems to be working fine, but I get no data at all – I found by accident that when I disconnect the ground I start getting data, but it’s just these numbers with random changes that don’t seem to pertain to the motion of the IMU… Heading never changes & the 2 angles only move up or down by 0.2-ish

    Filtered X angle= -88.85
    Filtered Y angle= 90.03
    Heading = 225.00
    Tilt compensated heading = 135.01

    Any ideas? I’ve tried 2 different ESP boards with the same outcome – Verifed the IMU is working properly on raspberry pi

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.