Klausj
Published © GPL3+

Compare a bunch of DS18B20 Temperature Sensors

To investigate the internal climate inside a room or even a fridge it is useful to measure the temperatures at different places precisely.

IntermediateFull instructions provided2 hours89
Compare a bunch of DS18B20 Temperature Sensors

Things used in this project

Story

Read more

Code

Library for LCD TFT 2,4" shield 8-bit parallel interface, ILI9341

Arduino
Supports essential function to control this TFT
No preview (download only).

Temperature Logger for up to 6 DS18B20

Arduino
Draws log graphs on TFT LCD
/*
  Temperature logger
  max 6 x DS18B20  connected to A5
  TFT LCD 2.4", 320 x 240 shield 8-bit parallel
  autoscaling, taking 320 samples in one hour
  scrolling by software
*/
#include <DallasTemperature.h>
// this lib requires <OneWire.h>
const byte thePin = A5;
OneWire oneWire(thePin);
DallasTemperature s(&oneWire);
int devNum; // Number of devices
const int M = 320; // x-axis
const int N = 6; // max number of sensors
float logs[M][N];
const int x_grid = M / 6;
byte y_grid;

#include "ILI9341.h"
const word color[] = { BLUE, RED, GREEN, CYAN, MAGENTA, YELLOW};
ILI9341 tft;
const int width = M;
const int height = 240;
// portrait mode: 320 px = 1 hour
const int dt = 3600000 / M;
int count = M;
int mini, maxi, minOld, maxOld;

void setup() {
  Serial.begin(9600);
  Serial.println(F(__FILE__));
  // ----- Sensoren DALLAS -----:
  const byte PREC = 12;
  s.begin();
  devNum = s.getDeviceCount();
  Serial.println(devNum);
  DeviceAddress da;
  for (int i = 0; i < devNum; i++)
    if (s.getAddress(da, i))
      s.setResolution(da, PREC);
  // ----- TFT -----:
  tft.begin();
  tft.fillScreen(BLACK);
}

void loop() {
  long t = millis() + dt;
  boolean change = false;
  getValues();
  // Skalierung:
  mini = 100; // impossible values
  maxi = -100;
  for (int i = 0; i < devNum; i++)
    for (int j = count; j < M; j++) {
      mini = min(mini, logs[j][i]);
      maxi = max(maxi, logs[j][i]);
    }
  maxi++;
  if ( (mini != minOld) || (maxi != maxOld) ) {
    minOld = mini;
    maxOld = maxi;
    change = true;
  }
  Serial.print(mini);
  Serial.print(" ");
  Serial.println(maxi);

  drawGrid(change); // neu zeichnen
  drawLogs();
  //while (millis() < t);
  if (count > 1) count--;
}

void getValues() {
  s.requestTemperatures();
  DeviceAddress da;// messen:
  for (int i = 0; i < devNum; i++)
    if (s.getAddress(da, i)) {
      Serial.print(F("\t"));
      float neu = s.getTempC(da);
      if (neu == DEVICE_DISCONNECTED_C)
        Serial.println("error");
      else {
        Serial.print(neu);
      }
      // kopiere alle um 1 nach links:
      for (int j = 0; j <= M - 2; j++)
        logs[j][i] = logs[j + 1][i];
      // trage neuen Wert hinten (rechts) ein:
      logs[M - 1][i] = neu; // <-----
    }
  Serial.println();
}

void drawLogs() {
  for (int i = 0; i < devNum; i++) {
    for (int j = count; j <= M - 2; j++) {
      int x1 = j + 1; // from
      int x2 = j + 2; // to
      // erase:
      int y1 = t2y(logs[j - 1][i]);
      int y2 = t2y(logs[j][i]); // erase
      // loesche alte Linie:
      tft.drawLine(x1, y1, x2, y2, BLACK);
      // draw:
      int z1 = y2;
      int z2 = t2y(logs[j + 1][i]); // draw
      // Schreibe neue Linie
      tft.drawLine(x1, z1, x2, z2, color[i]);
    }
  }
}

int t2y(float t) {
  return height * (1 - (t - mini)  / (maxi - mini));
}

void drawGrid(boolean erase) {
  if (erase) tft.fillScreen(BLACK);
  tft.setCursor(2, 2);
  tft.print("sensors: ");
  tft.print(devNum);
  y_grid = height / (maxi - mini);
  for (int y = 0, t = maxi; y <= height; y = y + y_grid, t--) {
    tft.drawDottedFastHLine(0, y, width, CYAN);
    tft.setCursor(2, y - 10);
    tft.print(t);
    tft.print(" C");
    tft.drawCircle(16, y - 10, 2, WHITE);
  }
  for (int x = 0, i = 0;
       x < width;
       x = x + x_grid, i = i + 10) {
    tft.drawDottedFastVLine(x, 0, height, CYAN);
    tft.setCursor(x - 12, height - 10);
    tft.print(i);
  }
  // x-Achse
  tft.drawFastHLine(0, height, width, WHITE);
  // y-Achse
  tft.drawFastVLine(0, 0, height, WHITE);
}

Credits

Klausj

Klausj

66 projects • 5 followers

Comments