domingo, 8 de abril de 2018

Utilizando vários sensores DS18B20 em uma única porta do Arduíno UNO




O sensor DS18B20 é um ótimo sensor e tem um excelente custo benefício, neste tutorial foi utilizado a versão a prova de água, ou seja podemos aferir a temperaturas de líquidos diretamente. Você pode ver a matéria anterior sobre esse assunto neste link.

No vídeo abaixo podemos ver alguns detalhes sobre esse sensor e na sequencia temos o códigos utilizados no Arduíno.





Como podemos ver no vídeo é necessário descobrir os endereços de cada sensor no Arduíno, utilizamos ó código abaixo para isso.


Código para descobrir os endereços de cada sensor DS18B20.

/* Modificado por Projeto Básico
    www.rpsilva100.blogspot.com
    www.youtube.com/c/projetobasicos
   fevereiro 2018
   Com base no exemplo em: http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html * / /

*/
#include <OneWire.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define SENSOR_PIN 2  // Any pin 2 to 12 (not 13) and A0 to A5

/*-----( Declare objects )-----*/
OneWire  ourBus(SENSOR_PIN);  // Create a 1-wire object

void setup()  /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  Serial.println("www.youtube.com/c/projetobasicos");
  Serial.println();

  discoverOneWireDevices();  // Everything happens here!
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  // Nothing happening here
}

/*-----( Declare User-written Functions )-----*/
void discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  Serial.print("Looking for 1-Wire devices...\n\r");// "\n\r" is NewLine
  while (ourBus.search(addr)) {
    Serial.print("\n\r\n\rFound \'1-Wire\' device with address:\n\r");
    for ( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n\r");
      return;
    }
  }
  Serial.println();
  Serial.print("Done");
  ourBus.reset_search();
  return;
}

Você irá encontrar valores parecidos com esse no monitor serial do Arduíno,


{0x28, 0xFF, 0xBC, 0xB0, 0x62, 0x16, 0x04, 0x7A };
{0x28, 0xFF, 0x4D, 0xEC, 0x62, 0x16, 0x04, 0xDB };
{0x28, 0xFF, 0x77, 0x25, 0x88, 0x16, 0x03, 0x66 };

 basta copiá-lo e substituir no código abaixo no trecho parecido com este


DeviceAddress Probe01 = {0x28, 0xFF, 0xBC, 0xB0, 0x62, 0x16, 0x04, 0x7A };
DeviceAddress Probe02 = {0x28, 0xFF, 0x4D, 0xEC, 0x62, 0x16, 0x04, 0xDB };
DeviceAddress Probe03 = {0x28, 0xFF, 0x77, 0x25, 0x88, 0x16, 0x03, 0x66 };




E finalmente o código para rodar os sensores no Arduíno.

/* Modificado por Projeto Básico
    www.rpsilva100.blogspot.com
    www.youtube.com/c/projetobasicos
   fevereiro 2018
   Com base no exemplo em: http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

*/


/*-----( Import needed libraries )-----*/
// Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS_PIN 2

/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress Probe01 = {0x28, 0xFF, 0xBC, 0xB0, 0x62, 0x16, 0x04, 0x7A };
DeviceAddress Probe02 = {0x28, 0xFF, 0x4D, 0xEC, 0x62, 0x16, 0x04, 0xDB };
DeviceAddress Probe03 = {0x28, 0xFF, 0x77, 0x25, 0x88, 0x16, 0x03, 0x66 };


void setup()   /****** SETUP: RUNS ONCE ******/
{
  // start serial port to show results
  Serial.begin(9600);
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);

  // Initialize the Temperature measurement library
  sensors.begin();

  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 10);
  sensors.setResolution(Probe02, 10);
  sensors.setResolution(Probe03, 10);


Serial.print("Número de dispositivos encontrados no barramento = ");
  Serial.println(sensors.getDeviceCount());
  Serial.print("Obtendo temperaturas ... ");
  Serial.println();

}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(1000);
  Serial.println();

  // Command all devices on bus to read temperature
  sensors.requestTemperatures();

  Serial.print("Sonda 01:   ");
  printTemperature(Probe01);
  Serial.println();

  Serial.print("Sonda 02:   ");
  printTemperature(Probe02);
  Serial.println();

  Serial.print("Sonda 03:   ");
  printTemperature(Probe03);
  Serial.println();


}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{

  float tempC = sensors.getTempC(deviceAddress);

  if (tempC == -127.00)
  {
    Serial.print("Error getting temperature  ");
  }
  else
  {
    Serial.print("C: ");
    Serial.print(tempC);
     }//
}// End printTemperature
//*********( THE END )***********


Espero que tenha gostado do sensor DS18B20, caso tenha alguma dúvida poste nos comentários.

2 comentários:

  1. Olá, qual o número máximo de sensores que eu posso colocar em uma porta? no 9 ja da erro =/

    ResponderExcluir
  2. Olá!

    Eu usei 10 sensores de boa com o cabo que veio neles, porém ao aumentar o comprimento do cabo deu erro, nesse caso se faz necessário utilizar um amplificador operacional, eu estou pesquisando um circuito para utilizar nos sensores, assim que estiver pronto postarei aqui.

    Obrigado

    ResponderExcluir