{"id":47873498,"url":"https://github.com/miceuz/i2c-moisture-sensor","last_synced_at":"2026-04-04T01:01:15.514Z","repository":{"id":33851093,"uuid":"37555909","full_name":"Miceuz/i2c-moisture-sensor","owner":"Miceuz","description":"I2C based soil moisture sensor","archived":false,"fork":false,"pushed_at":"2023-02-25T13:01:36.000Z","size":14167,"stargazers_count":238,"open_issues_count":4,"forks_count":73,"subscribers_count":32,"default_branch":"master","last_synced_at":"2024-01-27T03:43:12.164Z","etag":null,"topics":["avr","i2c","kicad","sensor","soil-moisture","soil-moisture-sensor"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Miceuz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2015-06-16T21:00:35.000Z","updated_at":"2023-12-30T12:20:41.000Z","dependencies_parsed_at":"2022-07-31T11:08:05.094Z","dependency_job_id":"b1c48803-3c6e-41bd-9883-1a554f4885a0","html_url":"https://github.com/Miceuz/i2c-moisture-sensor","commit_stats":{"total_commits":108,"total_committers":7,"mean_commits":"15.428571428571429","dds":"0.41666666666666663","last_synced_commit":"c687328d2fdbe45b1b6c9d6b92627460e9f3167d"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/Miceuz/i2c-moisture-sensor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Miceuz%2Fi2c-moisture-sensor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Miceuz%2Fi2c-moisture-sensor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Miceuz%2Fi2c-moisture-sensor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Miceuz%2Fi2c-moisture-sensor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Miceuz","download_url":"https://codeload.github.com/Miceuz/i2c-moisture-sensor/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Miceuz%2Fi2c-moisture-sensor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31383636,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-03T23:20:52.058Z","status":"ssl_error","status_checked_at":"2026-04-03T23:20:51.675Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["avr","i2c","kicad","sensor","soil-moisture","soil-moisture-sensor"],"created_at":"2026-04-04T01:00:38.774Z","updated_at":"2026-04-04T01:01:15.493Z","avatar_url":"https://github.com/Miceuz.png","language":"C","readme":"# i2c-moisture-sensor\n\nI2C based soil moisture sensor. A continuation of the [Chirp - plant watering alarm](https://github.com/Miceuz/PlantWateringAlarm) project. There is also an [RS485](https://github.com/Miceuz/rs485-moist-sensor) and an [analog](https://github.com/Miceuz/soil-moisture-sensor-analog) version available.\n\n[![](https://d2ss6ovg47m0r5.cloudfront.net/badges/tindie-smalls.png)](https://www.tindie.com/products/2330/)\n\n## I2C protocol\nAvailable registers for reading and writing.\n\n  Name | Register | R/W | Data length \n-------|----------|-----|-------------\nGET_CAPACITANCE   | 0x00 | (r) | 2\nSET_ADDRESS       | 0x01 | (w) | 1\nGET_ADDRESS       | 0x02 | (r) | 1\nMEASURE_LIGHT     | 0x03 | (w) | 0\nGET_LIGHT         | 0x04 | (r) | 2\nGET_TEMPERATURE   | 0x05 | (r) | 2\nRESET             | 0x06 | (w) | 0\nGET_VERSION       | 0x07 | (r) | 1\nSLEEP             | 0x08 | (w) | 0\nGET_BUSY          | 0x09 | (r) | 1\n\nGET_BUSY returns 1 if any measurement is in progress, 0 otherwise.\n\n## How to interpret the readings\n\n16bit values are returned most significant byte first.\n\nBoth light and moisture sensors give relative values. Meaning, more moisture will give you higher reading, more light, lower reading. They are not calibrated to any particular si units - this is up to you.\n\nMoisture is somewhat linear. I test all sensors before shipping and they give about 290 - 310 in free air at 5V supply.\n\nTemperature is in Celsius, value is in tenths of degrees Celsius. I.e. value 252 would mean 25.2°C.\n\nI didn't measure linearity of the light sensor, it gives 65535 in a dark room away form desk lamp. When it's dark, it takes longer to measure light, reading the light register while measurement is in progress will return the previous reading. Be aware, light sensor is pretty noisy.\n\nTemperature is measured by the thermistor on the body of the sensor. Calculated absolute measurement accuracy is better than 2%. \n\nNote Upon reading the moisture or temperature value, a value form the previous read command is returned and the new measurement is started. If you do rare measurements and want to act instantly, do two consecutive readings to get the most up to date data. Also you can read GET_BUSY register via i2c - it will indicate when the measurement is done. Basically the process goes like this: read from GET_CAPACITANCE, discard results, then read from GET_BUSY until you get '0' as an answer, then read form GET_CAPACITANCE again - the returned value is the soil moisture NOW.\n\n### Python library for Raspberry Pi\n\n*NOTE: if you experience problems on Raspberry Pi 3, slow down I2C bus speed by adding this line to /boot/config.txt:*\n**dtparam=i2c1_baudrate=30000**\n\n[Göran Lundberg](https://github.com/ageir) has released a Python library for Raspberry Pi: [https://github.com/ageir/chirp-rpi](https://github.com/ageir/chirp-rpi)\nIt has a very comprehensive documentation and covers a lot of functionality.\n\nSome features:\n* Uses a trigger function to trigger all enabled sensors. User selectable.\n* Get soil moisture in percent (requires calibration) or capacitance value.\n* Several temperature scales to choose from. Celcius, Fahrenheit and Kelvin.\n* Offset to calibrate the temperature sensor.\n* Measurement timestamps for all on board sensors.\n* Built in support for changing the I2C address of the sensor.\n* Deep sleep mode to conserve power.\n* Calibration tool for soil moisture.\n\n### Raspberry Pi examples\n\nThis is interface class provided by Daniel Tamm and Jasper Wallace\n```python\n#!/usr/bin/python\n# cannot use python3 because smbus not working there\n# Modified script from https://github.com/JasperWallace/chirp-graphite/blob/master/chirp.py\n# by DanielTamm\n\nimport smbus, time, sys\n\nclass Chirp:\n\tdef __init__(self, bus=1, address=0x20):\n\t\tself.bus_num = bus\n\t\tself.bus = smbus.SMBus(bus)\n\t\tself.address = address\n    \n\tdef get_reg(self, reg):\n\t\t# read 2 bytes from register\n\t\tval = self.bus.read_word_data(self.address, reg)\n\t\t# return swapped bytes (they come in wrong order)\n\t\treturn (val \u003e\u003e 8) + ((val \u0026 0xFF) \u003c\u003c 8)\n\n\tdef reset(self):\n\t\t# To reset the sensor, write 6 to the device I2C address\n\t\tself.bus.write_byte(self.address, 6)\n\n\tdef set_addr(self, new_addr):\n\t\t# To change the I2C address of the sensor, write a new address\n\t\t# (one byte [1..127]) to register 1; the new address will take effect after reset\n\t\tself.bus.write_byte_data(self.address, 1, new_addr)\n\t\t# second request is required since FW 0x26 to protect agains spurious address changes\n\t\tself.bus.write_byte_data(self.address, 1, new_addr)\n\t\tself.reset()\n\t\tself.address = new_addr\n\n\tdef moist(self):\n\t\t# To read soil moisture, read 2 bytes from register 0\n\t\treturn self.get_reg(0)\n\n\tdef temp(self):\n\t\t# To read temperature, read 2 bytes from register 5\n\t\treturn self.get_reg(5)\n\n\tdef light(self):\n\t\t# To read light level, start measurement by writing 3 to the\n\t\t# device I2C address, wait for 3 seconds, read 2 bytes from register 4\n\t\tself.bus.write_byte(self.address, 3)\n\t\ttime.sleep(1.5)\n\t\treturn self.get_reg(4)\n\n\tdef __repr__(self):\n\t\treturn \"\u003cChirp sensor on bus %d, addr %d\u003e\" % (self.bus_num, self.address)\n\nif __name__ == \"__main__\":\n\taddr = 0x20\n\tif len(sys.argv) == 2:\n\t\tif sys.argv[1].startswith(\"0x\"):\n\t\t\taddr = int(sys.argv[1], 16)\n\t\telse:\n\t\t\taddr = int(sys.argv[1])\n\tchirp = Chirp(1, addr)\n\n\tprint chirp\n\tprint \"Moisture\\tTemperature\\tBrightness\"\n\twhile True:\n\t\tprint \"%d\\t%d\\t%d\" % (chirp.moist(), chirp.temp(), chirp.light())\n\t\ttime.sleep(1)\n```\n\nThis is another RasPi example provided by user *krikk*\n```python\n#!/usr/bin/python\n\n#https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_I2C\nfrom Adafruit_I2C import Adafruit_I2C\nfrom time import sleep, strftime\nfrom datetime import datetime\ndeviceAddr = 0x20\n\ni2c = Adafruit_I2C( deviceAddr, -1, False )\n\n#to change adress\n#i2c.write8( 1, 0x22 )\n\n#reset sensor, we need this otherwise i get inconsistent light reading in the dark...\ni2c.write8( deviceAddr, 0x06 )\nsleep(5)\n\ni2c.write8(deviceAddr, 3)\nsleep(3)\nlight = i2c.readU16(4, False)\ntemp = i2c.readS16(5, False)/float(10)\nmoisture = i2c.readU16(0, False)\nprint \"Temperature\\tMoisture\\tBrightness\"\nprint str(temp) + \":\" + str(moisture) + \":\" + str(light)\n```\n\n### Arduino library\nIngo Fischer has written an Arduino library for the sensor, it has a couple of ready made examples: https://github.com/Apollon77/I2CSoilMoistureSensor \n\nBelow are old examples for bare-bones Arduino illustrating a basic I2C use.\n\n### Arduino example\n```arduino\n#include \u003cWire.h\u003e\n\nvoid writeI2CRegister8bit(int addr, int value) {\n  Wire.beginTransmission(addr);\n  Wire.write(value);\n  Wire.endTransmission();\n}\n\nunsigned int readI2CRegister16bit(int addr, int reg) {\n  Wire.beginTransmission(addr);\n  Wire.write(reg);\n  Wire.endTransmission();\n  delay(20);\n  Wire.requestFrom(addr, 2);\n  unsigned int t = Wire.read() \u003c\u003c 8;\n  t = t | Wire.read();\n  return t;\n}\n\nvoid setup() {\n  Wire.begin();\n  Serial.begin(9600);\n  writeI2CRegister8bit(0x20, 6); //reset\n}\n\nvoid loop() {\n  Serial.print(readI2CRegister16bit(0x20, 0)); //read capacitance register\n  Serial.print(\", \");\n  Serial.print(readI2CRegister16bit(0x20, 5)); //temperature register\n  Serial.print(\", \");\n  writeI2CRegister8bit(0x20, 3); //request light measurement \n  Serial.println(readI2CRegister16bit(0x20, 4)); //read light register\n}\n```\n### Note for ESP8266 based systems\nIn some cases the default ESP8266 Arduino I2C library has the clock stretching timeout set too low. If you experience intermittent communication, add this to your code:\n```\nWire.setClockStretchLimit(2500)\n```\n### Micropython\nMicropython library is available here: [https://github.com/scopelemanuele/pyChirpLib](https://github.com/scopelemanuele/pyChirpLib)\n\n\n## Address change example\nBy default the sensor comes with 0x20 set as an address, this is an example on how to change address for indivitual sensor:\n```arduino\n#include \u003cWire.h\u003e\n \nvoid writeI2CRegister8bit(int addr, int reg, int value) {\n  Wire.beginTransmission(addr);\n  Wire.write(reg);\n  Wire.write(value);\n  Wire.endTransmission();\n}\n \nvoid writeI2CRegister8bit(int addr, int value) {\n  Wire.beginTransmission(addr);\n  Wire.write(value);\n  Wire.endTransmission();\n}\n \nvoid setup() {\n  Wire.begin();\n  Serial.begin(9600);\n                                       //talking to the default address 0x20\n  writeI2CRegister8bit(0x20, 1, 0x21); //change address to 0x21\n  writeI2CRegister8bit(0x20, 6);       //reset\n  delay(1000);                         //give it some time to boot\n}\n \n/*loop scans I2C bus and displays foud addresses*/ \nvoid loop()\n{\n  byte error, address;\n  int nDevices;\n \n  Serial.println(\"Scanning...\");\n \n  nDevices = 0;\n  for(address = 1; address \u003c 127; address++ )\n  {\n    // The i2c_scanner uses the return value of\n    // the Write.endTransmisstion to see if\n    // a device did acknowledge to the address.\n    Wire.beginTransmission(address);\n    error = Wire.endTransmission();\n \n    if (error == 0)\n    {\n      Serial.print(\"I2C device found at address 0x\");\n      if (address\u003c16)\n        Serial.print(\"0\");\n      Serial.print(address,HEX);\n      Serial.println(\"  !\");\n \n      nDevices++;\n    }\n    else if (error==4)\n    {\n      Serial.print(\"Unknow error at address 0x\");\n      if (address\u003c16)\n        Serial.print(\"0\");\n      Serial.println(address,HEX);\n    }    \n  }\n  if (nDevices == 0)\n    Serial.println(\"No I2C devices found\\n\");\n  else\n    Serial.println(\"done\\n\");\n \n  delay(5000);           // wait 5 seconds for next scan\n}\n```\n### Particle Photon\n\nThere is a great [tutorial](ParticlePhoton-tutorial.md) by Miriam Cox for Particle Photon boards. Also there is a [library available](https://github.com/VintageGeek/I2CSoilMoistureSensor) by Mike.\n\n## Links and mentions\n\n* A [video from Growing Robot](https://www.youtube.com/watch?v=rB4vS7I0euA) about using the sensors with Raspberry Pi\n* A [complete Open Source logging solution](https://git.jeelabs.org/jcw/zelkova) employing a batch of 40 sensors in Crete for tree monitoring\n* [Chirp - Wemos - Azure bridge](https://github.com/korpiq/chirp-wemos-meter) ESP8266 based system that pushes sensor data to Azure cloud serivices. \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiceuz%2Fi2c-moisture-sensor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiceuz%2Fi2c-moisture-sensor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiceuz%2Fi2c-moisture-sensor/lists"}