# Soil Moisture Sensor with Circuit Playground Express and MakeCode

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/054/341/medium800/sensors_plant.jpg?1527110820)

These days, we all get busy and soon a week (or month) has gone by. Then we remember: water the plants! Run over to the plants and the soil is like a desert and we apologize to the poor plant.

This situation can be averted through a simple project using the [Adafruit Circuit Playground Express](https://www.adafruit.com/product/3333 "Adafruit Circuit Playground Express"). The amount of moisture in the soil can be measured by the board and indicators used to show if the plant needs watering.

All you need is a nail and an alligator clip cable. Adafruit carries a kit with the materials or you can buy the pieces separately.

### Circuit Playground Express Soil Sensor Mini Kit

[Circuit Playground Express Soil Sensor Mini Kit](https://www.adafruit.com/product/4060)
The thirst is real! Keep your flora (plants, [not the board](https://www.adafruit.com/product/659)&nbsp;\*wink\*) happy and hydrated with the **Circuit Playground Express Soil Sensor Mini Kit**. This kit will teach you all about how to keep your plants in good condition,...

In Stock
[Buy Now](https://www.adafruit.com/product/4060)
[Related Guides to the Product](https://learn.adafruit.com/products/4060/guides)
![Hand squeezing a water bottle into a potted plant with metal nail stuck into soil. Nail connects to Circuit Playground lit up green.](https://cdn-shop.adafruit.com/640x480/4060-01.jpg)

Separately:

### Part: Circuit Playground Express
quantity: 1
Circuit Playground Express Board
[Circuit Playground Express](https://www.adafruit.com/product/3333)

### Part: 3 x AA Battery Holder 
quantity: 1
Battery holder with On/Off Switch and JST connector to CPX
[3 x AA Battery Holder ](https://www.adafruit.com/product/3287)

### Part: Alkaline AA Batteries
quantity: 1
3 packaged together
[Alkaline AA Batteries](https://www.adafruit.com/product/3521)

### Part: Small Alligator Clip Test Lead
quantity: 1
Set of 6
[Small Alligator Clip Test Lead](https://www.adafruit.com/product/4100)

### Part: Nail or other piece of conductive metal
quantity: 1
2 to 4 inches (5-10 centimeters)

A nail is a common piece of metal to use but it could be any metal that isn't too broad that can stick into the soil of the pot with a decent depth. The metal/nail does not need to reach the bottom of the pot, only be an inch or two (2.5-5 cm) down in the soil.

## How It Works

The Circuit Playground Express can detect the capacitance (the ability to store an electric charge) on an analog pin (in our case, analog pin `A1`). As the moisture around a nail in the plant's pot increases, the value increases (we're creating a bigger capacitor). Dry soil doesn't work as well for electricity (like most dry materials except metals) so the value read by the Circuit Playground Express will be lower.

## Pick Your Plant

This project works best on indoor plants in a pot or indoor window bed. Outdoor use would require wanterproofing and the "water me" indication wouldn't be obvious if it was outdoors.

The plant in my example is a baby Maple tree in a smallish pot. As it grows, it'll need a bigger pot!

You will need to spend a few minutes with your chosen plant to get a good idea of when it is wet and dry to provide a good, accurate measurement. So pick out your plant and do not water it before starting.

You might want to look on the Internet as to what type of soil moisture makes the plant flourish best. You can adjust the moisture values in your program to "dial in" the best values for your particular plant.

# Soil Moisture Sensor with Circuit Playground Express and MakeCode

## Build

![](https://cdn-learn.adafruit.com/assets/assets/000/054/342/medium800/sensors_plant.jpg?1527110885)

Use the alligator clip wire combo to clip onto the Circuit Playground Express `A1` pad. The other end should clip onto your nail or piece of metal. The nail should be in the soil about 2 inches (5 centimeters).

For the Code portion of this build, you will want the Circuit Playground Express connected via a USB cable connected to a computer capable of running MakeCode. The USB cable will provide the Circuit Playground Express power for this portion of the build.

![](https://cdn-learn.adafruit.com/assets/assets/000/054/346/medium800/sensors_plant2.jpg?1527111378)

When your code is done on the next page, unplug the Circuit Playground Express from the USB cable and plug in your battery pack. Be sure your off/on switch on the battery pack is set to `on`.

# Soil Moisture Sensor with Circuit Playground Express and MakeCode

## Code

This project uses Microsoft MakeCode for the code to read the soil sensor.

If you are new to Microsoft MakeCode, Adafruit has an excellent getting started tutorial at&nbsp;[https://learn.adafruit.com/makecode](../../../../makecode "MakeCode Tutorial").

The following is a short program that takes a moisture value we read as dry and gives a red neopixel reading if the reading falls below this dry amount.

https://makecode.com/_3cwgMXVdcKVc

Your MakeCode screen should have the following MakeCode program loaded.

![](https://cdn-learn.adafruit.com/assets/assets/000/054/332/medium800/sensors_soil-code1.png?1527109247)

The value read by the Circuit Playground Express is set to a level that might be a good dry soil value `1500`. This number can be adjusted to give a better accuracy.

[Load this program into the Circuit Playground Express via Microsoft MakeCode.](https://makecode.com/_a3RToUMw4Hv5)

[Open this project in MakeCode](https://makecode.com/_a3RToUMw4Hv5)
Ensure you have your plant monitor hooked up as done on the Build page.

## For Windows

Click on the console output and see what reading is being printed out.

![](https://cdn-learn.adafruit.com/assets/assets/000/054/335/medium800/sensors_soil_log_button.png?1527109338)

Click on the Show console Device button and look at the output values. With the moisture monitor hooked up, the reading should be somewhat greater than about `890`.

## Manual Calibration, All Platforms

Typical values: `890` or so with no alligator clip, `2160` for an alligator clip and 3 inch nail, `2600` in my dry(ish) pot with plant, You can set it lower, the Adafruit crew has `1500` for dry soil. Your numbers may vary. Be sure you have your Circuit Playground Express away from your body and on a dry, non-conductive surface like a countertop and keep your hands away while making settings to ensure things are not biased by the surroundings.

If your soil is dry, then the number you have with everything hooked up and no lights, should be good for your dry soil baseline. The larger the number, the wetter the soil. Note the number you are reading.

Change the value of the variable `dry_value` in the `on start` block to the value while the pot is dry and the lights are not on. You can then add water to the pot which will make the number go up and the light will go off when the project detects the soil is reading \> `dry_value`. It may take a few tries to dial in the number perfect for your plant. This will be your dry soil benchmark.

Any time the Circuit Playground Express reads a value the value that is the dry value or lower, it will turn all the lights red, alerting you. If the read value is greater than `dry_value`, the NeoPixels will be turned off via the `clear` block.&nbsp;

Readings are taken every two seconds (2000 milliseconds) per the `pause` block on the bottom. If you want to increase the idle time between readings, add additional `pause` blocks. If you'd like a very long time between readings, see the trick at the bottom of the Going Further page.

Once you have your code "dialed in" with the final `dry_value`, you can download the final code via Microsoft MakeCode to the Circuit Playground Express.

Place fresh AA batteries into your battery pack.

You may now unplug the USB cable to your computer and plug in the battery pack into the black battery jack on the Circuit Playground Express. The green On light should be glowing next to the USB jack letting you know you have power.

## Final Setup

Set your plant in its final location. Place the Circuit Playground Express so you will notice the color of the NeoPixels as you walk by and the battery pack tucked away.

Go ahead and water your plant. The NeoPixels should be unlit. If they are still red, you'll need to go back and check your `dry_value`.

You're now set to have your Circuit Playground Express monitor your plant moisture!

# Soil Moisture Sensor with Circuit Playground Express and MakeCode

## Going Further

The Circuit Playground Express can alert via other NeoPixel animations. The design constraint would be how much battery you use over time. You can adjust the NeoPixel brightness in the `set brightness` block - even a value of 25 is easily seen.

If you always have a green "good" display and then it turns into a flashing red display, that's alerting. But having the NeoPixels always on will drain battery. Using the animations defined in the LIGHT block group also gives an animated display at the expense of battery power.

You can have a sound alert but this might become annoying. See the first code below. You can always have fun and have it play a tune. In practice, it becomes annoying (and drains battery), so lights are best.

![](https://cdn-learn.adafruit.com/assets/assets/000/054/355/medium800/sensors_soil-code2.png?1527112721 Adding a tone to the light display (not recommended))

## Assistive Technology

For a person with limited sight, the sound might prove to be a better choice. The light code could be removed to save power and the sound stretched to a longer time.&nbsp;

The code below allows for much longer times without too many pause blocks. This program waits 5 minutes (`300000` milliseconds) and makes a beep if the soil is dry. This may be better for someone who cannot see NeoPixels. Adjust the number of milliseconds in the `while` loop to increase or decrease from 5 minutes. Again, the sound should be such that it alerts but does not annoy.

https://makecode.com/_aK3XjV9yihKP

![](https://cdn-learn.adafruit.com/assets/assets/000/054/666/medium800/sensors_plant_sound.png?1527624738)


## Featured Products

### Circuit Playground Express Soil Sensor Mini Kit

[Circuit Playground Express Soil Sensor Mini Kit](https://www.adafruit.com/product/4060)
The thirst is real! Keep your flora (plants, [not the board](https://www.adafruit.com/product/659)&nbsp;\*wink\*) happy and hydrated with the **Circuit Playground Express Soil Sensor Mini Kit**. This kit will teach you all about how to keep your plants in good condition,...

In Stock
[Buy Now](https://www.adafruit.com/product/4060)
[Related Guides to the Product](https://learn.adafruit.com/products/4060/guides)
### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### Small Alligator Clip Test Lead (set of 6)

[Small Alligator Clip Test Lead (set of 6)](https://www.adafruit.com/product/4100)
Connect this to that without soldering using these small&nbsp;alligator clip test leads. 18" long cables with color-coded alligator clips on both ends. You get 6 pieces in 6 unique colors. Strong and grippy, these always come in handy! We often use these in conjunction with a multimeter...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4100)
[Related Guides to the Product](https://learn.adafruit.com/products/4100/guides)
### 3 x AA Battery Holder with On/Off Switch, JST, and Belt Clip

[3 x AA Battery Holder with On/Off Switch, JST, and Belt Clip](https://www.adafruit.com/product/3287)
This battery holder connects 3 AA batteries together in series for powering all kinds of projects. We spec'd these out because the box is compact, and 3 AA's add up to about 3.3-4.5V, a very similar range to Lithium Ion/polymer (Li-Ion) batteries, plus it has&nbsp;a nifty&nbsp;on-off...

In Stock
[Buy Now](https://www.adafruit.com/product/3287)
[Related Guides to the Product](https://learn.adafruit.com/products/3287/guides)
### Alkaline AA batteries (LR6) - 3 pack

[Alkaline AA batteries (LR6) - 3 pack](https://www.adafruit.com/product/3521)
Battery power for your portable project! These batteries are good quality at a good price, and work fantastic with any of the kits or projects in the shop that use AAs. This is a pack of **3 AA batteries**.  
  
These batteries are Alkaline (MnO2) chemistry, with a voltage...

In Stock
[Buy Now](https://www.adafruit.com/product/3521)
[Related Guides to the Product](https://learn.adafruit.com/products/3521/guides)

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Crickit Powered Mini Chair Swing Ride!](https://learn.adafruit.com/mini-chair-swing-ride.md)
- [Light Paintbrush with Circuit Playground](https://learn.adafruit.com/lightpaint-cplay.md)
- [Combo Dial Safe with Circuit Playground Express](https://learn.adafruit.com/combo-dial-safe-with-circuit-playground-express.md)
- [Circuit Playground Hot Potato](https://learn.adafruit.com/circuit-playground-hot-potato.md)
- [Crawling Animatronic Hand](https://learn.adafruit.com/crawling-hand-with-cpx-and-makecode.md)
- [Sparkle Skirt Playground](https://learn.adafruit.com/sparkle-skirt-playground.md)
- [CircuitPython-Powered 3-minute Nightlight](https://learn.adafruit.com/circuitpython-powered-gemma-nightlight.md)
- [Capacitive Touch Pulsing Heart](https://learn.adafruit.com/3d-printed-heart-capacitive-touch.md)
- [Introducing Adafruit Crickit #MakeRobotFriend](https://learn.adafruit.com/adafruit-crickit-creative-robotic-interactive-construction-kit.md)
- [Steven Universe Cosplay Shirt & Gem](https://learn.adafruit.com/steven-universe-cosplay-shirt-gem.md)
- [Sound Activated Shark Mask](https://learn.adafruit.com/sound-activated-shark-mask.md)
- [Desktop Dumpster Fire](https://learn.adafruit.com/desktop-dumpster-fire.md)
- [Obsidian Sword – Steven Universe](https://learn.adafruit.com/obsidian-sword-steven-universe.md)
- [Spy Theme Playback Device](https://learn.adafruit.com/spy-theme-playback-device.md)
- [Bunny Ears with MakeCode](https://learn.adafruit.com/bunny-ears-with-makecode.md)
