Miceuz / i2c-moisture-sensor

I2C based soil moisture sensor
Apache License 2.0
240 stars 72 forks source link

strange reading on the light sensor & bugs in example code?? #2

Closed krikk closed 5 years ago

krikk commented 8 years ago

hi!

i got your sensor running with a raspberry pi and after a lot of research and tries i got very good readings.. my raspberry pi code:

#!/usr/bin/python

#https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_I2C
from Adafruit_I2C import Adafruit_I2C
from time import sleep, strftime
from datetime import datetime
deviceAddr = 0x20

i2c = Adafruit_I2C( deviceAddr, -1, False )

#to change adress
#i2c.write8( 1, 0x22 )

#reset sensor, we need this otherwise i get inconsistent light reading in the dark...
i2c.write8( deviceAddr, 0x06 )
sleep(5)

i2c.write8(deviceAddr, 3)
sleep(3)
light = i2c.readU16(4, False)
temp = i2c.readS16(5, False)/float(10)
moisture = i2c.readU16(0, False)
print str(temp) + ":" + str(moisture) + ":" + str(light)

... only strange thing i noted there was that i had to reset the sensor always before reading values, because otherwise i got strange readings from the light sensor after a few reads... have no clue why... buts that ok with me.

but now i got a shiny new arduino uno and i tried to run your sensor with it, and once again i get wrong readings from the light sensor, e.g. i get a negative reading when measuring in a dark room

i used the provided example code from github a adapted it:

void loop(){

    //Reset i2c moisture sensor
    Serial.println("Reset i2c moisture sensor");
    writeI2CRegister8bit(0x20, 6); //reset
    delay(5000);

    writeI2CRegister8bit(0x20, 3); //request light measurement 
    delay(3000);
    int light = readI2CRegister16bit(0x20, 4); //read light register
    int moisture = readI2CRegister16bit(0x20, 0); //read capacitance register
    float temp = readI2CRegister16bit(0x20, 5)/(float)10; //temperature register
    char tempstr[15];
    dtostrf(temp,4, 1, tempstr);

    Serial.print("moisture: ");
    Serial.print(moisture); 
    Serial.print(", Temp: ");

    Serial.print(tempstr); 
    Serial.print(", Light: ");
    Serial.println(light); 
    delay(2000);
}

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;
}   

and i noted a few strange things in it:

unsigned int readI2CRegister16bit(int addr, int reg) {
...
}

why unsigned ?? ...what about negative temperature values (just tested with a cool-pack, give completely wrong values) (this works with my raspberry pi, python code) also the example code does not wait 3 seconds with reading light value after writing...?

all in all the provided example code seems wrong to me... i reads values, but i think there is something wrong with the conversion of the values... would be nice if you could have a look into this or give me some hints what i'm doing wrong... thx

Miceuz commented 8 years ago

Hi,

I'm not exactly sure why you get weird readings. Are aware that light measurement takes some time, if you query the sensor before reading is done, it returns the old value. So basically you should wait for ~3 - 5 seconds before reading light measurement result.

Thanks for the code!

On 11/07/2015 12:08 AM, krikk wrote:

hi!

i got your sensor running with a raspberry pi and after a lot of research and tries i got very good readings.. my raspberry pi code:

|#!/usr/bin/python

https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_I2C

from Adafruit_I2C import Adafruit_I2C from time import sleep, strftime from datetime import datetime deviceAddr = 0x20 i2c = Adafruit_I2C( deviceAddr, -1, False ) #to change adress #i2c.write8( 1, 0x22 ) #reset sensor, we need this otherwise i get inconsistent light reading in the dark... i2c.write8( deviceAddr, 0x06 ) sleep(5) i2c.write8(deviceAddr, 3) sleep(3) light = i2c.readU16(4, False) temp = i2c.readS16(5, False)/float(10) moisture = i2c.readU16(0, False) print str(temp) + ":"

  • str(moisture) + ":" + str(light) |

... only strange thing i noted there was that i had to reset the sensor always before reading values, because otherwise i got strange readings from the light sensor after a few reads... have no clue why... buts that ok with me.

but now i got a shiny new arduino uno and i tried to run your sensor with it, and once again i get wrong readings from the light sensor, e.g. i get a negative reading when measuring in a dark room

