Trinket Temperature & Humidity LCD Display

Transcript Of Trinket Temperature & Humidity LCD Display
Trinket Temperature & Humidity LCD Display
Created by Anne Barela
©Adafruit Industries
Page 1 of 13
https://learn.adafruit.com/trinket-temperature-humidity-lcd-display
Last updated on 2021-11-15 06:05:06 PM EST
©Adafruit Industries
Page 2 of 13
Table of Contents
Overview
5
• Software Libraries Used
6
The LCD Display
6
Adding the Sensor
9
Code
10
Debugging and Going Further
12
• Going Further
13
©Adafruit Industries
Page 3 of 13
©Adafruit Industries
Page 4 of 13
Overview
The first thing a microcontroller project must do is communicate, often with us humans. While the Trinket mini microcontroller does not have a serial monitor built in (like the Arduino Uno), it can talk over various protocols including software serial, I2C (two wire), and SPI. Adafruit sells a wide array of I2C devices including a backpack to interface with a number of nice liquid crystal (LCD) displays - perfect as it only requires two of the five Trinket pins.
Monitoring sensors is very common for Internet of Things (IoT) projects. Here we'll select the popular DHT series of temperature and humidity sensors.
This project can be placed in a very small enclosure and used anywhere environmental monitoring is needed. The code and concepts may be used in a number of your own projects.
©Adafruit Industries
Page 5 of 13
Software Libraries Used
To maximize the functionality of software on Trinket, two Arduino software libraries need to be installed. See the All About Arduino Libraries (https://adafru.it/aYM) tutorial for details on how to download and install these libraries.
• Adafruit_LiquidCrystal (https://adafru.it/leO) library - this provides support for I2C displays that Adafruit sells
• TinyDHT (https://adafru.it/cKe) library - this library is optimized for the Atmel ATtiny processors like Adafruit Trinket and Arduino Gemma. It uses integer math to avoid pulling in the code-heavy floating point support libraries.
The LCD Display
Adafruit carries many character LCD display varieties with multiple sizes and backlight colors.
The Adafruit I2C / SPI character LCD backpack (http://adafru.it/292) allows you to control these displays by sending data over the two wire I2C interface. Standard LCDs require a large number of digital pins, straining the capability of even an Arduino Uno. Use of the I2C backpack reduces the pins needed considerably.
This project features a 16x2 display, displaying temperature and humidity without using a great deal of memory (important on a small microcontroller like Trinket).
The I2C backpack may be assembled and placed on the back of the display. See the guide to backpack assembly (https://adafru.it/cKf) to prepare your display and the backpack.
The color displays have a couple of extra connectors - pins 16, 17, and 18 control the three color backlights. If you connect pin 16, the I2C will control the red light. You can choose to put a jumper from one of the backlight pins to backpack pin 16 to choose a different color or connect the pins high to keep them on all the time. Making the pin choice before soldering on the backpack allows you the most flexibility in choosing your backlight color. . Or you can just go with a 'classic' blue & white 16x2 LCD (https://adafru.it/cKw)
©Adafruit Industries
Page 6 of 13
©Adafruit Industries
Page 7 of 13
To test the display, wire the DAT pin to Trinket GPIO #0, the CLK pin to Trinket GPIO #2, 5V to the Trinket 5V line and GND to GND. Use the Fritzing wiring diagram on the previous page to check your wiring.
Ensure your Arduino IDE has been configured to program the Adafruit Trinket 5V 8 MHz for the Hello World test program (note for later: we will be changing this to Trinket 5V 16 MHz for the sensor program).
The display test program is a variation of the Hello World program. You need to install the Adafruit_LiquidCrystal library mentioned on the previous page.
Ensure you configure the Arduino IDE software for Trinket as noted in the tutorial Introducing Trinket. Also select Trinket 5V 8 MHz in the Arduino Tools menu.
/* Demonstration sketch for Adafruit i2c/SPI LCD backpack using MCP23008 I2C expander and the Trinket mini microcontroller
This sketch prints "Hello World!" to the LCD and shows a count in seconds. The circuit:
* 5V to Trinket 5V pin * GND to Trinket GND pin * CLK to Trinket pin GPIO #2 * DAT to Trinket pin GPIO #0 */
// include the library code: #include <Adafruit_LiquidCrystal.h>
// Connect via i2c, default address #0 (A0-A2 not jumpered) Adafruit_LiquidCrystal lcd(0);
void setup() { // set up the LCD lcd.begin(16, 2); lcd.setBacklight(HIGH); lcd.print("hello, world!");
}
// our display has 16 cols, 2 rows // Turn on the backlight // Print a message to the LCD.
void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // print the number of seconds since reset: lcd.print(millis()/1000);
}
If you plan on having more than one LCD or I2C device, see Changing the I2C Address (https://adafru.it/cKg) for details.
©Adafruit Industries
Page 8 of 13
What if I have no display?
Using the contrast potentiometer on the backpack (a small silver bump), turn the dial with a small screwdriver. Change the contrast until you can read the text.
Adding the Sensor
Once you have the display connected to the Trinket, you can expand the project in many ways. One of the most popular is to add sensor(s) of some sort. The Adafruit store has a wonderful selection to chose from (https://adafru.it/cKh).
The easiest place to add a sensor is to Trinket GPIO #1. GPIO pins #0 and #2 are used for the display and #3 and #4 are shared with the USB port. Using #3 and #4 is perfectly fine, but you may have to disconnect the connections on those pins when uploading software. Also, limiting a project to pins #0 to #2 gives you a similar pinout to the Adafruit Gemma wearable controller.
This project demonstrates using the DHT22 temperature and humidity sensor. It provides a 0-100% humidity reading with 2-5% accuracy and -40 to 80°C temperature readings with ±0.5°C accuracy. The sensor only requires one digital pin for readings, perfect.
You may want to review the Adafruit tutorial on DHT (https://adafru.it/cKi) sensors for more background.
©Adafruit Industries
Page 9 of 13
See the wiring on the Fritzing diagram (first page) and the picture (second page). It is fairly easy to connect to the DHT sensors. They have four pins - from left to right with the grilled opening facing you:
• VCC (3 to 5V power) • Data out • Not connected • Ground
You may ignore pin 3, its not used. Place a 1K ohm resistor between VCC (5V) and the data pin to act as a pull up on the data line. Do not use the 10K resistor normally used from 5v to the data pin.
Trinket GPIO Pin #1 is shared with the onboard red LED. Connecting the DHT, the sensor requires a 1,000 ohm pullup resistor (not provided) between Trinket GPIO #1 and 5V to pull up the voltage to be read by the Trinket.
Code
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)
©Adafruit Industries
Page 10 of 13
Created by Anne Barela
©Adafruit Industries
Page 1 of 13
https://learn.adafruit.com/trinket-temperature-humidity-lcd-display
Last updated on 2021-11-15 06:05:06 PM EST
©Adafruit Industries
Page 2 of 13
Table of Contents
Overview
5
• Software Libraries Used
6
The LCD Display
6
Adding the Sensor
9
Code
10
Debugging and Going Further
12
• Going Further
13
©Adafruit Industries
Page 3 of 13
©Adafruit Industries
Page 4 of 13
Overview
The first thing a microcontroller project must do is communicate, often with us humans. While the Trinket mini microcontroller does not have a serial monitor built in (like the Arduino Uno), it can talk over various protocols including software serial, I2C (two wire), and SPI. Adafruit sells a wide array of I2C devices including a backpack to interface with a number of nice liquid crystal (LCD) displays - perfect as it only requires two of the five Trinket pins.
Monitoring sensors is very common for Internet of Things (IoT) projects. Here we'll select the popular DHT series of temperature and humidity sensors.
This project can be placed in a very small enclosure and used anywhere environmental monitoring is needed. The code and concepts may be used in a number of your own projects.
©Adafruit Industries
Page 5 of 13
Software Libraries Used
To maximize the functionality of software on Trinket, two Arduino software libraries need to be installed. See the All About Arduino Libraries (https://adafru.it/aYM) tutorial for details on how to download and install these libraries.
• Adafruit_LiquidCrystal (https://adafru.it/leO) library - this provides support for I2C displays that Adafruit sells
• TinyDHT (https://adafru.it/cKe) library - this library is optimized for the Atmel ATtiny processors like Adafruit Trinket and Arduino Gemma. It uses integer math to avoid pulling in the code-heavy floating point support libraries.
The LCD Display
Adafruit carries many character LCD display varieties with multiple sizes and backlight colors.
The Adafruit I2C / SPI character LCD backpack (http://adafru.it/292) allows you to control these displays by sending data over the two wire I2C interface. Standard LCDs require a large number of digital pins, straining the capability of even an Arduino Uno. Use of the I2C backpack reduces the pins needed considerably.
This project features a 16x2 display, displaying temperature and humidity without using a great deal of memory (important on a small microcontroller like Trinket).
The I2C backpack may be assembled and placed on the back of the display. See the guide to backpack assembly (https://adafru.it/cKf) to prepare your display and the backpack.
The color displays have a couple of extra connectors - pins 16, 17, and 18 control the three color backlights. If you connect pin 16, the I2C will control the red light. You can choose to put a jumper from one of the backlight pins to backpack pin 16 to choose a different color or connect the pins high to keep them on all the time. Making the pin choice before soldering on the backpack allows you the most flexibility in choosing your backlight color. . Or you can just go with a 'classic' blue & white 16x2 LCD (https://adafru.it/cKw)
©Adafruit Industries
Page 6 of 13
©Adafruit Industries
Page 7 of 13
To test the display, wire the DAT pin to Trinket GPIO #0, the CLK pin to Trinket GPIO #2, 5V to the Trinket 5V line and GND to GND. Use the Fritzing wiring diagram on the previous page to check your wiring.
Ensure your Arduino IDE has been configured to program the Adafruit Trinket 5V 8 MHz for the Hello World test program (note for later: we will be changing this to Trinket 5V 16 MHz for the sensor program).
The display test program is a variation of the Hello World program. You need to install the Adafruit_LiquidCrystal library mentioned on the previous page.
Ensure you configure the Arduino IDE software for Trinket as noted in the tutorial Introducing Trinket. Also select Trinket 5V 8 MHz in the Arduino Tools menu.
/* Demonstration sketch for Adafruit i2c/SPI LCD backpack using MCP23008 I2C expander and the Trinket mini microcontroller
This sketch prints "Hello World!" to the LCD and shows a count in seconds. The circuit:
* 5V to Trinket 5V pin * GND to Trinket GND pin * CLK to Trinket pin GPIO #2 * DAT to Trinket pin GPIO #0 */
// include the library code: #include <Adafruit_LiquidCrystal.h>
// Connect via i2c, default address #0 (A0-A2 not jumpered) Adafruit_LiquidCrystal lcd(0);
void setup() { // set up the LCD lcd.begin(16, 2); lcd.setBacklight(HIGH); lcd.print("hello, world!");
}
// our display has 16 cols, 2 rows // Turn on the backlight // Print a message to the LCD.
void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // print the number of seconds since reset: lcd.print(millis()/1000);
}
If you plan on having more than one LCD or I2C device, see Changing the I2C Address (https://adafru.it/cKg) for details.
©Adafruit Industries
Page 8 of 13
What if I have no display?
Using the contrast potentiometer on the backpack (a small silver bump), turn the dial with a small screwdriver. Change the contrast until you can read the text.
Adding the Sensor
Once you have the display connected to the Trinket, you can expand the project in many ways. One of the most popular is to add sensor(s) of some sort. The Adafruit store has a wonderful selection to chose from (https://adafru.it/cKh).
The easiest place to add a sensor is to Trinket GPIO #1. GPIO pins #0 and #2 are used for the display and #3 and #4 are shared with the USB port. Using #3 and #4 is perfectly fine, but you may have to disconnect the connections on those pins when uploading software. Also, limiting a project to pins #0 to #2 gives you a similar pinout to the Adafruit Gemma wearable controller.
This project demonstrates using the DHT22 temperature and humidity sensor. It provides a 0-100% humidity reading with 2-5% accuracy and -40 to 80°C temperature readings with ±0.5°C accuracy. The sensor only requires one digital pin for readings, perfect.
You may want to review the Adafruit tutorial on DHT (https://adafru.it/cKi) sensors for more background.
©Adafruit Industries
Page 9 of 13
See the wiring on the Fritzing diagram (first page) and the picture (second page). It is fairly easy to connect to the DHT sensors. They have four pins - from left to right with the grilled opening facing you:
• VCC (3 to 5V power) • Data out • Not connected • Ground
You may ignore pin 3, its not used. Place a 1K ohm resistor between VCC (5V) and the data pin to act as a pull up on the data line. Do not use the 10K resistor normally used from 5v to the data pin.
Trinket GPIO Pin #1 is shared with the onboard red LED. Connecting the DHT, the sensor requires a 1,000 ohm pullup resistor (not provided) between Trinket GPIO #1 and 5V to pull up the voltage to be read by the Trinket.
Code
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)
©Adafruit Industries
Page 10 of 13