Feed on
Posts
Comments

I’ve been meaning to play around with accelerometers and found the MMA7361 on Ebay for $3.

IMG_2972

I checked if there was a MMA7361 library for the Arduino which there was so I tried it out but I kept receiving the same readings even if I disconnected the accelerometer from the Arduino – maybe I was doing something wrong. I briefly looked at the code and it seems they were doing a lot but the pin out of the MMA7361 made me believe it should be easy. Edit: I found there is a working example in Arduino > Sensors > ADXL3xx

mma1

I looked up the datasheet and found it’s pretty straight forward if you just want to read the x, y, z axis, a simple ADC reading of each pin should be all that we need. There is a sleep pin (SL) which puts it to sleep which makes it only draw 3uA. Some other pins include the 0g pin which goes high when all axis’s are at 0g, a self test pin (ST) and a g select pin (GS) that allows you to choose 1.5g or 6g sensitivity.

With some other accelerometers I believe you can have it pull a pin high if it detects movement but the one I got it doesn’t have that functionality – it would be very useful for battery powered designs. One thing to note is that 0g is about half of the VCC. The Z axis is only half VCC when the board vertical.

int xPin = A0;
int yPin = A1;
int zPin = A2;

int oldx = 0;
int oldy = 0;
int oldz = 0;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  int sensor0Value = analogRead(xPin);
  int sensor1Value = analogRead(yPin);
  int sensor2Value = analogRead(zPin);
  int moved = false;

  Serial.print("x = ");
  Serial.print(sensor0Value);
  if (moved == false && (sensor0Value - oldx > 100 || oldx - sensor0Value > 100)) {
    Serial.print("MOVED");
    moved = true;
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
  }

  ...

  oldx = sensor0Value;
  oldy = sensor1Value;
  oldz = sensor2Value;

  delay(50);
}

https://www.youtube.com/watch?v=D9VG7vnZ8Io

Here’s the simple Arduino sketch I used to read the axis and if there was a difference of 100 in the reading, then it says it’s been moved. VCC should be 3.3V and the sleep pin should be pulled high. I thought about what I could do with this and wanted something simple.

IMG_2969

Originally I was gluing all the leds together but it was a little too loose, so I grabbed a small box, drilled out the led holes 6 on each side with half red and half green and glued/soldered them in place.

I’ll put in an ATtiny25/45/85 with a 3V battery and it should be all good to go. I found that the MMA7361 module when in sleep mode still consumed about 1mA versus 1.5mA when it’s active. We can calculate how much g we read since 0.1g is 80mV assuming 800mV/g typical. Running from a 3V battery for 0.1g it’s 0.08V / 0.0029296875 = 27.3.

IMG_2973

// Read the initial X, Y, Z axis
int x_result = adcRead(x_axis);
...

// Re-read X, Y, Z axis for 500ms
int highest_value = 0;
for (int x = 0; x < 1000; x++) {
  int x_change = adcRead(x_axis) - x_result;
  ...
  // Convert negative to positive value
  if (x_change < 0) {
    x_change = x_change - (x_change * 2);
  }
  ...

  // If there was a 54 result difference (0.2g or more), find out the which axis had the most change
  // 0.1g is 80mV assuming 800mV/g typical. With a 3V battery, 0.16V / 0.0029296875 = 54.64
  if (x_change > 54 || y_change > 54 || z_change > 54) {
    if (x_change > highest_value) {
      highest_value = x_change;
    }
    ...
  }
}

// For every 0.1g change blink the LEDs
for (int x = 0; x < (highest_value / 27); x++) {
  PORTB |= (1<<led);
  watchdogSleep(T128MS);
  PORTB &= ~(1<<led);
  watchdogSleep(T128MS);
}

// Sleep for a bit
watchdogSleep(T128MS);

On the ATtiny I’m just reading the X, Y, Z axis for about 500ms and then finding out which axis had the largest g change and blinking the LEDs once for each g. Download Acceler_Box_v0.1

https://www.youtube.com/watch?v=SClwYg1DaPY

Putting all this together in the small box took a lot longer than I thought it would but it works!

One Response to “Playing around with Accelerometers and a small project”

  1. MikeK says:

    Great use of the dental floss container.

Leave a Reply