Interface PIC to Honeywell HIH-5030/5031 Humidity Sensor

Created by Kevin Culkins, last modified by Robert Nelson on Sep 08, 2017

image

Introduction

The Honeywell HIH-5031 is a low voltage analog humidity sensor, and when paired with the power sipping PIC18F14K22, makes a great combination in a battery-powered embedded system. Both devices are noted for low voltage operation and are ideal in battery-powered systems where the supply voltage is a nominal 3VDC. When operating, the HIH-5031 typically consumes 200uA.

Application

The demonstration software shows the capabilities of the PIC18 and the Honeywell humidity sensor. To read the humidity, any terminal program can be used on a host computer to communicate with the PIC18 via a serial port. When the PIC is queried, the PIC will respond with the humidity.

Overview

Unless responding to a request for humidity data, the PIC is in sleep mode and the sensor is powered down. When a request for the humidity is received by the PIC via the ESUART, the PIC wakes up and verifies that it is a valid command. If not a valid command the PIC will respond with an error message and go back to sleep. If valid, the PIC will apply power to the sensor, sample the sensor, process the data, parse the data for transmission, and respond with the relative humidity measurement to the host computer.

Host Terminal Program, Command and Response

Any terminal program running on the host computer should suffice to communicate with the PIC via a serial COM port. Terminal settings are 9600 baud, asynchronous mode, 8 bit, 1 stop bit, no parity.

There are only two single character commands available that will not cause an error response, “H” or “h” for Hello and “R” or “r” to Request humidity.

The “H”ello command is not required to establish communications with the embedded target, but it is useful to confirm that the communication channel is working since the embedded code doesn’t perform any sensor sampling or processing. The “H” command response is an asterisk, “*”. The “R”equest command requests the relative humidity and the response, for example, is formatted thus, “RH 40%”. If an unrecognized command is received, the response is “Error”.

EUSART

In this demonstration, the PIC is usually in sleep mode and requires an interrupt from the EUSART to wake-up and respond to a command. The EUSART is configured to use the Auto-Wake-up feature, where the EUSART will wake-up due to activity on the RX line. To avoid character errors or character fragments during a wake-up event, the wake-up character must be all zeros, followed by the command character. The PIC immediately returns to sleep mode after responding to an incoming character.

This literally means that the command character sent by the host terminal program must be preceded by sending 0x00 to wake-up the PIC. For example, when sending “H”ello, the host terminal program would send 0x00, followed by 0x48, and for “R”equest – send 0x00, followed by 0x52. Details of the EUSART Auto-Wake-Up On Break feature can be found Section 15.3.3 of the data sheet.

Preprocessor directives provide conditional compilation of particular lines of code for 3 different PIC clock speeds, 1MHz, 4MHz, or 8MHz (by default the available source code will run at 8MHz). The EUSART peripheral is configured appropriately for each of the PIC clock speeds so that serial communication rate will remain 9600 bps. To change the clock speed, open header file main.h and comment/uncomment one of the clock #defines.

// Following used for PIC internal oscillator setting.
// UART and ADC are confgured appropriately per PIC clock speed
// Uncomment for internal clock speed
//#define ONEMHZ
//#define FOURMHZ
#define EIGHTMHZ

The EUSART configuration code will build per the desired clock speed.

#ifdef ONEMHZ
  // This configuration is for the PIC clock at 1MHz, Rx interrupts, asynch,
  // 8 bit, 1 stop bit, no parity, 9600 baud
    OpenUSART(USART_TX_INT_OFF &
            USART_RX_INT_OFF &
            USART_ASYNCH_MODE &
            USART_EIGHT_BIT &
            USART_CONT_RX &
            USART_BRGH_HIGH,
            0x19);
    baudUSART(_BAUDCON_BRG16_MASK);
#elif defined(FOURMHZ)
  // This configuration is for the PIC clock at 4MHz, Rx interrupts, asynch,
  // 8 bit, 1 stop bit, no parity, 9600 baud
  OpenUSART(USART_TX_INT_OFF &
            USART_RX_INT_OFF &
            USART_ASYNCH_MODE &
            USART_EIGHT_BIT &
            USART_CONT_RX &
            USART_BRGH_HIGH,
            0x19);
#elif defined(EIGHTMHZ)
  // This configuration is for the PIC clock at 8MHz, Rx interrupts, asynch,
  // 8 bit, 1 stop bit, no parity, 9600 baud
  OpenUSART(USART_TX_INT_OFF &
            USART_RX_INT_OFF &
            USART_ASYNCH_MODE &
            USART_EIGHT_BIT &
            USART_CONT_RX &
            USART_BRGH_HIGH,
            0x33);
