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 5: Building a Touchless Handwash Timer

In Chapter 4, Building a Plant Watering System, we learned how the ADC interface works, and we used that knowledge to write libraries for a capacitive soil moisture sensor and a water level sensor. We also wrote a small library to control a buzzer and learned how relays work and used that knowledge to control a pump using our code. Then we used all this knowledge to build an automated plant watering system.

In this chapter, we are going to build a touchless handwash timer. After working through this chapter, you will know how ultrasonic sound sensors work and how to measure distance with them. We are going to utilize this knowledge to create a sensor that recognizes a hand that is between 20 and 30 centimeters away from the sensor to start a timer. The timer will then be displayed on a 7-segment display. While implementing this, we will also learn about the MAX7219 chip and how to use it to control different display types.

In this...

Technical requirements

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

  • An Arduino Nano 33 IoT
  • An HC-SR04 sensor
  • An external power supply module
  • HS420561K 4-Digit 7-segment display common cathode
  • A MAX7219 or MAX7221 serial input/output common-cathode display driver
  • One 10,000 Ohm resistor
  • One 1,000 Ohm resistor
  • One 2,000 Ohm resistor
  • 2 breadboards
  • Jumper wire cables

Most of the components are part of the so-called Arduino Starter Kit. If you do not have such a set, they can be acquired at any electronics supply store.

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

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

Introducing the Arduino Nano 33 IoT

We have reached a point where the TinyGo support for the Arduino UNO has reached its limit. At the time of writing, it is not possible to resolve this shortcoming for the current and following chapters using an Arduino UNO. The cause of this is a missing Serial Peripheral Interface (SPI) implementation for the Arduino UNO. However, we will be able to overcome this in the near future, as there is a Pull request for that opened by me. Additionally, the Alf and Vegard's RISC (AVR) backend in the TinyGo compiler toolchain has some problems in the versions used by the current TinyGo version, and the code won't compile. So, let's take a look at another board, which is fully supported by TinyGo—the Arduino Nano 33 IoT. Compared to UNO, the Nano 33 IoT is a powerhouse. Here are its technical specifications:

  • Microcontroller: AMD21 Cortex®-M0+ 32bit low power ARM MCU
  • Radio module: U-blox NINA-W102
  • Operating voltage...

Learning to measure distances

If you have ever wondered how touchless soap dispensers or touchless blow dryers register that there is a hand beneath them, there is a good chance that they are using the HC-SR04 ultrasonic sensor. We are going to use this sensor to measure the distance between an object and the sensor. Let's begin with the HC-SR04 sensor.

Understanding the HC-SR04 sensor

The HC-SR04 sensor emits an ultrasound at 40k Hz, which travels through the air and bounces back if the emitted pulse collides with any object in its path. The sensor cannot be used as a detector for other ultrasound pulses, as it only registers echoes from the exact same pulse that it itself emitted. Typically, these sensors look similar to the one in the following photograph:

Figure 5.2 – The HC-SR04 sensor

This sensor has the following technical specifications:

  • It has a detection range from 2 to 400 centimeters.
  • It draws less than 2 mA current...

Using 4-digit 7-segment displays

A 7-segment display can be used for multiple purposes. One of them is to display times, which is exactly what we want to do in our final project. But how can we control them?

The 4-digit display has 12 pins: one pin for each digit (from 0 to 9), one pin for each segment, and a pin for the dot. So, to display anything, we have to send a high signal to the digit we want to set and then just set all pins to high, which we need to represent the character we want to display.

For instance, if we want to display the character of "1" in the fourth digit, we would set pin 4 and pins B and C to high.

To get a better understanding of this, take a look at the following diagram:

Figure 5.10 – A 7-segment display pinout

From the preceding diagram, you can see that pins 1 to 4 are being used to select the digit.

The 7-segment A-G pins are being used to control the segments and the dot pin is being used to set the...

Putting it all together

In our final project of this chapter, we are going to make use of everything we have learned in the preceding sections. We are going to use the ultrasonic distance sensor to recognize a hand movement in close proximity to the sensor. We are using the 7-segment display to count down from 20 to 0 and we are going to use a buzzer, to provide an additional signal, for the timer start and the timer end. In Germany, it is officially recommended that we wash our hands for at least 20 seconds, which is why we will also add a timer for 20 seconds. Putting all of this together, we will create a touchless handwash timer.

Before we start to write the code to control the hand wash timer, we need to add a buzzer. We can add this by following these steps:

  1. Put the GND pin of the buzzer in D53 and the VCC pin of the buzzer into D54. If that is too close together for your buzzer's pins, just put the buzzer in and wire the following two wires accordingly.
  2. Connect...

Summary

In this chapter, we have learned about the technical specifications of the Arduino Nano 33 IoT and how to calculate the distance between an object and an ultrasonic distance sensor. Additionally, we learned how the sensor works internally and wrote a library for it. We also learned that unit testing is supported in TinyGo and wrote some tests for the ultrasonic distance sensor library. Then, we learned how to use a MAX7219 serial interfaced display driver to control a 7-segment display, and we wrote a library for the MAX7219 and the 7-segment display. At the end of this chapter, we put all of the drivers into a single project and only had to add a small amount of control logic to build a touchless handwash timer.

In the next chapter, we are going to learn how to use 16x02 LCD and ST7735 TFT displays.

Questions

  1. Is it possible to draw 5V output from the Arduino Nano 33 IoT?
  2. Why do we divide pulseLength by 2 when calculating the distance to an object?
  3. Change the code so that the handwash timer counts from 120 to 0. Use three digits to display the remaining seconds.
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