Social

Social

Social

Friday, February 26, 2016

Worm Bin Moisture Sensor with Arduino

Arduino connected to Rapsberry Pi with USB
Arduino Uno with analog moisture sensor and usb connected to Pi















I have my worm bin temperature sensors working very well with my Raspberry Pi.  I have now turned my attention to measuring soil moisture.

Moisture Sensing Choices:

  • Which soil moisture sensor should I use?
  • Which micro controller do I employ?
  • How many sensors are needed for practical use?

After a lot of searching for sensors, it seems that moisture sensing is not nearly as straightforward  as reading air or soil temperatures.  There are different ways of reading moisture.

I honestly do not know the ideal way to read moisture. It seems that measuring the electrical conductivity of the soil to look for moisture is a popular method so I will go with that.

Sensor Corrosion Problem

One significant problem with sensors using electrical resistance is that they do not last very long because they corrode due to the electricity and metal contacting wet soil.  To help minimize this issue, you can get them gold plated and only power the sensor before you take your reading and then turn it off to maximize sensor contact life.

Note: I am a noob regarding electrical things but it looks like I might be able to control power to the sensor with a transistor (It acts as an electronic switch).

I opted to go with a quick-and-dirty approach first.  The Raspberry Pi has no analog pins to read and write to.  Moisture sensors require analog connections to the microcontroller to work.  After all, 0 and 1 tell you wet or dry but no values in between.

The sensor I bought first is http://amzn.to/1Q68YSW

I happen to have an Arduino Uno that I played with a few years back, so its time to dust it off  and put to work.  Actually, my board is revision 3 which looks current.  

I proceed to install the Arduino ide and make sure it works with a few simple LED light blinking sketches.  All good so far.

My moisture sensor arrives from Amazon and I realize it has absolutely no documentation so I start googling what is printed on it "ywRobot mositure sensor" and come across some schematics and this helpful video... (it's actually wRobot I think)



I follow along with the video and within a couple of minutes I have it reading values 0.  I drop it into a shot glass of water and the reading changes to around 600 give or take.


Setup Raspberry Pi to Log Serial Data 

(That is send moisture readings via USB from your Arduino sketch to Pi)


Make sure Python is installed on Pi.  

You can attempt to install or check version.
install:
sudo apt-get install python 
or check python version:
python -V

Install moreutils... 

to get TS (time stamp) package.  moreutils is a basic linux grouping of hand features not included in standing linux distribution.

sudo apt-get install moreutils

Install grabserial

grabserial is a utility to read serial data from serial port of remote linux microcontroller and write to standard out (like a text file for later use).

wget https://github.com/tbird20d/grabserial/blob/master/grabserial


Test serial (USB) connection

lsusb

I see all the usb connected devices including my arduino:
Bus 001 Device 004: ID 2341:0043 Arduino SA Uno R3 (CDC ACM)

Get device id
This is a little tricky.  I used grep to get keyword related results since my results spanned multiple pages:

dmesg | grep tty
I looked through results and found my id of TTYACM0
You can just page through results with:
dmesg | more

Incidentally, the /dev directory is a listing of hardware devices and those prepended with 'tty' are serial devices.

NOTE: the symbol | is typed by using shift : on your keyboard.  It's called a pipe command.

Configure grabserial to use the correct serial port

use the text editor nano (You can us vi and others but vi has a steeper learning curve.):  
nano grabserial

within the file use the ctrl-w command to jump right to the lines to configure.  Search for 'sd.port' (which takes your down about 7 pages).

Change your setting to match your serial port id found in dmesg:

        sd.port="/dev/ttyACM0"
        sd.baudrate=9600

You are setting the baud rate to match what your arduino serial port was set to in the sketch you installed.

Within grabserial file save with CTRL-x to exit and Y to save changes.

Check that grabserial is working:

python grabserial

I get output:
  , 0
because my sensor is not in water so all works and I CTRL-c to stop.

to get the timestamp for each reading

python grabserial | ts
Feb 14 16:53:26  , 0


Permanently Log Moisture Readings

There are a lot of ways to process the data now.  You can save to database or save to text (csv) file.  You can then use a web page to display, chart, or export to your computer to use in excel or your favorite charting program.

To write the data to a csv file for use later with use:
python grabserial | ts  >  moisture.csv 

To add a new data file in our sensor directory (to chart with the other sensors):
python grabserial | ts  >  /home/pi/projects/temp-and-humidity/sensor-values/moisture.csv

or even better yet, run the command above with a cron job then edit the worm bin web page to read the data (/home/pi/projects/temp-and-humidity/public/index.html)   You can check this crontab tutorial for more info on doing this.

Using Raspberry Pi Only

In this post I am using the Arduino because the Raspberry Pi does not come with analog pins and I did not have a spare Raspberry Pi lying around.  However, there is a way to use analog devices with Raspberry Pi using an analog to digital controller (ADC).  I will explore that in another post.

Number of Sensors


I have heard suggestiongs to go with 1 sensor every foot.  In this case, my 3 x 4 ft bin could use 3 sensors.  Once I settle on a platform, I will pickup 2 more sensors, figure out the switchin them on and off method, solder up some long wires to a board, connect them to one of the controllers, and coat the electronics with heat shrink tubing and silicone conformal coating to prevent moisture damage.

Conclusion

You can use any micro controller to read analog inputs.  It may just come down to which platform you are confortable with.  There are major differences in Raspberry Pi (a full computer operating system)  and Arduino (real time microcontroller), but simple analog readings can be done with both platforms.


No comments:

Post a Comment