Skip to content

Interfacing Moisture Sensor with ATmega2560 Development Board

Introduction

In this guide, we will learn how to interface a moisture sensor with the ATmega2560 development board. A moisture sensor is used to measure the moisture content of soil or other materials. By connecting a moisture sensor to the ATmega2560, we can monitor the moisture level in the soil and use this information for applications such as automated irrigation systems, plant monitoring, or environmental sensing. In this guide you will learn to use the ADC to read analog values from the sensor.

Problem Statement

The objective of this project is to interface a moisture sensor with the ATmega2560 development board and utilize the sensor's output to measure and monitor the moisture level in the soil.

Requirements

To successfully complete this project, you will need the following components:

  • ATmega2560 development board.
  • Moisture sensor.
  • Micro USB cable
  • Breadboard.
  • Jumper Wires.
  • Potentiometer (Built-in)
  • 16X2 LCD

Connection Diagram

  • For LCD connection, refer to this page.
  • Connect VCC of moisture sensor to 5v of the ATmega2560 development board.
  • Connect GND of the sensor to GND of the ATmega2560 development board.
  • Connect Out/Do pin of the sensor to the ADC0, Pin 0 of the port F (Pin 97)of ATmega2560 development board
Pin Diagram
Pin Diagram

Code

  1. Open VS Code and paste the code into the file named main.c
  2. Additionally, add the library for LCD into the workspace of your project, which along with the main code can be found here. (Where you will find a main.c file where the main function is implemented.)
  3. To compile and convert this into hex run the following command in your terminal-
cmd
avr-gcc -Wall -g -Os -mmcu=atmega2560 -o soil_moisture.hex main.c
c
#define F_CPU 16000000UL
#include <avr/io.h>     //Defines pins, ports, etc.
#include <util/delay.h> //Defines delay functions
#include <stdio.h>      //Defines sprintf function
#include <util/delay.h> //Defines delay function
#include <stdlib.h>     //Defines dtostrf function
#include <string.h>     //Defines strcat function
#include "lcd.c"        //Include LCD header file
static inline void initADC0(void)
{
    ADMUX |= (1 << REFS0);                 // reference voltage on AVCC
    ADCSRA |= (1 << ADPS1) | (1 << ADPS0); // ADC clock prescaler /8
    ADCSRA |= (1 << ADEN);                 // enables the ADC
}
int main(void)
{
    float value; // variable to store ADC value
    char data[20];
    LCD_Init();  /* Initialize LCD */
    LCD_Clear(); /* Clear LCD */
    initADC0(); // initialize the ADC
    while (1)
    {
        LCD_gotoxy(0, 0);                      /* Enter column and row position */
        LCD_string("Moisture = ");             // display string on LCD
        ADCSRA |= (1 << ADSC);                 // start ADC conversion
        loop_until_bit_is_clear(ADCSRA, ADSC); // wait until ADC conversion is done
        value = ADC;                           // read ADC in
        value = (value * 100) / 1023;          // convert ADC value to percent
        dtostrf(value, 3, 2, data);            // convert float to string
        strcat(data, "%   ");                  /* Concatenate unit of % */
        LCD_gotoxy(11, 0);                     // enter column and row position
        LCD_string(data);                      // display data on LCD
        _delay_ms(500);                        // wait a bit
        LCD_Clear();                           // clear LCD
    }
    return 0;
}
  1. Now open AVRDUDES and from the Programmer (-c) dropdown select Any usbasp clone with the correct VID/PID
  2. In the Flash section, click on three dots and navigate to the desired hex file, and select it. Finally, click on Program to burn the hex file in the microcontroller.

Output

After uploading the code to the ATmega2560, the program will continuously read the output from the moisture sensor. The moisture level values will be shown on LCD in percentage. (Verify the readings by watering the soil)

Conclusion

By following this guide, you have successfully interfaced a moisture sensor with the ATmega2560 development board and learnt how to read analog readings and process them using ADC. You can now measure and monitor the moisture level in the soil or other materials using the sensor's output. This opens up possibilities for applications such as automated irrigation systems, plant monitoring, or environmental sensing. Feel free to modify the code and customize it to suit your specific needs. Enjoy exploring the capabilities of the ATmega2560 and moisture sensor in your projects!