LED traffic lights (3)

It is a simplification of tutorial 1.21.
There are a lot of situations, where you connect a switch at the Arduino. And therefore has the Arduino already built-in pull-up resistors. It’s not necessary to use an external resistor.
I will show you, how you can use the internal pull-up resistors.

Task:
Build a traffic lights, but trigger the action with a switch!

The circuit:

For this tutorial, wee need the following components:

  • LED red (i.e. 2V, max. 20 mA)
  • LED yellow (i.e. 2V, max. 20 mA)
  • LED green (i.e. 2V, max. 20 mA)
  • 3x resistors (min. 150 Ohm)

The LEDs are a standard components. We use  LEDs with 2V and a maximum current of 20 mA. Please, look tutorial 1.0 or 1.1  how to calculate the resistor. It’s the seem.

1.3.1-LED

In this tutorial, we use four pins – pin 9 to control the red LED, pin 10 to control the yellow LED, pin 11 to control the green LED and pin 4 to control the switch.

Here is the wiring diagram:

1.3.1-LED-circuit

We start our Arduino software to create the first sketch.

/*
 LED traffic lights with switch
 playground2014.wordpress.com
*/

int pinred = 9;           // PinPort LED red
int pinyellow = 10;       // PinPort LED yellow
int pingreen = 11;        // PinPort LED green
int pinswitch = 4;        // PinPort Switch
byte stat = 0;            // save the state of the switch action

void setup()  { 
  pinMode(pinred, OUTPUT);  
  pinMode(pinyellow, OUTPUT);  
  pinMode(pingreen, OUTPUT);
  pinMode(pinswitch, INPUT);  
  digitalWrite(pinswitch, HIGH);     // activate the built-in pull-up resistor

  // to initialize the traffic ligths with red
  digitalWrite(pinred, HIGH);
  digitalWrite(pinyellow, LOW);
  digitalWrite(pingreen, LOW);
} 

void loop()  { 

  // let us start: the traffic ligths is red
  digitalWrite(pinred, HIGH);
  digitalWrite(pinyellow, LOW);
  digitalWrite(pingreen, LOW);

  do  {
   stat = digitalRead(pinswitch);
   delay(100); 
  } while (stat == 1);

  // attention, we will switch
  digitalWrite(pinred, HIGH);
  digitalWrite(pinyellow, HIGH);
  digitalWrite(pingreen, LOW);
  delay(1000); 

  // it's green
  digitalWrite(pinred, LOW);
  digitalWrite(pinyellow, LOW);
  digitalWrite(pingreen, HIGH);
  delay(2000);

  // attention, we will switch
  digitalWrite(pinred, LOW);
  digitalWrite(pinyellow, HIGH);
  digitalWrite(pingreen, LOW);
  delay(1000);

  //it's red
  // we start again (loop)
}

void setup() { … }
Here is one change: First, we define pin 4 as input. And the next command “digitalWrite(pinswitch, HIGH)” activate the built-in pull-up resistor at this pin. That’s all.

See also: http://arduino.cc/en/Tutorial/DigitalPins

void loop() { … }
It’s the same like tutorial 1.21.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s