Skip to content

Instantly share code, notes, and snippets.

@analog-io
Created April 10, 2015 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save analog-io/011ceb14fe30e2ca877b to your computer and use it in GitHub Desktop.
Save analog-io/011ceb14fe30e2ca877b to your computer and use it in GitHub Desktop.
si7020 Interface sketch for energia
#include "Energia.h"
#include "I2C_SoftwareLibrary.h"
#define LED RED_LED
#define SCL_PIN P2_6 ///< pin for SCL
#define SDA_PIN P2_7 ///< pin for SDA
SoftwareWire Wire(SDA_PIN, SCL_PIN);
#define _address 0x40
uint8_t id[8];
uint8_t crc[8];
uint8_t led_state = 0;
void setup(void) {
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, HIGH);
Serial.begin(9600);
Serial.println("*** START");
Wire.begin();
Wire.begin();
Wire.beginTransmission(_address);
Wire.write(0xFA);
Wire.write(0x0F);
Wire.endTransmission(false);
Wire.requestFrom(_address, 8);
while (Wire.available() < 2);
id[4] = Wire.read();
crc[4] = Wire.read();
id[5] = Wire.read();
crc[5] = Wire.read();
id[6] = Wire.read();
crc[6] = Wire.read();
id[7] = Wire.read();
crc[7] = Wire.read();
Wire.begin();
Wire.beginTransmission(_address);
Wire.write(0xFC);
Wire.write(0xC9);
Wire.endTransmission(false);
Wire.requestFrom(_address, 4);
while (Wire.available() < 2);
id[0] = Wire.read();
crc[0] = Wire.read();
id[1] = Wire.read();
crc[1] = Wire.read();
id[2] = Wire.read();
crc[2] = Wire.read();
id[3] = Wire.read();
crc[3] = Wire.read();
}
void loop(void)
{
Wire.beginTransmission(_address);
Wire.write(0xE5);
Wire.endTransmission(false);
Wire.requestFrom(_address, 2);
while (Wire.available() < 2);
uint16_t rhMS = Wire.read();
uint16_t rhLS = Wire.read();
Wire.beginTransmission(_address);
Wire.write(0xE0);
Wire.endTransmission(false);
Wire.requestFrom(_address, 2);
while (Wire.available() < 2);
uint16_t MS = Wire.read();
uint16_t LS = Wire.read();
float t = (MS<<8)+LS;
float rh = (rhMS<<8)+rhLS;
t=((175.72*t)/65536-46.85)*9/5+32;
rh = (125*rh)/65536-6;
Serial.print("uid: ");
Serial.print(id[0],HEX);
Serial.print(id[1],HEX);
Serial.print(id[2],HEX);
Serial.print(id[3],HEX);
Serial.print(id[4],HEX);
Serial.print(id[5],HEX);
Serial.print(id[6],HEX);
Serial.print(id[7],HEX);
Serial.println("");
Serial.print("t:");
Serial.print(t,2);
Serial.print(", rh:");
Serial.println(rh,2);
delay(1000); // wait for a second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment