How can I turn Moisture Sensor analog into % Percentage

Hi Everyone,
I am looking for some help on a small project I’m working on with the photon. I’m using a moisture sensor from sparkfun, and a oled screen. I’d like to have the output on the oled screen be a “Moisture Content %”.

At this time I am just pulling the A0 reading, but not sure how to convert that into a MC% reading.

// This #include statement was automatically added by the Particle IDE.
#include "SparkFunMicroOLED/SparkFunMicroOLED.h"

  SparkFun Micro OLED Library Hello World Example
  Jim Lindblom @ SparkFun Electronics
  Original Creation Date: June 22, 2015

  This sketch prints a friendly, recognizable logo on the OLED Shield, then
  goes on to demo the Micro OLED library's functionality drawing pixels,
  lines, shapes, and text.

  Hardware Connections:
	This sketch was written specifically for the Photon Micro OLED Shield,
	which does all the wiring for you. If you have a Micro OLED breakout,
	use the following hardware setup:

    MicroOLED ------------- Photon
      GND ------------------- GND
      VDD ------------------- 3.3V (VCC)
    D1/MOSI ----------------- A5 (don't change)
    D0/SCK ------------------ A3 (don't change)
      D/C ------------------- D6 (can be any digital pin)
      RST ------------------- D7 (can be any digital pin)
      CS  ------------------- A2 (can be any digital pin)

  Development environment specifics:
  	IDE: Particle Build
  	Hardware Platform: Particle Photon
                       SparkFun Photon Micro OLED Shield

  This code is beerware; if you see me (or any other SparkFun
  employee) at the local, and you've found our code helpful,
  please buy us a round!

  Distributed as-is; no warranty is given.
#include "math.h"

// MicroOLED Object Declaration //
// Declare a MicroOLED object. If no parameters are supplied, default pins are
// used, which will work for the Photon Micro OLED Shield (RST=D7, DC=D6, CS=A2)
MicroOLED oled;
//MicroOLED oled(MODE_I2C, D7, 0);    // Example I2C declaration RST=D7, DC=LOW (0)


void setup()
  oled.begin();    // Initialize the OLED
  oled.clear(ALL); // Clear the display's internal memory
  oled.display();  // Display what's in the buffer (splashscreen)
  delay(1000);     // Delay 1000 ms
  randomSeed(analogRead(A0) + analogRead(A1));

void loop()
  readmoisture();  // read and dispaly moisture

void readmoisture()

    oled.clear(PAGE);            // Clear the display
    oled.setCursor(0, 0);        // Set cursor to top-left
    oled.setFontType(1);         // Smallest font
    oled.print("M%:");          // Print "Moisture"
    oled.setFontType(1);         // 7-segment font
    oled.print(analogRead(A0));  // Print a0 reading

@cwrisley, which moisture sensor are you using specifically?

Hi @cwrisley,

The A0 reading should be a value between 0 and 4095, which represents the voltage on the pin between 0v and 3.3v. Your moisture sensor probably changes its resistance based on the moisture content, but probably needs some calibration. If it were perfectly linear the conversion might be something like:

// scale any value from 0-4095 back into the 0-100 range
map(value, 0, 4095, 0, 100);

But yeah, @peekay123 is right, knowing the particular sensor is important, chances are it’s not perfectly linear, or might have stepped values, or even it might be digital, etc, etc. :slight_smile:



Thanks for the quick replies, it’s the sensor available here:

@cwrisley, the sensor puts out a value from GND to Vcc depending on the water level. It is recommended that you power the probe’s Vcc using a GPIO pin on the processor (digital output). This will provide 3.3v to the probe. Given this, the analog output of the probe will be 0-3.3v or 0-4095 from the ADC conversion. If a value of 0 = 0% humidity and 4095 is 100%, then as @Dave said, using map(value, 0, 4095, 0, 100); will convert the ADC value to a 0-100% humidity value. :smile:

1 Like

Thanks! Worked great

1 Like