Temperature Sensor with RobotDyn Uno - dimaatmelodromru/techdoc GitHub Wiki

Temperature Sensor with RobotDyn Uno, SD Card Logger, RTC and various displays

In this example we create a simple temperature data logger. We use an inexpensive DHT11 sensor, but you can apply given schematics and sketch to a more precise BMP280 sensor as well, except for the LED matrix due to conflicting interface.

NB: All temperatures in this article are given in Celsium degrees. To convert these values to the Fahrenheit scale please use the following formula:

T(°F) = T(°C) × 1.8 + 32

DHT11 Features

  • Power: DC 3.5V - 5.5V
  • Current:
    • 0.3mA while measuring;
    • 60μA at standby;
  • Humidity measurement (20%-80%) precision: 95%
  • Temperature measurement (0-50 °С) precision: 98%
  • Maximum poll frequency: 1Hz
  • Dimensions: 15.5x12x5.5mm / 0.6x0.5x0.22i"

BMP280 Features

  • Power: 1.71V - 3.6V
  • Interface: I2C or SPI
  • Current: 2.7μA while measuring at 1Hz poll
  • Atmospheric pressure: 300hPa - 1100hPa (±0.12hPa), equivalent to altitude range of -500 to 9000 m above sea level
  • Temperature: -40°С … +85°С (±0.01°С)
  • I2C max frequency: 3.4MHz
  • SPI max frequency: 10MHz
  • Dimensions: 21x18mm / 0.83x0.7"

Parts Being Used

  1. DHT11 sensor for temperature and humidity measurement or BMP280 for temperature, atmospheric pressure or altitude measurement
  2. 8x8 LED matrix (x4) or 16x2 LCD text display or 7-segment LED display
  3. Jumper wires
  4. RobotDyn Uno controller
  5. Data Logger SD card + Real Time Clock Shield

Pin Wiring

Arduino DH11 BMP280 LCD1602 LED matrix Segment LED SD RTC
5V VCC 5V 5V 5V 5V 5V 5V
GND GND GND GND GND GND GND GND
13 SCK SCL CLK
12 SDO DO
11 SDI SDI DI
9 CS
8 OUT CS
7 DIO
6 CLK
5 CS
4
A4 SCL
A5 SDA SCL

Connect LED matrices in a chain, SDO->SDI, CLK-CLK, CS->CS, 5V->5V, GND->GND

Software Libraries

Arduino IDE Settings

Sketch

#include "RTClib.h" //Real Time Clock Library
#include <LiquidCrystal_I2C.h>  //LCD1602 Library
#include <Adafruit_BMP280.h>  //BMP280 Library
#include <Adafruit_GFX.h> //Graphics Core Library

#include "TM1637.h" //Segment LED Library
#include "DHT.h"  //DH11 Sensor Library
#include <SPI.h>  //SPI Bus Library
#include <SD.h> //SD Card Library
#include <Max72xxPanel.h> //LED Matrix Display Library

//#define useSD
#define useDH11
//#define useBMP280
#define useLCD1602
//#define useTICKER   //if using LED Matrix Display
//#define useSEGMSCR  //if using Segment LED

#define addrLCD       0x38  //LCD1602 address
#define dht11PIN      8    //DH11 pin
#define sdPIN         9       //SD pin
#define csMtrxPIN     5       //Matrix CS pin
#define countTicker   4       //number of LED Matrix modules in use

String paramStr;
float temperature;
float pressure;
float altitude;
float humidity;

#ifdef useDH11
  DHT dht11(dht11PIN, DHT11);
#endif

#ifdef useBMP280
  #define BMP_SCK  13
  #define BMP_MISO 12
  #define BMP_MOSI 11
  #define BMP_CS   8
  Adafruit_BMP280 bmp280(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);
#endif

#ifdef useLCD1602
  LiquidCrystal_I2C lcd(addrLCD,16,2);
#endif

#ifdef useSEGMSCR
  TM1637 tm1637(6,7);
#endif

#ifdef useTICKER
  Max72xxPanel matrix = Max72xxPanel(csMtrxPIN, countTicker, 1);
#endif

RTC_DS1307 RTC;

void setup(){
  Serial.begin(115200);
  
  Wire.begin(); //I2C init

  #ifdef useLCD1602 //LCD1602 init
    lcd.init();
    lcd.clear();
    lcd.backlight();
  #endif

  #ifdef useSEGMSCR
    tm1637.init();
    tm1637.set(BRIGHT_TYPICAL);
  #endif

  #ifdef useBMP280   //BMP280 init
    if(!bmp280.begin()){
      Serial.println("BMP280 Error");
    }
  #endif
  
  #ifdef useDH11   //DH11 init
    dht11.begin();
  #endif

  #ifdef useSD
  if(!SD.begin(sdPIN)){
    Serial.println("SD Error");
    Serial.println("Break start");while(1){}
  }
  #endif

  RTC.begin();
  //RTC.adjust(DateTime(__DATE__, __TIME__)); //set time to PC clock
  
  if(!RTC.isrunning()){
    Serial.println("RTC Error");
    Serial.println("Break start");while(1){}
  }
  Serial.println("Std");
}
 
