Soil Moisture Sensor Example Project

Introduction

I say, if your knees aren’t green by the end of the day, you ought to seriously re-examine your life. ~Bill Watterson, Calvin & Hobbes

Green-thumbed techno junkies rejoice!  For today, we’re going to take an introductory, prototype look at what it takes to digitally monitor the soil moisture content of a common houseplant so we know when to water it.  We’re talking about using a single board computer to read from a soil moisture sensor from an Analog to Digital Convertor (ADC) and toggle an indicator LED using Digital Input and Output (DIO). Specifically, we’re going to be using a TS-7250-V2, but this guide can apply to many different boards.

Admittedly, the TS-7250-V2 is a bit overkill for the scope of this simple project, where a simple microcontroller would work, but it does provide us with a solid foundation for full-fledged autonomous garden projects. The TS-7250-V2 has five ADC channels, so you could hook it up to five different watering zones.  It also has plenty of DIO pins for controlling and interfacing with external hardware, like turning on or off water pipes for example.  Where things get really interesting is having the ability to run a web or API server from the Linux-powered board for access from anywhere in the world.  So, there’s definitely room to grow here (pun intended). With that, let’s dig in.

Things We’ll Need

Here’s a list of the items we’ll be prototyping with today:

Setting Up The Hardware

Where the rubber meets the road, or rather, where the sun meets the photosynthesizer.  We’re going to hook up our components to the single board computer.  For the soil moisture sensor, we need power, ground, and an ADC channel for the signal wire.  

In order to prevent oxidation of the probes, we’re not going to apply constant power.  Instead, we’re going to hook it up to a 3.3 VDC DIO pin so we can turn it on only when we’re ready to read what’s on the signal wire.  We’ll call this our power enable pin.  In order to visually see when power to the moisture sensor is active, we’re going to hook up a yellow LED to the DIO pin as well.  Finally, we’re going to need some sort of indicator to let us know when the plant needs watering, so we’re going to hook up a red LED to another DIO pin.  What we’re left with is something resembling the wiring diagram below.  

Notice, we’re connected to the DIO and A/D headers.  The red LED is connected to DIO_03, or GPIO #77 in software.  The power enable wire is connected to DIO_01, or GPIO #76 in software, which powers our moisture sensor and yellow LED.  A ground pin for all of our components is conveniently located on DIO_01.  After connecting everything, we can continue to software development, but before we do, let’s talk about protecting our sensor.

As we all know, electricity and water don’t get along, so before we go sticking our sensor into a moist environment, we should protect it.  For indoor applications, simply using hot glue can provide good protection.  For outdoor applications, hot glue won’t hold up to the sunshine and other elements very well, so conformal coating would be much better.  Here, we’re working with an indoor houseplant, so hot glue was used, as seen in the image below.

Okay, let’s move onto the exciting software stuff!

Programming The Software

Tighten up your thinking cap; we’re going to unearth some C code.  Now, this could very well be done in Python or another language, but we had some example C code already provided by Technologic Systems.  Also, those coming from a microcontroller world might be more comfortable in implementing their hardware in C/C++, so we’ve shaped the ground to match that sort of landscape.

Start by taking a look at the code at the embeddedarm/soil-watcher git repository.  There are several files here including the example C code pulled directly from Technologic Systems called evgpio.cand evgpio.h.  The evgpio library takes care of all the dirty details of dealing with the somewhat unique event driven GPIO of the TS-7250-V2.  This is about the only thing specific to this example project.  For other boards, you’ll need to utilize your own GPIO library.  If your board happens to use the popular GPIO sysfs interface, you’re in luck because we have example code available here in Cand here in C++.   Otherwise, refer to your board manual. With that said, let’s take a look at the main event — soil-watcher.c.

Essentially, what we’re looking at is a program that has a few functions, but primarily main(), setup() and run().  The setup() function sets everything up initially, preparing our system by turning off LEDs and initializing the GPIO pins.  The run() function contains an infinite while loop that will continually monitor soil moisture readings and control the sensor power and red LED.  The main() function simply parses command line parameters and calls setup() and run().  The other functions are mainly just helper functions.  The code was heavily commented to make it easy to digest and understand so that we don’t have to dig into too many overwhelming details in this writeup.  

