Projects / Three-axis g meter

Three-axis g meter

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO11.3 mASSD1306

    Install the libraries

    SSD1306 and GFX.

  2. NANO

    Check the module

    Confirm it takes 3.3 V.

  3. NANO

    Wire the sensor

    3V3, GND and X, Y, Z to A0, A1, A2.

  4. NANO

    Link the reference

    Nano 3V3 to AREF.

  5. NANO

    Wire the display and button

    A4, A5 and D2.

  6. NANO

    Upload the sketch

    Keep it flat for two seconds.

  7. NANO

    Tilt the board

    The axes change by up to 1 g.

  8. NANO

    Shake and reset

    Peaks show on the display.

In short

How do you build a three-axis g meter?

An analogue accelerometer shows acceleration on three axes in g and keeps the peak value of each. You need Arduino Nano, ADXL335 analog accelerometer, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 8 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ADXL335 VCCNano 3V3The chip is a 3.3 V part, never connect 5 V unless the module has a regulator
ADXL335 GNDNano GNDCommon ground
ADXL335 XNano A0X axis
ADXL335 YNano A1Y axis
ADXL335 ZNano A2Z axis
Nano 3V3Nano AREFMakes the readings steady against the same supply
OLED VCC and GNDNano 5V and GNDCheck the label on your module
OLED SDA and SCLNano A4 and A5I2C bus
ButtonNano D2 to GNDReset the peaks

Code

The ADXL335 gives three analogue voltages, one for each axis, that rise and fall with acceleration. At rest the axis pointing up reads one g because of gravity, so the board is also a tilt sensor. The chip runs on 3.3 volts. A bare chip must never see 5 volts, so power the board from the 3V3 pin, and if your module has a regulator and says it takes 5 volts, check the label before using it. Wiring the 3.3 volt pin to AREF and using the external reference makes the readings scale with the sensor supply, which keeps them steady. The sketch calibrates itself for two seconds at start, assuming the board is lying flat, then shows each axis in g with its peak value, which the button resets. The sensitivity is only roughly 330 millivolts per g, so for better accuracy do a flip test: measure the reading with an axis up and then down, take half of the difference and put that number in the sketch. Shake it gently to see small values and avoid dropping it.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int AX[3] = {A0, A1, A2};
const float COUNTS_PER_G = 102.0;      // about 330 mV per g at 3.3 V; tune it with the flip test
float offset[3];
float peak[3] = {0, 0, 0};

int readAvg(int pin) {
  long s = 0;
  for (int i = 0; i < 16; i++) { s += analogRead(pin); delay(1); }
  return s / 16;
}

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  analogReference(EXTERNAL);            // AREF is wired to 3.3 V
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println("OLED not found"); while (1) { } }
  oled.setTextColor(SSD1306_WHITE);

  oled.clearDisplay(); oled.setCursor(0, 0); oled.println("Keep it flat"); oled.println("Calibrating"); oled.display();
  delay(2000);
  offset[0] = readAvg(AX[0]);
  offset[1] = readAvg(AX[1]);
  offset[2] = readAvg(AX[2]) - COUNTS_PER_G;   // Z reads +1 g when it lies flat
}

void loop() {
  if (digitalRead(2) == LOW) { peak[0] = peak[1] = peak[2] = 0; }

  float g[3];
  for (int i = 0; i < 3; i++) {
    g[i] = (readAvg(AX[i]) - offset[i]) / COUNTS_PER_G;
    if (fabs(g[i]) > fabs(peak[i])) peak[i] = g[i];
  }

  oled.clearDisplay();
  oled.setTextSize(1);
  const char* names[3] = {"X", "Y", "Z"};
  for (int i = 0; i < 3; i++) {
    oled.setCursor(0, i * 14);
    oled.print(names[i]); oled.print(" "); oled.print(g[i], 2);
    oled.print("  pk "); oled.print(peak[i], 2);
  }
  oled.display();
  Serial.print(g[0], 2); Serial.print(","); Serial.print(g[1], 2); Serial.print(","); Serial.println(g[2], 2);
  delay(50);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

0.01,0.00,0.99
0.00,0.00,1.00
0.00,0.01,1.00
0.00,0.01,0.99
0.00,0.00,1.00
0.01,0.00,1.00

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Flat and stillX and Y near 0, Z near 1.00
Tilt onto its sideThat axis reads near 1.00
Tap the tableBrief peaks above 1 g
Press the buttonPeaks reset to zero
Upside downZ reads near minus 1.00

If it does not work

Readings off by a lot

Check that 3V3 is wired to AREF.

Always zero

Check the supply and the axis wires.

Z not 1 g

Fix COUNTS_PER_G with the flip test.

Noisy numbers

Keep the sensor wires short and away from the display.

Chip very hot

5 V on a bare chip, disconnect it at once.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the three-axis g meter?

Arduino Nano, ADXL335 analog accelerometer, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points.

How long does the three-axis g meter take to build?

About 90 minutes for a beginner maker, in 8 steps.

How do I know the three-axis g meter works?

Flat and still: X and Y near 0, Z near 1.00. Tilt onto its side: That axis reads near 1.00. Tap the table: Brief peaks above 1 g.

Readings off by a lot: what should I check?

Check that 3V3 is wired to AREF.

Always zero: what should I check?

Check the supply and the axis wires.

Z not 1 g: what should I check?

Fix COUNTS_PER_G with the flip test.

Noisy numbers: what should I check?

Keep the sensor wires short and away from the display.