Skip to content

Instantly share code, notes, and snippets.

@arduinoboard
Created March 20, 2012 19:17
Show Gist options
  • Save arduinoboard/2140072 to your computer and use it in GitHub Desktop.
Save arduinoboard/2140072 to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino Uno with a serial number of CC2B9309F0DG6LL0
#include <OneWire.h>
#define ZeroPercentVoltage 0.8;
float val = 0;
float RH = 0;
float my_room_temperature = 20; //in degrees C !
float max_voltage = 3.27;
OneWire ds(2); // Sensor DS18b20 on pin 2
void setup (void)
{
Serial.begin(9600);
}
void loop(void)
{
val = analogRead(A0);
delay(500);
//Serial.println(val);
//my_room_temperature = 20; // If you have temperature reading, put it here (centigrade!)
max_voltage = (3.27-(0.006706*my_room_temperature)) ; // The max voltage value drops down 0.006705882 for each degree C over 0C. The voltage at 0C is 3.27 (corrected for zero precent voltage)
float subval = ((val/1023)*5)-ZeroPercentVoltage;
RH = (subval/max_voltage)*100;
Serial.print("RH:");
Serial.println(RH);
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
if ( !ds.search(addr))
//search for next object on Onewire bus. For the first found, continue loop, if none found, restart from 0
{
ds.reset_search();
delay(250);
return;
}
if (OneWire::crc8(addr, 7) != addr[7]) //Check if there is no errors on transmission
{
Serial.println("CRC invalide...");
return;
}
if(addr[0] == 0x10) //Check if it's a DS18b20 on the bus or not
{
type_s = 1;
} else type_s=0;
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000);
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++)
{ // we need 9 bytes
data[i] = ds.read();
}
// convert the data to actual temperature
unsigned int raw = (data[1] << 8) | data[0];
if (type_s)
{
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10)
{
// count remain gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
}
else
{
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
}
celsius = (float)raw / 16.0;
my_room_temperature = celsius;
Serial.print("TEMP:");
Serial.println(celsius);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment