Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Soil Sensor analog values differ on Uno vs Pro Mini

Soil Sensor analog values differ on Uno vs Pro Mini

Scheduled Pinned Locked Moved Troubleshooting
soil sensorunopro mini
28 Posts 8 Posters 14.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Moshe LivneM Offline
    Moshe LivneM Offline
    Moshe Livne
    Hero Member
    wrote on last edited by
    #12

    Managed to make it a humidity sensor, which is probably close enough. Here is my sketch:
    #include <SPI.h>
    #include <MySensor.h>

    #define ANALOG_INPUT_SOIL_SENSOR A0
    #define CHILD_ID 0 // Id of the sensor child

    MySensor gw;
    MyMessage msg(CHILD_ID, V_HUM);
    int lastSoilValue = -1;

    void setup()
    {
    gw.begin();

    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
    // sets the soil sensor analog pin as input
    pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
    // Register all sensors to gw (they will be created as child devices)
    gw.present(CHILD_ID, S_HUM);
    }

    void loop()
    {
    // Read analog soil value
    int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);

    if (soilValue != lastSoilValue) {
    Serial.println(soilValue);
    gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100)); // Send the relative moisture. this will probably work only on nano or uno as I used 1023
    lastSoilValue = soilValue;
    }
    // Power down the radio and arduino until analog input changes.
    gw.sleep(1000);
    }

    F 1 Reply Last reply
    0
    • Moshe LivneM Offline
      Moshe LivneM Offline
      Moshe Livne
      Hero Member
      wrote on last edited by
      #13

      Oh, sleep should probably be set to 3600000 or more to get long sleep periods between measurements as I don't think soil moisture should be measured every second....

      1 Reply Last reply
      0
      • Moshe LivneM Moshe Livne

        Managed to make it a humidity sensor, which is probably close enough. Here is my sketch:
        #include <SPI.h>
        #include <MySensor.h>

        #define ANALOG_INPUT_SOIL_SENSOR A0
        #define CHILD_ID 0 // Id of the sensor child

        MySensor gw;
        MyMessage msg(CHILD_ID, V_HUM);
        int lastSoilValue = -1;

        void setup()
        {
        gw.begin();

        // Send the sketch version information to the gateway and Controller
        gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
        // sets the soil sensor analog pin as input
        pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
        // Register all sensors to gw (they will be created as child devices)
        gw.present(CHILD_ID, S_HUM);
        }

        void loop()
        {
        // Read analog soil value
        int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);

        if (soilValue != lastSoilValue) {
        Serial.println(soilValue);
        gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100)); // Send the relative moisture. this will probably work only on nano or uno as I used 1023
        lastSoilValue = soilValue;
        }
        // Power down the radio and arduino until analog input changes.
        gw.sleep(1000);
        }

        F Offline
        F Offline
        fred97
        wrote on last edited by
        #14

        @Moshe-Livne said:

        Managed to make it a humidity sensor, which is probably close enough. Here is my sketch:
        #include <SPI.h>
        #include <MySensor.h>

        #define ANALOG_INPUT_SOIL_SENSOR A0
        #define CHILD_ID 0 // Id of the sensor child

        MySensor gw;
        MyMessage msg(CHILD_ID, V_HUM);
        int lastSoilValue = -1;

        void setup()
        {
        gw.begin();

        // Send the sketch version information to the gateway and Controller
        gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
        // sets the soil sensor analog pin as input
        pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
        // Register all sensors to gw (they will be created as child devices)
        gw.present(CHILD_ID, S_HUM);
        }

        void loop()
        {
        // Read analog soil value
        int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);

        if (soilValue != lastSoilValue) {
        Serial.println(soilValue);
        gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100)); // Send the relative moisture. this will probably work only on nano or uno as I used 1023
        lastSoilValue = soilValue;
        }
        // Power down the radio and arduino until analog input changes.
        gw.sleep(1000);
        }

        hello ,

        i want to do same , but can you explaind the calcul is made ?

        for now i use light sensor sketch with analog input , but i ant to modify by /10 for having a moisture meter reading value 0to100% or maybe there is a S_Percentage come in next version of mysensors ?

        i use domotiz with gateway and for now only Light sensor work for read analog value .

        thanks

        Moshe LivneM 1 Reply Last reply
        0
        • F fred97

          @Moshe-Livne said:

          Managed to make it a humidity sensor, which is probably close enough. Here is my sketch:
          #include <SPI.h>
          #include <MySensor.h>

          #define ANALOG_INPUT_SOIL_SENSOR A0
          #define CHILD_ID 0 // Id of the sensor child

          MySensor gw;
          MyMessage msg(CHILD_ID, V_HUM);
          int lastSoilValue = -1;

          void setup()
          {
          gw.begin();

          // Send the sketch version information to the gateway and Controller
          gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
          // sets the soil sensor analog pin as input
          pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
          // Register all sensors to gw (they will be created as child devices)
          gw.present(CHILD_ID, S_HUM);
          }

          void loop()
          {
          // Read analog soil value
          int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);

          if (soilValue != lastSoilValue) {
          Serial.println(soilValue);
          gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100)); // Send the relative moisture. this will probably work only on nano or uno as I used 1023
          lastSoilValue = soilValue;
          }
          // Power down the radio and arduino until analog input changes.
          gw.sleep(1000);
          }

          hello ,

          i want to do same , but can you explaind the calcul is made ?

          for now i use light sensor sketch with analog input , but i ant to modify by /10 for having a moisture meter reading value 0to100% or maybe there is a S_Percentage come in next version of mysensors ?

          i use domotiz with gateway and for now only Light sensor work for read analog value .

          thanks

          Moshe LivneM Offline
          Moshe LivneM Offline
          Moshe Livne
          Hero Member
          wrote on last edited by
          #15

          @fred97 , i also use domoticz. If you read this thread form the start, you will see that the arduino converts the moisture reading to something in the range 0-1023 (or 682 if you use mini pro). 1023 is 0 moisture, 0 is 100% moisture. so, moisture content is (1023 - reading)/1023*100.

          Hope this answers your question!

          Moshe

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fred97
            wrote on last edited by
            #16

            thanks you ,

            i have try your sketch but don't work send only O

            send: 2-2-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0
            444
            send: 2-2-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0
            470
            send: 2-2-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0
            1023
            send: 2-2-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0
            

            i think the problem is inside converter in percentage ....

            240 when is in water and 1023 when completly dry .

            1 Reply Last reply
            0
            • Moshe LivneM Offline
              Moshe LivneM Offline
              Moshe Livne
              Hero Member
              wrote on last edited by Moshe Livne
              #17

              @fred97 did you change anything in the calculation? the values in the println seems correct. do not remove the static casts - they are essential.

              BulldogLowellB 1 Reply Last reply
              0
              • Moshe LivneM Moshe Livne

                @fred97 did you change anything in the calculation? the values in the println seems correct. do not remove the static casts - they are essential.

                BulldogLowellB Offline
                BulldogLowellB Offline
                BulldogLowell
                Contest Winner
                wrote on last edited by
                #18

                @Moshe-Livne said:

                Serial.println(soilValue);
                gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100))

                how about just using the map() function...

                Serial.println(soilValue);
                gw.send(msg.set(map(soilValue, 0, 1023, 0 ,100));
                
                Moshe LivneM 1 Reply Last reply
                0
                • BulldogLowellB BulldogLowell

                  @Moshe-Livne said:

                  Serial.println(soilValue);
                  gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100))

                  how about just using the map() function...

                  Serial.println(soilValue);
                  gw.send(msg.set(map(soilValue, 0, 1023, 0 ,100));
                  
                  Moshe LivneM Offline
                  Moshe LivneM Offline
                  Moshe Livne
                  Hero Member
                  wrote on last edited by
                  #19

                  @BulldogLowell would have used it if I knew it existed.... would it work is reverse? (1023,0, 0, 100)?

                  BulldogLowellB 1 Reply Last reply
                  0
                  • Moshe LivneM Moshe Livne

                    @BulldogLowell would have used it if I knew it existed.... would it work is reverse? (1023,0, 0, 100)?

                    BulldogLowellB Offline
                    BulldogLowellB Offline
                    BulldogLowell
                    Contest Winner
                    wrote on last edited by BulldogLowell
                    #20

                    @Moshe-Livne

                    Try it!!

                    int soilValue = 250;
                    
                    void setup() 
                    {
                      Serial.begin(115200);
                      Serial.println(map(soilValue, 1023,0,100,0));
                    }
                    
                    void loop() 
                    {
                      
                    }
                    

                    have you run into 123dcircuits yet?

                    Screen Shot 2015-05-24 at 6.26.37 AM.png

                    Moshe LivneM 1 Reply Last reply
                    0
                    • BulldogLowellB BulldogLowell

                      @Moshe-Livne

                      Try it!!

                      int soilValue = 250;
                      
                      void setup() 
                      {
                        Serial.begin(115200);
                        Serial.println(map(soilValue, 1023,0,100,0));
                      }
                      
                      void loop() 
                      {
                        
                      }
                      

                      have you run into 123dcircuits yet?

                      Screen Shot 2015-05-24 at 6.26.37 AM.png

                      Moshe LivneM Offline
                      Moshe LivneM Offline
                      Moshe Livne
                      Hero Member
                      wrote on last edited by
                      #21

                      @BulldogLowell not yet!!! my first encounter with arduino was only a few weeks ago.... so much to learn! the wood are lovely dark and deep, but i have promises to keep....

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        fred97
                        wrote on last edited by
                        #22

                        hello

                        it's working but is not inverted :+1:

                        314
                        send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:30
                        294
                        send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:28
                        287
                        send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:28
                        299
                        send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:29
                        313
                        send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:30
                        1023
                        send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:100
                        

                        100 is when is dry and 220/250 is when the sensor is in water .
                        i just need little more help for invert value , 100 = in water and 0 when is dry

                        thx

                        BulldogLowellB 1 Reply Last reply
                        0
                        • F fred97

                          hello

                          it's working but is not inverted :+1:

                          314
                          send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:30
                          294
                          send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:28
                          287
                          send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:28
                          299
                          send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:29
                          313
                          send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:30
                          1023
                          send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:100
                          

                          100 is when is dry and 220/250 is when the sensor is in water .
                          i just need little more help for invert value , 100 = in water and 0 when is dry

                          thx

                          BulldogLowellB Offline
                          BulldogLowellB Offline
                          BulldogLowell
                          Contest Winner
                          wrote on last edited by BulldogLowell
                          #23

                          @fred97

                          @BulldogLowell said:

                          gw.send(msg.set(map(soilValue, 0, 1023, 0 ,100));

                          like this:

                          gw.send(msg.set(map(soilValue, 100, 250, 0, 100));
                          

                          it maps 100 to zero and 250 to 100

                          Let me know if that works for you.

                          or... you can try this to bound the returned value by zero and 100:

                          gw.send((max(min(map(soilValue, 100,250,0,100), 100), 0)));
                          

                          confusing but is is mapping the value, and limiting to 100 and then making sure it is at least zero (i.e. non-negative).

                          or just:

                          gw.send(constrain(map(soilValue, 100,250,0,100), 0, 100));
                          
                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            fred97
                            wrote on last edited by
                            #24
                            gw.send(msg.set(map(soilValue, 100, 250, 0, 100)));```
                            
                            

                            you have forget one ) at last .

                            sensor started, id 2
                            send: 2-2-0-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1
                            send: 2-2-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
                            read: 0-0-2 s=255,c=3,t=6,pt=0,l=1:M
                            send: 2-2-0-0 s=255,c=3,t=11,pt=0,l=25,st=ok:Soil Moisture Sensor - an
                            send: 2-2-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                            send: 2-2-0-0 s=0,c=0,t=7,pt=0,l=5,st=ok:1.4.1
                            1023
                            send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:615
                            285
                            send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:123
                            

                            250 is when is in water and 1023 is dry , it's alway inverted , i can test 2 other exemple because there is a problem in sketch maybe ;or ) when is try this

                            gw.send((max(min(map(soilValue, 100,250,0,100), 100), 0)));
                            

                            and for other 2 exemple i can send to arduino compiling error

                            sketch_may24a.ino: In function 'void loop()':
                            sketch_may24a:31: error: no matching function for call to 'MySensor::send(long int)'
                            C:\Program Files (x86)\Arduino\libraries\MySensors/MySensor.h:142: note: candidates are: bool MySensor::send(MyMessage&, bool)
                            
                            BulldogLowellB 1 Reply Last reply
                            0
                            • F fred97
                              gw.send(msg.set(map(soilValue, 100, 250, 0, 100)));```
                              
                              

                              you have forget one ) at last .

                              sensor started, id 2
                              send: 2-2-0-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1
                              send: 2-2-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
                              read: 0-0-2 s=255,c=3,t=6,pt=0,l=1:M
                              send: 2-2-0-0 s=255,c=3,t=11,pt=0,l=25,st=ok:Soil Moisture Sensor - an
                              send: 2-2-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                              send: 2-2-0-0 s=0,c=0,t=7,pt=0,l=5,st=ok:1.4.1
                              1023
                              send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:615
                              285
                              send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:123
                              

                              250 is when is in water and 1023 is dry , it's alway inverted , i can test 2 other exemple because there is a problem in sketch maybe ;or ) when is try this

                              gw.send((max(min(map(soilValue, 100,250,0,100), 100), 0)));
                              

                              and for other 2 exemple i can send to arduino compiling error

                              sketch_may24a.ino: In function 'void loop()':
                              sketch_may24a:31: error: no matching function for call to 'MySensor::send(long int)'
                              C:\Program Files (x86)\Arduino\libraries\MySensors/MySensor.h:142: note: candidates are: bool MySensor::send(MyMessage&, bool)
                              
                              BulldogLowellB Offline
                              BulldogLowellB Offline
                              BulldogLowell
                              Contest Winner
                              wrote on last edited by
                              #25

                              @fred97

                              Post your entire sketch.

                              1 Reply Last reply
                              0
                              • F Offline
                                F Offline
                                fred97
                                wrote on last edited by
                                #26
                                #include <SPI.h>
                                #include <MySensor.h>
                                
                                #define ANALOG_INPUT_SOIL_SENSOR A0
                                #define CHILD_ID 0 // Id of the sensor child
                                
                                MySensor gw;
                                MyMessage msg(CHILD_ID, V_HUM);
                                int lastSoilValue = -1;
                                
                                void setup()
                                {
                                gw.begin();
                                
                                // Send the sketch version information to the gateway and Controller
                                gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
                                // sets the soil sensor analog pin as input
                                pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
                                // Register all sensors to gw (they will be created as child devices)
                                gw.present(CHILD_ID, S_HUM);
                                }
                                
                                void loop()
                                {
                                // Read analog soil value
                                int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);
                                
                                if (soilValue != lastSoilValue) {
                                Serial.println(soilValue);
                                gw.send(msg.set(map(soilValue, 100, 250, 0, 100)));
                                lastSoilValue = soilValue;
                                }
                                // Power down the radio and arduino until analog input changes.
                                gw.sleep(1000);
                                }
                                

                                is the same sketch but i have changed only

                                gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100)); // Send the relative moisture. this will probably work only on nano or uno as I used 1023
                                

                                by this

                                gw.send(msg.set(map(soilValue, 100, 250, 0, 100)));
                                

                                i don't understand why is alway inverted ??

                                BulldogLowellB 1 Reply Last reply
                                0
                                • F fred97
                                  #include <SPI.h>
                                  #include <MySensor.h>
                                  
                                  #define ANALOG_INPUT_SOIL_SENSOR A0
                                  #define CHILD_ID 0 // Id of the sensor child
                                  
                                  MySensor gw;
                                  MyMessage msg(CHILD_ID, V_HUM);
                                  int lastSoilValue = -1;
                                  
                                  void setup()
                                  {
                                  gw.begin();
                                  
                                  // Send the sketch version information to the gateway and Controller
                                  gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
                                  // sets the soil sensor analog pin as input
                                  pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
                                  // Register all sensors to gw (they will be created as child devices)
                                  gw.present(CHILD_ID, S_HUM);
                                  }
                                  
                                  void loop()
                                  {
                                  // Read analog soil value
                                  int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);
                                  
                                  if (soilValue != lastSoilValue) {
                                  Serial.println(soilValue);
                                  gw.send(msg.set(map(soilValue, 100, 250, 0, 100)));
                                  lastSoilValue = soilValue;
                                  }
                                  // Power down the radio and arduino until analog input changes.
                                  gw.sleep(1000);
                                  }
                                  

                                  is the same sketch but i have changed only

                                  gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100)); // Send the relative moisture. this will probably work only on nano or uno as I used 1023
                                  

                                  by this

                                  gw.send(msg.set(map(soilValue, 100, 250, 0, 100)));
                                  

                                  i don't understand why is alway inverted ??

                                  BulldogLowellB Offline
                                  BulldogLowellB Offline
                                  BulldogLowell
                                  Contest Winner
                                  wrote on last edited by
                                  #27

                                  @fred97 said:

                                  i don't understand why is alway inverted ??

                                  try to use this:

                                  gw.send(msg.set(constrain(map(soilValue, 1023, 250, 0, 100),0,100)));
                                  

                                  it maps 1023 to zero and 250 to 100... that is what you want, yes?

                                  #include <SPI.h>
                                  #include <MySensor.h>
                                  
                                  #define ANALOG_INPUT_SOIL_SENSOR A0
                                  #define CHILD_ID 0 // Id of the sensor child
                                  
                                  MySensor gw;
                                  MyMessage msg(CHILD_ID, V_HUM);
                                  int lastSoilValue = -1;
                                  
                                  void setup()
                                  {
                                    gw.begin();
                                  
                                    // Send the sketch version information to the gateway and Controller
                                    gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
                                    // sets the soil sensor analog pin as input
                                    pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
                                    // Register all sensors to gw (they will be created as child devices)
                                    gw.present(CHILD_ID, S_HUM);
                                  }
                                  
                                  void loop()
                                  {
                                    // Read analog soil value
                                    int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);
                                  
                                    if (soilValue != lastSoilValue) {
                                      Serial.println(soilValue);
                                      gw.send(msg.set(constrain(map(soilValue, 1023, 250, 0, 100),0,100)));
                                      lastSoilValue = soilValue;
                                    }
                                    // Power down the radio and arduino until analog input changes.
                                    gw.sleep(1000);
                                  }
                                  
                                  1 Reply Last reply
                                  1
                                  • F Offline
                                    F Offline
                                    fred97
                                    wrote on last edited by
                                    #28

                                    thanks you very much work great !

                                    1 Reply Last reply
                                    1
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    37

                                    Online

                                    11.7k

                                    Users

                                    11.2k

                                    Topics

                                    113.1k

                                    Posts


                                    Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • MySensors
                                    • OpenHardware.io
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular