Arduino Log #3: Making a Digital Thermometer

It’s getting hot in here

After some trial and error in my previous post, I was all set up with the Arduino. Now is time to turn it into something more useful than just a blinking light on the board, but first I need to obtain extra kit.

I obtained the following (no amazon affiliate links, just links to what I got):

Kit with breadboard, resistors and more

7 Segment Display

DHT22 Digital Humidity & Temperature Sensor

 

My first attempt at using the LCD Display ended up with the screen glowing the backlight, but contrast control through the potentiometer (or just using a resistor) regardless of pins, and I discovered that a much easier (and tidier) way to use that is through an I2C chip, which may come later. For now, I got the 7 segment display for that retro feel, and because it came with a library to use straight in the Arduino.

The result:

The above has pins going for power & ground from the display, and the thermometer itself, with a 10k resistor. I am using digital pins 13, 11 and 10 for the display, and digital pin 2 for the thermometer.

The libraries used were the HCMAX7219 & DHT Sensor. My aim was to keep the code as simple as possible, so I removed the humidity part of the thermometer code (for now).

The code:

// Code adapted from Hobby Components Display Library Example & DHT Sensor Library Example
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <HCMAX7219.h>
#include “SPI.h”
#define DHTPIN 2 // Pin which is connected to the DHT sensor.
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define LOAD 10 // 7 Seg Display PIN
HCMAX7219 HCMAX7219(LOAD);
DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
Serial.begin(9600);
// Initialize device.
dht.begin();
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
HCMAX7219.Init();
}

void loop() {
// Delay between measurements.
delay(delayMS);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
HCMAX7219.Clear();
HCMAX7219.print7Seg(“NO DATA”,8);
HCMAX7219.Refresh();
}
else {
int fulltemp = event.temperature * 100;
/* Clear the output buffer */
HCMAX7219.Clear();
/* Write some text to the output buffer */
HCMAX7219.print7Seg(“t”,8);
HCMAX7219.print7Seg(fulltemp,2,4);
HCMAX7219.print7Seg(“c”,1);
/* Send the output buffer to the display */
HCMAX7219.Refresh();
}
}

The above will initialise the thermometer and the display, check the sensors, and print out “t  <temperature> C”. I am multiplying the actual temperature by 100 so that we get a clean “24.5C” rather than “24.50C” which is less useful as it’s commonly 0. I then add the dot on the fulltemp area in the 2nd place from the right, as there is a C there.

The display updates every 2 seconds, and if the plug is pulled from the sensor, or if there is an error, the display will show “no data”.

As this is my first project with this, I’m pleased with the results. I’m sure the code can be cleaned up further, but for now, I have a full functioning thermometer running off the 7 segment display. Nice!