Here’s what our program outputs as I leave it sit on my desk and then touch the two prongs together with my fingers:

One of the finer points of our soil-watcher.c program is the (totally optional) ability to run as a daemon. This allows us to run the program in the background so-to-speak, although what’s actually happening is a little more complex.  What we care about is that it frees up our shell as our program goes about its own way.  We can see it running by using ps ax | grep soil-watcher.

This also redirects all output that the program would normally generate to /dev/null.  So, no logging will be available using this method.

The next thing we’ll want to do with our program is make it start automatically when the system powers on.  There are a few different ways of doing this, but the most popular is to add a init.d or systemd (example) script.  For our TS-7250-V2, we’ll use init.d, as documented in the “Starting Automatically” section of the TS-7250-V2 manual.  The file has already been created for you as soil-watcher.sh.  To get it working on startup, follow these commands:

The next thing we’ll want to do with our program is make it start automatically when the system powers on.  There are a few different ways of doing this, but the most popular is to add a init.d or systemd (example) script.  For our TS-7250-V2, we’ll use init.d, as documented in the “Starting Automatically” section of the TS-7250-V2 manual.  The file has already been created for you as soil-watcher.sh.  To get it working on startup, follow these commands:

Then, when you reboot your system, our soil-watcher program should be running.

Closing Remarks

Now, the prototyping phase is over.  If you’re satisfied with this mini-application, then you could start figuring out how to enclose the system and make it look like a finished product.  

I would start by creating a false bottom in the planter in which to house the board and wiring. Then, I’d run some sort of flexible, thin conduit in which to run the wires for the LED and moisture sensor.  A hole would be drilled in the planter and the red LED mounted in that hole, so it’s visible from the outside of the plant.  Then, whenever I was walking by and seen the red LED, I would know it’s time to water the plant.

Really, this is just the tip of the iceberg.  The principles taught in this tiny project of using DIO and ADC pins are applicable to many situations and projects.  As mentioned earlier, using the TS-7250-V2 single board computer is massively overkill for this tiny project, but you now have a powerful and sturdy foundation on which to build so much more, which I hope you’ve felt inspired to do so.  

If I were to take this further, I would want to build an automated garden with five different watering zones, hooked up to relays and solenoids to control water flow.  I’d incorporate an online dashboard into the project by utilizing the Apache webserver and MySQL database to track and display historical data. With Node.js easily installable, I’d bring some IoT flare to the table by creating a REST API which I could use to monitor my soil in real time and open water valves with the push of a button on my phone.

Sources:

https://github.com/embeddedarm/soil-watcher

https://wiki.embeddedarm.com/wiki/TS-7250-V2

ftp://ftp.embeddedarm.com/ts-arm-sbc/ts-7250-v2-linux/sources/

Practical Guide to Getting Started with the TS-4100

 

This practical guide gives us an opportunity to take a relaxed approach to getting started with the TS-4100 computer. We’re going to take a look at how to make our first connections, and setup the network. These are usually the first things we do before starting development. In the grand scheme of things, this is just a friendlier extrapolation from the official TS-4100 manual, so be sure to keep it handy for more advanced topics and specific details. The only assumption being made is that you’ve purchased the TS-4100 with a development kit, including the pre-programmed microSD card and TS-8551 reference board. Right then, let’s get started!

Continue reading “Practical Guide to Getting Started with the TS-4100”

Practical Guide to Getting Started with the TS-7800-V2

This practical guide gives us an opportunity to take a relaxed approach to getting started with the TS-7800-V2 single board computer. We’re going to take a look at how to make our first connections, and setup the network. These are usually the first things we do before starting development. In the grand scheme of things, this is just a friendlier extrapolation from the official TS-7800-V2 manual, so be sure to keep it handy for more advanced topics and specific details. The only assumption being made is that you’ve purchased the TS-7800-V2 with a development kit, including the pre-programmed microSD card and necessary cables.

For you TS-7800 users upgrading to the TS-7800-V2, you’re in for a treat. There’s a migration guide specifically created to help you with some of the nuances in upgrading. For this, take a look at the “Migration Path” section of the TS-7800-V2 Manual.

