Input Devices

The assignment for this week was to connect a sensor or an input device to one of our FabLab made microcontroller boards and develop a programm that reads the data form the sensor. Moreover we also had to show the data in the serial monitor. An input device is any hardware device that sends data to a computer, allowing you to interact with and control it. The most commonly used or primary input devices on a computer are the keyboard and mouse.

Soil Moisture Sensor

For this assignment I choose to use the Soil Moisture Sensor. It uses capacitance to measure the water content of soil (by measuring the dielectric permittivity of the soil, which is a function of the water content). To messure it you just need to insert the sensor into the soil you want to test, and the volumetric water content of the soil is reported in percent (you can see this through the serial monitor). I recommend to power the Soil Moisture between 3.3V - 5V. Please note that the analog value returned will vary depending on what voltage is provided for the sensor.

System Calibration

To get useful data out of the Soil Moisture Sensor, I recommend to calibrate it to whatever soil you plan to monitor. Different types of soil can affect the sensor, and you may get different readings from one composition to the next.

For example I get different results, when I attach the sensor to a 3.3 Volt or to a 5 Volt VCC pin. Below you can see the results, when the sensor is attached to the 5 volt supply. The values in the serial monitor show, when the senor is dry 0 and when it is completely saturated with moisture around 880.

But, if I take the VCC pin and connect it to the 3.3V supply, the values change. As expected, they get lower since there is less resolution between 0V and 3.3V than there is between 0V and 5V.

Once you have an idea what values the sensor is outputting in completely dry and completely wet situations, it’s time to calibrate your sensor for the specific soil you want to monitor. Do the same test, only this time test your soil when it is as dry as possible, then measure it when the soil is completely saturated with moisture. Get these values and compare them to the ones from the previous calibration. It will give you the best insight into what values mean for your specific plant and soil. Now you can use the map() function to adjust your code accordingly.

Creating the code

Below you can see the code I created. If the soil of your plant is dry the green light will trun on. Moreover it shows the value measured by the sensor on the serial monitor.

#include <SoftwareSerial.h> // to use the Serial with an ATtiny45 you need to import this library
SoftwareSerial mySerial(2, 0); // These are RX, TX 

//constants won't change. They're used here to set pin numbers:
const int LED = 1;          // I connected the green LED on the ATtiny45 Pin 6
const int Sensor_PIN = PB3; // Analog input pin that the sensor is attached to
int output_value;         // value output to the PWM (analog out)

void setup() {
   // initialize digital LED pins as an output
   pinMode(LED, OUTPUT);
   // initialize Sensor pin as an input
   pinMode(Sensor_PIN, INPUT);
   // initialize serial communications at 4800 bps:
   mySerial.begin(4800);
   }

void loop() {
    // read the analog in value:
   output_value = analogRead(Sensor_PIN);
   // map it to the range of the analog out:
   // 0 is the lowest value and 880 is highest value
   // I convert the value from 0 to 880 default of the calibration. to 0 to 500. means: 0 = 0 and 880 = 500
   output_value = map(output_value,0,880,0,500);
   // if the value is lower than 251 
  if(output_value <= 250) {
    // the soil doesn not have enough water, print the value
     mySerial.println(output_value);
     mySerial.print("%");
     mySerial.println("I am Dry!");
     // turn on the LED to give a signal 
    digitalWrite(LED, HIGH);
    delay(1000);
    // otherwise the soil has enough water 
  } else if (output_value >= 400) {
    mySerial.println(output_value);
    mySerial.println("%");
    mySerial.println("Im fine!");
    // turn of the LED to give a signal 
    digitalWrite(LED, LOW);
    delay(1000);
  }
 
   // wait 1 second before the next loop for the analog-to-digital
   delay(1000);
   }
                    

Before starting to connect my FabLab board to the arduino, I uploaded the ArduinoISP code onto the Arduino Uno. To communicate through the Arduino Software with the ATtiny45 board, I had to check the pinout of the ATtiny45. As you can see below pin 7 on the ATtiny45 is addressed by using pin number 2 in the Arduino Software.

Now I started to connect the Arduion with my board to upload the code.

To start the serial connection I had to connected the SCK and MOSI pins, which I initialize in the code to the software serial, with an USB TTL serial cable to my computer.

To check if my code worked, I put two diffrent type soil in a can.

Furthermore I opened the arduino serial monitor and selected the correct baud rate of 4800 baud. When the soil was dry the green light turned on and when it had enough water the green light turned off, as you can see below in the video. Moreover I showed the value on the serial monitor