IAN LANG ELECTRONICS

CIRC-01 is the easiest circuit to build and introduces the digital outputs to the user in making the large 10mm LED blink on and off. It introduces the user to the concept of the setup, the loop, digital high and digital low, and the delay function, as well as getting them used to the interface by which the microcontroller is programmed. The circuit and wiring diagram as shown in the book are below:

CIRC-01

Go Back

You might be wondering what that piece of paper is doing between the components and the breadboard. Well, and I kid you not, it's a map that they've produced to show you where the bits should go in the breadboard! Honestly, even if you've never picked up an LED before you can't go wrong. Once you've assembled the circuit if you plug it into the USB port you'll find the LED flashes before you've done anything else; and that's because it's on pin 13. Don't worry that's normal. Now you have to upload code to the chip, and this is done by the user interface. There's a step by step guide to doing it (see link left). Here's what the user interface looks like with the code for this circuit in the window:

Arduino Software

Those of you who are au fait with C (the programming language, not lightspeed) will grasp from this that it shares some common traits. The semi-colons after every command are annoying but they have to be there.

Void Setup is the part of the command that will run once when the power is applied. You use this to set starting conditions, often in simple structures you can leave it empty. You'll notice the curly brackets { }  that are at the start and end of the batch of code; they're also at the start and end of Void Loop. They demarcate just that- the start and end of a batch of code. Void Loop is the part of the programming that repeats- once it gets to the end, it goes back to the beginning. Let's go through:

 

pinmode (13, OUTPUT)      tells the chip that pin 13 is being used as an output

digitalWrite  (13, HIGH)       Sets pin 13 high and provides juice for the LED

delay (1000)                          makes the chip wait for 1 sec (1000 millisecs) before executing the next line

digitalWrite (13, LOW)        Sets pin 13 Low (no output) and so the LED goes off

delay (1000)                         makes the chip wait for 1 sec and then it goes back digitalWrite (13, HIGH).

 

We don't need a microprocessor to do this. We could do it more cheaply with a 555 Timer. What we can't do is control the brightness with a 555. But we can with this. Here's some more code:

 

/*

 Fading

 

 This example shows how to fade an LED using the analogWrite() function.

 

 The circuit:

 * LED attached from digital pin 9 to ground.

 

 Created 1 Nov 2008

 By David A. Mellis

 Modified 17 June 2009

 By Tom Igoe

 

 http://arduino.cc/en/Tutorial/Fading

 

 This example code is in the public domain.

 

 */

 

 

int ledPin = 9;    // LED connected to digital pin 9

 

void setup()  {

  // nothing happens in setup

}

 

void loop()  {

  // fade in from min to max in increments of 5 points:

  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);        

    // wait for 30 milliseconds to see the dimming effect    

    delay(30);                            

  }

 

  // fade out from max to min in increments of 5 points:

  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);        

    // wait for 30 milliseconds to see the dimming effect    

    delay(30);                            

  }

}

 

Here are two more command lines:   for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) sets up a loop. The value of the brightness of the LED depends on the value of the variable fadeValue, when it's 0 the LED is off, when 255 the LED is at maximum brightness. The curly brackets between this line and delay(30) are the commands that will be carried out by the loop, and they are to increase the LED brightness by the value of fadevalue and then wait. The loop will carry on until fadeValue = 255, and the increment of fadeValue is +5, so it will do it 51 times. this will take 1530 milliseconds (51 X 30) and so the duration of the loop and the time it takes the LED to get to maximum brightness is 1.5 secs or thereabouts. The command analogwrite is the one that causes this to happen, ledPin (which is set to pin 9 at the beginning) is told to output to the level of fadeValue. Of course this is not true analogue, it is 256 discrete steps, but that's enough to make it look analogue.

Once the 51 rounds of the first loop are done, the second can begin. Exactly the same thing happens, except that the increment of fadeValue is -5, and so in fact it's a decrease. The pin outputs less each round by five discrete steps, and the LED gets dimmer. Once this loop is done, the whole shebang starts again.

Blinky lights are all very well but they don't have much real application. Here's some code to simulate traffic-light action:

 

 

int yellowpin = 9;    //amber is pin 9

int redpin = 12; // red is pin 12

int greenpin = 11;// green is pin 11

void setup()  {

 

 }

 

void loop()  {  

 digitalWrite (12, HIGH);//red on

 delay (5000);

 digitalWrite (9,HIGH);// amber on

 delay (2000);

 digitalWrite (12,LOW);//red off

 digitalWrite (9,LOW);// amber off

 digitalWrite (11,HIGH);// green on

 delay (5000);

  digitalWrite (11,LOW);// green off

   digitalWrite (9,HIGH);// amber on

    delay (5000);digitalWrite (9,LOW);// amber off

     

}

 

I wrote and tested this and I promise you it works very well indeed. You don't get a yellow LED in the kit, they're cheap enough at Maplin or just use the big 10mm to simulate amber. You connect the LEDs in just the same way as you do with the first blinky;  red anode to pin 12, green anode to pin 11 and yellow anode to pin 9, and all cathodes to a common ground via a 560 ohm resistor each. The yellow one might require a slightly smaller resistor if it's too dim. The first thing that happens is pin 12 goes on, so does the red LED. 5 seconds later yellow comes on and you get red and amber. 2 seconds after that red and amber go off and green comes on. 5 seconds after that, green goes off and amber on, 5 seconds later amber goes off. The command goes back to the top meaning red comes on and the whole thing starts again. Simple but effective, just the way we like 'em.

 

More > Copy & Paste Code from Here: