Skip to content

Instantly share code, notes, and snippets.

@Miceuz
Created July 22, 2015 18:09
Show Gist options
  • Save Miceuz/3f420d7f1ef9dabc9243 to your computer and use it in GitHub Desktop.
Save Miceuz/3f420d7f1ef9dabc9243 to your computer and use it in GitHub Desktop.
Standalone I2C soil moisture sensor example
/*
I2C sensor - arduino example
Connection
Chirp pin 1 - no connection
Chirp pin 2 - Arduino VCC
Chirp pin 3 - Arduino A5
Chirp pin 4 - Arduino A4
Chirp pin 5 - no connection
Chirp pin 6 - Arduino GND
*/
#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);
writeI2CRegister8bit(0x20, 6); //reset
}
void loop() {
Serial.print(readI2CRegister16bit(0x20, 0)); //read capacitance register
Serial.print(", ");
Serial.print(readI2CRegister16bit(0x20, 5)); //temperature register
Serial.print(", ");
writeI2CRegister8bit(0x20, 3); //request light measurement
Serial.println(readI2CRegister16bit(0x20, 4)); //read light register
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment