Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Creative DIY Microcontroller Projects with TinyGo and WebAssembly

You're reading from   Creative DIY Microcontroller Projects with TinyGo and WebAssembly A practical guide to building embedded applications for low-powered devices, IoT, and home automation

Arrow left icon
Product type Paperback
Published in May 2021
Publisher Packt
ISBN-13 9781800560208
Length 322 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Tobias Theel Tobias Theel
Author Profile Icon Tobias Theel
Tobias Theel
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Chapter 1: Getting Started with TinyGo 2. Chapter 2: Building a Traffic Lights Control System FREE CHAPTER 3. Chapter 3: Building a Safety Lock Using a Keypad 4. Chapter 4: Building a Plant Watering System 5. Chapter 5: Building a Touchless Handwash Timer 6. Chapter 6: Building Displays for Communication using I2C and SPI Interfaces 7. Chapter 7: Displaying Weather Alerts on the TinyGo Wasm Dashboard 8. Chapter 8: Automating and Monitoring Your Home through the TinyGo Wasm Dashboard 9. Assessments 10. Afterword 11. Other Books You May Enjoy Appendix – "Go"ing Ahead

Lighting an external LED

Before we start to build a more complex circuit, let's begin with lighting up an external LED. As soon as this is working, we are going to extend the circuit step by step. We begin with a single red LED. Lighting up an external LED is a bit different compared to lighting up an onboard LED. We are going to need something on which we can place the LED, and we will need some wires as well as a basic understanding of resistors, which will help us to prevent the LED from taking damage. That is why we are going to look at each component one by one.

Using breadboards

Breadboards are used for prototyping, as they do not require you to directly solder components. We are going to build all our projects using breadboards.

A breadboard typically consists of two parts:

  • The power bus
  • Horizontal rows

Each side of a breadboard has a power bus. The power bus provides a + (positive) lane and a - (ground) lane. The positive lane is colored red and the ground lane is colored blue. The individual slots are connected inside the power bus.

The slots of a single horizontal row are also connected. A signal in one slot is also available in the next slot. Different horizontal rows are not connected, unless we put a cable in there to create a connection. Here's what a breadboard looks like:

Figure 2.1 – A breadboard – image taken from Fritzing

Figure 2.1 – A breadboard – image taken from Fritzing

Understanding LED basics

The Arduino UNO has an operating voltage of 5V, which is too high for most LEDs. So, we need to reduce the voltage to something our LEDs can handle. For that reason, we will be using 220 Ohm resistors to draw current from the line in order to protect the LED from damage. If you do not have 220 Ohm resistors, you can also use 470 Ohm as well; anything between 220 and 1K (1K = 1,000) Ohm will be fine.

If you want to really make sure that the resistor matches the needs of the LED, you can also calculate the resistor value as follows:

R = (Vs – Vled) / Iled

Where:

  • R is the resistor value.
  • Vs is the source voltage.
  • Vled is the voltage drop across the LED.
  • Iled is the current through the LED.

    Note

    LEDs have anode (+) and cathode (-) leads. The anode lead is longer.

    Different colors need to be driven with different voltages. When using the same resistors and voltages for the different LED colors, you will notice that some colors will be brighter compared to others.

Using GPIO ports

GPIO stands for General Purpose Input Output. That means we can use these pins for input as well as output for digital signals. We can either set a GPIO pin to High or Low, or read a High or Low value from the port.

Note

We should never draw more than a maximum of 40.0 mA (milliampere) from a single GPIO port. Otherwise, we could permanently damage the hardware.

Building the circuit

Now let's build our first circuit on the breadboard:

  1. Put a red LED in the G column of the horizontal rows. Put the cathode in G12 and the anode in G13.
  2. Connect F12 with the ground lane on the power bus.
  3. Connect F13 and E13 using a 220 Ohm resistor. (Anything between 220 and 1,000 Ohms is okay.)
  4. Connect Pin 13 from the GPIO ports to A13.
  5. Connect the GND port to the ground lane on the power bus.

    Note

    The descriptions on your breadboard might differ from the ones I am using. If that is the case, you'll need to build the circuit by checking the next figure.

The circuit should now look like the following:

Figure 2.2 – Image of the circuit – image taken from Fritzing

Figure 2.2 – Image of the circuit – image taken from Fritzing

Writing the code

We start off by creating a new folder named Chapter02 in our project workspace. This folder will be used for all parts of this chapter. Inside the Chapter02 folder, we create a blinky-external folder and create a new main.go file inside.

The structure should look like the following:

Figure 2.3 - Project structure for writing the code

Figure 2.3 - Project structure for writing the code

We import the machine and time packages and put the following code into the main function:

  1. Declare and initialize a variable named outputConfig with a new PinConfig in output mode:
    outputConfig := machine.PinConfig{Mode: machine.
                    PinOutput}
  2. Declare and initialize a variable named greenLED with a value of machine.D13:
    greenLED := machine.D13
  3. Configure the LED with the outputConfig instance we created earlier, by passing it as a parameter into the Configure function:
    redLED.Configure(outputConfig)
  4. We then loop endlessly:
    for {
  5. Set redLED to Low (off):
      redLED.Low()
  6. Sleep for half a second. Without sleeping, the LED would be turned on and off at an extremely high rate, so we sleep after each change in a state:
      time.Sleep(500 * time.Millisecond)
  7. Set the redLED to High (on):
      redLED.High()
  8. Sleep for half a second:
      time.Sleep(500 * time.Millisecond)
    }
  9. Now flash the program using the tinygo flash command using the following command:
    tinygo flash –target=arduino Chapter02/blinky-external/main.go

When the flash progress completes and the Arduino restarts, the red LED should blink at intervals of 500 ms.

Congratulations, you have just built your first circuit and written your first program to control external hardware! As we now know how to connect and control external LEDs on a breadboard, we can continue to build a more advanced circuit. Let's do just that in the next section.

You have been reading a chapter from
Creative DIY Microcontroller Projects with TinyGo and WebAssembly
Published in: May 2021
Publisher: Packt
ISBN-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.
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 $19.99/month. Cancel anytime
Banner background image