CBOX-Shout
Procurar
Quem está conectado?
Há 1 usuário online :: 0 registrados, 0 invisíveis e 1 visitante Nenhum
O recorde de usuários online foi de 34 em Dom Ago 13, 2023 1:30 pm
Estatísticas
Temos 318 usuários registradosO último membro registrado é Buggsoulmitor
Os nossos membros postaram um total de 1475 mensagens em 209 assuntos
Arduino + DS18B20
Página 1 de 1
Arduino + DS18B20
Deixo aqui o meu código deste excelente sensor de temperatura testado por mim, estes sensores têm um endereço diferente para cada um, ou seja podemos implementar quantos quisermos num projecto(limitado pela tecnologia OneWire).
DS18B20 Datasheet
Código:
Comentem![Wink](https://2img.net/i/fa/i/smiles/icon_wink.gif)
DS18B20 Datasheet
Código:
- Código:
#include <OneWire.h>
//This is an example.
//The DS18B20 is conected with Vdd and GND to GND on the arduino because it will work with parasite power mode.
//The data wire must have a pull up resistor from 4,7k Ohms to 10k Ohms.
OneWire ds(10); // onewire pin
int HighByte, LowByte, TReading, SignBit, Tc_100, Fract, Whole;
void setup(void)
{
Serial.begin(9600);
}
void loop(void)
{
byte i;
byte present = 0;
byte data[12];
byte addr[8];
if (!ds.search(addr))
{
Serial.print("No more addresses.\n");
ds.reset_search();
return;
}
if ( OneWire::crc8( addr, 7) != addr[7])
{
Serial.print("CRC is not valid!\n");
return;
}
if ( addr[0] == 0x10 || addr[0] == 0x28) {}
else
{
Serial.print("Device family is not recognized: 0x");
Serial.println(addr[0],HEX);
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1);// start conversion, with parasite power on at the end
delay(1000);// maybe 750ms is enough, maybe not
present = ds.reset();
ds.select(addr);
ds.write(0xBE);//request data
//Read data from the Sensor
for ( i = 0; i < 9; i++)// we need 9 bytes
{
data[i] = ds.read();
}
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit)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)Serial.print("-");//negative temperatures
Serial.print(Whole);//byte Whole;->0-255
Serial.print(".");//dot between integrer and fraction
if (Fract < 10)Serial.print("0");
Serial.print(Fract);
//Sensor Adress Display
Serial.print(" R=");
for( i = 0; i < 8; i++)
{
Serial.print(addr[i], HEX);
}
Serial.print("\n");
}
Comentem
![Wink](https://2img.net/i/fa/i/smiles/icon_wink.gif)
HFernandes- Moderador
- Mensagens : 306
Data de inscrição : 28/09/2011
Idade : 33
Localização : Coimbra/Mealhada
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos
|
|