Temperatur und Luftfeuchtigkeit mit dem Arduino Uno messen

Measure temperature and air humidity with the Arduino Uno

To build a measuring device for temperature and air humidity, which displays values on a colour display (the colour changes depending on how hot or cold it is) you can use an Arduino Uno microcontroller board and a Grove expansion kit.

Suitable for: Beginners with coding experience
Time requirement: approx. 1.5 hours
Budget: approx. £65
You will need: 1x Arduino Uno Rev. 3, 1x Arduino Shield – Base Shield V2 (Grove Base-Shield), 1x Grove LCD with RGB-Backlight, 1x Arduino Grove temperature and humidity sensor pro (precise), 1x USB-Kabel 2.0, Hi-Speed A-St to B-St , 1m

Also required: Internet connection, computer with browser, power supply for the Arduino

Hardware

1. The Arduino Uno Rev. 3

The Arduino Uno microcontroller board has everything you need for a microcontroller project, and you don’t require extensive knowledge about electronics. It also offers manifold opportunities for demanding projects.

Programming is made via a USB port, which doubles as an interface for the distribution of data. To power the Arduino, connect it to a power supply or use batteries with 9-12 volt (pay attention to the polarity of the plug – plus on the inside, minus on the outside) or connect via the USB port.

2. The Grove Base Shield

The temperature and air humidity sensors, as well as the display, come from the grove extension kit, which allows for a quick and easy turnaround. The Grove Base Shield is simply put on the Ardunio and offers four analogue, eight digital and four I²C connections with a uniform connector kit.

The LED status and rest button of the Arduino board are covered by the Base Shield but they can be accessed on the Base Shield itself.

Important: For an Arduino, the voltage switch on the Grove Basie Shield needs to be on 5 volt.

3. The temperature and air humidity sensor

The Grove temperature and humidity sensor is based on a DHT22 temperature and humidity sensor, which measures temperature to ± 0.5 °C exactly and the relative air humidity to ± 2 %.

The sensor requires a random, analogue connection and we suggest using an A0 for this project.

4. The display

The Grove LCD with RGB-Backlight possesses two lines with 16 characters respectively and allows for an individual definition of characters. The background colour can be adjusted.

The display requires a random I²C Anschluss. As I²C is a bus system, an arbitrary one can be chosen.

The Software

Arduino Studio

The Arduino Studio is the development environment for the Arduino. This is where programmes are created (so-called “sketches”) and uploaded to the Arduino. Moreover, the serial interface that is made available via the USB port is accessible.

Arduino Studio can be downloaded free of charge from the Arduino website: https://www.arduino.cc/en/Main/Software

Once the Arduino is connected to the PC, simply go to the subsection “tools” -> “port” in the menu and select “Arduino/Genuino”, to establish a working connection to the Arduino.

The libraries

To use the individual Shields and Grove building blocks, the respective libraries provide access to the required programming interfaces. The library for the SD-Shield is available by default; for the Grove elements, the following need to be installed:

Grove temperature and air humidity sensor pro

Download: https://github.com/Seeed-Studio/Grove_Temperature_And_Humidity_Sensor/archive/master.zip

Documentation: http://wiki.seeed.cc/Grove-Temperature_and_Humidity_Sensor_Pro/

Grove LCD RGB display

Download: https://github.com/Seeed-Studio/Grove_LCD_RGB_Backlight/archive/master.zip

Documentation: http://wiki.seeed.cc/Grove-LCD_RGB_Backlight/

Download the libraries and install them by choosing “sketch” -> “Integrate library” -> “add ZIP libraries” in the menu.

For every library, you can access sample programmes in “files” -> “examples”

The first programme: retrieving data from the sensor

To retrieve data from the temperature sensor to the serial interface, you need to use the following code:

#include <SPI.h> // Load library for serial interface
 #include <DHT.h> // Load library for sensor

// Create entity for serial interface.
 // A0 is the connection at the Grove Base Shield, DHT22 refers to the sensor type
 DHT dht(A0, DHT22);

void setup() { // Is performed once at the beginning

Serial.begin(9600); // Initialise serial interface
 dht.begin(); // Initialise sensor
 }

void loop() { // Performed in an infinite loop
 float humidity = dht.readHumidity(); // Retrieve data
 float temperature = dht.readTemperature(); // Retrieve data

if (isnan(temperature) || isnan(humidity)) { // Was retrievement successful?
 Serial.println("Error when reading DHT");
 } else {
 // Display data
 Serial.print(temperature);
 Serial.print(" | ");
 Serial.print(humidity);
 Serial.println();
 }
 delay(1000); // Pause programme for 1s
 }

Every Arduino programme has two methods:

setup() is used once, at the start of the programme – i.e. when it is newly installed to the Arduino, the reset button is pressed or the electricity supply is interrupted. Generally, this is where devices are initialised.

