Binary Thermometer

1 minute read

“Necessity is the mother of invention” says a well-known proverb. I was in the office one day, because it was a day before Christmas the temperature was low. I needed to measuere the temperature but didn’t have a thermometer on hand. However, I found some LEDs, DHT11 sensor and an old Arduino Uno board, so i decided to build a binary thermometer :-) Because i had only 5 LEDs temperature range is from 0 to 32 degrees Celsius.

Code snippet:

 1#include "DHT.h"
 2
 3#define DHTPIN 2        // Digital pin connected to the DHT sensor
 4#define DHTTYPE DHT11   // DHT 22  (AM2302), AM2321
 5
 6DHT dht(DHTPIN, DHTTYPE);
 7
 8
 9void setup() {
10  Serial.begin(9600);
11  Serial.println(F("DHTxx online!"));
12  dht.begin();
13
14}
15
16void loop() {
17  int number = dht.readTemperature();
18
19  // Check if any reads failed 
20  if (isnan(number)) {
21    Serial.println(F("Failed to read from DHT sensor!"));
22    return;
23  }
24  int leds[5] = {5,6,7,8,9};
25  int i = 0;  
26  for(int i=0;i<=4;i++){
27    pinMode(leds[i], OUTPUT);
28  }
29  //Serial.println(t);
30  while (number>=1) {
31    digitalWrite(leds[i],number%2);
32    number/=2;
33    i++;
34  }  
35  delay(2000);
36}