Nano accelerometer project stops working

A few years back I wired an acelerometer to a buzzer making different tones depending on movement and speed.

I recently wired this project to a nano. It works and beeps to the movement for a minute or so then stops and has to be rebooted.

It is powered by 9v. The lights are still on the nano and accelerometer when it stops functioning.

I tried using the serial monitor to see if it was throwing any errors when it stops working but none.

How could I go about troubleshooting this?

I suggest to use a multimeter to measure voltages at various points and see if they are as expected.

Don't expect a 9V block battery to last long powering a nano, even if fresh.

It's actually a 9v adapter that plugs into a 12v auto cigarette lighter plug. I've also had a laptop and the 9v hooked up at the same time and doing the same thing.

could you share the code?
That would help a lot...
...mayby you are counting the millis in a variable, which isn´t declared an an unsigned long

Sounds very much like brown out.

I have a new voltage meter delivering tomorrow. For now, here is my code:

    MPU6050 Triple Axis Gyroscope & Accelerometer. Simple Accelerometer Example.
    Read more:
    (c) 2014 by Korneliusz Jarzebski

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;
#include "pitches.h"

void setup() 

  Serial.println("Initialize MPU6050");

  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");

  // If you want, you can set accelerometer offsets
  // mpu.setAccelOffsetX();
  // mpu.setAccelOffsetY();
  // mpu.setAccelOffsetZ();

void checkSettings()
  Serial.print(" * Sleep Mode:            ");
  Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled");
  Serial.print(" * Clock Source:          ");
    case MPU6050_CLOCK_KEEP_RESET:     Serial.println("Stops the clock and keeps the timing generator in reset"); break;
    case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break;
    case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break;
    case MPU6050_CLOCK_PLL_ZGYRO:      Serial.println("PLL with Z axis gyroscope reference"); break;
    case MPU6050_CLOCK_PLL_YGYRO:      Serial.println("PLL with Y axis gyroscope reference"); break;
    case MPU6050_CLOCK_PLL_XGYRO:      Serial.println("PLL with X axis gyroscope reference"); break;
    case MPU6050_CLOCK_INTERNAL_8MHZ:  Serial.println("Internal 8MHz oscillator"); break;
   Serial.print(" * Accelerometer:         ");
    case MPU6050_RANGE_16G:            Serial.println("+/- 16 g"); break;
    case MPU6050_RANGE_8G:             Serial.println("+/- 8 g"); break;
    case MPU6050_RANGE_4G:             Serial.println("+/- 4 g"); break;
    case MPU6050_RANGE_2G:             Serial.println("+/- 2 g"); break;
  Serial.print(" * Accelerometer offsets: ");
  Serial.print(" / ");
  Serial.print(" / ");

int State;
int State2;
unsigned long mark;

void loop()
  Vector rawAccel = mpu.readRawAccel();
  Vector normAccel = mpu.readNormalizeAccel();

 // Serial.print(" Xraw = ");

 // Serial.print(" Yraw = ");

 // Serial.print(" Zraw = ");


  case 0:
  if (rawAccel.XAxis <= -2300)
  { State = 1;
  mark = millis();

  case 1:
  if(millis()-mark>500) State = 2;
  if(rawAccel.XAxis >= -2299) State = 0;

  case 2:
  tone(8, NOTE_A7, 500);
  State = 0;

  case 0:
  if (rawAccel.XAxis >= 2900)
  { State2 = 1;
  mark = millis();

  case 1:
  if(millis()-mark>500) State2 = 2;
  if(rawAccel.XAxis <= 2899) State2 = 0;

  case 2:
  tone(8, NOTE_AS7, 500);
  State2 = 0;


The voltage coming in from the 9v adapter plugged into a 12v cigarette lighter plug is 9.18v.
The voltage from the nano to the accelerometer is 5.08v.

Anyone know what’s causing my program to stop working and have to reset power?

Should each state switch have it's own copy of mark?

Good point. I didn’t catch that. That needs to be corrected anyway but hopefully it will also keep things from locking up.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.