loop() is used in a continuous loop once the setup is completed. This is where the actual programming logic happens – data is retrieved from the sensor, the success of the retrieval  is tested and data is transferred to the serial interface. The delay at the end is of particular importance: This is where the entire execution is interrupted for one second, so the console can be read on the computer.

The programme is built with “sketch” à “verify/compile” in order to copy it onto the Arduino. You will also receive notifications for programming errors and potentially occurring issues. It is possible that a red alert will pop up in the status output of Arduino Studio as NAN is newly defined. This stems from the library of the temperature sensor and can be ignored.

With “sketch” → “upload”, the programme is written on the Arduino and is built automatically in the process. You can see the following output by choosing “Tools” à “Serial monitor”:

26.10 | 64.50
26.20 | 64.50
26.20 | 64.50

Controlling the display

The first steps for the LCD display are to define an entity of the display and an array with the pixel fields for a new character. This character is registered in setup() with the display. In loop(), we choose the colour red and put the text for the new character in the display.

#include <Wire.h>
#include <rgb_lcd.h>

rgb_lcd lcd; // Create entity for display interface

byte HEART_SYMBOL = 0; // Telling name for character

byte heart[8] = { // Allocation of individual pixel
 0b00000,
 0b01010,
 0b11111,
 0b11111,
 0b11111,
 0b01110,
 0b00100,
 0b00000
 };

void setup(){
 lcd.begin(16, 2); // Initialise display - 2 lines with 16 characters respectively
 lcd.createChar(HEART_SYMBOL, heart); // register heart symbol
 }

void loop() {
 int red = 255;
 int green = 0;
 int blue = 0;
 lcd.setRGB(red, green, blue);
 lcd.setCursor(0, 0); // Move cursor to the beginning of the first line on the display
 lcd.print("I ");
 lcd.write(HEART_SYMBOL);
 lcd.print(" Arduino");
 lcd.setCursor(0, 1); // Move cursor to the beginning of the second line on the display
 lcd.print("===========");
 }

The programme is, again, written on the Arduino by selecting “Sketch” à “Upload”. Results are immediately visible on the display.

The finished programme

To show both temperature and air humidity on the display, both programmes are combined. The display is not yet able to display a –character, so we define this in a similar way to that of the previous programme.

In setup(), serial interface, sensor and display are initialised and the new symbol is registered with the display.

In loop(), temperature and air humidity are retrieved and either colour values are calculated from the temperature or everything is displayed. Formulas are chosen to display a strong, blue colour and a strong, red colour if the temperature is greater or equals ‘hottest’. If the green value remains on 0, the transition from blue to red goes through purple. If the transition is to remain white and the display should only be coloured at the respective ends, alternative formulas can be used.

The method limit (float colour) ensures that colour values remain in the acceptable range of 0.255 – otherwise, temperatures lesser than ‘coldest’ would, for example, yield negative colour values. The conversion of a floating comma number (float) to an integer colour value (int) happens automatically, and the decimal digits are omitted.

#include <Wire.h>
#include <rgb_lcd.h>
#include <DHT.h>

rgb_lcd lcd;
 DHT dht(A0, DHT22);

byte DEGREE_SYMBOL = 0;
 byte degree[8] = {
 0b00000,
 0b00010,
 0b00101,
 0b00010,
 0b00000,
 0b00000,
 0b00000,
 0b00000
 };

void setup(){
 Serial.begin(9600); // Initialise serial port
 lcd.begin(16, 2); // Initialise display – two lines with 16 characters respectively
 lcd.createChar(DEGREE_SYMBOL, degree); // Register the new "°" character on the display
 dht.begin(); // Initialise sensor
 }

void loop() {
 float humidity = dht.readHumidity();
 float temperature = dht.readTemperature();

if (isnan(temperature) || isnan(humidity)) {
 Serial.println("Error when reading sensor");
 } else {
 const float coldest = 18;
 const float hottest = 30;
 int red = limit(255 * (temperature - coldest) / (hottest - coldest));
 // Colours from blue to violet to red
 int green = 0;
 // Red and blue the closer it gets to the ends, white in the centre
 // int green = limit(255 * sqrt(max(0, (hottest - temperature)*(temperature -
 coldest))) / (hottest - coldest));
 int blue = limit(255 * (hottest - temperature) / (hottest - coldest));
 lcd.setRGB(red, green, blue);

lcd.setCursor(0, 0);
 lcd.print("T: ");
 lcd.print(temperature);
 lcd.write(DEGREE_SYMBOL);
 lcd.print("C");

lcd.setCursor(0, 1);
 lcd.print("H: ");
 lcd.print(humidity);
 lcd.print("%");
 }
 delay(100);
 }

float limit(float color) { // Colour values need to be in the are of 0..255
 if (color < 0) {
 return 0;
 }
 else if (color > 255) {
 return 255;
 }
 else {
 return color;
 }
 }

Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *