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 1: Getting Started with TinyGo

In my opinion, Go is easy to learn, easy to read, and easy to write. The language is not overloaded with fancy features but rather focuses on being concise. The built-in concurrency, fast compile times, high execution performance, and rich standard libraries make a great mix for an awesome language. This is why I want to take you on a journey from very basic high-level Go programs to the depths of microcontrollers utilizing the full power of TinyGo.

In this chapter, we are going to set up TinyGo and learn how to get code completion to work in VS Code and different editors. After this is done, we will have a look at the Arduino UNO and its technical specifications. We are going to compare TinyGo with Go and talk about what makes TinyGo special compared to other languages on microcontrollers. At the end of this chapter, we will write, compile, deploy, and run our first TinyGo program on a real microcontroller. Having all these topics covered...

Technical requirements

In order to continue, you need to have the following:

  • Go must be installed
  • GOPATH must be set up
  • Git must be installed
  • An Arduino Uno, preferably the Rev3 Edition but you can also use other Arduino Uno boards

You can find all code examples from this chapter in the following GitHub repository: https://github.com/PacktPublishing/Creative-DIY-Microcontroller-Projects-with-TinyGo-and-WebAssembly/tree/master/Chapter01

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

Understanding what TinyGo is

TinyGo is an independently written compiler, with its own runtime implementation. It is intended to be used for microcontroller programming, WebAssembly (WASM), and CLI tools. TinyGo heavily makes use of the LLVM infrastructure to optimize and compile code to binaries that a microcontroller can understand.

The first release of TinyGo (v0.1) was published on February 1, 2019 on GitHub. Since then, the project has quickly implemented lots of features and never stopped adding support for more microcontrollers, sensors, displays, and other devices.

On February 2, 2020, TinyGo announced that it is now officially a Google-sponsored project. This was a big step for the complete project.

How TinyGo works

The TinyGo compiler uses a different set of steps than other languages to transform Go source code to machine code. We will not be going into the details though, but let's take a look at an overview of the compiler pipeline:

  1. We write the...

Setting up TinyGo

The easiest way to install TinyGo and all its dependencies is to follow the Quick Start Guides for Linux, macOS, Windows, and Docker at the following link: https://tinygo.org/getting-started/.

As these guides cover important parts, I will only cover the Quick Start part for x64-based architectures and only for Debian-based operating systems such as Ubuntu for Linux.

The first thing to do before we start the setup is to check the latest version of TinyGo. To do so, go to https://github.com/tinygo-org/tinygo/releases and check for the newest release. Now, keep this information written down somewhere or memorize it as we'll be using it later.

Installing on Linux

The following steps cover installing TinyGo on a Linux derivate, which is based on Debian:

  1. We use the following command to download the deb package from GitHub and install it using dpkg:
    wget https://github.com/tinygo-org/tinygo/releases/download/v0.15.0/tinygo_0.15.0_amd64.deb
    sudo dpkg...

Understanding IDE integration

Having a properly set up IDE is truly a blessing as we benefit from its features of code completion, functional linting, and so on. This way, we do not have to investigate the source code or documentation for every function we want to call.

In this section, we will look at the process of integrating TinyGo into VS Code, Goland, and other editors. This enables us to choose whatever editor we prefer to use.

VS Code integration

VS Code offers an extension system, which makes it easy to integrate the Go and TinyGo toolset into the IDE. We are going to install the Go Extension, which offers support for the Go programming language. Afterward, we are going to install the TinyGo extension, which brings TinyGo support.

The Go extension

We install the Go extension using the Extensions view using the following steps:

  1. Open the Extensions view either by clicking on the Extensions icon or pressing Ctrl + Shift + X.
  2. Search for Go.
  3. Select...

The Arduino UNO

The Arduino UNO is one of the most popular boards out there. It is powered by an 8-bit ATmega328P microcontroller, and as of the writing of this book, there are lots of derivates from the original Arduino UNO boards. Let's get to know it better in the following subsections.

Getting to know the technical specifications

As you can see in the following table, the ATmega328P has only 16 MHz and 32 KB Flash memory. Standard Go produces a Hello World program of about 1.2 MB, which would not even fit on this microcontroller. So, we are working with very limited hardware here, but you will see that this is sufficient to build amazing projects.

Here is a brief look at the technical specs of the Arduino UNO:

Table 1.1 – Technical specifications

Note

Consider the DC current per I/O pin of 20 mA as an upper limit. You should not exceed this limit to prevent damaging your microcontroller.

Let's have a look at the pinout next...

Checking out the Hello World of Things

A Hello World program is the typical way to start the journey in a new programming language. A Hello World program on a microcontroller looks a bit different compared to a normal Hello World program. We are going to write a Hello World program to let the built-in LED blink. Let's get started!

Getting the requirements ready

To get started with our program, we need the following:

  • An Arduino UNO
  • One USB cable to connect it to your computer

Preparing the project

Follow these steps closely for your project:

  1. Create a new folder named ch1 in the root of your project.
  2. Inside the folder, we need to create a folder named hello-world-of-things and inside it, we are going to create a new main.go file.
  3. Your structure should now look like the following:

Figure 1.12 – The project structure

As we have now prepared our project, we can go on and write our first program.

Programming...

Summary

We have learned what TinyGo actually is, how it differs from standard Go, we have acquired basic knowledge about the Arduino UNO itself, how to set up TinyGo, how to set up IDE integration, and finally, wrote and flashed our first program onto real hardware and made an LED blink with our code. Isn't that an interesting start?

We are going to build a traffic light controller system in the next chapter.

Questions

  1. Which command can be used to find out the needed environment variable values for the IDE integration?
  2. Which command can be used to flash a program onto a microcontroller?
  3. Why do we have to sleep a certain amount of time when giving voltage or taking voltage away from the LED?
  4. How would you let the LED blink S-O-S in morse code?
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