#endif

Analog-to-Digital Converter (ADC)

The sensor analog voltage output and voltage reference (the PIC’s I/O pin that powers the sensor) are both sampled by the PIC’s 10-bit ADC.

The sensor source code includes the temperature compensation adjustment of the relative humidity. However, since a temperature sensor isn’t included in this demonstration, the temperature value used in the compensation equation is fixed at 25C.

Floating point data types (24-bit) are used in the relative humidity calculations. The compiler does provide for formatting strings with float data types, but there is a large increase in code size when used. To keep code size down the final floating point result is broken down into a fractional and integral component. The integral component is formatted (as an integer) in a string and transmitted to the host terminal program. This process does introduce additional rounding errors.

As mentioned in the EUSART section, Preprocessor directives provide conditional compilation of particular lines of code for 3 different PIC clock speeds, 1MHz, 4MHz, or 8MHz. The ADC peripheral is configured appropriately for the desired clock speed so the ADC acquisition and conversion time are within specification.

#ifdef ONEMHZ
  // Conversion Clock Fosc/2
  // Acquisition 2 TAD
  // Right Justified
  ADCON2 = 0x88;
  #elif defined(FOURMHZ)
  // Conversion Clock Fosc/8
  ADCON2 = 0x89;
  #elif defined(EIGHTMHZ)
  // Conversion Clock Fosc/16
  ADCON2 = 0x8D;
  #endif

Hardware

The microcontroller provides serial communication to the host PC, provides power to the humidity sensor, and samples the humidity sensor when requested.

The reference voltage for the sensor measurement is the sensor supply voltage. No need for a separate voltage reference. As long as the supply voltage is stable, an accurate measurement will be possible. The microcontroller ADC samples both the sensor supply voltage and sensor output voltage every time there is a request for relative humidity.

The PIC uses its EUSART peripheral for asynchronous serial communications to/from the host computer. While not depicted in the schematic, serial communication with the PIC will require a RS232 transceiver (such as MAX3232 or equivalent) to communicate to a host computers COM port or USB-to-serial adapter. Serial communication settings: 9600 bps, asynchronous mode, 8 bits, 1 stop bit, no parity.

The Honeywell HIH-5031 (and HIH-5030) humidity sensor is ratiometric, so any change in the sensor supply voltage will result in a proportional change in the sensor output voltage. The HIH-5031 humidity sensor is condensation-resistant and factory-fitted with a hydrophobic filter, while the HIH-5030 does not include this feature.

Hardware Development

It isn’t necessary to create a PCB for evaluating the humidity sensor. With a bit of careful persuasion, the humidity sensor pins may be adjusted to allow insertion into a solderless breadboard. If modifying the pin pitch of the sensor makes you nervous, solder extensions to the leads instead.

The PIC and other passives/devices can be found in through hole packaging.

An MPLAB® ICD3 Programmer/Debugger, PICkit 3 Programmer/Debugger, or REAL Ice In-Circuit Emulator are compatible with the PIC18 for development purposes.

Software

The demonstration software (C language source code) referenced in this eewiki article is a Microchip MPLAB X project.

It is left to the reader to install and familiarize themselves with MPLAB X IDE, the XC8 compiler, and hardware connections to/from the PIC18 device.

Download and unzip the compressed project file HIH_PIC.zip (369.3 KB) .

Open MPLAB X IDE, connect the hardware to your computer, and select “Open Project”. Navigate to the previously unzipped file folder and select project “Humidity.X”. Open the project properties to verify hardware and software tool selection matches your installation. If the PIC18F14K22 is replaced with a different PIC, edit the project properties configuration to reflect this device change.

Make and Program device.

A prebuilt Hex file is available for the PIC18F14K22 in folder "…\Humidity_X\Hexfile”, title “Humidity.X.production.hex”.

Supporting Files

Software

HIH_PIC.zip - see above link

Schematic

Honeywell_Microchip_Rev_A.pdf (18.6 KB)

BOM

Honeywell_Microchip_BOM_Rev_A.xls (22.5 KB)

Application Notes and other Supporting Documents.

Humidity Sensor Performance Characteristics

Humidity Sensor Theory and Behavior

Humidity Sensor Moisture and Psychrometrics

Thermoset Polymer-based Capacitive Sensors

Questions/Comments

Any questions or comments please go to Digi-Key’s TechForum