Fab Academy 9

Input Devices

Soil Moisture Sensor


For this project I first tried to program a Soil Moisture Sensor (the one with two sticks) I bought to check the moisture of my plants soil by capacitive conductivity, which means that the amount of water between the two sticks generates certain conducticity which can be measured and then turned into an action.

The kit also comes with another water humidity sensor, that measures the amount of rain or reacts to when the rain falls. You can also use this one to measure the amount of wather inside a bowl, since it also reacts to conductivity, so the more water it has around, the more conductivity it has and therefore, more signal it sends. I dind´t used this one in my experiments anyway.

This is the tutorial I started from.

ESP8266 12-E Chip Pinout

Serial Read:

The first thing I did was to do a serial read to understand the values that it was giving me.

That showed me that the values ocillated from 0 to 430 more or less. 0 when it had no current between the two sticks (no conductivity), 400-430 when it was inside water or fully moistured soil, but there was another value when the soil was almost dry (similar to fingers conductivity) which was between 50 and 100.


ESP8266 12-E Chip Pinout

Building the Code

Since the values were around 400, I diveded them by 4, to turn them into an average percentage, for the Serial.printIn.

I wanted to check 3 values (Dry, Almost Dry, Wet), so I added 3 different color leds to my system (Red, Yellow, Green) with 220 ohms resistors.

I started tweaking the threshold code to set it up, so that each led lighted up when it was between a different threshold value.

I declared the main threshold to be 100, and decided that:

If the DATA was >= than (THRESHOLD + 200), the GREEN LED turns ON.

If the DATA was <= than THRESHOLD, the RED Led turns ON.

If the DATA was > than THRESHOLD and < than the (THRESHOLD + 200), the YELLOW LED turn ON.

And I added a delay to loop this process each 500 milliseconds.



I was having some troubles at he begining because I didn´t know how to have two conditions togheter like in the case of the YELLOW LED, so in some moment two leds where turning on at the same time, but I solved it with an AND code:

        else if (data >= (threshold) and (data < threshold + 200)) 

I was also having troubles with diferenciating the conditions of the three leds, because I was only usinf IF code, but I solved it by adding somo ELSE IF.

PCB Board Connection

(BOM) Bill Of Materials

Home Made
Moisture Sensor


After trying with a comercial soil sensor, I wanted to try the tutorial for creating my own soil sensor. In this one I basically followed the instructions to build it, and then copied the code.

For this sensor, different to the one I previously did, the main difference is that they create the sensor with two bolts attached to wires. This works practicaly the same as commercial ones, bi generating a signal based on the conductivity between the two bolts generated by the water that moistures the soil.

Another difference is that they use a RGB 4 LEGS LED, instead of 3 different color Leds that I was using.

One thing to keep in mind is that since you are not using an average distance between the two bolts, maybe is a good idea to add them something so that the distance is always the same.

To solve that, I used a piece of cardboard and some tape, to create some structure for the bolts and wires, and it looks pretty nice and less sturdy, and it will also help you to have an equal measure condition every time, since the distance between the bolts maintains the same.

ESP8266 12-E Chip Pinout

BOM (Bill Of Materials)

3 x 220 ohm Resistors
1 x 10K ohm Resistor
1 x RGB Diffused Common Anode LED
2 Bolts
8 x Male/male jumper wires
1 x Breadboard
1 x Arduino UNO (in this case)

Sensor Optimization (optional)

ESP8266 12-E Chip Pinout

The Original Code:

The tutorial code differs from the one I used before, because:

- The type of led they use.
- How the translation of data into a percentage is averaged .
- The way they declare the ELSE IF and IF values.
- The way they set the values for TOO WET and TOO     DRY.

My main learning from this code was that they declare two thresholds for TOO WET and TOO DRY, and for each led color they use s simplier function for them by just using <= or >= than thresholds. In that way they are able to leave the third LED option without any condition, so it will light with every data inbetween this two assigned conditions.

I really didn´t understood the code percentage operation, and I don´t know where these numbers they multiply and sum come from, so I changed the code 

    int percent = 2.718282 * 2.718282 * (.008985 *     moistVal + 0.207762);



My Modified Code:

1) Read sensor totally dry (it should be 0)

2) Read sensor inside a water glass. (that should give you the highest reading)

3) Divide highest sensor read number by 100. (In my case it was 870/100 = 8.7)

4) Divide Sensor read in the code by the number you got to have a percentage:

    moistVal = analogRead(moistPin);
    int percent = moistVal/8.7;
    Serial.println("% Moisture ");

