Finished DHT-Trinket-LCD in Frame

Trinket Pro Humidity and Temperature Sensor for Your Desk

So, I wanted to measure the temperature and humidity in my office.  Typically, a simple hygrometer and thermometer could work.  But, how do you log the information?  Paper and pencil?  What happens when I am away?  So, in this project we wire up a small micro-controller, temperature and humidity sensor, screen, and a micro-SD card adapter to log the data. Then we use the 3D printer to make a nice little frame.

Parts:
Adafruit Pro Trinket – 5V
10k Ohm Resistors (3)
DHT11 basic temperature-humidity sensor
Small Prototype Boards
SainSmart IIC/I2C/TWI Serial 2004 20×4 LCD Module Shield For Arduino UNO MEGA R3
MicroSD card breakout board+
A small SD card
Jumper Wires
Old USB phone charger

Step 1: Solder on the header pins to the trinket micro controller and SD card breakout board

Adafruit Pro Trinket 5V Chip new and pinsheader pins in prototyping boardadafruit 5V microSD breakout board about to have pins soldered onSolder chip to pins
New Trinket Pro Chip
Pin Header
Set on Chip
Solder in Pins

Step 2: Press the trinket, card board, and DHT into the prototyping board

Trinket Pro on prototyping board - onlySD board, DHT, Trinket Pro on prototyping board - no wires yet
Pro Trinket
SD Board, DHT, Trinket

Step 3: Connections:
— Trinket Pin 10 to SD Card “CS”
— Trinket Pin 11 to SD Card “DI”
— Trinket Pin 12 to SD Card “DO”
— Trinket Pin 13 to SD Card “CLK”
— Trinket Pin “5V” to SD Card “5V”
— Trinket Pin “G” to SD Card “GND”
— Trinket Pin “5V to DHT Pin 1 (looking at it from the front Left pin is 1)
— Trinket Pin “GND” to DHT Pin 4
— Trinket Pin 8 to DHT Pin 2
— 10kΩ Resistor from Trinket “5V” to Trinket Pin 8 (this is a 5V digital pull-up)
— 10kΩ Resistor from Trinket Pin A4  (this is a I2C 5V pull-up, hidden under chip in photo)
— 10kΩ Resistor from Trinket Pin A5 (this is a I2C 5V pull-up, hidden under chip in photo)
— Cable from Trinket Pin A4 to Screen Pin “SDA”
— Cable from Trinket Pin A5 to Screen Pin “SCL”
— Cable from Trinket Pin “BUS” to Screen Pin “VCC”
— Calbe from Trinket Pin “G” to Screen Pin “GND”

Trinket Pro, DHT, SD Card Board - Wired up on Prototyping Board

 

Step 4: Write the software
The software needs to do 3 things:
a) collect temperature and humidity from DHT
b) Format and display the info on the screen
c) Save a copy to the SD card

Software Structure:

// Libs for the Screen (Need wire for I2C and Liquid Crystal to setup Display
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Libs for Sensor (Provides sensor functions)
#include "DHT.h"

// Libs for SD card
#include <SPI.h>
#include <SD.h>

// Constants for Sensor
#define DHTPIN 8 // What digital pin we're connected to?
#define DHTTYPE DHT11 // Sensor Type

// Constants for SD Card
const int chipSelect = 10;

// Setup DHT sensor's connection pin and type
DHT dht(DHTPIN, DHTTYPE); 

// Setup Screen Settings
// Set the LCD I2C address to 0x27
// It has 20 chars and 4 lines
LiquidCrystal_I2C lcd(0x27,20,4); 

void setup() { 
 lcd.init();          // Initialize the lcd
 lcd.backlight();     // turns off the backlight for effect
 lcd.setCursor(0,0); 
 lcd.print("Initializing SD card...");
 
 // See if the SD card is inserted and can be initialized:
 if (!SD.begin(chipSelect)) {
 lcd.print("Card failed, or not present");
 // can't do do anything with it
 return;
 }
 lcd.print("card initialized.");  //Found the card!

dht.begin();  //Start the DHT functions
}

