From 2e59a2520911727dba2c577721c2b6c87e744c87 Mon Sep 17 00:00:00 2001 From: Yadwinder Singh Date: Thu, 21 Apr 2016 18:14:11 +0530 Subject: [PATCH] first commit --- C/SI7006_A20.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 C/SI7006_A20.c diff --git a/C/SI7006_A20.c b/C/SI7006_A20.c new file mode 100755 index 0000000..7de3d49 --- /dev/null +++ b/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 +#include +#include +#include +#include + +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); + } +}