Hi...
Now its running well, after having had some help froma a friend of mine...
It is measuring the temperature, humidity and air pressure in mBar.
you can see what is done bellow... as well as all the hardware used.
Code: Select all
/*
Temperature + Humidity Sensor with information printed in a SPI 128x64 OLED
Including two warning leds, one for high humidity and a seccond on with 3 steps of
high temperature warning.
Blue LED - High humidity detected (>= 70%) - 2 blinks every 3 secconds
Red LED - High temperature warning:
- 1 blink every 3 secconds = temperature higher than 30ºC
- 2 blink every 3 secconds = temperature higher than 33ºC
- 3 blink every 3 secconds = temperature higher than 37ºC
Connect pin 1 (on the left) of the sensor to +5V
Connect pin 2 of the sensor to whatever your DHTPIN is
Connect pin 4 (on the right) of the sensor to GROUND
Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
Connect oled DC to D11
Connect oled CS to D12
Connect oled CLK to D10
Connect oled MOSI to D09
Connect oled RESET to D13
Barometer SDA and SCL are connected to pins A4 and A5
PAY ATTENTION!!!! BOAR SENSOR IS +3V
If you connect it to +5v you will burn it
Material needed:
1x Arduino nano v.3
1x Adafruit 128x64 1.3" oled display
1x DHT sensor
1x BMPo85 barometric sensor (I2C)
2x resistor (for leds)
1x Blue led
1x Red led
Have fun!!!!
*/
// -------------------- Thermometer DHT -----------------------
#include "DHT.h"
#define DHTPIN 8 // what pin where the sensor is connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
int temp_int;
int hum_int;
int press_int1;
int press_int2;
// --------------------- Baro -----------------------------
#include <Wire.h>
#define BMP085_ADDRESS 0x77 // I2C address of BMP085
const unsigned char OSS = 0; // Oversampling Setting
// Calibration values
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
// b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
// so ...Temperature(...) must be called before ...Pressure(...).
long b5;
short temperature;
long pressure;
// ---------- oLed -------------------
#define OLED_DC 11
#define OLED_CS 12
#define OLED_CLK 10
#define OLED_MOSI 9
#define OLED_RESET 13
#include <SSD1306.h>
SSD1306 oled(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
//------------------------- SETUP ---------------------------
void setup() {
oled.ssd1306_init(SSD1306_SWITCHCAPVCC);
oled.display(); // show splashscreen
delay(1000);
Serial.begin(9600);
Wire.begin();
bmp085Calibration();
dht.begin();
pinMode(6, OUTPUT); // Temperature warning LED ( RED ) - Dout 6
pinMode(7, OUTPUT); // Humidity warning LED ( BLUE ) - Dout 7
}
//-------------------------- LOOP ----------------------------
void loop() {
// --------------- Baro ------------------------
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
Serial.print("Temperature: ");
Serial.print((temperature / 10), DEC);
Serial.println(" C");
Serial.print("Pressure: ");
Serial.print(pressure, DEC);
Serial.println(" Pa");
Serial.println();
//------------- temp + hum sensor conf. ---------------
float h = dht.readHumidity();
float t = dht.readTemperature();
//--------------Sensor error condition on screen ---------------
if (isnan(t) || isnan(h)) {
oled.clear(); // clears the screen and buffer
oled.drawstring(18, 1, "Weather Station");
oled.drawstring(27, 4, "Sensor error");
oled.drawstring(15, 5, "Check Connection");
oled.display();
delay(2000);
}
//------------- Sensors information on oLed --------------
else {
oled.clear(); // clears the screen and buffer
oled.drawstring(19, 1, "Weather Station");
oled.drawstring(45, 2, "Tavira"); // Write the name of your city here
oled.drawstring(17, 4, "Temperature:");
temp_int = (int)(t); // converter para int
if(temp_int < 0){ // temperatura negativa
oled.drawchar( 92, 4, '-'); // imprimir o sinal de menos
temp_int = -temp_int; // tornar temp positiva
}
else{
oled.drawchar( 92, 4, ' ');
}
oled.drawchar( 92, 4, 48 + temp_int/10);
oled.drawchar(97, 4, 48 + temp_int%10);
oled.drawstring(102, 4, " C");
//-------- Humidity -----------
oled.drawstring(17, 5, "Humidity :");
hum_int = (int)(h); // converter para int
{
oled.drawchar( 92, 5, ' ');
}
oled.drawchar( 92, 5, 48 + hum_int/10);
oled.drawchar(97, 5, 48 + hum_int%10);
oled.drawstring(102, 5, " %");
//-------- Air pressure -----------
oled.drawstring(17, 6, "Pressure :");
press_int1 = (int)(pressure / 10000); // converter para int
press_int2 = (int)((pressure / 100) - 1000);
{
oled.drawchar(82, 6, ' ');
}
oled.drawchar(82, 6, 48 + press_int1/10);
oled.drawchar(87, 6, 48 + press_int1%10);
oled.drawchar(92, 6, 48 + press_int2/10);
oled.drawchar(97, 6, 48 + press_int2%10);
oled.drawstring(102, 6, " mB");
//----------- Graphic frame ---------------
oled.drawline(0, 4, 126, 4, 1); // Top line
oled.drawchar(0, 0, 213); // Top Left corner
oled.drawchar(121, 0, 182); // Top Right corner
oled.drawchar(0, 1, 185);
oled.drawchar(121, 1, 185);
oled.drawchar(0, 2, 185);
oled.drawchar(121, 2, 185);
oled.drawline(0, 28, 126, 28, 1); // Center line
oled.drawchar(0, 3, 198); // Center Left Edge
oled.drawchar(121, 3, 181); // Center Right Edge
oled.drawchar(0, 4, 185);
oled.drawchar(121, 4, 185);
oled.drawchar(0, 5, 185);
oled.drawchar(121, 5, 185);
oled.drawchar(0, 6, 185);
oled.drawchar(121, 6, 185);
oled.drawline(0, 60, 121, 60, 1); // Bottom line
oled.drawchar(0, 7, 210); // Bottom Left Corner
oled.drawchar(121, 7, 188); // Bottom Right corner
oled.display();
delay(3000);
}
//------------ Sensor LEDs status conditions ---------------
//--------- Humidity --------------
if (h >=70.00) {
digitalWrite(7, HIGH);
delay(100);
digitalWrite(7, LOW);
delay(100);
digitalWrite(7, HIGH);
delay(100);
digitalWrite(7, LOW);
}
else {
digitalWrite(7, LOW);
}
//------------ Temperature Warning -------------------
if (t >=30.00 && t <33.00) {
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
}
else {
digitalWrite(6, LOW);
}
if (t >=33.00 && t <37.00) {
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(100);
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
}
else {
digitalWrite(6, LOW);
}
if (t >=37.00) {
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(100);
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(100);
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(100);
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
}
else {
digitalWrite(6, LOW);
}
}
// ---------------- VOID BMP085Caçibration ---------------------------
// Stores all of the bmp085's calibration values into global variables
// Calibration values are required to calculate temp and pressure
// This function should be called at the beginning of the program
void bmp085Calibration()
{
ac1 = bmp085ReadInt(0xAA);
ac2 = bmp085ReadInt(0xAC);
ac3 = bmp085ReadInt(0xAE);
ac4 = bmp085ReadInt(0xB0);
ac5 = bmp085ReadInt(0xB2);
ac6 = bmp085ReadInt(0xB4);
b1 = bmp085ReadInt(0xB6);
b2 = bmp085ReadInt(0xB8);
mb = bmp085ReadInt(0xBA);
mc = bmp085ReadInt(0xBC);
md = bmp085ReadInt(0xBE);
}
// Calculate temperature given ut.
// Value returned will be in units of 0.1 deg C
short bmp085GetTemperature(unsigned int ut)
{
long x1, x2;
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long)mc << 11)/(x1 + md);
b5 = x1 + x2;
return ((b5 + 8)>>4);
}
// Calculate pressure given up
// calibration values must be known
// b5 is also required so bmp085GetTemperature(...) must be called first.
// Value returned will be pressure in units of Pa.
long bmp085GetPressure(unsigned long up)
{
long x1, x2, x3, b3, b6, p;
unsigned long b4, b7;
b6 = b5 - 4000;
// Calculate B3
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
// Calculate B4
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
p += (x1 + x2 + 3791)>>4;
return p;
}
// Read 1 byte from the BMP085 at 'address'
char bmp085Read(unsigned char address)
{
unsigned char data;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 1);
while(!Wire.available())
;
return Wire.read();
}
// Read 2 bytes from the BMP085
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp085ReadInt(unsigned char address)
{
unsigned char msb, lsb;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 2);
while(Wire.available()<2)
;
msb = Wire.read();
lsb = Wire.read();
return (int) msb<<8 | lsb;
}
// Read the uncompensated temperature value
unsigned int bmp085ReadUT()
{
unsigned int ut;
// Write 0x2E into Register 0xF4
// This requests a temperature reading
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x2E);
Wire.endTransmission();
// Wait at least 4.5ms
delay(5);
// Read two bytes from registers 0xF6 and 0xF7
ut = bmp085ReadInt(0xF6);
return ut;
}
// Read the uncompensated pressure value
unsigned long bmp085ReadUP()
{
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
// Write 0x34+(OSS<<6) into register 0xF4
// Request a pressure reading w/ oversampling setting
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x34 + (OSS<<6));
Wire.endTransmission();
// Wait for conversion, delay time dependent on OSS
delay(2 + (3<<OSS));
// Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF6);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 3);
// Wait for data to become available
while(Wire.available() < 3)
;
msb = Wire.read();
lsb = Wire.read();
xlsb = Wire.read();
up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
return up;
}