//Main program Goes HERE!
void loop() {
 // Wait a few seconds between measurements.
 delay(30000);

 // Clear the Screen
 lcd.clear();
 
 // Get humidity from Sensor and store as variable "h"
 float h = dht.readHumidity();
 
 // Read temperature as Celsius (the default)
 // Store temperature in variable "t"
 float t = dht.readTemperature();
 
 // Read temperature as Fahrenheit (isFahrenheit = true)
 float f = dht.readTemperature(true);

 // Compute heat index in Fahrenheit (the default)
 float hif = dht.computeHeatIndex(f, h);
 // Compute heat index in Celsius (isFahreheit = false)
 float hic = dht.computeHeatIndex(t, h, false);

 // Print info to the screen
 lcd.setCursor(0,0);  // Top Line
 lcd.print("Humidity: ") & lcd.print(h) & lcd.print(" %");
 lcd.setCursor(0,1);  // 2nd line
 lcd.print("Temp: ") & lcd.print(t) & lcd.print(" *C ");
 lcd.setCursor(0,2);  // 3rd line 
 lcd.print("Temp: ") & lcd.print(f) & lcd.print(" *F "); 
 lcd.setCursor(0,3);  // 4th line
 lcd.print("Heat Index: ") & lcd.print(hif) & lcd.print(" *F");

 // Make a string for assembling the data to save in log file
 // This a comma separated file that will look like this:
 // H = 10 %, T = 32 *F, T = 0 *C, HI = 12 *F
 String dataString = "";
 dataString += "H = " ;
 dataString += String(h);
 dataString += " %, T = ";
 dataString += String(f);
 dataString += "*F , T = ";
 dataString += String(t);
 dataString += "*C , HI = ";
 dataString += String(hif);
 dataString += "*F";

 // Open the file. Only one file can be open at a time
 // The file is or will be called datalog.txt
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 
 // If the file is open, you can write to it
 // You print the assembled string (called dataString) into the file
 // println command prints the string and moved down one line in the file
 // the next reading will be printed on the next line
 // then close the file and start all over again, go back to the top of loop code 
 if (dataFile) {
 dataFile.println(dataString);
 dataFile.close();
 }
}

Step 5: Upload the software

Step 6: 3D print the screen holder

You can get the stl file here.

3d printing of a lcd screen frame holder

3D Printing the Frame

Step 7: Insert LCD into the Frame, stick on the control board

Screen Connected to Trinket Pro For DHT Sensor Project

Screen and board inserted into the frame.

Temp-Humidity-Sensor Frame Assembled Back

Control board fits on the frame ledge.

Step 8: DONE!

Finished DHT-Trinket-LCD in Frame

Finished Temperature and Humidity Sensor Powered by a USB phone charger

 

Excel Automation – VBA to Help Make Surface-Plots

Imagine that you have a complicated excel spreadsheet that might have more than one variable and calculates a numerical output. And, you would like to make a surface plot of the result as the two variables change.

Surface plots are useful for displaying complicated data that tend to change a function of 2 or more parameters. For example, a product yield might depend on both temperature and reaction time.  So, to make a surface plot showing this behavior, you would choose different times and temperatures and record the resulting yield.  In this example, we picked 10 temperatures and 10 times.  That is 100 scenarios that will need to be entered, calculated, recorded, and then plotted.  – Do you really want to type a 100 different values and copy and paste each result to fill in the grid?  What if you decide you picked the wrong range?  Start over?

A faster method is to build a VBA Macro that can do the typing for you.

Here is what to do:

Start with your worksheet that does the calculations. Open it and have a single worksheet that accepts the inputs and gives the outputs.

Then out where you want the data to go… make a new workbook.

Next, define the range for analysis.  Start by making an empty data grid.  Your ranges for testing will go along the edges.  In this example, time will vary from 2 to 72 hours and Temperature from 50 to 500.  I have picked 10 times and temperatures and entered the values that I want to test here on the worksheet.  The calculated result will go in the cell that crosses the row and column.

Excel Grid

