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 3: Building a Safety Lock Using a Keypad

We gained basic knowledge of using LEDs, GPIO ports, and resistors in the last chapter. We also learned how to handle input and output. In this chapter, we are going to build a safety lock using a keypad. We will be able to input a passcode in the keypad that triggers a servomotor to unlock a lock. This will be achieved by splitting up the project into individual steps and putting it all together at the end of the chapter.

After working through this chapter, we will know how to write information to the serial port and how to monitor this information. This is a great way to easily debug an application. Then, we are going to write our own driver for a 4x4 keypad, which can be used as passcode input in our case. This 4x4 keypad can also be used as controller input, or as input to start different parts of a program. With that covered, we are going to write the logic to control a servomotor. Servomotors can be used as a lock mechanism...

Technical requirements

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

  • One Arduino Uno
  • One 4x4 membrane keypad
  • One SG90 servomotor
  • One red LED
  • One green LED
  • 14 jumper wires
  • Two 220 Ohm resistors
  • A breadboard

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

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

Writing to the serial port

An easy way to debug your programs on a microcontroller is to write messages to the serial port. You can later use this technique to debug your program, by printing the current step or sensor values, for example.

Let's write a small program to see how writing to a serial port is done. We start by creating a new folder named Chapter03 in the project directory, and inside this new directory, we create another directory named writing-to-serial. Now we have to create a new main.go file and insert an empty main() function. The folder structure should now look like the following:

Figure 3.1 – The folder structure for writing to serial port

Now, follow these steps:

  1. We print the word starting followed by a space and print the word program followed by an \n:
    print("starting ")
    print("program\n")
  2. We endlessly loop, print Hello World, and sleep for a second:
    for {
      println("Hello...

Monitoring the serial port

As we are writing debug logs or other messages to the serial port, we need a convenient way to monitor these logs. An easy way to monitor the serial port on all operating systems is to use PuTTy.

Let's first look at how to install PuTTy on various platforms:

  • Linux: On Linux, PuTTy is available through apt. We can install it using the following command:
    sudo apt install putty

    Alternatively, we can find tar.gz here: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

  • MacOS: On Mac, PuTTy is available using brew. We can install it using the following command:
    brew install Putty
  • Windows: On Windows, we can download PuTTy from here: https://www.putty.org/. We simply download and run the .msi file.

As we have now installed PuTty, it is time to monitor our serial port:

  1. Make sure the program from the previous section is flashed on your microcontroller and the USB cable is plugged in.
  2. The next step is starting PuTTy...

Monitoring input from a keypad

In this section, we are going to read input from a 4x4 keypad and print the pressed button to the serial port. Since TinyGo does not have a driver for this keypad, we will look at how to create a driver. This will help you understand the process and you can use this knowledge when you need to use other unsupported hardware.

As part of this exercise, I have also followed the process of adding this to the TinyGo codebase and it should be supported in the future. We are going to start by learning how to connect the keypad. Then we will move on to writing a driver, and then we are going to have a brief look at how new drivers are added to TinyGo.

Building the circuit

We start off by assembling the circuit. We are going to need a 4x4 keypad and eight jumper wires. Although we could use jumper cables to directly wire the keypad to the Arduino ports, we are going to wire it through a breadboard. We are going to add more components to this in the upcoming...

Writing the driver

As we want to have reusable code, we are going to write a driver package for the keypad. The driver will provide an easy-to-use interface while hiding the more complicated implementation logic. Doing it this way, we can simply reuse the package in later projects even beyond the book. The official TinyGo drivers typically provide a constructor-like function that creates a new instance of the driver and a Configure function that takes care of initialization. We are also going to provide a similar API.

Just like in our previous projects, we are going to start by creating a new folder named controlling-keypad inside the Chapter03 folder. Then, we are going to create a main.go file with an empty main function. Also, we need to create a new folder named keypad and create a new file named driver.go, and then name the package keypad. Your project structure should now look like the following:

Figure 3.8 – Project structure for writing the driver...

Finding drivers for TinyGo

As of the time of writing, there are 53 devices supported by TinyGo. The driver we just wrote, which I am going to contribute to TinyGo, will support 54 devices. But where can we find drivers for devices that we want to use? The answer is simple: there is a repository for this purpose. You can find it at https://github.com/tinygo-org/drivers.

In the next chapter, we will learn how to use such drivers when using different types of displays.

Contributing drivers to TinyGo

The TinyGo community happily appreciates all contributions. If you develop a driver for a device and want to contribute it to TinyGo, you can follow these simple steps:

  1. Open an issue and explain what you want to add and how you plan to implement it.
  2. Fork the repository.
  3. Create a new branch based on the dev branch.
  4. Create a pull request.

You can find the contribution guidelines at the following link: https://github.com/tinygo-org/drivers/blob/release/CONTRIBUTING...

Controlling a servomotor

As we are now able to read the input to the keypad, the thing that is missing to build a safety lock is some kind of motor. For that case, we are going to use an SG90 servomotor. As of the time of writing, the timings on the Arduino Uno are not accurate enough to completely control the SG90 servomotor, but that is not a problem for our use case. We are just going to move the servo in one direction, which is clockwise. Also, there is currently no official driver for the SG90 servomotor, so we are going to write our own!

Understanding SG90 servomotors

SG90 servomotors are controlled by Pulse Width Moduluation (PWM). Basically, the SG90 reads inputs in a 50 Hz period. During this period, we can tell the servomotor to adjust itself to a certain angle by setting a signal for a certain amount of time. The signal length is called the duty cycle. After the duty cycle, we wait for the rest of the period. Depending on the duty cycle (the pulse width), the SG90...

Building a safety lock using a keypad

We now know how to read input from a keypad and how to control a servomotor. We are going to use this knowledge to build a safety lock that opens when the correct passcode has been entered through the keypad. As we wrote libraries to control the servo and read data from the keypad, we only need to write the logic to check a passcode and light up LEDs. We are going to let the red LED blink each time a key is being pressed. When we enter a wrong passcode, we light up the red LED for 3 seconds. When we enter the correct passcode, we light up the green LED for 3 seconds and trigger the servomotor.

Building the circuit

We are going to reuse the circuits we built in the previous sections of this chapter. As we already have a servo and the keypad wired, we just have to add the LEDs and the resistors.

To build the final circuit, follow these steps:

  1. Connect a GND port from the Arduino Uno with the GND lane on the power bus.
  2. Place a...

Summary

In this chapter, we have learned how to write messages to the serial port and how to configure PuTTy to monitor messages on the serial port. We have then used this knowledge to output keypresses on a keypad that we controlled using a driver that we wrote. During that procedure, we learned how to write drivers for devices that currently have no official drivers and also learned about the contribution process of the driver's repository from TinyGo.

Then we learned how to control a servomotor and wrote a library to do so. As the last step, we combined everything we learned in this chapter to build a safety lock that accepts a passcode to open up the lock. This knowledge can be very useful if you ever want to build a door lock or a flight control system, where you need to control servomotors. The keypad can also be used as a gamepad, where you use the keys as input. As a bonus, we also wrote two drivers that we can reuse in all upcoming projects after finishing the book...

Questions

  1. Having learned about the coordinate system we used for the keypad, what are the coordinates for key 3?
  2. In our final project, we checked whether the input is correct when the correct passcode length has been reached. How would you change the code to get it to check whether the passcode is correct when the key number has been pressed?
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