Be sure to set your Board type as Adafruit Trinket 5V 16 MHz, and Programmer as USBtinyISP in the Arduino IDE under the Tools menu. This is a change from many Trinket programs which were run on 8 MHz, take note.

To get the timing correct on the DHT sensor, the Trinket is clocked to 16 MHz (same as the Arduino Uno) in the first line in the setup() routine. This requires the 5V Trinket as the 3 volt Trinket is not guaranteed to function at 16 MHz. This also limits use to Trinket and not Gemma. 

The code:

 Demonstration sketch for Adafruit LCD backpack
 using MCP23008 I2C expander and DHT Temperature/Humidity Sensor
 Uses the 5 volt Trinket mini microcontroller with the
 Trinket set at 16 MHz due to timing reading the DHT sensor
 This sketch prints the temperature and humidity to the LCD
 The circuit:
 * 5V to Arduino 5V pin
 * GND to Arduino GND pin
 * Display i2c backpack CLK to Trinket GPIO #2 
 * Display i2c backpack DAT to Trinket GPIO #0 
 * DHT Temperature Sensor to Trinket GPIO #1 (with 1K ohm pullup to 5V)

// Connect DHT pin 1 (on the left) of the sensor to +5V
// Connect DHT pin 2 of the sensor to whatever your DHTPIN is
// Connect DHT pin 4 (on the right) of the sensor to GROUND
// Connect a 1K resistor from pin 2 (data) to pin 1 (power) of the sensor

// include the library code
#include <Adafruit_LiquidCrystal.h>  // LiquidCrystal using the Wire library
#include <TinyDHT.h>        // lightweit DHT sensor library
#include <avr/power.h>      // needed to up clock to 16 MHz on 5v Trinket

// Uncomment whatever type sensor you are using!
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22     // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
#define TEMPTYPE 1        // Use Fahrenheit (0 for celsius)

// Trinket GPIO #1 would be better but LED prevents digital talk with DHT sensor
#define DHTPIN 1          // Sensor connected to GPIO #1 (use a
                          //  1K ohm pullup to 5V to make it work!)
DHT dht(DHTPIN, DHTTYPE); // Define Temp Sensor

// Connect display via  i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);

void setup() {  // first line sets a 5V Trinket to 16 MHz operation
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1); // 5V Trinket: run at 16 MHz
  dht.begin();  // Initialize DHT Teperature Sensor
  lcd.begin(16, 2);  // set up the LCD's number of rows and columns: 
  lcd.setBacklight(HIGH); // Set backlight (HIGH - on)

void loop() {
  int8_t h = dht.readHumidity();               // Read humidity
  int16_t t = dht.readTemperature(TEMPTYPE);   // read temperature

  lcd.setCursor(0, 0); 
  if ( t == BAD_TEMP || h == BAD_HUM ) { // if error conditions (see TinyDHT.h)
     lcd.print("Bad read on DHT");       //   print error message
  } else {
     lcd.print("Humidity: ");            // write to LCD
     lcd.setCursor(12, 0); 
     lcd.print(" % ");
     lcd.setCursor(0, 1); 
     lcd.setCursor(7, 1); 
     lcd.setCursor(10, 1); 
  delay(2000);  // Read temp every second (2000 ms) (DHT sensor max rate)

The above code compiles to 4,760 bytes of 5,310 available. This leaves 550 bytes of code if you wish to add additional functionality. Note that adding display text will use the available space quickly. Also, if you use decimal (floating point) numbers, you will most likely exceed the space available. The Arduino IDE built-in functions to do floating point math are somewhat large (this is why the DHT library was edited to create the TinyDHT library: to use integer math, which limits precision to one degree and one percent but saves space).

If the display is not showing values you expect, ensure you compiled it with the board being the Adafruit Trinket 5V 16 MHz (not 8 MHz). Also check the sensor wiring.

This guide was first published on Sep 28, 2013. It was last updated on Sep 28, 2013.

This page (Code) was last updated on Sep 28, 2013.

Text editor powered by tinymce.