i used the provided example code from github a adapted it:

|void loop(){ //Reset i2c moisture sensor Serial.println("Reset i2c moisture sensor"); writeI2CRegister8bit(0x20, 6); //reset delay(5000); writeI2CRegister8bit(0x20, 3); //request light measurement delay(3000); int light = readI2CRegister16bit(0x20, 4); //read light register int moisture = readI2CRegister16bit(0x20, 0); //read capacitance register float temp = readI2CRegister16bit(0x20, 5)/(float)10; //temperature register char tempstr[15]; dtostrf(temp,4, 1, tempstr); Serial.print("moisture: "); Serial.print(moisture); Serial.print(", Temp: "); Serial.print(tempstr); Serial.print(", Light: "); Serial.println(light); delay(2000); } 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; } |

and i noted a few strange things in it:

|unsigned int readI2CRegister16bit(int addr, int reg) { ... } |

why unsigned ?? ...what about negative temperature values (just tested with a cool-pack, give completely wrong values) (this works with my raspberry pi, python code) also the example code does not wait 3 seconds with reading light value after writing...?

all in all the provided example code seems wrong to me... i reads values, but i think there is something wrong with the conversion of the values... would be nice if you could have a look into this or give me some hints what i'm doing wrong... thx

— Reply to this email directly or view it on GitHub https://github.com/Miceuz/i2c-moisture-sensor/issues/2.

[ok]

mic.

Apollon77 commented 8 years ago

I think you need an "Unsigned int" for the light reading ... then you will never get negaive values.

For Temperature it could work with "signed" values ... will test the next days ...

Apollon77 commented 8 years ago

I tested the sensor today in "cold" environment an git interesting "light" values - it seems that the light sernsor don't like cold :-) I've put the sensor in my -14 degree celsius fridge ... each reading in the following list is 3 seconds delay: At start of the test the values wew at around 236-238 capaticitance, 22,7 degree celsius and ligth around 25/26.000 as you see in the first values. Then I moved to the fridge and light was changig, but this is also not interesting. Look at the timepoint where temperature in falling ...

Soil Moisture Capacitance: 238, Temperature: 22.70, Light: 25833 Soil Moisture Capacitance: 236, Temperature: 22.70, Light: 26611 Soil Moisture Capacitance: 237, Temperature: 22.70, Light: 26522 Soil Moisture Capacitance: 237, Temperature: 22.70, Light: 26289 Soil Moisture Capacitance: 238, Temperature: 22.70, Light: 26027 Soil Moisture Capacitance: 236, Temperature: 22.70, Light: 29537 Soil Moisture Capacitance: 237, Temperature: 22.70, Light: 33503 Soil Moisture Capacitance: 237, Temperature: 22.80, Light: 33672 Soil Moisture Capacitance: 237, Temperature: 22.80, Light: 31238 Soil Moisture Capacitance: 237, Temperature: 22.60, Light: 16495 Soil Moisture Capacitance: 237, Temperature: 22.70, Light: 16924 Soil Moisture Capacitance: 237, Temperature: 22.50, Light: 18798 Soil Moisture Capacitance: 238, Temperature: 22.70, Light: 44514 Soil Moisture Capacitance: 237, Temperature: 22.70, Light: 29258

=======> in Fridge :-)

