Search icon CANCEL
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Creative DIY Microcontroller Projects with TinyGo and WebAssembly

You're reading from  Creative DIY Microcontroller Projects with TinyGo and WebAssembly

Product type Book
Published in May 2021
Publisher Packt
ISBN-13 9781800560208
Pages 322 pages
Edition 1st Edition
Languages
Author (1):
Tobias Theel Tobias Theel
Profile icon Tobias Theel

Table of Contents (13) Chapters

Preface 1. Chapter 1: Getting Started with TinyGo 2. Chapter 2: Building a Traffic Lights Control System 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 LED when a button is pressed

Until now, we have only used code to directly control hardware components. Let's now try to read the state of a button in order to control an LED. We will need the following components:

  • At least 6 jumper wires
  • One LED (the color does not matter)
  • One 220 Ohm resistor
  • One 4-pinned-button (push down button)
  • One 10K Ohm resistor

Now let's go on to build the circuit.

Building the circuit

The following circuit extends the one we previously built. So, if you still have the previous circuit assembled, you just have to add the button part. The next circuit consists of two component groups. The first group is used to control an LED, and the second group is used to read the button state.

Adding the LED component

We start off with the LED circuit:

  1. Place an LED with the cathode in G12 and the anode in G13.
  2. Use a 220 Ohm resistor to connect F13 with D13.
  3. Connect port D13 from the GPIO ports with A13 using a jumper wire.
  4. Connect F12 with the ground lane of the power bus using a jumper wire.

Adding the button component

Now we are going to add a button:

  1. Use a jumper wire to connect A31 with the positive lane of the power bus.
  2. Use a 10K Ohm resistor to connect the ground lane of the power bus with B29.
  3. Connect D29 with port D2.
  4. Place the push button with one pin in E29, one in E31, one in F29, and the last pin in F31.

Our circuit should now look similar to the following:

Figure 2.4 – The circuit – image taken from Fritzing

Figure 2.4 – The circuit – image taken from Fritzing

Note

Before we start to write the code for this circuit, we need to learn how these buttons work.

As the button will not work if you place it incorrectly onto the breadboard, let's have a look at the button again.

The 4 pins on the button are grouped into two pins each. So, two pins are connected to each other. Looking at the back of the button, we should be able to see that two opposing pins are connected to each other. So, the button won't work as expected when you place it rotated by 90°.

Programming the logic

Before diving into the code, we will create a new folder named light-button inside the Chapter02 folder and create a main.go file in it, with an empty main function, using the following:

Figure 2.5 – The folder structure for the logic

Figure 2.5 – The folder structure for the logic

Let's now look at the main function and the pull-up resistor.

The main function

We want to light the LED when the button is pressed. To achieve this, we need to read from a pin and check for its state using the following steps:

  1. Initialize the outPutConfig variable with PinConfig in PinOutput mode. This config is going to be used to control the LED pin:
    outputConfig := machine.PinConfig{Mode: machine.
                    PinOutput}
  2. Initialize the inputConfig variable with PinConfig in PinInput mode. This config is being used for the pin that reads the button state and therefore needs to be an input:
    inputConfig := machine.PinConfig{Mode: machine.PinInput}
  3. Initialize the led variable with a value of machine.D13, which is the pin we have connected to led:
    led := machine.D13
  4. Configure led as output by passing outputConfig as the parameter, which is the pin that is connected to the button:
    led.Configure(outputConfig)
  5. Initialize the buttonInput variable with a value of machine.D2:
    buttonInput := machine.D2
  6. Configure buttonInput as an input by passing inputConfig as the parameter:
    buttonInput.Configure(inputConfig)
  7. As we do not want the program to be terminated after checking the button state a single time, we use an endless loop to repeat and check forever:
    for {
  8. Check the current state of the button. It will be true if the button is pressed:
      if buttonInput.Get() {
  9. If the button is pressed, we light up the LED:
        led.High()
  10. We are calling continue here, so we do not execute the led.Low() call:
        continue
       }
  11. If the button is not pressed, we turn the LED off:
        led.Low()
    }

    Note

    Do not forget to import the machine package, otherwise the code will not compile.

Now flash the program using the tinygo flash command:

tinygo flash –target=arduino Chapter02/light-button/main.go

After successfully flashing, the LED should light up when you press the button.

The pull-up resistor

You may have wondered why we need a 10K Ohm resistor in the button circuit. The 10K Ohm resistor is used to prevent the signal/pin from floating. Floating pins are bad, as an input pin in a floating state is indeterminate. When trying to read a value from a pin, we expect to get a digital value – 1 or 0, or true or false. Floating means that the value can change rapidly between 1 and 0, which happens without pull-up or pull-down resistors. Here's some further reading on floating pins: https://www.mouser.com/blog/dont-leave-your-pins-floating.

As an alternative to the 10K Ohm external resistor, an internal resistor can be used.

Configuring an input pin to use an internal resistor is done as follows:

inputConfig := machine.PinConfig{
               Mode: machine.PinInputPullup
}

We have now learned how to control an LED using an input signal, which was given by a button. The next step is to build the traffic lights flow to control three LEDs.

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 €14.99/month. Cancel anytime}