Soil moisture and distance sensor with IR remote control
A project that can measure soil moisture or distance till the nearest object and displaying them on a LCD (using I2C).
Components and supplies
IR receiver with remote control
Ultrasonic Sensor - HC-SR04 (Generic)
Arduino Nano R3
Male/Female Jumper Wires
Gravity: Analog Soil Moisture Sensor For Arduino
I2C 16x2 Arduino LCD Display Module
Jumper wires (generic)
Project description
Code
Arduino_project.ino
arduino
1/*---Libraries---*/ 2#include "IRremote.h" //Library for IR sensor 3#include <Wire.h> //Library for I2C 4#include <LiquidCrystal_I2C.h> //Library for LCD 5#include <SR04.h> //Library for distance sensor 6 7/*---PINs declaration---*/ 8int PIN_receiver = 10; //PIN for IR signal 9int PIN_moisture = A0; //PIN for soild moisture sensor 10//PINs for distance sensor 11int TRIG_PIN = 12; 12int ECHO_PIN = 11; 13 14/*-----Object declaration-----*/ 15IRrecv receiver(PIN_receiver); //Object for 'IRrecv' 16decode_results result; //Object for 'decode_results' 17LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); //Object for LCD 18// You may have a different I2C adress (0x27 is the one that worked for me) for the LCD 19// If this code doesn't work try to change the I2C adress with this one 0x3F 20// Also you can run a code to see the adress of your I2C 21// https://create.arduino.cc/projecthub/abdularbi17/how-to-scan-i2c-address-in-arduino-eaadda 22SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); //Object for distance sensor 23 24void setup() 25{ 26 receiver.enableIRIn(); //Open the IR receiver 27 lcd.init(); //Open LCD 28 lcd.backlight(); //'Turn on the light' 29 lcd.setCursor(0, 0); //Setting the cursor on the first row, first column 30 lcd.print("Moisture->0"); //Showing the option on LCD, printing 'Moisture->0' 31 lcd.setCursor(0,1); //Setting the cursor on the second row, first column 32 lcd.print("Distance->1"); //Showing the option on LCD, printing 'Distance->0' 33 //If you press '0' (zero) you will get the value of the soil moisture 34 //If you press '1' (one) you will get the value of the distance till the nearest object 35} 36 37//Function that returns the number of the pressed button 38int IRCommand() 39{ 40 switch(result.value) 41 { 42 // If the adress is '0xFF6897' the button is '0', 43 case 0xFF6897: return 0; // If the adress is '0xFF6897' the button is '0' 44 case 0xFF30CF: return 1; // If the adress is '0xFF30CF' the button is '1' 45 } 46} 47 48void loop() 49{ 50 if(receiver.decode(&result)) //Checking if a button was pressed 51 { 52 if(IRCommand()==0) //if the pressed button is '0' 53 { 54 lcd.clear(); //clear the text on the LCD 55 lcd.setCursor(0,0); 56 lcd.print("Moisture[%]:"); 57 //Reading the soil moisture value from the sensor 58 //(It can be between 415 (wet) and 995(dry). I decided to make a map (dictionary) to show the value as a percentage 59 //The value returned by your sensor may be different but you can calibrate it by showing the value (returned from the sensor) 60 //on the LCD while you keep your sensor in air (soil moisture->0%) or in water (soil moisture->100%) 61 int percentage = map(analogRead(PIN_moisture),995,415,0,100); //995 -> 0% and 415 -> 100% 62 //Checking if the value is over 100 or under 0 and corecting it (can happen sometimes, the sensor is crappy) 63 if(percentage<0) 64 { 65 percentage = 0; 66 } 67 if(percentage>100) 68 { 69 percentage = 100; 70 } 71 //Printing the percentage 72 lcd.setCursor(0, 1); 73 lcd.print(percentage); 74 delay(500);//Take a break ~500ms 75 } 76 if(IRCommand()==1) //if the pressed button is '1' 77 { 78 lcd.clear(); //clear the text on the LCD 79 lcd.setCursor(0, 0); 80 lcd.print("Distance[cm]:"); 81 lcd.setCursor(0, 1); 82 lcd.print(sr04.Distance()); //Reading and pinting the distance value from the sensor 83 delay(500); //Take a break ~500ms 84 } 85 receiver.resume();//Reset the current IR command and waiting for a new one 86 } 87} 88
Library for IR sensor
c_cpp
1inary file (no preview
Library for LCD with I2C
c_cpp
1inary file (no preview
Library for IR sensor
c_cpp
1inary file (no preview
Arduino_project.ino
arduino
1/*---Libraries---*/ 2#include "IRremote.h" //Library for IR sensor 3#include 4 <Wire.h> //Library for I2C 5#include <LiquidCrystal_I2C.h> //Library for LCD 6#include 7 <SR04.h> //Library for distance sensor 8 9/*---PINs declaration---*/ 10int 11 PIN_receiver = 10; //PIN for IR signal 12int PIN_moisture = A0; //PIN for soild 13 moisture sensor 14//PINs for distance sensor 15int TRIG_PIN = 12; 16int ECHO_PIN 17 = 11; 18 19/*-----Object declaration-----*/ 20IRrecv receiver(PIN_receiver); 21 //Object for 'IRrecv' 22decode_results result; //Object for 'decode_results' 23LiquidCrystal_I2C 24 lcd = LiquidCrystal_I2C(0x27, 16, 2); //Object for LCD 25// You may have a different 26 I2C adress (0x27 is the one that worked for me) for the LCD 27// If this code doesn't 28 work try to change the I2C adress with this one 0x3F 29// Also you can run a code 30 to see the adress of your I2C 31// https://create.arduino.cc/projecthub/abdularbi17/how-to-scan-i2c-address-in-arduino-eaadda 32SR04 33 sr04 = SR04(ECHO_PIN,TRIG_PIN); //Object for distance sensor 34 35void setup() 36{ 37 38 receiver.enableIRIn(); //Open the IR receiver 39 lcd.init(); //Open LCD 40 41 lcd.backlight(); //'Turn on the light' 42 lcd.setCursor(0, 0); //Setting the 43 cursor on the first row, first column 44 lcd.print("Moisture->0"); //Showing 45 the option on LCD, printing 'Moisture->0' 46 lcd.setCursor(0,1); //Setting the 47 cursor on the second row, first column 48 lcd.print("Distance->1"); //Showing 49 the option on LCD, printing 'Distance->0' 50 //If you press '0' (zero) you will 51 get the value of the soil moisture 52 //If you press '1' (one) you will get the 53 value of the distance till the nearest object 54} 55 56//Function that returns 57 the number of the pressed button 58int IRCommand() 59{ 60 switch(result.value) 61 62 { 63 // If the adress is '0xFF6897' the button is '0', 64 case 0xFF6897: 65 return 0; // If the adress is '0xFF6897' the button is '0' 66 case 0xFF30CF: 67 return 1; // If the adress is '0xFF30CF' the button is '1' 68 } 69} 70 71void 72 loop() 73{ 74 if(receiver.decode(&result)) //Checking if a button was pressed 75 76 { 77 if(IRCommand()==0) //if the pressed button is '0' 78 { 79 lcd.clear(); 80 //clear the text on the LCD 81 lcd.setCursor(0,0); 82 lcd.print("Moisture[%]:"); 83 84 //Reading the soil moisture value from the sensor 85 //(It can 86 be between 415 (wet) and 995(dry). I decided to make a map (dictionary) to show 87 the value as a percentage 88 //The value returned by your sensor may be 89 different but you can calibrate it by showing the value (returned from the sensor) 90 91 //on the LCD while you keep your sensor in air (soil moisture->0%) or in 92 water (soil moisture->100%) 93 int percentage = map(analogRead(PIN_moisture),995,415,0,100); 94 //995 -> 0% and 415 -> 100% 95 //Checking if the value is over 100 or under 96 0 and corecting it (can happen sometimes, the sensor is crappy) 97 if(percentage<0) 98 99 { 100 percentage = 0; 101 } 102 if(percentage>100) 103 104 { 105 percentage = 100; 106 } 107 //Printing the 108 percentage 109 lcd.setCursor(0, 1); 110 lcd.print(percentage); 111 112 delay(500);//Take a break ~500ms 113 } 114 if(IRCommand()==1) //if 115 the pressed button is '1' 116 { 117 lcd.clear(); //clear the text on the 118 LCD 119 lcd.setCursor(0, 0); 120 lcd.print("Distance[cm]:"); 121 122 lcd.setCursor(0, 1); 123 lcd.print(sr04.Distance()); //Reading and 124 pinting the distance value from the sensor 125 delay(500); //Take a break 126 ~500ms 127 } 128 receiver.resume();//Reset the current IR command and waiting 129 for a new one 130 } 131} 132
Library for LCD with I2C
c_cpp
1inary file (no preview
Library for distance sensor
c_cpp
1inary file (no preview
Downloadable files
Connections
I connected the PINs like this: IR sensor - 'OUT' to D10 LCDD with I2C - 'SDA' to A4, 'SCL' to A5 Moisture sensor - 'A0' to A0 Distance sensor - 'Echo' to D11, 'Trig' to D12
Connections

Connections
I connected the PINs like this: IR sensor - 'OUT' to D10 LCDD with I2C - 'SDA' to A4, 'SCL' to A5 Moisture sensor - 'A0' to A0 Distance sensor - 'Echo' to D11, 'Trig' to D12
Connections

Comments
Only logged in users can leave comments