Soil Moisture Capacitance: 237, Temperature: 22.80, Light: 49732 Soil Moisture Capacitance: 238, Temperature: 22.70, Light: 59615 Soil Moisture Capacitance: 239, Temperature: 21.20, Light: 60379 Soil Moisture Capacitance: 239, Temperature: 19.40, Light: 62946 Soil Moisture Capacitance: 238, Temperature: 18.00, Light: 65221 Soil Moisture Capacitance: 239, Temperature: 16.80, Light: 2377 Soil Moisture Capacitance: 240, Temperature: 15.90, Light: 5015 Soil Moisture Capacitance: 240, Temperature: 14.70, Light: 7588 Soil Moisture Capacitance: 241, Temperature: 13.70, Light: 10411 Soil Moisture Capacitance: 241, Temperature: 12.90, Light: 13260 Soil Moisture Capacitance: 241, Temperature: 11.90, Light: 15969 Soil Moisture Capacitance: 241, Temperature: 11.00, Light: 19006 Soil Moisture Capacitance: 241, Temperature: 10.30, Light: 21965 Soil Moisture Capacitance: 242, Temperature: 9.40, Light: 24788 Soil Moisture Capacitance: 243, Temperature: 8.70, Light: 27819 Soil Moisture Capacitance: 242, Temperature: 7.90, Light: 30899 Soil Moisture Capacitance: 242, Temperature: 7.20, Light: 33837 Soil Moisture Capacitance: 244, Temperature: 6.50, Light: 36629 Soil Moisture Capacitance: 244, Temperature: 5.80, Light: 39785 Soil Moisture Capacitance: 244, Temperature: 5.20, Light: 42828 Soil Moisture Capacitance: 245, Temperature: 4.60, Light: 45913 Soil Moisture Capacitance: 245, Temperature: 3.90, Light: 48760 Soil Moisture Capacitance: 245, Temperature: 3.40, Light: 51619 Soil Moisture Capacitance: 245, Temperature: 2.70, Light: 54787 Soil Moisture Capacitance: 246, Temperature: 2.20, Light: 57772 Soil Moisture Capacitance: 246, Temperature: 1.60, Light: 60723 Soil Moisture Capacitance: 246, Temperature: 1.20, Light: 63549 Soil Moisture Capacitance: 247, Temperature: 0.60, Light: 1006 Soil Moisture Capacitance: 247, Temperature: 0.20, Light: 3900 Soil Moisture Capacitance: 247, Temperature: -0.30, Light: 6955 Soil Moisture Capacitance: 247, Temperature: -0.80, Light: 9639 Soil Moisture Capacitance: 248, Temperature: -1.30, Light: 12380 Soil Moisture Capacitance: 248, Temperature: -1.80, Light: 15314 Soil Moisture Capacitance: 247, Temperature: -2.30, Light: 17992 Soil Moisture Capacitance: 248, Temperature: -2.80, Light: 20789 Soil Moisture Capacitance: 248, Temperature: -3.00, Light: 23535 Soil Moisture Capacitance: 248, Temperature: -3.50, Light: 25958 Soil Moisture Capacitance: 249, Temperature: -4.10, Light: 28836 Soil Moisture Capacitance: 249, Temperature: -4.20, Light: 31497 Soil Moisture Capacitance: 249, Temperature: -4.60, Light: 33924 Soil Moisture Capacitance: 249, Temperature: -5.20, Light: 36519 Soil Moisture Capacitance: 249, Temperature: -5.30, Light: 38986 Soil Moisture Capacitance: 249, Temperature: -5.60, Light: 41580 Soil Moisture Capacitance: 249, Temperature: -6.00, Light: 44005 Soil Moisture Capacitance: 249, Temperature: -6.20, Light: 46106 Soil Moisture Capacitance: 250, Temperature: -6.60, Light: 48673 Soil Moisture Capacitance: 251, Temperature: -6.90, Light: 50834 Soil Moisture Capacitance: 251, Temperature: -7.30, Light: 53472 Soil Moisture Capacitance: 251, Temperature: -7.50, Light: 55486 Soil Moisture Capacitance: 251, Temperature: -7.90, Light: 57828 Soil Moisture Capacitance: 251, Temperature: -8.00, Light: 59703 Soil Moisture Capacitance: 251, Temperature: -8.30, Light: 62157 Soil Moisture Capacitance: 251, Temperature: -8.50, Light: 64149 Soil Moisture Capacitance: 251, Temperature: -8.80, Light: 624 Soil Moisture Capacitance: 252, Temperature: -8.90, Light: 2633 Soil Moisture Capacitance: 251, Temperature: -9.30, Light: 4599 Soil Moisture Capacitance: 252, Temperature: -9.40, Light: 6953 Soil Moisture Capacitance: 252, Temperature: -9.70, Light: 8264 Soil Moisture Capacitance: 252, Temperature: -9.80, Light: 7946

=======> out of fridge

