SHT7

From Pinguino-Wiki
Jump to: navigation, search

The SHT7 is a temperature and relative humidity sensor. It works like an I2C device, except it is not a really I2C.

This code use float to ascii conversion ( stdlib.h ) and print a float result on CDC.

// How to communicate with Pinguino and a SHT7 sensor
// Jean-Pierre Mandon 2011
 
#include <stdlib.h>
#include <string.h>
 
#define DATA 0             // SDA is connected to pin 0 of Pinguino
#define CLOCK 1            // SCK is connected to pin 1 of Pinguino
 
void setup()
{
	pinMode(DATA,OUTPUT);
	pinMode(CLOCK,OUTPUT);
	digitalWrite(DATA,1);
	digitalWrite(CLOCK,0);
}
 
unsigned int send_command(unsigned char value)
{
	unsigned char i;
	unsigned char lowbyte;
	unsigned char highbyte;
 
	// start condition
	digitalWrite(DATA,1);
	delay(1);
	digitalWrite(CLOCK,HIGH);
	delay(1);
	digitalWrite(DATA,LOW);
	delay(1);
	digitalWrite(CLOCK,LOW);
	delay(1);
	digitalWrite(CLOCK,HIGH);
	delay(1);
	digitalWrite(DATA,HIGH);
	delay(1);
	digitalWrite(CLOCK,LOW);
	delay(1);
 
	// send command
	for (i=0;i<8;i++)
	{
		if ((value&0x80)==0x80)
			digitalWrite(DATA,1);
		else
			digitalWrite(DATA,0);
		value=value<<1;
		delay(1);
		digitalWrite(CLOCK,1);
		delay(1);
		digitalWrite(CLOCK,0);
		delay(1);
	}
 
	// wait for acknowledge   
	pinMode(DATA,INPUT);
	digitalWrite(CLOCK,1);
	delay(5);
	if (digitalRead(DATA)) 
		return 0xFFFF;    // bad acknowledge
	digitalWrite(CLOCK,0);
 
	// wait for conversion time
	delay(500);
 
	// check the data line to know if measurement is OK
	if (digitalRead(DATA))
		return 0xFFFF;
 
	// reading measurement value high byte
	highbyte=0;
	for (i=0;i<8;i++)
	{
		digitalWrite(CLOCK,1);
		delay(1);
		if (digitalRead(DATA))
			highbyte=highbyte+1;   
		digitalWrite(CLOCK,0);
		delay(1);
		highbyte=highbyte<<1;
		delay(1);
	}
 
	// acknowledge MSB   
	pinMode(DATA,OUTPUT);
	digitalWrite(DATA,0);
	digitalWrite(CLOCK,1);
	delay(1);
	digitalWrite(CLOCK,0);
	delay(1);
 
	// reading measurement value low byte
	pinMode(DATA,INPUT);
	lowbyte=0;
	for (i=0;i<8;i++)
	{
		digitalWrite(CLOCK,1);
		delay(1);
		if (digitalRead(DATA)) lowbyte=lowbyte+1;       
		digitalWrite(CLOCK,0);
		delay(1);
		lowbyte=lowbyte<<1;
		delay(1);
	}
 
	// Set High to DATA line to end communication   
	pinMode(DATA,OUTPUT);
	digitalWrite(DATA,1);   
	return(lowbyte+(highbyte*256));
}
 
// reset sensor
void reset_SHT7()
{
	unsigned char i;
	pinMode(DATA,OUTPUT);   
	digitalWrite(DATA,1);
	for (i=0;i<10;i++)
	{
		digitalWrite(CLOCK,1);
		delay(1);
		digitalWrite(CLOCK,0);
		delay(1);
	}
}
 
void loop()
{
	unsigned int i;
	unsigned char chaine[];
	float temperature;
 
	delay(3000);
	CDC.print("start conversion\n\r",18);
	while(1)
	{
		delay(500);
		i=send_command(3);                          // send command 3 to SHT7 ( read temperature )
		if (i<65535)                                // if no error
		{
			temperature=(-40+(0.01*i))/4;       // convert measurement to real value
			x_ftoa(temperature,chaine,2,2);     // convert float value to ascii
			strcat(chaine,"\n\r");              // add CR/LF to string
			CDC.print(chaine,strlen(chaine));   // print string on CDC
		}
		reset_SHT7();
	}
}
Personal tools