Multiplexing Traffic Lights easily in a PIC microcontroller

Multiplexing Traffic Lights easily in a PIC microcontroller

Multiplexing is a important tool for improving the maneuverability of sequential hardware (such as traffic lights) and for reducing the number of pins required in a PIC microcontroller. In a previous post called Multiplexing Principles, this benefits were explained in further detail. Moreover, this post will implement a multiplexing traffic lights with a PIC microcontroller. In order to learn how to do it yourself, keep reading. At the bottom you find a form to download the code. Timer 0 and Timer 1 are used.

Before reading this, you will need to know how to setup MPLAB IDE and how to connect the PICkit to the PIC microcontroller. If not, check them out first. Check out also another projects for beginners in 8-bit PIC microcontrollers.

Requirements for Multiplexing Traffic Lights

Components and Devices

In this tutorial, all the components are Through Hole.

  • PIC16F684: 1 unit.
  • LED, any size and preferably red, yellow and green colors: 12 units.
  • Resistors:
    • 330Ω, 1/2 or 1/4W: 12 units.
    • 1KΩ, 1/2 or 1/4W: 4 units.
  • BJT Transistors:
    • 2n3904 or 2n3906: 4 units.
  • PIC16F684
  • TO-92 package

PIC microcontroller

For this tutorial, I will use the 8-bit microcontroller PIC16F684. This one is the very first one that I learned to program. Very practical too. Be sure to select Through Hole or ‘TH' version. Surface Mounted won't fit in the breadboard.

PIC6F684
PIC6F684

Download the datasheet here. I recommend that you print in reduced A4 all the datasheet because it will serve you as quick reference.

Tools and Machinery

  • Breadboard: 1 unit.
  • PICkit (3 or 4): 1 unit.
  • AC/DC Power Adapter to 5V DC, with at least 500mA: 1 unit.
  • Jumper or UTP Wires: various.
  • PICKIT 4

The reason behind Multiplexing

Multiplexing is attractive for displays, lights and other indicators. It reduces the physical pins required for the circuit and increases its expandability; thus, it becomes modular.

If you have read about the article, click here to read it first.

Schematics for Multiplexing Traffic Lights

Let's design the schematic for this circuit, and then implement it in the breadboard.

Schematic for the PIC microcontroller

The circuit requires a few assignments of pins. First, the PICkit pins and the 32768Hz Oscillator for Timer 1. This pins are reserved and they cannot be changed. Second, the 4 pins to indicate the state of the traffic lights (P1 -> P4), each one. Finally, 3 pins to indicate the state of the lights (RED, YELLOW, GREEN). For the information of the traffic light, we require 7 pins in total. Port C have general purpose pins, so let's use them first. Since Port C has only 6 pins, let's use the pin 2 of Port A for the state of GREEN.

Schematic for the Multiplexed Traffic Lights

In order to turn on and off each traffic light, one transistor is assigned to each one. The base of the transistor goes in series with a 1KΩ resistor and then connected to the pins P1->P4 of the PIC microcontroller. The LEDs (with the 330Ω resistor) are connected to its respective color. For example, R1, R5, R9 and R13 are all connected to pin RC4 and therefore creating the RED node. This is called a bus network.

Code for Multiplexing Traffic Lights

Initial Configuration

On MPLab, select ‘Production’ tab from the main menu and then clic ‘Set Configuration bits’. Select the following options:

C

Configuration Function

Pins

Let's determine the functionality of each pin, located in the config() function. Establish all the Port C and pin 2 from Port A as output with the registers TRISA and TRISC. Also disconnect the Analog-to-Digital and Comparators features using the registers as shown in the code below. A couple of screenshots were taken from the datasheet.

C

Timer 0 and Timer 1

Make sure that you have read how Timer 0 and Timer 1 are configured. Click here to read about timers.

C
C

Interrupts

Without this activation, no timer would be useful!

C

Global Variables

The variables where the information about the lights and the state of the sequence are stored. This has to accessed from anywhere in the program; some of them are going to be used by Timer 0 and Timer 1.

Every red and yellow light has their variable. Green light wasn't considered since they are the opposite of the red light, always. The turn and sequence variables are used in the timers to know the position of the loop.

The macros (#define) are used to give a nickname to the ports implemented to control the information of the lights and the traffic lights.

C

Functions Prototypes

Required by the compiler.

C

Interrupt Function

If you haven’t read about the Interrupt Sources or how to handle Functions in C, check them out first.

Create the function called void __interrupt(void) interruptFunction(). From this place, all the interrupts are handled. Specify the enable bit and flag bit in order to enter the function. In addition, write the Routine_Tmr0() to specify where it should go next if Timer 0 requires the attention.

C

Timer 0

In the beginning, all traffic lights are turned off and the information of the lights are loaded, which are RED, YELLOW and the opposite of RED which is treated as green. Finally, the traffic lights are lighted again and now they carry new information. This process repeats itself in each interrupt. Because it is so fast, you can't notice it.

C

Timer 1

Every second, the traffic light advances in the sequence of a typical traffic light.

C

Extra Coding

Don’t forget to add the While(1) in the Main function to make it run forever!

C

Download the code of the Multiplexing Traffic Lights

If you would like to see and read the whole code through, enter your name and e-mail in the form below to download the project. I promise that I won't send you spam; just relevant content to the blog. If you don't see any form below, please click here.

Download the Code

and subscribe to get updates from techZorro direct in your inbox.

    We won't send you spam. Unsubscribe at any time.

    Testing the Multiplexing Traffic Lights

    If you don't know how to compile and code a project into a PIC microcontroller, follow this link to my tutorial.

    If everything executed properly, you should see a message in the bottom of the Output window confirming that the programming process has been complete.

    Photo

    A picture of the circuit was taken as reference.

    Video

    Although in the video the LEDs appears to blink, to the human eye it is imperceptible.

    But it gets better. See the full tutorial on YouTube.

    Conclusion

    The schematic design and the writing of code are expressed in this post. Furthermore, a video and foto are shown so it gives evidence how the circuitry looks like and works.

    Resources

    • 8-bit microcontroller PIC16F684 datasheet. Link.
    • PIC16F684 cannot get ports RC3 RC2 RC1 RC0 to work as input. Link.
    • C Language: #define Directive (macro definition). Link.

    You have reached this far!

    Thank you for reading the blog post. Your comments and suggestions are welcomed. At the bottom of this page, leave a message or just say hi! The whole team of techZorro will appreciate it. Don't forget to share it on social media as well.

    techZorro’s Index of Content

    Click on the following link to browse likewise content in the blog in techZorro. This index will help you see what you are looking for in a bird’s eye view.

    techZorro's Newsletter!

    If you enjoyed this blog post, please subscribe to techZorro’s newsletter so you don’t miss any future blog posts!

    techZorro's Index of Content

    Keep Reading!

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Don't know how to start?

    Find out how you can learn step-by-step.

      Juditova

      Variable Frequency Drives: how this controller can transform induction motors forever

      Juditova         Juditova        
      Variable Frequency Drives: how this controller can transform induction motors forever           Variable Frequency Drives: how this controller can transform induction motors forever          
      Back 15 seconds
      Forward 60 seconds
      more
        Back 15 seconds
        Forward 60 seconds
        Currently Playing