ARDUINO LENDO TEMPERATURA  COM  DS18B20
Autor: Ronaldo | Publicado: 2017-02-11T22:01:00-08:00



MEDINDO TEMPERATURA &nbsp;COM &nbsp;DS18B20



O sensor DS18B20 pode efetuar leituras com precisão de até ±0,5 ºC, e enviar as informações para o microcontrolador utilizando apenas 1 fio. Outras características do DS18B20:
CI : DS18B20 (Datasheet)
Tensão de operação: 3 a 5,5V


Faixa de medição de temperatura: -55 ºC a +125 ºC
Precisão: +/- 0,5 ºC entre -10 ºC e +85 ºC
Ponta de aço inoxidável (6 x 50mm), com cabo de 108 cm de comprimento
Cada sensor possui um endereço físico, assim você pode colocar vários sensores como esse em um mesmo barramento, utilizando uma única porta do microcontrolador, e efetuar a leitura individualmente.
LIGAÇÃO DO SENSOR DS18B20 AO ARDUINO
O sensor possui 3 fios para ligação ao Arduino: vermelho (Vcc), preto (GND) e amarelo (dados). &nbsp;Vamos alimentar o sensor com 5V, e utilizar a porta 3 do Arduino como entrada de dados.
Obs: Em alguns casos, esse sensor pode ser fornecido com fios nas cores: vermelho (Vcc), amarelo (GND) e verde (dados).
Para melhor visualização dos dados, utilizei display &nbsp;1.8 ST7735 TFT para gerar grafico.
No meu arduino tenho selettorde voltagem para 3V3 e 5V, usei o 3V3 com o LCD.
é recomendavel quem for trabalahr &nbsp;com 5V, use conversores de sinal, ou divisor resistivo para não danificar o lcd.
PROGRAMA DS18B20
Para o programa precisamos de algumas bibliotecas, a Dallas Temperature (download) e também a OneWire (download). Essas duas bibliotecas trabalham em conjunto para extrair os dados do sensor.
Para usar o &nbsp;display &nbsp;1.8 ST7735 TFT , é necessário baixar as bibliotecas , &nbsp;Adafruit_GFX e &nbsp;ST7735 TFT Controller. &nbsp;Descompacte as bibliotecas e copie as pastas DallasTemperature e OneWire, &nbsp;Adafruit_GFX e Adafruit &nbsp;ST7735 TFT Controller para dentro da pasta LIBRARIES da IDE do Arduino.
Links:
Dallas DS18b20 http://www.hacktronics.com/code/DallasTemperature.zip
Onewire http://www.pjrc.com/teensy/arduino_libraries/OneWire.zip
Adafruit_GFX https://github.com/adafruit/Adafruit-GFX-Library
Adafruit ST7735 TFT Controller https://github.com/adafruit/Adafruit-ST7735-Library

ESQUEMA DE LIGAÇÃO DO TFT.





ESQUEMA DE LIGAÇÃO COM ARDUINO.










&nbsp; CÓDIGO DO ARDUINO.


&nbsp; #include &lt;Adafruit_GFX.h&gt; &nbsp; &nbsp;// Core graphics library
&nbsp; #include &lt;Adafruit_ST7735.h&gt; // Hardware-specific library
&nbsp; #include &lt;OneWire.h&gt;
&nbsp; #include &lt;DallasTemperature.h&gt;
&nbsp; #include &lt;SPI.h&gt;

&nbsp; // dads do lcd
&nbsp; #define TFT_CS &nbsp; &nbsp; 10
&nbsp; #define TFT_RST &nbsp; &nbsp;9
&nbsp; #define TFT_DC &nbsp; &nbsp; 8
&nbsp; #define TFT_SCLK 13
&nbsp; #define TFT_MOSI 11

&nbsp; // parametros do sensor

&nbsp; // Porta do pino de sinal do DS18B20
&nbsp; #define ONE_WIRE_BUS 3
&nbsp; OneWire oneWire(ONE_WIRE_BUS);
&nbsp; float tempMin = 999;
&nbsp; float tempMax = 0;
&nbsp; int barra = 0;
&nbsp; char conta = 0;
&nbsp; DallasTemperature sensors(&amp;oneWire);
&nbsp; DeviceAddress sensor1;

&nbsp; //inicializacao display
&nbsp; Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, &nbsp;TFT_DC, TFT_RST);