Open Visual Basic Editor (in Excel’s Developer Tab
Create a new module (Insert > Module) and put it in your personal workbook for future use.VBA Project Tree

There is an editor window for your module. It looks like an empty text file. You can open modules by double clicking on them.  New modules will be blank…

Start by defining a subroutine called TestProgram(). The command Sub indicates it is subroutine, followed by its name, and () is where you would define pass through variables from another macro.

Type:
Sub TestProgram()

Press enter.

End Sub should appear at the bottom of the editor to automatically close off the subroutine.

Press enter again to leave some space.

Now we will define a For loop for the rows and columns. A For loop defines a variable and sets it equal to a starting number.  For n = 1 to 10 step 1 will create a variable n and set it equal to 1. Any commands between For n = 1 to 10 step 1  and Next n will be executed.  When the commands have run, a Next n statement will tell n to increment by the value defined by step and repeat the process.  Here, step 1 means n goes from n to n+1 and then the commands execute again.  The process keeps going until n = 10.  These loops can be nested and the inner most loop finishes before the outermost loop.

We use two For loops to move along the rows and columns of the data grid. The row loop is nested in the column loop.  So, it will fix a column and move down the column by incrementing the rows by 1. Them move to the next column.  I entered 10 different times and temperatures that I wanted to test in the empty data grid, so we set the upper limit at 10.

Type:
For n = 1 to 10 step 1  
     For r = 1 to 10 step 1  

You needed to have an open workbook to get the variables and store the results. We need to make sure that file is active first. In this example, “Price Comparison.xlsx” is the name of my workbook where the results will be saved.

Type:
Windows(“Price Comparison.xlsx”).Activate

Now, define two internal variables to store the values you want to test. The variable (HydroT and SL) will be set to the values you entered in the empty grid.  In this example, the Times are in a column, so the row will increment downward with each iteration.  The Cells() command tells VBA to go to the currently active sheet and look for that cell.  Here, we are going to use Cells(row + offset from the top, column the values are in).  We include an offset to account for the distance from the edges of the worksheet.  So, the first Time we want to test is 6 rows down from the top and three columns in from the left edge.  So, that is 5 empty cells + r, and r starts at 1.  The temperature values are in row 5 and start in column D, or 3 empty columns + over from the left side.

Remember that r is the row and n is the column variable the will change during the For loop.  And will increment along the rows and columns were you put the conditions you wanted to test.

Type:
HydroT = Cells(5 + r, 3).Value
SL = Cells(5, 3 + n).Value

Now, we switch to the workbook that does the calculations.
Type:
Windows(“RL-SingleProduct-V7(60pct).xlsm”).Activate

In this example, my calculations are done is RL-SingeProduct-V7(60pct).xlsm workbook. You will replace this text with the name of your workbook.

Now, in the calculation workbook.  I assume you only have one worksheet active… You need put the variables into the worksheet’s appropriate cells. For this, you can either use the Cells(row,column) function or the less direct Range() command.  The Range() command calls out name of the cell, which can also be a named label.  In my example cell C23 is the time entry cell and G25 is the temperature entry cell.  So, Range(“C23”).Select activates the cell and ActiveCell.FormulaR1C1 = HydroT passes the value of the HydroT into the cell. We could also use Cells(23,3).Value = HydroT to put the value into the cell.  My calculating spreadsheet uses iteration on selection.  So, I have included an additional Range(“G25”).Select to force the sheet to re-iterate.

Type:
Range(“C23”).Select
ActiveCell.FormulaR1C1 = HydroT
Range(“G25”).Select
ActiveCell.FormulaR1C1 = SL
Range(“G25”).Select

Now, we need to get the results back into the macro and put them into the starting workbook. For my workbook, the result comes out in cell H2.  So, we collect the value and put it into the variable temp for safe keeping.  I am using the Range() command, Cells(2,9).Value would work as well. The Rage() would let me call out a named cell later for another project.  That would also let me move the results to a any location in the file and Excel will follow the name’s pointed location and not an absolute cell reference. This a good option if you don’t have everything on a single worksheet.

Type:
temp = Range(“H2”).Value

The value is now stored in temp.  Next, go back to the workbook that will store the results.

Type:
Windows(“Price Comparison.xlsx”).Activate

Excel needs to put the values into the cell in the grid. The calculated answers are the intersection of the two variables.  In my example, that starts at row 6 and column 4.  We offset the row by 6 to move down 6 lines.  So, we take r and add 6 to it.  The column starts in column D, that is 4 columns from the left.  So, take the column variable n and add 3 to move over 3 spots.  Remember that r and n start at 1 and count to 10.  Set the value of the cell to the result stored in temp.

Type:
Cells(5 + r, 3 + n) = temp

We need to increment the and n to do the next iteration. We nested the rows in the columns.  So, we need to increment the rows first.  After the macro goes from row 1 to row 10, then it will go to the next column and do rows 1 to 10 again.  The order of Next r and Next n matter.

Type:
Next r
Next n

Your macro should end with End Sub to tell it that your subroutine is done.

Macro Example

Save everything!

Now with the calculating workbook open and the input/output worksheet selected, and the results workbook open and selected to the worksheet with the variables grid, run the macro. You can goto the VBA editor window, click in the macro code area and then click the green Play Button, use the menu to choose Run > Run Sub/User Form, or use the Developer ribbon and click on Macros to choose the macro you just made.  The macro will work and report the data back into the grid.

Excel Grid with Data

Now you can create your 3D surface plot by selecting the data block and inserting a surface plot. Excel Surface PlotAdd some labels, analysis and …

Done!

 

Hope this helps save you time.