5) I also used the 8.7 I´ve got before, to declare what was to wet or too dry for me, and then multiply it by the percentage I wanted. In my case it is Too Dry if it is below 30% or Too Wet if it is above 70%:

    int tooDry = 8.7*30;
    int tooWet = 8.7*70;

6) I Also added a fancy Serial.printIn after each read, so that it tells you if it´s too dry, too wet, or good moisture.



Since I did this assignment during last year lockdown, and I only had commercial boards, I created a board with an Attinny 44 that has male pin headers previously connected to some pins to easily add inputs or outputs, and two integrated led's.

I programed it with an AVR-ISP, with the arduino IDE, while powering it through the FTDI VCC and GND pins. I had a lot of trouble with the programmers of my boards, because I had a Barduino 2.2 before, but never documented the work with that board, and after some time, the micro-usb broke and peeled the copper. 

Then I soldered a whole Barduino 2.0 again, but the FTDI was not recognizing it... so we changed the FTDI cable for an FTDI board... It worked for a moment, but then stopped loading the code, arguing that “A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header”, which seems to be a very common problem with the ESP32 chips. There where some solutions people documented like joining PIN2 and GND, or pressing the reset button while connecting it to the USB and while uploading it... but none of them worked.

: (



On the Pab-T-tiny44 I connected an LDR sensor and got the readings from it with an analog read, and created a conditional that turned on the Red light if the sensor was below certain threshold I defined, and to turn on the Green light if it was above. The Green and the Red leds where already integrated on the board.

Since the ATtiny44 does not has serial, I used the SoftwareSerial library, which converts the FTDI into a serial port for communicate between the board and the computer. Here is a piece of example of how it works:

// Include library.
#include <SoftwareSerial.h>

// Create serial port using the RX and TX
SoftwareSerial mySerial(0, 1); // RX, TX (0 y 1)

void setup() {

  // Use "mySerial.XXX" instead of using "serial.XXX" as you usually do.
  mySerial.println("Hello, world?");

Below you will find a picture of the important settings in the IDE and under it, the code link and code.



Arduino KY-018 Photoresistor module, used to measure light intensity. It can determine the presence or absence of light.

This module consists of a photoresistor and a 10 kΩ in-line resistor. The photoresistor's resistance will decrease in the presence of light and increase in the absence of it. The output is analog and determines the intensity of light.

Operating Voltage:        3.3V to 5V


When you use only the LDR, without being part of the module, you should always add a 10KΩ resistor that connects the GND and the Signal Pin.

The LDR Photoresistor has no polarity, so both legs work the same. Connect one of the legs to +3V3 and connect the other one to GND using a 10K resistor like if it was a wire, and also connect that leg to the GPIO A0 using a wire.

LEG 1:     3v3
LEG 2:    10kΩ to GND + A0 (or some analog pin)

Equation to calculate Resistance of LDR:
[R-LDR =(R1 (Vin - Vout))/ Vout]
R1 = 10,000 Ohms , Vin = 5.0 Vdc.

[Vout = ADC * (Vin / 1024)]
Vout = Output voltage from potential Divider.

SOURCE: Arduino Forum

ESP8266 12-E Chip Pinout


#define LDR 2
#define REDLight 7
#define GREENLight 8

float LDRval = 0;
float sunny = 0; // calibrate sensor value WHEN LIGHT
float dark = 1024; // calibrate sensor value WHEN DARK

float treshold = 50; // define treshold to turn lights ON or OFF

    SoftwareSerial mySerial(0, 1); // RX, TX (0 y 1)

void setup() {

pinMode(LDR, INPUT);
pinMode(6, OUTPUT);
pinMode(8, OUTPUT);

void loop() {

if (analogRead(LDR) < 500) {

} else {


LDRval = map(analogRead(LDR),sunny,dark,0,100);

mySerial.print("LDR Read: ");
mySerial.print(" Light Percent: ");
mySerial.println(" %");


Reflections and POWAR:

Getting information from inputs and making them interact between them is one of the things I like most from FabAcademy, and form my project POWAR I'm reading an LDR, a DHT11, a capacitive soil moisture sensor, and a tank water level sensor and sending this information through MQTT to Node red to later on gather information from a weather API online, and work with other actuators.

Even dough, is one of the things I'm most used to, it was a hard assignment because of the use of the other boards and programers, with some failures that frustrate you a bit, but in the end you find another way.


Built with Mobirise web page creator