Reader small image

You're reading from  Creative DIY Microcontroller Projects with TinyGo and WebAssembly

Product typeBook
Published inMay 2021
PublisherPackt
ISBN-139781800560208
Edition1st Edition
Tools
Right arrow
Author (1)
Tobias Theel
Tobias Theel
author image
Tobias Theel

Tobias Theel works as the Technical Lead and DevOps for a German FinTech startup fino and since 2020 he has also started working for RegTech startup, ClariLab, as Lead Software Engineer. Being a software architect and an expert for Go and TinyGo alongside C# and Java, he is also iSAQB certified. Theel is a highly enthusiastic community contributor and is among the top 10% responders in C# and Unity3D as well as top 20% responders in .NET, Go, and Visual Studio on StackOverflow. When not programming for fino or ClariLab, he can be found developing games, mainly at game jams such as the Ludum Dare Jam, where he develops games from scratch within 72 hours. As an active speaker at tech talks and a participant for numerous hackathons, Theel loves to share his knowledge of software development with fellow enthusiasts.
Read more about Tobias Theel

Right arrow

Chapter 4: Building a Plant Watering System

In the previous chapters, we learned how to write to the serial port and how to monitor the serial port on our computers. Furthermore, we learned how to write drivers for components, which have not yet been implemented by the TinyGo community, and we used this knowledge to write a driver for a 4x4 keypad and a servo motor in Chapter 3, Building a Safety Lock Using a Keypad.

We are now going to build on top of this knowledge in this chapter by introducing a new type of pin and we are going to build an automated plant watering system using some new devices. We will be able to pump water from a container into a plant's soil, measure the soil's moisture, check the water level of a container, and let a buzzer beep when the water level in the container is below a certain threshold. This will be achieved by splitting the project up into single steps and putting it all together at the end of the chapter.

After working through this...

Technical requirements

We are going to need the following components for this project:

  • An Arduino UNO
  • Capacitive Soil Moisture Sensor v1.2
  • K-0135 Water Level Sensor
  • Passive buzzer with 2 pins
  • Micro submersible water pump DC 3V-5V
  • Breadboard power supply module
  • Jumper wires
  • One breadboard
  • One 100 Ohm resistor

These components can usually be found in online stores and also in local electronic supply stores. Most components used in this book are also part of so-called Arduino Starter Kits.

You can find the code for this chapter on GitHub: https://github.com/PacktPublishing/Creative-DIY-Microcontroller-Projects-with-TinyGo-and-WebAssembly/tree/master/Chapter04

The Code in Action video for the chapter can be found here: https://bit.ly/3tlhRnx

Reading soil moisture sensor data

When automatically watering plants, we need to know when we have to add water to the soil. An easy way to detect that the soil is too dry is to use a soil moisture sensor. We are going to use a capacitive soil moisture sensor in this project, which provides the readings as an analog signal.

The sensor has the following technical specifications:

  • A 3.3 V to 5.0 V supply range
  • A 3.3 V operating range
  • An analog output in the range of 1.5 V to 3.3 V
  • An operating current of 5 mA

Sensors from other manufacturers might differ slightly in these specs. Datasheets are usually provided by the vendor you buy the hardware from. We'll now start off by assembling the circuit.

Assembling the circuit

