Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yadwinder Singh authored and Yadwinder Singh committed Apr 21, 2016
1 parent c5307a7 commit 2e59a25
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions C/SI7006_A20.c
@@ -0,0 +1,68 @@
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// SI7006-A20
// This code is designed to work with the SI7006-A20_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Humidity?sku=SI7006-A20_I2CS#tabs-0-product_tabset-2

#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-1";
if((file = open(bus, O_RDWR)) < 0)
{
printf("Failed to open the bus. \n");
exit(1);
}
// Get I2C device, SI7006-A20 I2C address is 0x40(64)
ioctl(file, I2C_SLAVE, 0x40);

// Send humidity measurement command, NO HOLD MASTER(0xF5)
char config[1] = {0xF5};
write(file, config, 1);
sleep(1);

// Read 2 bytes of humidity data
// humidity msb, humidity lsb
char data[2] = {0};
if(read(file, data, 2) != 2)
{
printf("Erorr : Input/output Erorr \n");
}
else
{
// Convert the data
float humidity = (((data[0] * 256 + data[1]) * 125.0) / 65536.0) - 6;

// Output data to screen
printf("Relative Humidity : %.2f RH \n", humidity);
}

// Send temperature measurement command, NO HOLD MASTER(0xF3)
config[0] = 0xF3;
write(file, config, 1);
sleep(1);

// Read 2 bytes of temperature data
// temp msb, temp lsb
if(read(file, data, 2) != 2)
{
printf("Erorr : Input/output Erorr \n");
}
else
{
// Convert the data
float cTemp = (((data[0] * 256 + data[1]) * 175.72) / 65536.0) - 46.85;
float fTemp = cTemp * 1.8 + 32;

// Output data to screen
printf("Temperature in Celsius : %.2f C \n", cTemp);
printf("Temperature in Fahrenheit : %.2f F \n", fTemp);
}
}

0 comments on commit 2e59a25

Please sign in to comment.