ADC Pi with HIH-4000

Created 02/10/2015 | ADC Pi with HIH-4000 Series Humidity Sensor

In this tutorial we will use the ADC Pi or ADC Pi Plus with a Honeywell HIH-4000 Series Humidity Sensor to make a digital humidity reader. For this you will need your Raspberry Pi, an ADC Pi or ADC Pi Plus, and a Honeywell HIH-4000 Series Humidity Sensor.

We will use the AB Electronics python library to talk to the ADC Pi, you can download the library from github using the following command in a terminal, and this will download the full python library into the currently selected folder.

git clone https://github.com/abelectronicsuk/ABElectronics_Python_Libraries.git

You will need to enable i2c on your Raspberry Pi, see our other tutorial on i2c: I2C, SMBus and Raspbian Linux

The AB Electronics python library uses another library called python-smbus, you can install it using apt-get with the following commands.

sudo apt-get update

sudo apt-get install python-smbus

Finally you will want to tell python where you download the AB Electronics python library by adding it into PYTHONPATH.

export PYTHONPATH=${PYTHONPATH}:~/Desktop/ABElectronics_Python_Libraries/ADCPI/

If you downloaded it to somewhere other than your desktop you will need to change the path to match your download destination.

Now all of the libraries are installed and the Raspberry Pi is configured to use i2c we can begin building our project.

Parts Used:

ADC Pi Plus or ADC Pi Zero

Honeywell HIH-4000 Series Humidity Sensor (Datasheet)

100K Resistor

Connecting the Sensor to the ADC Pi Plus

Click to enlargeIf you haven’t done so install your ADC Pi Plus onto the Raspberry Pi by connecting it onto the GPIO header.  Make sure your Raspberry Pi is turned off when you do this to minimise the risk of damaging the Raspberry Pi or the ADC Pi Plus.

Next connect the HIH4000 sensor to the ADC Pi Plus. Pin 1 connects to +5V, Pin 2 to input 1 on the ADC Pi via a 100K resistor, Pin 3 connects to GND.

For this tutorial we will start by creating a new python program file called demo-hih4000.py.  You can use your favourite text editor for writing the program.  You can find a complete example of demo-hih4000.py in the ABElectronics_Python_Libraries/ADCPI/ folder.

At the top of your program you will need to import the ABEHelpers library, ADCPI library and time library. We also add variables to hold the values related to the sensor data conversion.

#!/usr/bin/python
from ABE_ADCPi import ADCPi
from ABE_helpers import ABEHelpers
import time

resistor_multiplier = 6.95225 # use 100K resistor in series with the input
zero_offset = 0.826 # zero offset value from calibration data printout
slope = 0.031483 # slope value from calibration data printout

The ADCPI library is used for all communication with your ADC Pi Plus, it gives you control over almost everything that can be done with the MCP3424 controller.

The ABEHelpers class contains a function called get_smbus() that allows us to find out which I2C bus is being used on the Raspberry Pi.  Create an instance of the ABEHelpers class called i2c_helper and use it to create an i2c_bus object.

i2c_helper = ABEHelpers()

i2c_bus = i2c_helper.get_smbus()

Next we need to create an instance of the ADCPI class using the i2c_bus object and call it adc

adc = ADCPi(bus, 0x68, 0x69, 18)

0x68 and 0x69 are the I2C address for the ADC chips, if you have changed the address selection jumpers on your ADC Pi Plus then you will need to change these numbers to match the new addresses.

Now create a function to calculate the humidity based on the voltage output from the ADC Pi Plus

def calcHumidity(inval):
    voltage = inval * resistor_multiplier
    humidity = (voltage - zero_offset) / slope
    return humidity

We are going to need is a loop so that the same commands can run over and over.  This can be done with a simple while loop.

while True:

As True is always true the while loop will keep running until you exit the program with a Ctrl-C.

In order to clear the console for each read we need to issue a clear command.

    # clear the console
    os.system('clear')

Next we read from channel 1 on the ADC Pi Plus and use the calcHumidity function to convert the voltage to a humidity and print to the console.

     # read from adc channels and print to screen
	 print ("Humidity on channel 1: %0.1f%%" % calcHumidity(adc.read_voltage(1)))

Using the time.sleep method we add a 0.5 second delay before repeating the read again.

     time.sleep(0.5)

time.sleep takes one variable, a number which represents the number of seconds to wait.  1 will make the program sleep for 1 second while 0.1 would wait for 100ms.

That is everything we need to make read from the TMP36 sensor using your ADC Pi or ADC Pi Plus, your program should now look like this.

#!/usr/bin/python
from ABE_ADCPi import ADCPi
from ABE_helpers import ABEHelpers
import time
import os

resistor_multiplier = 6.95225 # use 100K resistor in series with the input
zero_offset = 0.826 # zero offset value from calibration data printout
slope = 0.031483 # slope value from calibration data printout

i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x68, 0x69, 18)

def calcHumidity(inval):
    voltage = inval * resistor_multiplier
    humidity = (voltage - zero_offset) / slope
    return humidity

while (True):

    # clear the console
    os.system('clear')

    # read from adc channels and print to screen
    print ("Humidity on channel 1: %0.1f%%" % calcHumidity(adc.read_voltage(1)))

    # wait 0.5 seconds before reading the pins again
    time.sleep(0.5)

Save your program as "demo-hih4000.py" and run it in a command terminal using

python demo-hih4000.py

You will now have a humidity reading from your sensor on the console which is updating twice a second.

Shop

Support

Click to contact us.
X

Have any Questions?

Send us any questions about this page or product and we will reply within 24 hours.

Submit your Message

X

Have any Questions?

Thank you for contacting us.

We will respond to your enquiry in the next 24 hours.

You can find answers to many questions and additional product support in our Knowledge Base and the Community Forum.