개발자/Arduino

MMA8452Q 3축 가속도 센서 모듈 아두이노 코드 digital accelerometer

지구빵집 2018. 4. 19. 21:36
반응형




하호~ 질린다. 5개 센서들을 모조리 테스트 하고 포스팅 하려니 힘이 쭉 빠지네. 마지막 센서. 

3축 가속도 센서 MMA8452Q 센서다.


회로 연결은 아래 그림을 참고한다.




데이터북은 다음 링크를 참고한다.


MMA8452Q, 3-axis, 12-bit/8-bit digital accelerometer - https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf



SparkFun Triple Axis Accelerometer Breakout - MMA8452Q Arduino Library 라이브러리는 아래 링크를 참고한다.


https://github.com/sparkfun/SparkFun_MMA8452Q_Arduino_Library 


아래에 소스코드를 나타낸다. Sparkfun_MMA8452Q_Basic 파일 소스코드인데 안나온다. ㅠ.ㅠ



일단 센서 라이브러리는 아래 링크를 참고한다.


MMA8452Q is a tiny and low-power three-axis accelerometer

https://github.com/zamanandrew/MMA8452Q


압축파일을 풀면 폴더가 MMA8452Q-master 생긴다. 이 폴더 통채로 아두이노 프로그램이 설치된 폴더의 library 폴더 아래에 복사해 넣는다.


센서의 특징을 아래에 나타낸다. 

FEATURES

  • Normal and fast raw axes values reading
  • Scale configuration (allowed 2g, 4g and 8g scales)
  • Custom offset calibration
  • Low noise mode
  • Orientation detection (portrait/landscape and back/front)
  • Auto-WAKE/SLEEP mode and detection


처음에 소스코드를 입력하면 동작을 잘 안한다. 그래서 MMA8452Q-master 폴더 아래의  MMA8452Q.cpp 파일을 열어보니 36 라인에 #define MMA8452Q_ADDRESS 0x1D 가 있다.


우리 회로는 MMA8452Q IC의 SA0 핀을 GND 로 묶어 놓았다. 그래서 주소는 1C 가 된다. 따라서 이 부분을 아래와 같이 수정해서 컴파일하고 실행하니 잘된다.


MMA8452Q-master 폴더 아래의  MMA8452Q.cpp 파일 36line 수정


#define MMA8452Q_ADDRESS 0x1C


소스코드를 아래에 나타낸다. 심플 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <MMA8452Q.h>
#include <Wire.h> /* so the Arduino IDE auto-detects the dependency */
 
MMA8452Q accel;
 
int axes[3];
 
void setup() {
  Serial.begin(9600);
 
  if (accel.begin())
    while (1); /* error */
}
 
void loop() {
  /* get and print raw axes values */
  accel.axes(axes);
 
  Serial.print("x: ");
  Serial.print(axes[0]);
  Serial.print(", y: ");
  Serial.print(axes[1]);
  Serial.print(", z: ");
  Serial.println(axes[2]);
 
  delay(1000);
}
cs


실행시키면 아래와 같은 아름다운 결과가 나오는데, 정확한 것은 다시 분석을 해야하니 가장 심플한 예제코드를 본다고 생각하고 한다.


x: 45, y: -2021, z: -88

x: 55, y: 55, z: 992

x: 50, y: 55, z: 1003

x: 54, y: 56, z: 994

x: 57, y: 58, z: 1000

x: 184, y: 122, z: 1035

x: -1013, y: 41, z: 196

x: -1020, y: -18, z: 125

x: -1031, y: -35, z: 137

x: 953, y: -123, z: 199



example 폴더의 코드도 잘 돌아간다. 아래에 나타낸다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <MMA8452Q.h>
#include <Wire.h> /* so the Arduino IDE auto-detects the dependency */
 
MMA8452Q accel;
 
int axes[3];
uint8_t orientation;
 
void setup() {
  Serial.begin(9600);
 
  if (accel.begin())
    while (1); /* error */
 
  /* the board MUST be inactive during setup */
  accel.active(false);
 
  /* set scale to 2g (default) */
  accel.scale(2);
 
  /* disable fast read (default) */
  accel.fastRead(false);
 
  /* enable orientation detection */
  accel.detectOrientation(true);
 
  /* enable auto-sleep/wake mode */
  /* accel.autoSleep(true); */
 
  /* enable low noise mode */
  /* accel.lowNoise(true); */
 
  /* calibrate axes */
  /* accel.offset(-10, -2, 0); */
 
  /* enable the board */
  accel.active(true);
}
 
void loop() {
  /* check board current mode */
  switch (accel.sysmod()) {
    case MMA8452Q::STANDBY:
      Serial.println("StandBy");
      break;
 
    case MMA8452Q::SLEEP:
      Serial.println("Sleep");
      break;
 
    case MMA8452Q::WAKE:
      Serial.println("Wake");
      break;
  }
 
  /* get and print raw axes values */
  accel.axes(axes);
 
  Serial.print("x: ");
  Serial.print(axes[0]);
  Serial.print(", y: ");
  Serial.print(axes[1]);
  Serial.print(", z: ");
  Serial.println(axes[2]);
 
  /* get orientation status and check if it changed from last read */
  if (accel.orientation(&orientation)) {
    /* get and print portrait orientation status */
    switch (accel.portrait(orientation)) {
      case HIGH:
        Serial.println("Portrait Up");
        break;
 
      case LOW:
        Serial.println("Portrait Down");
        break;
 
      default:
        Serial.println("No Portrait");
        break;
    }
 
    /* get and print landscape orientation status */
    switch (accel.landscape(orientation)) {
      case HIGH:
        Serial.println("Landscape Right");
        break;
 
      case LOW:
        Serial.println("Landscape Left");
        break;
 
      default:
        Serial.println("No Landscape");
        break;
    }
 
    /* get and print back/front orientation status */
    if (accel.backFront(orientation))
      Serial.println("Back");
    else
      Serial.println("Front");
  }
 
  delay(1000);
}
cs

컴파일 업로드 하면 아래와 같은 아름다운 결과 화면을 씨리얼 포트를 통하여 볼 수 있다. 


Wake

x: 57, y: 44, z: 1006

Portrait Up

No Landscape

Front

Wake

x: 61, y: 50, z: 1000

Wake

x: 61, y: 53, z: 1000

Wake

x: 57, y: 54, z: 992

Wake

x: 58, y: 53, z: 986

Wake

x: 84, y: -250, z: 901

Wake

x: 50, y: -688, z: 702

Portrait Up

No Landscape

Front

Wake

x: 37, y: -715, z: 650










반응형