When you’ve finished, be sure take a look at PWM Primer with the TS-7800-V2.  Good stuff there about working with dimming LEDs and controlling servo motors.

Right then, let’s get started! Continue reading “Practical Guide to Getting Started with the TS-7800-V2”

PWM Primer with the TS-7800-V2

In this PWM crash course, we’ll be taking a look at what PWM is and how to use it by way of example. First, we’ll control the brightness of an LED and make it breathe, then we’ll control the position of a servo motor. This will all be done using the PWM channels on a TS-7800-V2.

Continue reading “PWM Primer with the TS-7800-V2”

Real World Example in Working with I2C Sensor Device

Let’s take a look at what it takes to read sensor data from an I2C interface (aka I2C, IIC, TwoWire, TWI).  In particular, we’ll be reading data from the NXP MPL3115A2 Altimeter/Barometer/Temperature sensor.  The principles found in this guide can also be applied generically, even to your ambifacient lunar waneshaft positioning sensor of your turboencabulator.

Continue reading “Real World Example in Working with I2C Sensor Device”

Example XBee Project: Opened Door Alert via Email/SMS

Imagine this: You have a five-year-old son who has grown tall enough, and smart enough to open the door to your home office, packed with all your super fun gizmos and trinkets. It has a lock, but being the lackadaisical creature you are, you forget to lock it. You’ll only be gone for a minute or two, after all! Well, that was just enough time for your son to sneak in, rip up all the jumper wires from your breadboard, find a permanent marker, and well, you know how this ends.

In this (oddly specific) example project we’re going to be coming up with a solution to avoid such a disaster by building a wireless, internet connected, SMS door alert system using:

This way, we’ll receive a text message every time the door is opened and be able to rush to the scene of the future crime.
Continue reading “Example XBee Project: Opened Door Alert via Email/SMS”

Now Sampling the TS-4100

TS-4100 Computer on Module powered by NXP i.MX6 UL Processor.

Feb 15, 2018 – Technologic Systems announced their latest Computer-on-Module, the TS-4100, has entered in to their engineering sampling program (see below for details). The TS-4100 is the first Technologic Systems Computer-on-Module to feature the NXP i.MX 6 UltraLite processor, featuring a single ARM Cortex A7 core, operating at speeds up to 695MHz. The NXP i.MX 6UL processors offer scalable performance and multimedia support, along with low power consumption. Technologic Systems allows you to take full advantage of the integrated power management module to optimize power sequencing throughout the board design to achieve 300 mW typical power usage, making this CoM perfect for embedded applications with strict power requirements. The TS-4100 is perfect for industrial embedded applications for medical, automotive, industrial automation, smart energy and many more applications.

Read the full press release on www.embeddedarm.com…

 

A Friendly Introduction to XBee

Digi XBee radios sure are handy for wireless communication in embedded systems, so let’s take a look from a newbie perspective at how to get two of ‘em talking to each other quickly.

This tutorial can be applied generically to any setup with any two XBee radios, so long as you have them plugged in and ready to work with a serial port. That being said, this is a list of parts used in this tutorial:

Continue reading “A Friendly Introduction to XBee”

Practical Guide to Getting Started with the TS-7670

This practical guide gives us an opportunity to take a relaxed approach to getting started with the TS-7670 single board computer. We’re going to take a look at how to make our first connections, and setup the network. These are usually the first things we do before starting development. In the grand scheme of things, this is just a friendlier extrapolation from the official TS-7670 manual, so be sure to keep it handy for more advanced topics and specific details. The only assumption being made is that you’ve purchased the TS-7670 with a development kit, including the pre-programmed microSD card. Right then, let’s get started!

Continue reading “Practical Guide to Getting Started with the TS-7670”

Getting Started with GPS on the TS-7670

One big draw to the TS-7670 is the GPS receiver feature. When paired with a wireless network, such as through WiFi or cellular, it becomes a powerful tool in asset tracking or geo surveying. In this quick guide, we’re going to go through what it takes to get GPS working and how to read the data.

Continue reading “Getting Started with GPS on the TS-7670”