pixl.js with analog moister sensor

Posted on
of 2
/ 2
  • Hello!
    I have a analog moister sensor, how do I make it work? How can I read the proper values from pin A0. I have a CR2032 battery. And i think the sensor can run on 3,3V

    function onTimer() {
      // Get the soil moister as a string
      var h = analogRead(A0);
      // Clear display
      // Use the small font for a title
      g.drawString("Soil moister:");
      // Use a large font for the value itself
      g.drawString(h, (g.getWidth()-g.stringWidth(h))/2,10);
      // Update the screen
    // Update soil moister every 2 seconds
    // Update soil moister immediately

    2 Attachments

    • Bild 2018-05-29 kl. 22.08.jpg
    • Bild 2018-05-29 kl. 22.10.jpg
  • Hi! I think your wiring might be a bit off - the power wires seem to be connected to 3.3v and RST. See: http://www.espruino.com/Pixl.js#pinout

    Basically you want to move the blue wire 3 pins downwards to connect it to GND, and that will probably help a lot.

  • Diffrent numbers but thet change i sequence. Thanks for the sudgestion

    1 Attachment

    • 7FBFB674-9DC2-4B62-8DB9-98577A2CA547.jpeg
  • I think IM doing something wrong any ideas? Does anyone here have an idea how to extract data from it? Is it even analog? The 3 wires att sig, vcc and gnd.

  • I don't really know - do you get an obvious digital output out of it if you stick it in water?

  • i have something similar from a grooove sensor kit. i can try it this weekend, or at least give the arduino code. i know i just did an analog read off of a0. the values ranged from 0 - 800 or so. using it for plant soil moisture, i had values between 300 and 650 as acceptable moisture.

  • Thank you Im looking forward to your findings or your code :)

  • No it just cycles between some values not dependant on the environment

  • There is big chances the moisture sensor is analog, like the one from Adafruit (https://www.sparkfun.com/products/13322).

    Then, the code is quite easy:
    Just use the analogRead(PIN) function with the correct pin name and there it is. Then you can add some threshold or whatever you want depending on your application

  • Thank you, so My code above should do the trick?

  • It should. If not, could you please share the sensor model? (reference, name, manufacturer or datasheet)

  • here's the arduino code i'll try to convert to a pixl when i get home this weekend. it reads from a moisture sensor, and a temperature/humidity sensor, and displays the values to an rgb display. unfortunately i won't be able to change the display color based on the moisture level, but it will be a good test. in my case i'm using an arduino shield with a grove sensor kit, so it will be a good test on the pixl as a shield as well.

    [#include](https://forum.espruino.com/se­arch/?q=%23include) "Arduino.h"
    [#include](https://forum.espruino.com/se­arch/?q=%23include) "Wire.h" 
    [#include](https://forum.espruino.com/se­arch/?q=%23include) <rgb_lcd.h>
    [#include](https://forum.espruino.com/se­arch/?q=%23include) <TH02_dev.h>
    [#include](https://forum.espruino.com/se­arch/?q=%23include) <stdio.h>
    [#include](https://forum.espruino.com/se­arch/?q=%23include) <math.h>
    [#include](https://forum.espruino.com/se­arch/?q=%23include) <TimeLib.h>
    [#include](https://forum.espruino.com/se­arch/?q=%23include) <TimeAlarms.h>
    rgb_lcd lcd;
    const int button = 2;       // connect a button
    const int buzzer = 3;       // connect a buzzer
    // Test code for Grove - Moisture Sensor 
    int sensorPin = A0; // select the input pin for the potentiometer
    int moisture = -1; // variable to store the value coming from the sensor7
    int prec = 0;
    int buttonPress = 0;
    int displayOn = 1;
    void setup() {
        // declare the ledPin as an OUTPUT:
        pinMode(button, INPUT); //set button as an INPUT device
        pinMode(buzzer, OUTPUT);   //set LED as an OUTPUT device
        // set up the LCD's number of columns and rows:
        lcd.begin(16, 2);
        // Set the LCD background color
        Serial.println("****TH02_dev demo by seeed studio****\n");
        /* Power up,delay 150ms,until voltage is stable */
        /* Reset HP20x_dev */
        /* Determine TH02_dev is available or not */
        Serial.println("TH02_dev is available.\n");   
        Alarm.timerRepeat(5, readSensors);
        Alarm.timerRepeat(60, displayOff);
    void loop() {
        buttonPress = digitalRead(button); //read the status of the button
        if( buttonPress && !displayOn ) {
          displayOn = 1;
        //digitalWrite(buzzer, btn);
    void readSensors() {
      Serial.println("Reading sensors");
      // read the value from the sensor:
      moisture = analogRead(sensorPin);
      Serial.print("Moisture = " );
      float temper = TH02.ReadTemperature(); 
      Serial.println("Temperature: ");   
      float humidity = TH02.ReadHumidity();
      Serial.println("Humidity: ");
      char message1[16];
      char message2[16];
      float ftemp = 1.8*temper + 32;
      long t = lround(ftemp);
      int tm = int((ftemp-floor(ftemp))*100)%100;
      long h = lround(humidity);
      int hm = int((humidity-floor(humidity))*100)%100;­
      long m = moisture;
      sprintf(message1, "%ld.%dF, %ld.%dRH", t, tm, h, hm);
      sprintf(message2, "Moisture: %3ld", m);
    void displayOff() {
      if( buttonPress ) {
        buttonPress = 0;
      else {
        displayOn = 0;
    void setColor() {
      if( displayOn ) {
        if( moisture < 0 ) {
        else if( moisture < 300 ) {
        else if( moisture < 650 ) {
        else {
  • I suspect that the kind of sensor that J{a}SON is using is this one: http://wiki.seeedstudio.com/Grove-Temper­ature_and_Humidity_Sensor_Pro/
    If true, It is probably discussing through I2C, which cannot be the case of the sensor of @furuskog, which has only 3 pins (I2C would have at least 4 pins)

  • the moisture sensor is only using 3 of the pins: vcc, gnd, sig. it's this one

  • Well in this case @furuskog probably owns some kind of clone of this moisture sensor, it should work fine with a simple analogRead(PIN). If not working, possible issues could be

    1. the sensor is not correctly wired up
    2. the PIN is not the one which is connected to the sensor
    3. the soil humidity is way too low and the sensor is continuously reading the minimal value
    4. the transistor on the moisture sensor is dead
  • i'm going to convert my shield arduino code to pixl anyway, just as an exercise. it was accurate on the arduino as a setup for my indoor plant starters, but it didn't have a sleep mode, and drained the batteries too quickly. i look forward to seeing how energy efficient the pixl can be with this setup.

  • Nice, code :)

    1. 🤔
    2. A0
    3. Tested in a glas of water
    4. I have 4 sensors tested with 2 so far :)
  • Well, can you try to call analogRead(A0) with the sensor unplugged? The pin should pick-up random values and you'll be able to see them change in the console

  • yes I have done that it cycles through.

  • Are you powering the sensor with 3.3 or 5.0 V?

  • hmm 3.3v seems to work now.

    in water
    h = 0.45629882812
    h = 0.45678710937
    h = 0.45971679687
    h = 0.46044921875
    h = 0.46020507812
    h = 0.4619140625
    h = 0.4619140625
    h = 0.462890625
    h = 0.46362304687

    in air
    h = 0.00024414062
    h = 0.00048828125
    h = 0.0009765625
    h = -0.00024414062
    h = 0.00024414062

    Now i just have to calculate it in %, an have som treshold for 0% because of the fluxuation, maybe an upper limit too?

    I will check the code you guys provided :)

  • I came up with this

    var high = 0.55;
    var low = 0.0017;
    function map_value(val, in_min, in_max, out_min, out_max){
      var result = (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
      if(result < out_min) {
        return out_min;  
      else if(result > out_max) {
        return out_max;
      return result;
    function onTimer() {
      var h = analogRead(A0);
      var hp = map_value(h, low, high, 0, 100);
      var percentage = hp.toFixed(0)+"%";
      // Clear display
      // Use the small font for a title
      g.drawString("Soil moister:");
      // Use a large font for the value itself
      g.drawString(percentage, 5,10);
      // Update the screen
    // Update soil moister every 2 seconds
    setInterval(onTimer, 2000);
    // Update soil moister immediately

    Feedback on the code is welcome

    I have 3 pucks im gooing to try next :) Im gooing to send via bluetooth to my pixl.js

    Strange the high and low changes with battery vs plug

  • Strange the high and low changes with battery vs plug

    Yes that's is probably the downside of this sensor. Due to its circuit, the sensor output may be a function of VCC. You could try to power it through an LDO (low dropout regulator).

    Or you can also monitor its voltage through a resistor divider an compensate digitally ?

    Anyway, good to know that you finally got it work!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

pixl.js with analog moister sensor

Posted by Avatar for furuskog @furuskog