I2C Module List

main
yikth 5 years ago
commit 42aa54f73c

@ -0,0 +1,23 @@
# I2C Component
* I2C Modules
| Item | Board | Chipset | Address |
| --------------------------------------- | ---------- | ----------- | --------------------------------------------- |
| 3-Axis Magnetic Electronic Compass | GY271 | HMC5883L | 0x1E |
| 3-Axis Magnetometer Acceleration Sensor | GY-LSM303C | LSM 303C | accelerometer : 0x1D, magnetometer : 0x1E |
| Infrared Temperature Sensor | GY-906 | MLX90614ESF | 0x5A |
* Datasheet
+ [HMC5883L](https://cdn-shop.adafruit.com/datasheets/HMC5883L_3-Axis_Digital_Compass_IC.pdf)
+ [LSM303C](https://www.mouser.com/datasheet/2/389/lsm303agr-954987.pdf)
+ [MLX90614ESF](https://www.melexis.com/-/media/files/documents/datasheets/mlx90614-datasheet-melexis.pdf)
* Arduino Library
+ [GY271](.\arduino-lib\Mecha_QMC5883L)
+ [GY-906](https://github.com/adafruit/Adafruit-MLX90614-Library)
* Tutorial
+ [GY271](https://www.instructables.com/id/Tutorial-to-Interface-HMC-5833L-With-Arduino-Uno/)
+ [GY-LSM303C](https://learn.adafruit.com/lsm303-accelerometer-slash-compass-breakout/coding)
+ [GY-906](https://create.arduino.cc/projecthub/SurtrTech/contactless-temperature-sensor-mlx90614-1e7bc7)

@ -0,0 +1,77 @@
#include "MechaQMC5883.h"
#include <Wire.h>
void MechaQMC5883::setAddress(uint8_t addr){
address = addr;
}
void MechaQMC5883::WriteReg(byte Reg,byte val){
Wire.beginTransmission(address); //start talking
Wire.write(Reg); // Tell the HMC5883 to Continuously Measure
Wire.write(val); // Set the Register
Wire.endTransmission();
}
void MechaQMC5883::init(){
WriteReg(0x0B,0x01);
//Define Set/Reset period
setMode(Mode_Continuous,ODR_200Hz,RNG_8G,OSR_512);
/*
Define
OSR = 512
Full Scale Range = 8G(Gauss)
ODR = 200HZ
set continuous measurement mode
*/
}
void MechaQMC5883::setMode(uint16_t mode,uint16_t odr,uint16_t rng,uint16_t osr){
WriteReg(0x09,mode|odr|rng|osr);
}
void MechaQMC5883::softReset(){
WriteReg(0x0A,0x80);
}
/**
* read values from device
* @return status value:
* - 0:success
* - 1:data too long to fit in transmit buffer
* - 2:received NACK on transmit of address
* - 3:received NACK on transmit of data
* - 4:other error
* - 8:overflow (magnetic field too strong)
*/
int MechaQMC5883::read(int* x,int* y,int* z){
Wire.beginTransmission(address);
Wire.write(0x00);
int err = Wire.endTransmission();
if (err) {return err;}
Wire.requestFrom(address, 7);
*x = (int)(int16_t)(Wire.read() | Wire.read() << 8);
*y = (int)(int16_t)(Wire.read() | Wire.read() << 8);
*z = (int)(int16_t)(Wire.read() | Wire.read() << 8);
byte overflow = Wire.read() & 0x02;
return overflow << 2;
}
int MechaQMC5883::read(int* x,int* y,int* z,int* a){
int err = read(x,y,z);
*a = azimuth(y,x);
return err;
}
int MechaQMC5883::read(int* x,int* y,int* z,float* a){
int err = read(x,y,z);
*a = azimuth(y,x);
return err;
}
float MechaQMC5883::azimuth(int *a, int *b){
float azimuth = atan2((int)*a,(int)*b) * 180.0/PI;
return azimuth < 0?360 + azimuth:azimuth;
}

@ -0,0 +1,61 @@
#ifndef Mecha_QMC5883
#define Mecha_QMC5883
#include "Arduino.h"
#include "Wire.h"
#define QMC5883_ADDR 0x0D
//REG CONTROL
//0x09
#define Mode_Standby 0b00000000
#define Mode_Continuous 0b00000001
#define ODR_10Hz 0b00000000
#define ODR_50Hz 0b00000100
#define ODR_100Hz 0b00001000
#define ODR_200Hz 0b00001100
#define RNG_2G 0b00000000
#define RNG_8G 0b00010000
#define OSR_512 0b00000000
#define OSR_256 0b01000000
#define OSR_128 0b10000000
#define OSR_64 0b11000000
class MechaQMC5883{
public:
void setAddress(uint8_t addr);
void init(); //init qmc5883
void setMode(uint16_t mode,uint16_t odr,uint16_t rng,uint16_t osr); // setting
void softReset(); //soft RESET
int read(int* x,int* y,int* z); //reading
int read(int* x,int* y,int* z,int* a);
int read(int* x,int* y,int* z,float* a);
float azimuth(int* a,int* b);
private:
void WriteReg(uint8_t Reg,uint8_t val);
uint8_t address = QMC5883_ADDR;
};
#endif

@ -0,0 +1,138 @@
# Mechasolution QMC5883L Library
[한글 설명 바로가기](https://github.com/keepworking/Mecha_QMC5883/blob/master/README_KO.md)
## Arduino Code
There are a few simple rules for using that library. Please read the following summary and apply it to your project
### Basic Elements
Required header files (#include ...) and Setup side code.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
void setup(){
Wire.begin();
}
```
### Object Declaration
The object declaration method. It is used outside the setup statement, and a name such as qmc can be changed to any other name you want.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
```
### initialization
QMC5883 Sensor's setting function.
The init function allows you to take advantage of the features of the QMC5883 sensor by default.
```cpp
void setup(){
Wire.begin();
qmc.init();
}
```
If you want more detailed settings, you can use it as follows.
```cpp
void setup(){
Wire.begin();
qmc.init();
qmc.setMode(Mode_Standby,ODR_200Hz,RNG_8G,OSR_512);
}
```
The values used for setMode can take the following values:
```
Mode : Mode_Standby / Mode_Continuous
ODR : ODR_10Hz / ODR_50Hz / ODR_100Hz / ODR_200Hz
ouput data update rate
RNG : RNG_2G / RNG_8G
magneticfield measurement range
OSR : OSR_512 / OSR_256 / OSR_128 / OSR_64
over sampling rate
```
### Read values
How to read the measured sensor value is as follows.
```cpp
void loop(){
int x,y,z;
qmc.read(&x,&y,&z);
}
```
and we can get azimuth too.
```cpp
void loop(){
int x,y,z;
int a;
//float a; //can get float value
qmc.read(&x,&y,&z,&a);
}
```
also can claculate azimuth you want
```cpp
void loop(){
int x,y,z;
int a;
qmc.read(&x,&y,&z);
a = qmc.azimuth(&y,&x);
}
```
## Basic example
It can be seen as a collection of the contents mentioned above.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}
void loop() {
int x,y,z;
qmc.read(&x,&y,&z);
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.println();
delay(100);
}
```

@ -0,0 +1,138 @@
# Mechasolution QMC5883 Library
HMC5883 지자기 나침반 센서의 수명 만료(EOL)로인해 그동안의 HMC5883 센서는 생산이 중단 되었고, 대체 상품인 QMC5883으로 변경이 되었습니다.
## Arduino Code
해당 라이브러리를 이용하기 위한 몇가지 간단한 규칙이 있습니다. 아래 정리된 내용을 읽어주시고 사용하시는 프로젝트에 적용해 주세요
### 기본 요소
필수적으로 필요한 헤더파일(#include...)과 Setup 쪽 코드 입니다.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
void setup(){
Wire.begin();
}
```
### 객체 선언
객체 선언 방식입니다. setup문 밖에서 사용이 되며 qmc와 같은 이름은 사용자가 원하는 다른 이름으로 변경이 가능합니다.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
```
### 사용 설정
QMC5883 센서의 설정 함수 입니다.
init 함수를 사용하면 기본 설정으로 QMC5883센서의 기능을 이용할 수 있습니다.
```cpp
void setup(){
Wire.begin();
qmc.init();
}
```
좀더 세세한 설정을 원한다면 다음과 같이 사용이 가능합니다.
```cpp
void setup(){
Wire.begin();
qmc.init();
qmc.setMode(Mode_Standby,ODR_200Hz,RNG_8G,OSR_512);
}
```
setMode에 사용되는 값들은 다음 값들을 이용할 수 있습니다.
```
Mode : Mode_Standby / Mode_Continuous
ODR : ODR_10Hz / ODR_50Hz / ODR_100Hz / ODR_200Hz
ouput data update rate
RNG : RNG_2G / RNG_8G
magneticfield measurement range
OSR : OSR_512 / OSR_256 / OSR_128 / OSR_64
over sampling rate
```
### 값 읽기
측정한 센서의 값을 읽는 법은 다음과 같습니다.
```cpp
void loop(){
int x,y,z;
qmc.read(&x,&y,&z);
}
```
방위각에 대한 값입니다.
```cpp
void loop(){
int x,y,z;
int a;
//float a; //float 형도 지원됩니다.
qmc.read(&x,&y,&z,&a);
}
```
별도로 원하는 방위각도 구할 수 있습니다.
```cpp
void loop(){
int x,y,z;
int a;
qmc.read(&x,&y,&z);
a = qmc.azimuth(&y,&x);
}
```
### 기본 예제
다음은 라이브러리 기본 예제인 raw입니다.
위에 소개된 내용의 총집합으로 볼 수 있습니다.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}
void loop() {
int x,y,z;
qmc.read(&x,&y,&z);
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.println();
delay(100);
}
```

@ -0,0 +1,29 @@
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}
void loop() {
int x, y, z;
int azimuth;
//float azimuth; //is supporting float too
qmc.read(&x, &y, &z,&azimuth);
//azimuth = qmc.azimuth(&y,&x);//you can get custom azimuth
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.print(" a: ");
Serial.print(azimuth);
Serial.println();
delay(100);
}

@ -0,0 +1,25 @@
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}
void loop() {
int x,y,z;
qmc.read(&x,&y,&z);
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.println();
delay(100);
}

@ -0,0 +1,53 @@
#include <Wire.h> //I2C Arduino Library
#define addr 0x0D //I2C Address for The HMC5883
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(addr); //start talking
Wire.write(0x0B); // Tell the HMC5883 to Continuously Measure
Wire.write(0x01); // Set the Register
Wire.endTransmission();
Wire.beginTransmission(addr); //start talking
Wire.write(0x09); // Tell the HMC5883 to Continuously Measure
Wire.write(0x1D); // Set the Register
Wire.endTransmission();
}
void loop() {
int x, y, z; //triple axis data
//Tell the HMC what regist to begin writing data into
Wire.beginTransmission(addr);
Wire.write(0x00); //start with register 3.
Wire.endTransmission();
//Read the data.. 2 bytes for each axis.. 6 total bytes
Wire.requestFrom(addr, 6);
if (6 <= Wire.available()) {
x = Wire.read(); //MSB x
x |= Wire.read() << 8; //LSB x
z = Wire.read(); //MSB z
z |= Wire.read() << 8; //LSB z
y = Wire.read(); //MSB y
y |= Wire.read() << 8; //LSB y
}
// Show Values
Serial.print("X Value: ");
Serial.println(x);
Serial.print("Y Value: ");
Serial.println(y);
Serial.print("Z Value: ");
Serial.println(z);
Serial.println();
delay(500);
}

@ -0,0 +1,25 @@
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}
void loop() {
int x,y,z;
qmc.read(&x,&y,&z);
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.println();
delay(100);
}
Loading…
Cancel
Save