Beaglebone and SHT31 humidity sensor in C example

0
103

In this article we look at an SHT31 connected to a Beaglebone first of all lets look at the sensor.

The SHT31 humidity sensor series combines multiple functions and various interfaces (I2C, analog voltage output) with a applications-friendly, very wide operating voltage range (2.15 to 5.5 V). The SHT31 humidity sensor is available in both large and small volumes.

The SHT3x series build on a completely new and optimized CMOSens® chip, which allows for increased reliability and improved accuracy specifications. The SHT3x offers a range of new features, such as enhanced signal processing, two distinctive and user-selectable I2C addresses, an alert mode with programmable humidity and temperature limits, and therefore communication speeds of up to 1 MHz.

OutputI²C, Voltage Out
Supply voltage range2.15 to 5.5 V
Energy consumption4.8µW (at 2.4 V, low repeatability, 1 measurement / s)
RH operating range0 – 100% RH
T operating range-40 to +125°C (-40 to +257°F)
RH response time8 sec (tau63%)

 

Parts Required

 

NameLink
Beaglebone BeagleBone Black TI AM3358 Cortex-A8 development BB-Black Rev.C
SHT31SHT31 Temperature & Humidity Sensor module
Connecting wireFree shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Schematic/Connection

beaglebone and sht31
beaglebone and sht31


Code Example

This is a controleverything example – they have code examples for various platforms. This is the C example from https://github.com/ControlEverythingCommunity/SHT31

 

#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
 
void main() 
{
	// Create I2C bus
	int file;
	char *bus = "/dev/i2c-2";
	if((file = open(bus, O_RDWR)) < 0) 
	{
		printf("Failed to open the bus. \n");
		exit(1);
	}
	// Get I2C device, SHT31 I2C address is 0x44(68)
	ioctl(file, I2C_SLAVE, 0x44);
 
	// Send high repeatability measurement command
	// Command msb, command lsb(0x2C, 0x06)
	char config[2] = {0};
	config[0] = 0x2C;
	config[1] = 0x06;
	write(file, config, 2);
	sleep(1);
 
	// Read 6 bytes of data
	// temp msb, temp lsb, temp CRC, humidity msb, humidity lsb, humidity CRC
	char data[6] = {0};
	if(read(file, data, 6) != 6)
	{
		printf("Error : Input/output Error \n");
	}
	else
	{
	// Convert the data
	double cTemp = (((data[0] * 256) + data[1]) * 175.0) / 65535.0  - 45.0;
	double fTemp = (((data[0] * 256) + data[1]) * 315.0) / 65535.0 - 49.0;
	double humidity = (((data[3] * 256) + data[4])) * 100.0 / 65535.0;
 
	// Output data to screen
	printf("Temperature in Celsius : %.2f C \n", cTemp);
	printf("Temperature in Fahrenheit : %.2f F \n", fTemp);
	printf("Relative Humidity is : %.2f RH \n", humidity);
	}
}

Save this as sht31.c, I used the Cloud 9 IDE

First of all compile the c program.

$>gcc SHT31.c -o SHT31

finally run the c program like this.

$>./SHT31

Output

After running you should see something like this

debian@beaglebone:/var/lib/cloud9/$ ./sht31
Temperature in Celsius : 24.41 C
Temperature in Fahrenheit : 75.94 F
Relative Humidity is : 56.37 RH

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.