&nbsp; void setup(void) {
&nbsp; &nbsp; Serial.begin(9600);
&nbsp; &nbsp; sensors.begin();
&nbsp; &nbsp; // Localiza e mostra enderecos dos sensores
&nbsp; &nbsp; Serial.println("Localizando sensor DS18B20...");
&nbsp; &nbsp; Serial.print("Foram encontrados ");
&nbsp; &nbsp; Serial.print(sensors.getDeviceCount(), DEC);
&nbsp; &nbsp; Serial.println(" sensores.");
&nbsp; &nbsp; if (!sensors.getAddress(sensor1, 0))
&nbsp; &nbsp; &nbsp; Serial.println("Sensor nao encontrado !");
&nbsp; &nbsp; // Mostra o endereco do sensor encontrado no barramento
&nbsp; &nbsp; Serial.print("Endereco sensor: ");
&nbsp; &nbsp; mostra_endereco_sensor(sensor1);
&nbsp; &nbsp; Serial.println();
&nbsp; &nbsp; //inicializa lcd
&nbsp; &nbsp; tft.initR(INITR_BLACKTAB); &nbsp;// You will need to do this in every sketch
&nbsp; &nbsp; tft.fillScreen(ST7735_BLACK);
&nbsp; &nbsp; //tft print function!
&nbsp; &nbsp; tft.setTextColor(ST7735_YELLOW);
&nbsp; &nbsp; tft.setRotation(3);
&nbsp; &nbsp; tft.setTextSize(2);
&nbsp; &nbsp; tft.setCursor(35, 10);
&nbsp; &nbsp; tft.println(" TEMP = &nbsp;C");
&nbsp; &nbsp; tft.setTextSize(1);
&nbsp; &nbsp; tft.setCursor(131, 10);
&nbsp; &nbsp; tft.println("O");
&nbsp; &nbsp; termo();
&nbsp; &nbsp; tft.setTextColor(ST7735_WHITE);
&nbsp; &nbsp; tft.setTextSize(0);
&nbsp; &nbsp; tft.setCursor(19, 59);
&nbsp; &nbsp; tft.print("50");
&nbsp; &nbsp; tft.setTextSize(0);
&nbsp; &nbsp; tft.setCursor(19, 106);
&nbsp; &nbsp; tft.print("0");
&nbsp; &nbsp; tft.setTextSize(0);
&nbsp; &nbsp; tft.setCursor(19, 7);
&nbsp; &nbsp; tft.print("100");
&nbsp; &nbsp; tft.setTextSize(0);
&nbsp; &nbsp; tft.setCursor(19, 82);
&nbsp; &nbsp; tft.print("25");
&nbsp; &nbsp; tft.setTextSize(0);
&nbsp; &nbsp; tft.setCursor(19, 32);
&nbsp; &nbsp; tft.print("75");
&nbsp; &nbsp; tft.setTextColor(ST7735_BLUE);
&nbsp; &nbsp; tft.setTextSize(2);
&nbsp; &nbsp; tft.setCursor(10, 40);
&nbsp; &nbsp; tft.print(" &nbsp; LIDA");
&nbsp; &nbsp; tft.setTextSize(2);
&nbsp; &nbsp; tft.setCursor(20, 65);
&nbsp; &nbsp; tft.print(" &nbsp; MIN");
&nbsp; &nbsp; tft.setTextSize(2);
&nbsp; &nbsp; tft.setCursor(20, 90);
&nbsp; &nbsp; tft.print(" &nbsp; MAX");
&nbsp; }

&nbsp; void loop() {

&nbsp; &nbsp; // Le a informacao do sensor
&nbsp; &nbsp; if (sensors.getAddress(sensor1, 0))
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; sensors.requestTemperatures();
&nbsp; &nbsp; &nbsp; float tempC = sensors.getTempC(sensor1);
&nbsp; &nbsp; &nbsp; // Atualiza temperaturas minima e maxima
&nbsp; &nbsp; &nbsp; if (tempC &lt; tempMin)
&nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; tempMin = tempC;
&nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; if (tempC &gt; tempMax)
&nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; tempMax = tempC;
&nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; barra = (int)tempC;
&nbsp; &nbsp; &nbsp; // &nbsp;EXIBE NO DISPLAY

&nbsp; &nbsp; &nbsp; tft.setTextColor(ST7735_RED);
&nbsp; &nbsp; &nbsp; apaga_linha(100, 40);
&nbsp; &nbsp; &nbsp; tft.setTextSize(2);
&nbsp; &nbsp; &nbsp; tft.setCursor(100, 40);
&nbsp; &nbsp; &nbsp; tft.println(tempC, 1);
&nbsp; &nbsp; &nbsp; //tft.print("C");

&nbsp; &nbsp; &nbsp; tft.setTextColor(ST7735_GREEN);
&nbsp; &nbsp; &nbsp; apaga_linha(100, 65);
&nbsp; &nbsp; &nbsp; tft.setCursor(100, 65);
&nbsp; &nbsp; &nbsp; tft.println(tempMin, 1);

&nbsp; &nbsp; &nbsp; tft.setTextColor(ST7735_YELLOW);
&nbsp; &nbsp; &nbsp; apaga_linha(100, 90);
&nbsp; &nbsp; &nbsp; tft.setCursor(100, 90);
&nbsp; &nbsp; &nbsp; tft.println(tempMax, 1);
&nbsp; &nbsp; &nbsp; tft.setTextColor(ST7735_GREEN);
&nbsp; &nbsp; &nbsp; tft.setTextSize(1);
&nbsp; &nbsp; &nbsp; apaga_linha(99, 104);
&nbsp; &nbsp; &nbsp; tft.setCursor(45, 110);
&nbsp; &nbsp; &nbsp; tft.print(" &nbsp;SENSOR OK &nbsp;");
&nbsp; &nbsp; &nbsp; if (tempMin &gt; 50)
&nbsp; &nbsp; &nbsp; tempMin = 999;
&nbsp; &nbsp; &nbsp; tft.fillRect(5, 12, 7, 99, ST7735_RED);
&nbsp; &nbsp; &nbsp; tft.fillRect(5, 12, 7, 100 - barra, ST7735_BLACK);
&nbsp; &nbsp; }
&nbsp; &nbsp; else //(!sensors.getAddress(sensor1, 0))
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; tft.setTextColor(ST7735_RED);
&nbsp; &nbsp; &nbsp; tft.setTextSize(1);
&nbsp; &nbsp; &nbsp; apaga_linha(99, 104);
&nbsp; &nbsp; &nbsp; tft.setCursor(45, 110);
&nbsp; &nbsp; &nbsp; tft.print(" &nbsp;SENSOR &nbsp;ERRO ");
&nbsp; &nbsp; &nbsp; tempMin = 999;
&nbsp; &nbsp; &nbsp; tempMax = 0;
&nbsp; &nbsp; }