Soil Moisture Capacitance: 253, Temperature: -10.10, Light: 53029 Soil Moisture Capacitance: 252, Temperature: -9.30, Light: 27325 Soil Moisture Capacitance: 251, Temperature: -6.60, Light: 24511 Soil Moisture Capacitance: 251, Temperature: -4.50, Light: 11866 Soil Moisture Capacitance: 250, Temperature: -1.40, Light: 17357 Soil Moisture Capacitance: 249, Temperature: 1.20, Light: 65535 Soil Moisture Capacitance: 249, Temperature: 3.50, Light: 33331 Soil Moisture Capacitance: 247, Temperature: 4.20, Light: 30776 Soil Moisture Capacitance: 247, Temperature: 5.00, Light: 44845 Soil Moisture Capacitance: 247, Temperature: 6.10, Light: 40233 Soil Moisture Capacitance: 247, Temperature: 7.40, Light: 34273 Soil Moisture Capacitance: 245, Temperature: 7.80, Light: 32019 Soil Moisture Capacitance: 246, Temperature: 8.30, Light: 31617 Soil Moisture Capacitance: 247, Temperature: 8.80, Light: 31225 Soil Moisture Capacitance: 245, Temperature: 9.20, Light: 30780 Soil Moisture Capacitance: 246, Temperature: 9.50, Light: 30539 Soil Moisture Capacitance: 244, Temperature: 9.90, Light: 30480 Soil Moisture Capacitance: 245, Temperature: 10.30, Light: 30286 Soil Moisture Capacitance: 245, Temperature: 10.60, Light: 30011 Soil Moisture Capacitance: 245, Temperature: 11.10, Light: 29898 Soil Moisture Capacitance: 245, Temperature: 11.40, Light: 32254 Soil Moisture Capacitance: 244, Temperature: 11.60, Light: 31949 Soil Moisture Capacitance: 243, Temperature: 11.80, Light: 31144 Soil Moisture Capacitance: 245, Temperature: 12.00, Light: 31249 Soil Moisture Capacitance: 244, Temperature: 12.20, Light: 31461 Soil Moisture Capacitance: 245, Temperature: 12.40, Light: 30896 Soil Moisture Capacitance: 244, Temperature: 12.50, Light: 30861 Soil Moisture Capacitance: 243, Temperature: 12.90, Light: 31110 Soil Moisture Capacitance: 244, Temperature: 13.00, Light: 32471 Soil Moisture Capacitance: 244, Temperature: 13.30, Light: 30134 Soil Moisture Capacitance: 244, Temperature: 13.50, Light: 29866 Soil Moisture Capacitance: 244, Temperature: 13.70, Light: 29531 Soil Moisture Capacitance: 243, Temperature: 13.80, Light: 30341 Soil Moisture Capacitance: 243, Temperature: 14.00, Light: 30090 Soil Moisture Capacitance: 244, Temperature: 14.10, Light: 43019 Soil Moisture Capacitance: 243, Temperature: 14.30, Light: 43913 Soil Moisture Capacitance: 241, Temperature: 14.60, Light: 44032 Soil Moisture Capacitance: 242, Temperature: 14.70, Light: 42442 Soil Moisture Capacitance: 241, Temperature: 14.80, Light: 43643 Soil Moisture Capacitance: 242, Temperature: 14.90, Light: 43372 Soil Moisture Capacitance: 242, Temperature: 15.00, Light: 43235 Soil Moisture Capacitance: 241, Temperature: 15.20, Light: 43515 Soil Moisture Capacitance: 242, Temperature: 15.30, Light: 43508 Soil Moisture Capacitance: 241, Temperature: 15.40, Light: 43604 Soil Moisture Capacitance: 241, Temperature: 15.50, Light: 43936 Soil Moisture Capacitance: 241, Temperature: 15.60, Light: 43199 Soil Moisture Capacitance: 241, Temperature: 15.70, Light: 42500 Soil Moisture Capacitance: 241, Temperature: 15.80, Light: 43058 Soil Moisture Capacitance: 241, Temperature: 15.90, Light: 41432 Soil Moisture Capacitance: 241, Temperature: 16.00, Light: 42455 Soil Moisture Capacitance: 241, Temperature: 16.00, Light: 42792 Soil Moisture Capacitance: 241, Temperature: 16.10, Light: 42958 Soil Moisture Capacitance: 241, Temperature: 16.20, Light: 42982 Soil Moisture Capacitance: 241, Temperature: 16.20, Light: 43183 Soil Moisture Capacitance: 240, Temperature: 16.30, Light: 42227 Soil Moisture Capacitance: 241, Temperature: 16.40, Light: 41986 Soil Moisture Capacitance: 241, Temperature: 16.50, Light: 42236 Soil Moisture Capacitance: 241, Temperature: 16.50, Light: 42164 Soil Moisture Capacitance: 241, Temperature: 16.60, Light: 41854 Soil Moisture Capacitance: 241, Temperature: 16.70, Light: 41599 Soil Moisture Capacitance: 241, Temperature: 16.70, Light: 41494 Soil Moisture Capacitance: 241, Temperature: 16.80, Light: 41367 Soil Moisture Capacitance: 241, Temperature: 16.80, Light: 40449 Soil Moisture Capacitance: 241, Temperature: 16.90, Light: 43227 Soil Moisture Capacitance: 240, Temperature: 16.90, Light: 40574 Soil Moisture Capacitance: 241, Temperature: 17.00, Light: 38462 Soil Moisture Capacitance: 241, Temperature: 16.90, Light: 39633 Soil Moisture Capacitance: 240, Temperature: 17.10, Light: 39510 Soil Moisture Capacitance: 240, Temperature: 17.20, Light: 40795 Soil Moisture Capacitance: 241, Temperature: 17.20, Light: 39975 Soil Moisture Capacitance: 241, Temperature: 17.30, Light: 39884 Soil Moisture Capacitance: 240, Temperature: 17.40, Light: 39632 Soil Moisture Capacitance: 240, Temperature: 17.50, Light: 38240 Soil Moisture Capacitance: 241, Temperature: 17.60, Light: 38284 Soil Moisture Capacitance: 240, Temperature: 17.60, Light: 38304 Soil Moisture Capacitance: 240, Temperature: 17.60, Light: 38158 Soil Moisture Capacitance: 240, Temperature: 17.70, Light: 40588 Soil Moisture Capacitance: 240, Temperature: 17.80, Light: 40763 Soil Moisture Capacitance: 240, Temperature: 17.90, Light: 40662 Soil Moisture Capacitance: 239, Temperature: 18.00, Light: 40908 Soil Moisture Capacitance: 239, Temperature: 18.00, Light: 40921 Soil Moisture Capacitance: 240, Temperature: 18.10, Light: 40740 Soil Moisture Capacitance: 239, Temperature: 18.20, Light: 40634 Soil Moisture Capacitance: 239, Temperature: 18.20, Light: 40424 Soil Moisture Capacitance: 239, Temperature: 18.30, Light: 40328 Soil Moisture Capacitance: 239, Temperature: 18.50, Light: 39957 Soil Moisture Capacitance: 239, Temperature: 18.60, Light: 39529 Soil Moisture Capacitance: 239, Temperature: 18.70, Light: 39543 Soil Moisture Capacitance: 239, Temperature: 18.80, Light: 39563 Soil Moisture Capacitance: 239, Temperature: 18.80, Light: 39571 Soil Moisture Capacitance: 239, Temperature: 18.90, Light: 39357 Soil Moisture Capacitance: 239, Temperature: 19.00, Light: 39202 Soil Moisture Capacitance: 239, Temperature: 19.10, Light: 39434 Soil Moisture Capacitance: 239, Temperature: 19.20, Light: 38983 Soil Moisture Capacitance: 239, Temperature: 19.20, Light: 38279 Soil Moisture Capacitance: 239, Temperature: 19.20, Light: 37948 Soil Moisture Capacitance: 239, Temperature: 19.40, Light: 37956 Soil Moisture Capacitance: 238, Temperature: 19.40, Light: 38438 Soil Moisture Capacitance: 238, Temperature: 19.50, Light: 38336 Soil Moisture Capacitance: 239, Temperature: 19.60, Light: 39197 Soil Moisture Capacitance: 239, Temperature: 19.70, Light: 39226 Soil Moisture Capacitance: 239, Temperature: 19.70, Light: 39099 Soil Moisture Capacitance: 238, Temperature: 19.80, Light: 38939 Soil Moisture Capacitance: 239, Temperature: 19.90, Light: 39050 Soil Moisture Capacitance: 238, Temperature: 19.90, Light: 39018 Soil Moisture Capacitance: 239, Temperature: 20.00, Light: 38854 Soil Moisture Capacitance: 239, Temperature: 20.00, Light: 38811 Soil Moisture Capacitance: 239, Temperature: 20.10, Light: 38782 Soil Moisture Capacitance: 239, Temperature: 20.20, Light: 38704 Soil Moisture Capacitance: 237, Temperature: 20.20, Light: 38454 Soil Moisture Capacitance: 239, Temperature: 20.20, Light: 38472 Soil Moisture Capacitance: 239, Temperature: 20.40, Light: 38044 Soil Moisture Capacitance: 239, Temperature: 20.40, Light: 37082 Soil Moisture Capacitance: 238, Temperature: 20.50, Light: 37150 Soil Moisture Capacitance: 238, Temperature: 20.50, Light: 37222 Soil Moisture Capacitance: 238, Temperature: 20.60, Light: 37089 Soil Moisture Capacitance: 238, Temperature: 20.70, Light: 37510 Soil Moisture Capacitance: 238, Temperature: 20.70, Light: 37667 Soil Moisture Capacitance: 239, Temperature: 20.80, Light: 37511 Soil Moisture Capacitance: 238, Temperature: 20.80, Light: 37349 Soil Moisture Capacitance: 237, Temperature: 20.90, Light: 37378 Soil Moisture Capacitance: 238, Temperature: 20.90, Light: 37240 Soil Moisture Capacitance: 238, Temperature: 20.80, Light: 36767 Soil Moisture Capacitance: 238, Temperature: 21.00, Light: 36551 Soil Moisture Capacitance: 239, Temperature: 21.10, Light: 36232 Soil Moisture Capacitance: 238, Temperature: 21.10, Light: 36672 Soil Moisture Capacitance: 239, Temperature: 21.20, Light: 36529 Soil Moisture Capacitance: 238, Temperature: 21.20, Light: 35903 Soil Moisture Capacitance: 238, Temperature: 21.20, Light: 36084 Soil Moisture Capacitance: 238, Temperature: 21.30, Light: 37124 Soil Moisture Capacitance: 238, Temperature: 21.30, Light: 37112 Soil Moisture Capacitance: 237, Temperature: 21.40, Light: 36191 Soil Moisture Capacitance: 237, Temperature: 21.40, Light: 36540 Soil Moisture Capacitance: 238, Temperature: 21.50, Light: 36324 Soil Moisture Capacitance: 238, Temperature: 21.50, Light: 35653 Soil Moisture Capacitance: 237, Temperature: 21.50, Light: 35304 Soil Moisture Capacitance: 238, Temperature: 21.60, Light: 35540 Soil Moisture Capacitance: 238, Temperature: 21.60, Light: 35644 Soil Moisture Capacitance: 237, Temperature: 21.60, Light: 34698 Soil Moisture Capacitance: 238, Temperature: 21.70, Light: 34436 Soil Moisture Capacitance: 237, Temperature: 21.70, Light: 34479 Soil Moisture Capacitance: 237, Temperature: 21.70, Light: 35180 Soil Moisture Capacitance: 238, Temperature: 21.80, Light: 37110 Soil Moisture Capacitance: 238, Temperature: 21.80, Light: 37160 Soil Moisture Capacitance: 237, Temperature: 21.80, Light: 37003 Soil Moisture Capacitance: 237, Temperature: 21.90, Light: 37267 Soil Moisture Capacitance: 238, Temperature: 21.90, Light: 37347 Soil Moisture Capacitance: 237, Temperature: 21.90, Light: 37342 Soil Moisture Capacitance: 238, Temperature: 22.00, Light: 37356 Soil Moisture Capacitance: 238, Temperature: 22.00, Light: 37447 Soil Moisture Capacitance: 237, Temperature: 22.00, Light: 37334 Soil Moisture Capacitance: 237, Temperature: 22.00, Light: 36942 Soil Moisture Capacitance: 237, Temperature: 22.20, Light: 36964 Soil Moisture Capacitance: 237, Temperature: 22.20, Light: 37280 Soil Moisture Capacitance: 237, Temperature: 22.20, Light: 36745 Soil Moisture Capacitance: 237, Temperature: 22.30, Light: 36608 Soil Moisture Capacitance: 238, Temperature: 22.30, Light: 37242 Soil Moisture Capacitance: 237, Temperature: 22.30, Light: 37187 Soil Moisture Capacitance: 237, Temperature: 22.40, Light: 37129 Soil Moisture Capacitance: 237, Temperature: 22.40, Light: 36813

