Creative DIY Microcontroller Projects with TinyGo and WebAssembly
eBook - ePub

Creative DIY Microcontroller Projects with TinyGo and WebAssembly

  1. 322 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Creative DIY Microcontroller Projects with TinyGo and WebAssembly

About this book

Explore embedded programming, and get hands-on with real-world embedded projects relating to IoT, low-powered devices, and other complex systems using TinyGo and WebAssemblyKey Features• Build creative embedded apps with TinyGo using low-powered devices and microcontrollers• Understand the practicality involved in integrating hardware and sensors while programming them using TinyGo• Use TinyGo in modern browsers to display embedded applications' statistics on WebAssembly dashboardsBook DescriptionWhile often considered a fast and compact programming language, Go usually creates large executables that are difficult to run on low-memory or low-powered devices such as microcontrollers or IoT. TinyGo is a new compiler that allows developers to compile their programs for such low-powered devices. As TinyGo supports all the standard features of the Go programming language, you won't have to tweak the code to fit on the microcontroller. This book is a hands-on guide packed full of interesting DIY projects that will show you how to build embedded applications. You will learn how to program sensors and work with microcontrollers such as Arduino UNO and Arduino Nano IoT 33. The chapters that follow will show you how to develop multiple real-world embedded projects using a variety of popular devices such as LEDs, 7-segment displays, and timers. Next, you will progress to build interactive prototypes such as a traffic lights system, touchless hand wash timer, and more. As you advance, you'll create an IoT prototype of a weather alert system and display those alerts on the TinyGo WASM dashboard. Finally, you will build a home automation project that displays stats on the TinyGo WASM dashboard. By the end of this microcontroller book, you will be equipped with the skills you need to build real-world embedded projects using the power of TinyGo.What you will learn• Discover a variety of TinyGo features and capabilities while programming your embedded devices• Explore how to use display devices to present your data• Focus on how to make TinyGo interact with multiple sensors for sensing temperature, humidity, and pressure • Program hardware devices such as Arduino Uno and Arduino Nano IoT 33 using TinyGo• Understand how TinyGo works with GPIO, ADC, I2C, SPI, and MQTT network protocols• Build your first TinyGo IoT and home automation prototypes• Integrate TinyGo in modern browsers using WebAssemblyWho this book is forIf you are a Go developer who wants to program low-powered devices and hardware such as Arduino UNO and Arduino Nano IoT 33, or if you are a Go developer who wants to extend your knowledge of using Go with WebAssembly while programming Go in the browser, then this book is for you. Go hobbyist programmers who are interested in learning more about TinyGo by working through the DIY projects covered in the book will also find this hands-on guide useful.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Creative DIY Microcontroller Projects with TinyGo and WebAssembly by Tobias Theel in PDF and/or ePUB format, as well as other popular books in Computer Science & Hardware. We have over one million books available in our catalogue for you to explore.

Information

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, you will have learned how to write, build, and run programs on microcontrollers.
In this chapter, we're going to cover the following main topics:
  • Understanding what TinyGo is
  • Setting up TinyGo
  • Understanding the IDE integration
  • The Arduino UNO
  • Checking out the Hello World of things

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 Go source code.
  2. This source code gets translated to Go SSA (Static Single Assignment).
  3. The Go SSA is transformed to LLVM IR by the TinyGo compiler package.
  4. The initialization code in the LLVRM IR is interpreted by the TinyGo interp packages. This step optimizes globals, constants, and more.
  5. The result is then optimized by some LLVM optimization passes (such as string to []byte optimization).
  6. The result is then again optimized by the LLVM optimizer.
  7. Next, some fixes are done by the compiler package.
  8. And as the last step, LLVM creates the machine code.
If this sounds complicated right now, don't worry – we don't have to take care of this process. TinyGo does all this for us. Now let's have a look at what makes TinyGo special compared to Go.

Comparing TinyGo to Go

TinyGo can compile some, but not all Go programs. Let's have a look at an example that can be compiled by both. Let's write a small Hello World program in Go—build it and check its size:
  1. This is the most minimal Hello World program I can currently think of:
    package main
    func main() {
    print("Hello World\n")
    }
    It does not need an external package such as fmt to print the line.
  2. I will be using Go 1.15.2 on an Ubuntu 20.01 operating system. To check your c...

Table of contents

  1. Creative DIY Microcontroller Projects with TinyGo and WebAssembly
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
  6. Chapter 1: Getting Started with TinyGo
  7. Chapter 2: Building a Traffic Lights Control System
  8. Chapter 3: Building a Safety Lock Using a Keypad
  9. Chapter 4: Building a Plant Watering System
  10. Chapter 5: Building a Touchless Handwash Timer
  11. Chapter 6: Building Displays for Communication using I2C and SPI Interfaces
  12. Chapter 7: Displaying Weather Alerts on the TinyGo Wasm Dashboard
  13. Chapter 8: Automating and Monitoring Your Home through the TinyGo Wasm Dashboard
  14. Appendix – "Go"ing Ahead
  15. Assessments
  16. Afterword
  17. Other Books You May Enjoy