&nbsp; &nbsp; //delay(1000);
&nbsp; &nbsp; conta++;
&nbsp; &nbsp; if (conta == 1)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; tft.setTextSize(1);
&nbsp; &nbsp; &nbsp; tft.setCursor(45, 110);
&nbsp; &nbsp; &nbsp; tft.print(" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |");
&nbsp; &nbsp; }
&nbsp; &nbsp; if &nbsp;(conta == 2)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; tft.setTextSize(1);
&nbsp; &nbsp; &nbsp; tft.setCursor(45, 110);
&nbsp; &nbsp; &nbsp; tft.print(" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /");
&nbsp; &nbsp; }

&nbsp; &nbsp; if &nbsp;(conta == 3)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; tft.setTextSize(1);
&nbsp; &nbsp; &nbsp; tft.setCursor(45, 110);
&nbsp; &nbsp; &nbsp; tft.print(" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -");
&nbsp; &nbsp; }
&nbsp; &nbsp; if (conta == 4)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; tft.setTextSize(1);
&nbsp; &nbsp; &nbsp; tft.setCursor(45, 110);
&nbsp; &nbsp; &nbsp; tft.print(" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \\");
&nbsp; &nbsp; &nbsp; conta = 1;
&nbsp; &nbsp; }

&nbsp; &nbsp; delay(500);
&nbsp; }
&nbsp; void apaga_linha (int16_t x, int16_t y)
&nbsp; {
&nbsp; &nbsp; tft.fillRect(x, y, 50, 16, ST7735_BLACK);
&nbsp; }

&nbsp; void termo (void)
&nbsp; {
&nbsp; &nbsp; tft.drawRect(4, 11, 9, 100, ST7735_WHITE);
&nbsp; &nbsp; tft.fillRect(5, 12, 7, barra, ST7735_YELLOW);
&nbsp; &nbsp; tft.drawRect(40, 4, 119, 122, ST7735_MAGENTA);


&nbsp; &nbsp; //MONTA DIVISOES TERMOMETRO
&nbsp; &nbsp; tft.drawLine(14, 11, 17, 11, ST7735_WHITE); // LINHA DE 100
&nbsp; &nbsp; tft.drawLine(13, 86, 16, 86, ST7735_WHITE); // LINHA DE 75
&nbsp; &nbsp; tft.drawLine(13, 36, 16, 36, ST7735_WHITE); // LINHA DE 25
&nbsp; &nbsp; tft.drawLine(13, 61, 16, 61, ST7735_WHITE); // LINHA DE 50
&nbsp; &nbsp; tft.drawLine(14, 110, 16, 110, ST7735_WHITE); // LINHA DE 0
&nbsp; &nbsp; tft.drawLine(13, 98, 14, 98, ST7735_WHITE); //
&nbsp; &nbsp; tft.drawLine(13, 73, 14, 73, ST7735_WHITE); //
&nbsp; &nbsp; tft.drawLine(13, 48, 14, 48, ST7735_WHITE); //
&nbsp; &nbsp; tft.drawLine(13, 23, 14, 23, ST7735_WHITE); // L
&nbsp; &nbsp; tft.drawCircle(8, 118, 8, ST7735_WHITE);
&nbsp; &nbsp; tft.fillCircle(8, 118, 7, ST7735_RED);
&nbsp; }
&nbsp; void mostra_endereco_sensor(DeviceAddress deviceAddress)
&nbsp; {
&nbsp; &nbsp; for (uint8_t i = 0; i &lt; 8; i++)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; // Adiciona zeros se necessário
&nbsp; &nbsp; &nbsp; if (deviceAddress[i] &lt; 16) Serial.print("0");

&nbsp; &nbsp; }
&nbsp; }