Recently, the weather is so hot and you could fry eggs on the sidewalk. It is a good job that there are three sensors around me when I want to do an indoor temperature test. Below are sensors in need.
ds18b20
#include <OneWire.h>
#include <Wire.h>
#include "DFRobot_RGBLCD.h"
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
DFRobot_RGBLCD lcd(16,2); //16 characters and 2 lines of show
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600);
lcd.init();
lcd.setRGB(0, 0, 255);
}
void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
delay(1000);
lcd.setCursor(0,0);
lcd.print("Tep: ");
lcd.print(temperature);
delay(1000);
delay(100); //just here to slow down the output so it is easier to read
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
LM35_CW
#include <Wire.h>
#include "DFRobot_RGBLCD.h"
DFRobot_RGBLCD lcd(16,2); //16 characters and 2 lines of show
void setup()
{
Serial.begin(9600);//Set Baud Rate to 9600 bps
lcd.init();
lcd.setRGB(0, 0, 255);
}
void loop()
{
uint16_t val;
double dat;
val=analogRead(A0);//Connect LM35 on Analog 0
dat = (double) val * (5/10.24);
Serial.print("Tep:"); //Display the temperature on Serial monitor
Serial.print(dat);
Serial.println("C");
delay(500);
//lcd.print("hello!");
//delay(1000);
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("Tep: ");
lcd.print(dat);
delay(1000);
// lcd.clear();
}
#include <Wire.h>
#include "DFRobot_RGBLCD.h"
#include <Adafruit_NeoPixel.h>
char col;
unsigned int PMSa = 0, FMHDSa = 0, TPSa = 0, HDSa = 0, PMSb = 0, FMHDSb = 0, TPSb = 0, HDSb = 0;
unsigned int PMS = 0, FMHDS = 0, TPS = 0, HDS = 0, CR1 = 0, CR2 = 0;
unsigned int lcd_a = 0, lcd_b = 0, lcd_c = 0;
unsigned char buffer_RTT[40] = {}; //串口接收数据
unsigned long last_lcd_time = 0, last_light_time = 0;
#define PIN 8 //灯IO
#define NUMBER 2 //共有2个灯
char tempStr[15];
DFRobot_RGBLCD lcd(16, 2); //RGB address; 16 characters and 2 lines of show
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMBER, PIN, NEO_GRB + NEO_KHZ800); //灯参数
void setup()
{
Serial.begin(9600);
lcd.init();//初始化LCD1602 RGB屏
strip.begin(); //启动RGB灯
lcd.setRGB(0, 255, 0); //设置屏的初始背光颜色
lcd.setCursor(0, 0 ); //设置从(0,0)开始显示
lcd.print("T:");//显示T:
lcd.setCursor(9, 0 );
lcd.print("H:");
lcd.setCursor(0, 1 );
lcd.print("F:");
lcd.setCursor(9, 1 );
lcd.print("P:");
}
void loop()
{
lcd_a = random(256);//读取随机值
delayMicroseconds(10);//延迟,意在使得三个随机值没有去同一个(指随机值变化过再取,也有可能相同)
lcd_b = random(256);
delayMicroseconds(10);
lcd_c = random(256);
if (millis() - last_lcd_time > 30000) //每3秒变化一次显示屏背光(可能会出现与上一次相同)
{
lcd.setRGB(lcd_a, lcd_b, lcd_c); //设置背光颜色
last_lcd_time = millis();//取系统时间
}
if (millis() - last_light_time > 500) //RGB灯亮的颜色,此处设置好了颜色,没有随机
{
strip.setPixelColor(0, strip.Color( 255, 227, 132) ); //0,为第一个RGB灯;255为R,227为G,132为B,可进行调整
strip.setPixelColor(1, strip.Color( 255, 227, 132) );
strip.show();//RGB使能控制,无这个函数,RGB灯不改变状态
last_light_time = millis();//取系统时间
}
while (Serial.available() > 0) //检测是否有串口数据
{
for (int i = 0; i < 40; i++) //读取串口数据
{
col = Serial.read();
buffer_RTT[i] = (char)col;
delay(2);
}
Serial.flush();
//lcd.clear();
CR1 = (buffer_RTT[38] << 8) + buffer_RTT[39];
CR2 = 0;
for (int i = 0; i < 38; i++)
CR2 += buffer_RTT[i];
if (CR1 == CR2) //校验
{
PMSa = buffer_RTT[12]; //读取PM2.5高八位数据
PMSb = buffer_RTT[13]; //读取PM2.5低八位数据
PMS = (PMSa << 8) + PMSb; //PM2.5数据
FMHDSa = buffer_RTT[28]; //读取甲醛高八位数据
FMHDSb = buffer_RTT[29]; //读取甲醛低八位数据
FMHDS = (FMHDSa << 8) + FMHDSb; //甲醛数据
TPSa = buffer_RTT[30]; //读取温度高八位数据
TPSb = buffer_RTT[31]; //读取温度低八位数据
TPS = (TPSa << 8) + TPSb; //温度数据
HDSa = buffer_RTT[32]; //读取湿度高八位数据
HDSb = buffer_RTT[33]; //读取湿度低八位数据
HDS = (HDSa << 8) + HDSb; //湿度数据
}
else
{
PMS = 0;
FMHDS = 0;
TPS = 0;
HDS = 0;
}
}
lcd.setCursor(2, 0 );
sprintf(tempStr,"%d%d.%d",TPS/100,(TPS/10)%10,TPS%10); //显示温度
lcd.print(tempStr);
lcd.write(0xdf); //显示°
lcd.print('C'); //显示C
lcd.setCursor(11, 0 );
sprintf(tempStr,"%d%d.%d",HDS/100,(HDS/10)%10,HDS%10); //显示湿度
lcd.print(tempStr);
lcd.print('%'); //显示%
lcd.setCursor(2, 1 ); //
lcd.print((float)FMHDS/1000); // 显示甲醛
lcd.print((int)FMHDS%10); // 显示甲醛最后一位小数,单位 mg/m³ 毫克每立方米
lcd.setCursor(11, 1 ); //
sprintf(tempStr,"%d%d%d",PMS/100,(PMS/10)%10,PMS%10); //显示PM2.5个、十、百位单位ug/m³ 微克每立方米
lcd.print(tempStr);
}