Y sigue el vicio – Arduino con termómetro Dallas DS18B20
Este es un post rápido
Mis camaradas del foro Infojardin me preguntaron como diablos se podría usar el Arduino para controlar humedad, temperatura y demás cuestiones en los invernaderos
Asi que aprovechando que llego mi termómetro Dallas DS18B20 (parece un transistor normal pero esta interesante, hasta protocolo de comunicación propietario tiene, el OneWire de Dallas (ahora Maxim)), vamos armando algo para leer temperatura y de acuerdo a un rango emita alguna señal, puede ser activando un ventilador y con eso bajando temperatura en el invernadero, que se yo.
Pero empecemos por lo fácil y sencillo, ponerlo a trabajar y obtener mediciones.
Aquí esta el cableado, muy sencillo pero con un cambio respecto a otros sensores, la lectura no se realiza a través de un puerto análogo, mas bien un puerto digital y la resistencia de 4.7K ohms va del pin 3 al 2.

El código para iniciarlo esta bastante complejo, trate de usar la librería OneWire para Arduino pero en mi Mac no funciona muy bien, me salen errores al compilar (ademas que según leí en la versión 1 del IDE de Arduino cambiaron nombres de librerías que hacen referencia otras, todo un relajo).
Me encontré este código que no usa la librería, así que ahí le echan un ojo.
#define TEMP_PIN 7
void OneWireReset(int Pin);
void OneWireOutByte(int Pin, byte d);
byte OneWireInByte(int Pin);
void setup() {
digitalWrite(TEMP_PIN, LOW);
pinMode(TEMP_PIN, INPUT);
Serial.begin(9600);
delay(100);
Serial.print("temperatura:\n");
}
void loop(){
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
OneWireReset(TEMP_PIN);
OneWireOutByte(TEMP_PIN, 0xcc);
OneWireOutByte(TEMP_PIN, 0x44);
OneWireReset(TEMP_PIN);
OneWireOutByte(TEMP_PIN, 0xcc);
OneWireOutByte(TEMP_PIN, 0xbe);
LowByte = OneWireInByte(TEMP_PIN);
HighByte = OneWireInByte(TEMP_PIN);
TReading = (HighByte << 8 ) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
if (SignBit) // If its negative
{
Serial.print("-");
}
Serial.print(Whole);
Serial.print(".");
if (Fract < 10)
{
Serial.print("0");
}
Serial.print(Fract);
Serial.print("\n");
delay(5000); // 5 second delay. Adjust as necessary
}
void OneWireReset(int Pin) // reset. Should improve to act as a presence pulse
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT); // bring low for 500 us
delayMicroseconds(500);
pinMode(Pin, INPUT);
delayMicroseconds(500);
}
void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
byte n;
for(n=8; n!=0; n--)
{
if ((d & 0x01) == 1) // test least sig bit
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(5);
pinMode(Pin, INPUT);
delayMicroseconds(60);
}
else
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(60);
pinMode(Pin, INPUT);
}
d=d>>1; // now the next bit is in the least sig bit position.
}
}
byte OneWireInByte(int Pin) // read byte, least sig byte first
{
byte d, n, b;
for (n=0; n<8; n++)
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(5);
pinMode(Pin, INPUT);
delayMicroseconds(5);
b = digitalRead(Pin);
delayMicroseconds(50);
d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
}
return(d);
}
El dia que termine mi headlight quiza inicie un proyecto para controlar cosas de estas, ya sea una pecera o algun invernadero o plantas con riego programado