We only need some cables, the sensor itself, and a breadboard to begin with. Depending on the manufacturer of the sensor, the labels on the port of your sensor might differ. The one I use has the following labels:

  • AOUT (short...

Reading water level sensor data

As we plan to have a water tank later in the chapter, it will be beneficial to have a water level sensor, so we can tell when the tank is empty. We'll start off by adding the sensor to our existing circuit. Follow these steps to do so:

  1. Connect pin A4 from the Arduino with F22 on the breadboard using a jumper cable.
  2. Connect pin D3 from the Arduino with F21 on the breadboard using a jumper cable.
  3. Connect J22 on the breadboard with the S (Signal) port on the sensor using a jumper cable.
  4. Connect J21 on the breadboard with the + (VCC) port on the sensor using a jumper cable.
  5. Connect - GND from the sensor with GND on the power bus.

The result should now look like the following figure:

Figure 4.8 – Water level sensor – image is taken from Fritzing

After assembling this, we can continue to also create a small library for this sensor.

Writing a water level sensor library

There are...

Controlling a buzzer

We are going to write a very simple buzzer library. We only want the buzzer to make any sound, regardless of the pitch. We start off by adding the buzzer to the circuit. To do so, follow these steps:

  1. Connect D4 from the Arduino to A31 on the breadboard using a jumper wire.
  2. Use a 100 Ohm resistor to connect E31 with G31 on the breadboard.
  3. Connect the VCC pin from the buzzer with J31.
  4. Connect the GND pin to GND on the power bus.

The circuit should now look like the following figure:

Figure 4.11 – Buzzer – image taken from Fritzing

As we have now added the buzzer to the circuit, we can now start to write our library.

Writing a buzzer library

The buzzer library will have two functions: Configure(), which sets up the pin, and the Beep() function, which will make the sound.

We start off by creating a new folder named buzzer inside the Chapter04 folder. Inside the new folder, create a file named...

Controlling a pump

As pumps tend to draw more current than simple sensors, we are not going to power the pump directly through a GPIO port. Drawing too much current could permanently damage the Arduino. So, we will use an external power supply and a relay to power the pump. Before we start assembling the circuit, let's have a brief look at how relays work.

Working with relays

A relay that is used for microcontroller projects typically comes mounted on a board, which typically has six ports. It has three input ports: VCC, GND, and Signal. It also has three output ports: normally open, common, and normally closed.

When a high signal is given, the current flows between normally open and common.

When a low signal is given, the current flows between normally closed and common.

As we now know how to use a relay, we can continue to add the new components to our circuit. To do so, follow these steps:

  1. Connect the GND pin from the relay to GND on the power bus using...

Watering your plants

We are now going to utilize every component we created in the past sections. Putting everything together, we will be building a completely automated plant watering system.

To start off, we need to create a new folder named plant-watering-system inside the Chapter04 folder. Inside the new folder, create a new main.go file with an empty main() function inside. The final project structure should now look like the following:

Figure 4.17 – Project structure for plant watering system

Now, inside the main function, follow these steps:

  1. Initialize the ADC interface:
    machine.InitADC()
  2. Initialize a new soilSensor:
    soilSensor := soil.NewSoilSensor(18000, 34800, machine.
                  ADC5, machine.D2)
    soilSensor.Configure()
  3. Initialize a new waterLevelSensor:
    waterLevelSensor := waterlevel.NewWaterLevel(7000,
            ...

Summary

In this chapter, we learned how to read sensor values using the ADC interface. We also learned how the ADC interface translates voltage to digital values, and then we utilized this knowledge to write the soil moisture sensor library.

We then wrote the water level sensor library by utilizing the knowledge we gathered in the first project of this chapter. Then we learned how to use a buzzer and wrote a very simple library that enables us to let a buzzer create warning sounds. After that, we learned how relays work and utilized this knowledge to control a pump using a library we wrote. At the end of this chapter, we put all the libraries in a single project and only had to add a small amount of control logic to build the automatic plant watering system.

In the next chapter, we are going to learn how to use supersonic sensors and how to control seven-segment displays.

Questions

  1. Why are the water level sensor and soil moisture sensor not permanently powered?
  2. When is the circuit between normally open and GND closed in a relay?

References

The Capacitive Soil Moisture Sensor fritzing part was part of a collection from the following repository: https://github.com/OgreTransporter/fritzing-parts-extra

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Creative DIY Microcontroller Projects with TinyGo and WebAssembly
Published in: May 2021Publisher: PacktISBN-13: 9781800560208
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Tobias Theel

Tobias Theel works as the Technical Lead and DevOps for a German FinTech startup fino and since 2020 he has also started working for RegTech startup, ClariLab, as Lead Software Engineer. Being a software architect and an expert for Go and TinyGo alongside C# and Java, he is also iSAQB certified. Theel is a highly enthusiastic community contributor and is among the top 10% responders in C# and Unity3D as well as top 20% responders in .NET, Go, and Visual Studio on StackOverflow. When not programming for fino or ClariLab, he can be found developing games, mainly at game jams such as the Ludum Dare Jam, where he develops games from scratch within 72 hours. As an active speaker at tech talks and a participant for numerous hackathons, Theel loves to share his knowledge of software development with fellow enthusiasts.
Read more about Tobias Theel