You can also see that after moving it our of the fridge the light values start normalizing again ...

Any idea?! Except that the sensor do't work well <0 degree celsius :-)

Miceuz commented 8 years ago

Well, yes, light measurement is a hack that worked ok for room temperature operated devices ;) If you apply large temperature swings to the sensor, light measurement goes crazy as it depends on AVR pin leakage current and on parasitic capacitance in the LED.

Apollon77 commented 8 years ago

:-)) ok for me personally because I will not really use it, but should maybe be noted on the info-page ...

sekdiy commented 8 years ago

Shouldn't the light counter top out at 65534 (2^16 - 1) instead of 65535 (2^16) in main.c, line 129 (https://github.com/Miceuz/i2c-moisture-sensor/blob/master/src/main.c#L129)?

I'd recommend to #include <limits.h> and then use the UINT_MAX constant.

I've noticed this possibility after experiencing a discontinuity in my readings (the jump around 9 pm in the attached image):

screen shot 2016-06-04 at 01 16 07

krikk commented 8 years ago

i have a few sensors in testing now and all i can say is, that the light reading is a bit jumpy if it comes to low light conditions ... have a look on my graphs for 5 i2c-moisture-sensors and one BH1750 light sensor: image ...i first thought resetting the sensor before each reading will get better results, but this is not the case... we all know the light reading is a "hack" so i can live with it...

