Ambient LightセンサーISL29034をArduinoから制御する(I2C)

# ISL229034

![ISL29034.png](https://qiita-image-store.s3.amazonaws.com/0/63450/ebe8e721-767f-4d6e-66f0-12041874c975.png)

# Datasheet
[Datasheet](https://octopart.com/isl29034iroz-t7-intersil-46958301)

# Slave Address

| Slave address | ??? |
|:-:|:-:|

# PowerOn

| 項目  | アドレス | Bit7 | Bit6 | Bit5 | Bit4 | Bit3 | Bit2 | Bit1 | Bit0 | 
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|COMMAND-I|0x00| OP7 | OP6 | OP5 | RESERVED| RESERVED| RESERVED| RESERVED| RESERVED| RESERVED|

| OP7<br>Bit7 | OP6<br>Bit6 | OP5<br>Bit5 | 概要 |
|:-:|:-:|:-:|:--|
| 0 | 0 | 0 | Power-down the device (Default) |
| 0 | 0 | 1 | The device measures ALS only once every integration cycle. This is the lowest operating mode. (Note 11) | 
| 0 | 1 | 0 | IR once |
| 0 | 1 | 1 | Reserved (DO NOT USE) |
| 1 | 0 | 0 | Reserved (DO NOT USE) |
| 1 | 0 | 1 | Measures ALS continuously |
| 1 | 1 | 0 | Measures IR continuous |
| 1 | 1 | 1 | Reserved (DO NOT USE) |

```cpp:arduino
#define ISL29034_POWER_DOWN 0b000
#define ISL29034_ALS_ONLY 0b001
#define ISL29034_ALS_CONTINUOSULY 0b101
#define ISL29034_IR_CONTINUOSULY 0b110

#define ISL29034_COMMAIN_I_REG 0x00

void PowerOn()
{
  writeI2c(ISL29034_COMMAIN_I_REG, ISL29034_ALS_CONTINUOSULY);  
}
```

# Configuration

| 項目  | アドレス | Bit7 | Bit6 | Bit5 | Bit4 | Bit3 | Bit2 | Bit1 | Bit0 | 
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|COMMAND-II|0x01| RESERVED | RESERVED | RESERVED | RESERVED| RES1 |  RES0 | RANGE1 | RANGE0 |

| RANGE1<br>Bit1 | RANGE0<br>Bit0 |  FULL SCALE LUX RANGE |
|:-:|:-:|:-:|:--|
| 0 | 0 | 1,000 |
| 0 | 1 | 4,000 | 
| 1 | 0 | 16,000 | 
| 1 | 1 | 64,000 | 

| RES1<br>Bit3 | RES2<br>Bit2 |  NUMBER OF CLOCK CYCLES | n-BIT ADC |
|:-:|:-:|:-:|:--|
| 0 | 0 | 65,536 | 16 |
| 0 | 1 | 4,096 | 12 | 
| 1 | 0 | 256  | 8 | 
| 1 | 1 | 16 | 4 |

```cpp:arduino
#define ISL29034_LUX_RANGE_1000 0b00
#define ISL29034_LUX_RANGE_4000 0b01
#define ISL29034_LUX_RANGE_16000 0b10
#define ISL29034_LUX_RANGE_64000 0b11

#define ISL29034_CLOCK_65536 0b0000
#define ISL29034_CLOCk_4096 0b0100
#define ISL29034_CLOCK_256 0b1000
#define ISL29034_CLOCK_16 0b1100

#define ISL29034_COMMAIN_II_REG 0x01

void Configuration()
{
  writeI2c(ISL29034_COMMAIN_II_REG, ISL29034_LUX_RANGE_64000|ISL29034_CLOCK_65536);  
}
```

# Ambientの読み取り

```cpp:arduino
#define ISL29034_DATA 0x02

uint16_t ReadData()
{
  uint16_t ambient;
  uint8_t buffer[2];

  readI2c(ISL29034_DATA, 2, buffer);
  ambient = (((uint16_t)buffer[1])<<8) | (uint16_t)buffer[0];

  return ambient;
}
```

# Lower Interrupt Threshold Registers 
ToDo

# Upper Interrupt Threshold Registers 
ToDo

# I2Cのアクセス用コード

```cpp:Arduino
#define ISL29034_SLAVE_ADDRESS 0x??

// I2Cへの書き込み
void writeI2c(byte register_addr, byte value) {
  Wire.beginTransmission(ISL29034_SLAVE_ADDRESS);  
  Wire.write(register_addr);         
  Wire.write(value);                 
  Wire.endTransmission();        
}

// I2Cへの読み込み
void readI2c(byte register_addr, int num, byte *buf) {
  Wire.beginTransmission(ISL29034_SLAVE_ADDRESS); 
  Wire.write(register_addr);           
  Wire.endTransmission(false);         

  //Wire.beginTransmission(DEVICE_ADDR); 
  Wire.requestFrom(HTS221_SLAVE_ADDRESS, num);  

  int i = 0;
  while (Wire.available())
  {
    buf[i] = Wire.read(); 
    i++;   
  }
  //Wire.endTransmission();         
}
```