Skip to content

Instantly share code, notes, and snippets.

@neufuture
Last active August 29, 2015 14:14
Show Gist options
  • Save neufuture/1a9efcbcfcd465c00760 to your computer and use it in GitHub Desktop.
Save neufuture/1a9efcbcfcd465c00760 to your computer and use it in GitHub Desktop.
Arduino code for Chrip moisture sensor
#include <Wire.h>
void writeI2CRegister8bit(int addr, int value) {
Wire.beginTransmission(addr);
Wire.write(value);
Wire.endTransmission();
}
unsigned int readI2CRegister16bit(int addr, int reg) {
Wire.beginTransmission(addr);
Wire.write(reg);
Wire.endTransmission();
delay(20);
Wire.requestFrom(addr, 2);
unsigned int t = Wire.read() << 8;
t = t | Wire.read();
return t;
}
void setup() {
Wire.begin();
Serial.begin(9600);
void("setup complete");
writeI2CRegister8bit(0x20, 6); //reset
delay(500);
writeI2CRegister8bit(0x20, 6);
}
void loop() {
unsigned int moisture = readI2CRegister16bit(0x20, 0);
if(moisture < 1000)
Serial.println(moisture);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment