Soil Moisture Sensor with the BBC micro:bit

In this tutorial we will be using the BBC micro:bit, a soil moisture sensor and a relay to measure soil moisture and water the plant if the soil is dry. In the first part of the tutorial we will write code to get the BBC micro:bit to read the value from the soil moisture sensor and depending on the reading we will decide if we need to water the plant. Within the second part of the tutorial we will program the BBC micro:bit, so that on pressing a button the BBC micro:bit will read soil moisture value from the sensor and then make a decision on the need to water the plant.

About the Soil Moisture Sensor: A soil moisture sensor can read the amount of moisture present in the soil surrounding it. It’s a low tech sensor, but ideal for monitoring an urban garden, or your pet plant’s water level. This is a must have tool for a connected garden!!!! A soil moisture sensor uses the two probes to pass current through the soil, and then it reads that resistance to get the moisture level. More water makes the soil conduct electricity more easily (less resistance), while dry soil conducts electricity poorly (more resistance). A simple project with a soil moisture sensor can be helpful to remind us to water our indoor plants or to monitor the soil moisture in our garden. 

Resistive Soil Moisture Sensor

The soil moisture sensor has four pins of which we will be using three. These include –

  1. Vcc – Connected to 3.3V power supply
  2. Gnd – Connected to Ground
  3. A0 – Connected to one of the pins of the BBC micro:bit

Relay – A relay is a programmable electrical switch that can be used to turn devices on and off using code. A relay can be used to turn on or turn off low voltage devices, special relays with opt-isolators are used to switch high voltage devices. We will only be dealing with low voltage devices. PLEASE DO NOT use this tutorial with high voltage relays, you do so at your own peril.

Simple Low Voltage Relay

One side of the relay has three different pins i.e. GND, VCC and SIG. These are connected as follows –

  1. GND – Connected to Ground on the BBC micro:bit
  2. Vcc – Connected to 3V3 on the BBC micro:bit
  3. Sig – Connected to a digital pin so we can trigger the relay i.e. turn it on or off

The pins on the other side of the relay include –

  1. No – Normally Open
  2. NC – Normally Closed
  3. Common – Connected to Ground

Among the pins listed above we will connect Normally Open or NO to one of the terminals of the motor while the Common terminal is connected to the ground.

BBC micro:bit – The micro:bit v2 is a programmable board for kids from the BBC foundation aimed to help kids bring their ideas to life. From making games, to building electronic circuits, to creating music and even controlling robots. The micro:bit comes with neat hardware such as a 25 LED display, buttons, in-built speakers, Bluetooth connectivity and sensors for temperature, motion & light. The new micro:bit v2 has a built-in microphone and speaker to allow sound-sensing and sound-making without the need to attach another device. It also introduces capacitive touch sensing, a power-saving mode and more computing power for the classroom.

The new BBC micro:bit v2 features –

  1. Onboard speaker
  2. MEMs Microphone with LED indicator
  3. Touch-sensitive logo
  4. Built-in sleep/off mode that means the board can be powered-down with batteries connected
  5. Discrete regulator that can supply up to 190mA of current to external accessories

Here’s some of the improvements that the v2 has over v1 –

  1. Notched edge connector. To make it easier to connect things like crocodile clips and conductive thread
  2. Power LED indicator. In addition to the USB activity indicator, a power LED shows whether the micro:bit is powered on or off
  3. Gold plated antenna. To easily identify the radio/Bluetooth component

Here’s details of the processor and other peripherals that the BBC micro:bit v2 includes –

  1. Target MCU, Nordic Semiconductor nRF52833 (64MHz Cortex-M4F, 512kB Flash, 128kB RAM)
  2. Interface MCU: NXP KL27, 256kB Flash (128kB reserved for future enhancement), 32kB RAM
  3. Motion sensor: ST LSM303
  4. MEMS microphone: Knowles SPU0410LR5H-QB-7 MEMS
  5. Power consumption 300mA (up to 190mA for accessories)

Additional details can be found at the BBC micro:bit hardware page.

If you want to dive into the details and keen to see a comparison of the technical specifications between the BBC micro:bit v1 and micro:bit v2, then check this out (Source : BBC micro:bit website microbit.org) –

microbit v1 and v2 technical specifications (source: microbit.org)

Pinout diagram for the BBC micro:bit – To help you work out which pins are located where and what they are called you can refer to the pinout diagram for the BBC micro:bit provided below.

Bill Of Material – Here’s the Bill Of Material for this tutorial.

  1. 1 x BBC micro:bit
  2. 1 x Resistive Soil Moisture sensor
  3. 1 x Relay
  4. 10 x Male to Male Dupont cables
  5. 1 x micro:bit extension board
  6. 1 x Motor

Circuit Diagram – Here’s the circuit you will need to put together. .

BBC micro:bit with Soil Moisture Sensor, Relay & Water Pump

The above diagram was put together in Fritzing. The connections are to be made as follows –

  1. Signal Pin for Pin 2 connected to the A0 of the Resistive Soil Moisuture Sensor
  2. Ground of Resistive Soil Moisuture Sensor connected to Ground on the micro:bit extension board
  3. Vcc of Resistive Soil Moisuture Sensor connected to 3V on the micro:bit extension board

One side of the relay has three different pins i.e. GND, VCC and SIG. These are connected as follows –

  1. GND – Connected to Ground on the micro:bit extension board
  2. Vcc – Connected to 5V on the micro:bit extension board
  3. Sig – Connected to Signal Pin for Pin 0 of the micro:bit extension board so we can trigger the relay i.e. turn it on or off

The pins on the other side of the relay include –

  1. No – Normally Open Connected to black lead of the motor
  2. NC – Normally Closed is not being used
  3. Common – Connected to Ground

The red lead of the motor is connected to the 5v on the micro:bit extension board

Code

Here’s the code for the first part of the that you’ll need to put together. You are encourage to not just copy the code, but rather ask yourself the meaning of each of the lines of code as you type them out and use Google where you need additional support.

micro:bit block coding : automated soil moisture measurements

Here’s the code for the second part of the that you’ll need to put together. You are encourage to not just copy the code, but rather ask yourself the meaning of each of the lines of code as you type them out and use Google where you need additional support.

micro:bit block coding : soil moisture measurements triggered by pressing button A

Summary – We hope you’ve enjoyed building a smart soil moisture sensor setup using the BBC micro:bit, Resistive Soil Moisture sensor and a simple low voltage relay. In this module you learned how to wire up all of the components, write the code, upload it to the Pi Pico and test everything out. Please drop us a note at trevor at hack2 dot live if you have any questions.

Leave a Reply

Your email address will not be published. Required fields are marked *