cheeeeee commented 5 years ago

I know this is an old post, but I figured I would throw this here if anyone else was looking at the light sensor on the I2C moisture monitor in arduino. ` void I2CSensorRead() { while (soilMoistureSensor.isBusy()) delay(50); // available since FW 2.3 soilMoisture = soilMoistureSensor.getCapacitance(); soilTemperature = soilMoistureSensor.getTemperature() / (float) 10; soilTemperatureF = soilTemperature * 1.8 + 32; lightValue = soilMoistureSensor.getLight(true); // experiment = soilMoistureSensor.lightSmooth(); experimenttwo = toLux(lightValue); soilMoistureSensor.sleep(); // available since FW 2.3 }

float toLux(int raw) {

unsigned int rawRange = 6553;
float logRange = 5;
float nowLux;

double logLux = (logRange * raw) / rawRange;

nowLux = pow(0.5, logLux);

return pow(2.7475, (nowLux * 10));

}`

This is basically an inverse function based off of https://learn.adafruit.com/adafruit-ga1a12s202-log-scale-analog-light-sensor/use-it that will make lower light yield lower numbers and higher light nigher numbers. You might need to tweak this if you use it by adjusting the base in

return pow(2.7475, (nowLux * 10));

Power base of 2.7475 seems to be the sweet spot for me but you can go about as low as 2.7 and as high as 2.8. Increasing this too much causes an overflow.

I'm sort of using a non standard library. At some point if I can manage to refine it I might post it in a public repository. In the mean time the .cpp and .h files can be found here https://pastebin.com/4zuGdHtL and here https://pastebin.com/cHap3DQg. Maybe someone that's better at libraries can get that functioning better. It's worth noting that since there's a delay of 3000ms every time a light reading takes place, if you average 5 readings you multiply the delay by 5. This means it takes at least 5 times as long to report a reading back. If I understand all this correctly, the maker is basically using the LED on D3 as a photo-resistor which explains why the readings are backwards because of the circuit polarity (i think????).

Miceuz commented 5 years ago

To get the idea how does the light sensing work, please look for "led as light sensor" posts on hackaday and whatnot. The basic idea being that parasitic capacitance is charged when reverse-biasing it. Then this capacitance discharges in two ways - via pin leakage and via photo electric effect - more light shines into the led, faster it discharges. By timing this discharge event one can get an estimate of light level there is.

The method is very cheap, but very inaccurate - readings will depend wildly on ambient temperature and from sensor to sensor. Thus it is just suitable for rough estimate is it dark or not.