Build it step by step
0 of 8 doneInstall the libraries
SSD1306 and GFX.
Check the module
Confirm it takes 3.3 V.
Wire the sensor
3V3, GND and X, Y, Z to A0, A1, A2.
Link the reference
Nano 3V3 to AREF.
Wire the display and button
A4, A5 and D2.
Upload the sketch
Keep it flat for two seconds.
Tilt the board
The axes change by up to 1 g.
Shake and reset
Peaks show on the display.
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
| From | To | Why |
|---|---|---|
| ADXL335 VCC | Nano 3V3 | The chip is a 3.3 V part, never connect 5 V unless the module has a regulator |
| ADXL335 GND | Nano GND | Common ground |
| ADXL335 X | Nano A0 | X axis |
| ADXL335 Y | Nano A1 | Y axis |
| ADXL335 Z | Nano A2 | Z axis |
| Nano 3V3 | Nano AREF | Makes the readings steady against the same supply |
| OLED VCC and GND | Nano 5V and GND | Check the label on your module |
| OLED SDA and SCL | Nano A4 and A5 | I2C bus |
| Button | Nano D2 to GND | Reset 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.
| Check | Expected |
|---|---|
| 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 |
| Press the button | Peaks reset to zero |
| Upside down | Z 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 →
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.
