Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Traffic Lights With a Raspberry Pi and Windows 10 IoT Core

DZone's Guide to

Traffic Lights With a Raspberry Pi and Windows 10 IoT Core

This quick how-to is a great spin on the classic Raspberry Pi blinky app. Learn how to build your own streetlight with a Pi and Windows 10 IoT Core!

· IoT Zone ·
Free Resource

Most of beginner examples for Raspberry Pis introduce how to turn on and off LEDs. I wrote a slightly more complex starting example, but it has some inspiration from real life – my example simulates traffic lights. I’m an IoT newbie, but using Microsoft tools, it was actually easy to build this little example. This blog post is a short introduction about what I did.

Getting Started

To get started, we need some electronic parts (I expect you already have a Raspberry Pi with Windows 10 IoT Core installed):

  • 3 LED lamps (green, orange, red)
  • 3 resistors
  • Jumper wires.

The resistors you need depend on what LED lamps you buy. Ask the electronics store about the appropriate ones if you are newbie like me. They need to know that you are using a 3.3V power supply.

On the wiring side, things are actually so simple that even I was able to get it right on the first shot.

Connecting traffic lights to Raspberry Pi

After the wiring is done, it’s time to write some code.

Writing UWP Application

There are two options for UWP applications – a blank UWP application and the Background Application (IoT). What you prefer depends on what you want to do next. If you just want to try things out, then the blank UWP application is a good starting point, as you can later build a UI for your application.

Here I’m using the blank UWP application. After creating the project, add a reference to Windows IoT Extensions for the UWP. Open the code-behind file of MainPage.xaml and add the following code.

public sealed partial class MainPage : Page
{
    private IList<GpioPin> _pins = new List<GpioPin>();
 
    public MainPage()
    {
        InitializeComponent();
 
        Loaded += MainPage_Loaded;
    }
 
    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        Init();

        foreach (var pin in _pins)
        {
            pin.Write(GpioPinValue.High);
        }

        Task.Delay(10000).Wait();

        Stop();
    }

    private void Init()
    {
        var gpio = GpioController.GetDefault();
 
        if (gpio == null)
        {
            Debug.WriteLine("GPIO controller is missing!");
            return;
        }
 
        _pins.Add(gpio.OpenPin(17));
        _pins.Add(gpio.OpenPin(18));
        _pins.Add(gpio.OpenPin(27));
 
        foreach (var pin in _pins)
        {
            pin.Write(GpioPinValue.Low);
            pin.SetDriveMode(GpioPinDriveMode.Output);
        }
    }

    private void Stop()
    {
        foreach (var pin in _pins)
        {
            pin.Write(GpioPinValue.Low);
        }
    }
}


The code here is just to test that the wiring went well and everything works. The init() method initializes GPIO pins and adds them to a list. The stop() method turns off all LEDs. The foreach loop in MainPage_Loaded turns all LEDs on for 10 seconds. If we run the code and all LEDs light up for 10 seconds, then everything is okay.

The cryptic constants 17, 18, and 27 are actually GPIO pin numbers. Here is the reference image from the Raspberry Pi 2 & 3 Pin Mappings page by Microsoft.

Raspberry Pi 2 and 3 GPIO pin mappings

If there are problems wiring things together, then make sure you followed all the instructions accurately.

Simulating Traffic Lights

Let’s focus now on the primitive code needed to mimic traffic lights. I’m using a separate method for the traffic light cycle so I can later add another method to blink the LED lights. The play() method runs 10 traffic light cycles.

public sealed partial class MainPage : Page
{
    private IList<GpioPin> _pins = new List<GpioPin>();
 
    public MainPage()
    {
        InitializeComponent();
 
        Loaded += MainPage_Loaded;
    }
 
    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        Init();
        Play();
        Stop();
    }
 
    private void Play()
    {
        for(var i = 0; i < 10; i++)
        {
            TrafficLights();
        }
    }
 
    private void TrafficLights()
    {
        var red = _pins[0];
        var yellow = _pins[1];
        var green = _pins[2];

        // Start with red and process to green
        red.Write(GpioPinValue.High);
        Sleep(5000);
        yellow.Write(GpioPinValue.High);
        Sleep(2000);
        Stop();
        green.Write(GpioPinValue.High);
        Sleep(5000);

        // Blink green and switch to yellow
        green.Write(GpioPinValue.Low);
        Sleep(500);
        green.Write(GpioPinValue.High);
        Sleep(500);
        green.Write(GpioPinValue.Low);
        Sleep(500);
        green.Write(GpioPinValue.High);
        Sleep(500);
        green.Write(GpioPinValue.Low);
        Sleep(500);
        yellow.Write(GpioPinValue.High);
        Sleep(2000);
        Stop();
    }
 
    private void Sleep(int milliseconds)
    {
        Task.Delay(milliseconds).Wait();
    }
 
    private void Init()
    {
        var gpio = GpioController.GetDefault();
 
        if (gpio == null)
        {
            Debug.WriteLine("GPIO controller is missing!");
            return;
        }
 
        _pins.Add(gpio.OpenPin(17));
        _pins.Add(gpio.OpenPin(18));
        _pins.Add(gpio.OpenPin(27));
 
        foreach (var pin in _pins)
        {
            pin.Write(GpioPinValue.Low);
            pin.SetDriveMode(GpioPinDriveMode.Output);
        }
    }
 
    private void Stop()
    {
        foreach(var pin in _pins)
        {
            pin.Write(GpioPinValue.Low);
        }
    }
}


Wrapping Up

Using Windows 10 IoT Core and Visual Studio, it is easy to control LED lights connected to a Raspberry Pi. Furthermore, connecting everything together on the electronics side was very simple. Although the code here is primitive and noobish, it still works and is easy enough to our simple solution. Of course, when looking at the code above, I detected one repeating pattern that helps to generalize this solution. It will be the topic of my next post about Raspberry Pis and LED lights.

Topics:
iot ,windows 10 iot core ,raspberry pi ,led ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}