Arduino IDE 1.6.5 + BH1750 + CD74HC4067 多工器
Arduino Uno 上的 I2C 通道非常方便去串聯很多感應器,但有時會碰到一次會要接許多相同的感應器如 BH1750 ,受人之託是要作一個 如同該網友所作的 Multiple BH1750 on i2c bus Digital Light Sensor + Arduino (16xBH1750),剛好我也有2組 CD74HC4067,CD74HC4067在露天上可以買到,我買的是這一塊:
因為 I2C 有 SCL及 SDA 所以需要2片 CD74HC4067, BH1750 上還有另一條 ADDR 就選擇用 LOW,也就是不接。
先看一張亂接圖:
手工接的,真怕短路。但是可以執行喔。 最重要的是 CD74HC4067 上 S0 S1 S2 S3 這是用來選擇要讀取的BH1750,SCL及 SDA 分別接到不同板子上的 Cx 上。
#include <Wire.h>
#include <BH1750FVI.h>
BH1750FVI LightSensor;
//Mux control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;
void setup() {
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
Serial.begin(57600);
LightSensor.begin();
LightSensor.SetAddress(Device_Address_L);
/*
set the Working Mode for this sensor
Select the following Mode:
Continuous_H_resolution_Mode
Continuous_H_resolution_Mode2
Continuous_L_resolution_Mode
OneTime_H_resolution_Mode
OneTime_H_resolution_Mode2
OneTime_L_resolution_Mode
The data sheet recommanded To use Continuous_H_resolution_Mode
*/
LightSensor.SetMode(Continuous_H_resolution_Mode);
Serial.println("Running...");
}
void loop() {
for (int i = 0; i < 12; i ++) {
readMux(i);
LightSensor.begin();
LightSensor.SetAddress(Device_Address_L);
LightSensor.SetMode(Continuous_H_resolution_Mode);
uint16_t lux = LightSensor.GetLightIntensity();// Get Lux value
Serial.print("L");
Serial.print(i + 1);
Serial.print(": ");
Serial.print(lux);
Serial.print(" ");
delay(1000);
}
Serial.println(" lux");
}
//用來選擇讀取
int readMux(int channel) {
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4] = {
{0, 0, 0, 0}, //channel 0
{1, 0, 0, 0}, //channel 1
{0, 1, 0, 0}, //channel 2
{1, 1, 0, 0}, //channel 3
{0, 0, 1, 0}, //channel 4
{1, 0, 1, 0}, //channel 5
{0, 1, 1, 0}, //channel 6
{1, 1, 1, 0}, //channel 7
{0, 0, 0, 1}, //channel 8
{1, 0, 0, 1}, //channel 9
{0, 1, 0, 1}, //channel 10
{1, 1, 0, 1}, //channel 11
{0, 0, 1, 1}, //channel 12
{1, 0, 1, 1}, //channel 13
{0, 1, 1, 1}, //channel 14
{1, 1, 1, 1} //channel 15
};
for (int i = 0; i < 4; i ++) {
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
}
CD74HC4067 |
BH1750 |
先看一張亂接圖:
Arduino CD 74HC4067 |
完程圖
#include <Wire.h>
#include <BH1750FVI.h>
BH1750FVI LightSensor;
//Mux control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;
void setup() {
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
Serial.begin(57600);
LightSensor.begin();
LightSensor.SetAddress(Device_Address_L);
/*
set the Working Mode for this sensor
Select the following Mode:
Continuous_H_resolution_Mode
Continuous_H_resolution_Mode2
Continuous_L_resolution_Mode
OneTime_H_resolution_Mode
OneTime_H_resolution_Mode2
OneTime_L_resolution_Mode
The data sheet recommanded To use Continuous_H_resolution_Mode
*/
LightSensor.SetMode(Continuous_H_resolution_Mode);
Serial.println("Running...");
}
void loop() {
for (int i = 0; i < 12; i ++) {
readMux(i);
LightSensor.begin();
LightSensor.SetAddress(Device_Address_L);
LightSensor.SetMode(Continuous_H_resolution_Mode);
uint16_t lux = LightSensor.GetLightIntensity();// Get Lux value
Serial.print("L");
Serial.print(i + 1);
Serial.print(": ");
Serial.print(lux);
Serial.print(" ");
delay(1000);
}
Serial.println(" lux");
}
//用來選擇讀取
int readMux(int channel) {
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4] = {
{0, 0, 0, 0}, //channel 0
{1, 0, 0, 0}, //channel 1
{0, 1, 0, 0}, //channel 2
{1, 1, 0, 0}, //channel 3
{0, 0, 1, 0}, //channel 4
{1, 0, 1, 0}, //channel 5
{0, 1, 1, 0}, //channel 6
{1, 1, 1, 0}, //channel 7
{0, 0, 0, 1}, //channel 8
{1, 0, 0, 1}, //channel 9
{0, 1, 0, 1}, //channel 10
{1, 1, 0, 1}, //channel 11
{0, 0, 1, 1}, //channel 12
{1, 0, 1, 1}, //channel 13
{0, 1, 1, 1}, //channel 14
{1, 1, 1, 1} //channel 15
};
for (int i = 0; i < 4; i ++) {
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
}
留言
張貼留言
請多指教