void loop(){
  DateTime now = RTC.now();
  
  #ifdef useDH11
    temperature = dht11.readTemperature();
    humidity = dht11.readHumidity();
    #ifdef useLCD1602
      lcd.setCursor(5,0);
      if(now.hour() < 10){lcd.print("0");}lcd.print(now.hour());lcd.print(":");
      if(now.minute() < 10){lcd.print("0");}lcd.print(now.minute());
      lcd.setCursor(0,1);lcd.print("T=");lcd.print(temperature) + "C";
      lcd.setCursor(9,1);lcd.print("H=");lcd.print(humidity);
    #endif
    #ifdef useTICKER
      String mxStr = "";
      if(now.hour() < 10){mxStr += "0";}mxStr += String(now.hour()) + ":";
      if(now.minute() < 10){mxStr += "0";}mxStr += String(now.minute());
      mxStr += " t=" + String(int(temperature)) + "C  H=" + String(int(humidity)) + "%";
      matrixPrint(mxStr);
    #endif
    #ifdef useSEGMSCR
      String dTemp = String((int)temperature);
      tm1637.display(0, dTemp[0]-48);
      tm1637.display(1, dTemp[1]-48);
      tm1637.display(3, 12);
    #endif
    
  #endif

  #ifdef useBMP280
    temperature = bmp280.readTemperature();
    pressure = bmp280.readPressure()*0.00750063755419211;
    altitude = bmp280.readAltitude(1013.25);
    #ifdef useLCD1602
      lcd.setCursor(5,0);
      if(now.hour() < 10){lcd.print("0");}lcd.print(now.hour());lcd.print(":");
      if(now.minute() < 10){lcd.print("0");}lcd.print(now.minute());
      lcd.setCursor(0,1);lcd.print("T=");lcd.print(temperature) + "C";
      lcd.setCursor(9,1);lcd.print("P=");lcd.print(pressure);
    #endif
    #ifdef useTICKER
      String mxStr = "";
      if(now.hour() < 10){mxStr += "0";}mxStr += String(now.hour()) + ":";
      if(now.minute() < 10){mxStr += "0";}mxStr += String(now.minute());
      mxStr += " t=" + String(int(temperature)) + "C P=" + String(int(pressure)) + " A=" + String(int(altitude));
      matrixPrint(mxStr);
    #endif
    #ifdef useSEGMSCR
      String dTemp = String((int)temperature);
      tm1637.display(0, dTemp[0]-48);
      tm1637.display(1, dTemp[1]-48);
      tm1637.display(3, 12);
    #endif
  #endif

  paramStr = "";
  paramStr += String(now.hour());paramStr += ":";
  paramStr += String(now.minute());paramStr += ":";
  paramStr += String(now.second());
  paramStr += " T:";paramStr += String(temperature);
  paramStr += " P:";paramStr += String(pressure);
  paramStr += " A:";paramStr += String(altitude);
  paramStr += " H:";paramStr += String(humidity);
  
  #ifdef useSD
  File dataFile = SD.open("file.txt", FILE_WRITE);
  dataFile.println(paramStr);
  dataFile.close();
  #endif

  Serial.println(paramStr);

}

void matrixPrint(String tape){
  #ifdef useTICKER
  int spacer = 1;
  int width = 5 + spacer;
  for ( int i = 0 ; i < width * tape.length() + matrix.width() - 1 - spacer; i++ ) {

    matrix.fillScreen(LOW);

    int letter = i / width;
    int x = (matrix.width() - 1) - i % width;
    int y = (matrix.height() - 8) / 2; // center the text vertically

    while ( x + width - spacer >= 0 && letter >= 0 ) {
      if ( letter < tape.length() ) {
        matrix.drawChar(x, y, tape[letter], HIGH, LOW, 1);
      }

      letter--;
      x -= width;
    }

    matrix.write(); // Send bitmap to display

    delay(30);
  }
  #endif
}

DH11 + LCD1602 + SD + RTC

DH11 + LED matrix + SD + RTC

DH11 + сегментный индикатор + SD + RTC

Results

When your device is being used for some time, file.txt will be created on the SD card with the following contents:

10:04:07 T:34.73 P:734.59 A:376.27 H:0.00
10:04:09 T:34.73 P:734.59 A:376.27 H:0.00
10:04:10 T:34.73 P:734.59 A:376.27 H:0.00

⚠️ **GitHub.com Fallback** ⚠️