光照模块GY30和arduino通讯

没有评论

2014年5月30日 at 下午9:58分类:硬件

连4根线,VCC、GND、SCL、SDA,SCL、SDA连接arduino的A5、A4脚:

GY30

arduino pro mini

代码:

#include <Wire.h>

// GY-30
// BH1750FVI
// in ADDR 'L' mode 7bit addr
#define ADDR 0b0100011
// addr 'H' mode
// #define ADDR 0b1011100

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Wire.begin();
pinMode(13, OUTPUT);

Wire.beginTransmission(ADDR);
Wire.write(0b00000001);
Wire.endTransmission();

}

void loop() {
// put your main code here, to run repeatedly:
int val = 0;

// reset
Wire.beginTransmission(ADDR);
Wire.write(0b00000111);
Wire.endTransmission();

digitalWrite(13, LOW);
delay(100);

Wire.beginTransmission(ADDR);
Wire.write(0b00100000);
Wire.endTransmission();

// typical read delay 120ms
delay(120);

Wire.requestFrom(ADDR, 2); // 2byte every time

for (val=0; Wire.available()>=1; ) {
char c = Wire.read();
//Serial.println(c, HEX);
val = (val << 8) + (c & 0xFF);
}

val = val / 1.2;
Serial.print("lx: ");
Serial.println(val);

// Serial.println("OK");
digitalWrite(13, HIGH);
delay(500);

}

 

修正I2C通讯程序

没有评论

2014年5月30日 at 下午3:39分类:硬件

#include <Wire.h>

int dustPin=0;
int dustVal=0;
String dust="";
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
void setup(){
Wire.begin(5);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
}

void loop(){
getDust();
}
void requestEvent()
{
char charBuf[dust.length()+1];
dust.toCharArray(charBuf, dust.length()+1);
Wire.write(charBuf);
}
void receiveEvent(int howMany)
{
while (1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.print(x); // print the integer
}
void getDust(){
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin);
if(dustVal<10){
dust="000"+String(dustVal);
}else if(dustVal<100){
dust="00"+String(dustVal);
}else if(dustVal<1000){
dust="0"+String(dustVal);
}else{
dust=String(dustVal);
}
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(offTime);

delay(3000);
Serial.println(dust);

}

注:不需要OneWire

参考:

http://www.eefocus.com/zhang700309/blog/12-01/236815_59f78.html

http://blog.csdn.net/jcdjx/article/details/24356035

解决PL2303 win8.1驱动问题

没有评论

2014年5月30日 at 下午12:51分类:硬件

http://tieba.baidu.com/p/2378715876

 

全新TTL数据线 无线路由刷机线 变砖修复线 路由必备

白色接:TX

绿色接:RX

黑色:地

PM2.5数值

没有评论

2014年5月30日 at 上午11:56分类:硬件

参考文章:

http://www.sfan20.cn/post-111.html

http://www.howmuchsnow.com/arduino/airquality/

 

代码:

int dustPin=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;

int dustVal=0;
float ppm=0;
char s[32];
float voltage = 0;
float dustdensity = 0;
float ppmpercf = 0;

void setup(){
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
pinMode(dustPin, INPUT);

ppm =0;
}

void loop(){

digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin); // read the dust value
ppm =dustVal;
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(offTime);

voltage = ppm*0.0049;//电压=模拟读取值*(5V/1024),因为AD变换器为10位,将5V内的电压值转换成0-1024读数
dustdensity = 0.17*voltage-0.1;//电压和灰尘浓度PM2.5(mg/立方米)的函数关系
ppmpercf = (voltage-0.0256)*120000;// 反算PM2.5到PM0.5的函数关系,电压值小时有效
if (ppmpercf < 0)
ppmpercf = 0;
if (dustdensity < 0 )
dustdensity = 0;
if (dustdensity > 0.5)
dustdensity = 0.5;
dustdensity=dustdensity*1000;

读取空气检测器程序

没有评论

2014年5月29日 at 下午11:55分类:硬件

读取数据,直接输出到串口:
int dustPin=0;
float dustVal=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
void setup(){
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
pinMode(dustPin, INPUT);
}
void loop(){
// ledPower is any digital pin on the arduino connected to Pin 3 on the sensor
digitalWrite(ledPower,LOW);
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin);
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH);
delayMicroseconds(offTime);
delay(1000);
if (dustVal>36.455)
Serial.println((float(dustVal/1024)-0.0356)*120000*0.035);
}
如果arduino 和树莓派通讯进行信息上传,程序如下:

#include <OneWire.h>
#include <Wire.h>

int dustPin=0;
int dustVal=0;
String dust=”";
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
void setup(){
Wire.begin(5);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
}

void loop(){
getDust();
}
void requestEvent()
{
char charBuf[dust.length()+1];
dust.toCharArray(charBuf, dust.length()+1);
Wire.write(charBuf);
}
void receiveEvent(int howMany)
{
while (1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.print(x); // print the integer
}
void getDust(){
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin);
if(dustVal<10){
dust=”000″+String(dustVal);
}else if(dustVal<100){
dust=”00″+String(dustVal);
}else if(dustVal<1000){
dust=”0″+String(dustVal);
}else{
dust=String(dustVal);
}
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(offTime);

delay(3000);
Serial.println(dust);

}

需要先下载OneWire