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

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.

Previous PageNext Page
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