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

Building traffic lights

We know how to light up a single LED, and we also know how to light up an LED using a button input. The next step is to build a circuit using three LEDs and to write the code to light them up in the correct order.

Building the circuit

To build the circuit, we need the following components:

  • Three LEDs (preferably red, yellow, and green)
  • Three 220 Ohm resistors
  • Seven jumper wires

We start by first setting up the components using the following steps:

  1. Connect GND from the Arduino to any ground port on the power bus.
  2. Place the first (red) LED with the cathode in G12 and the anode in G13.
  3. Place the second (yellow) LED with the cathode in G15 and the anode in G16.
  4. Place the third (green) LED with the cathode in G18 and the anode in G19.
  5. Connect F13 with D13 using a 220 Ohm resistor.
  6. Connect F16 with D16 using a 220 Ohm resistor.
  7. Connect F19 with D19 using a 220 Ohm resistor.
  8. Connect F13 to Ground on the power bus using a jumper wire.
  9. Connect F16 to Ground on the power bus using a jumper wire.
  10. Connect F19 to Ground on the power bus using a jumper wire.
  11. Connect port D13 to A12 using a jumper wire.
  12. Connect port D16 to A12 using a jumper wire.
  13. Connect port D19 to A12 using a jumper wire.

Your circuit should now look similar to the following figure:

Figure 2.6 – The traffic lights circuit – image taken from Fritzing

Figure 2.6 – The traffic lights circuit – image taken from Fritzing

We have now successfully set up the circuit. Now we can continue to write some code to control the LEDs.

Creating a folder structure

We start off by creating a new folder named traffic-lights-simple inside the Chapter02 folder. Also, we create a main.go file inside the new folder and start off with an empty main function. Your project structure should now look like this:

Figure 2.7 - Folder structure for the circuit

Figure 2.7 - Folder structure for the circuit

Writing the logic

We have successfully set up our project structure to continue. We are going to implement the following flow:

RED -> RED-YELLOW -> GREEN -> YELLOW -> RED

This is a typical flow for traffic lights with three bulbs.

We are going to configure three pins as output, and afterward, we want to endlessly loop and light up the LEDs in this flow.

Inside the main function, we write the following:

  1. Initialize a new variable named outputConfig as PinConfig using the PinOutPut mode:
    outputConfig := machine.PinConfig{Mode: machine.
                    PinOutput}
  2. Initialize a new variable named redLED with the value machine.D13 and configure it as output:
    redLED := machine.D13
    redLED.Configure(outputConfig)
  3. Initialize a new variable named yellowLED with the value machine.D12 and configure it as output:
    yellowLED := machine.D12
    yellowLED.Configure(outputConfig)
  4. Initialize a new variable named greenLED with the value machine.D11 and configure it as output:
    greenLED := machine.D11
    greenLED.Configure(outputConfig)

We have now initialized our variables to act as output pins. The next step is to light up the LEDs in the correct order. We basically have four phases, which just need to repeat in order to simulate a real traffic light. Let's go through these one by one:

  1. We are going to handle the phases in an endless loop:
    for {
  2. For RED-Phase, turn on the red LED and wait for a second:
        redLED.High()
        time.Sleep(time.Second)
  3. For RED-YELLOW-Phase, turn on the yellow LED and wait for a second:
        yellowLED.High()
        time.Sleep(time.Second)
  4. For GREEN-PHASE, turn off the yellow and red LEDs and turn on the green LED and wait for a second:
        redLED.Low()
        yellowLED.Low()
        greenLED.High()
        time.Sleep(time.Second)
  5. For YELLOW-Phase, turn off the green LED and turn on the yellow LED, then wait for a second and turn off yellow again, so we can start cleanly with RED-Phase again:
        greenLED.Low()
        yellowLED.High()
        time.Sleep(time.Second)
        yellowLED.Low()
    }

The complete content of the function is available at the following URL:

https://github.com/PacktPublishing/Programming-Microcontrollers-and-WebAssembly-with-TinyGo/blob/master/Chapter02/traffic-lights-simple/main.go

Note

Don't forget to import the time and machine packages.

We have now assembled and programmed a complete traffic lights flow. The next step is to combine everything we have built to complete our project.

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