APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Watch the blinking lights


2007/11/12

This simple project demonstrates the analog output capability of the Wiring.org I/O board.

All you need for this is the Wiring I/O board, some hookup wires, a breadboard, and two LEDS. A voltmeter makes it easier to check your wiring if you aren't sure of yourself, but isn't necessary.

I took the program from Wirings Sketchbook examples and modified it for this project.



// LED Fade 
// by BARRAGAN <http://barraganstudio.com>
// Modified by Tony Lawrence for two leds

// Demonstrates the use of PWM pins (analog output) by dimming 
// a light (LED) from off to maximum intensity and back.

// Created 5 February 2004
// Revised 7 May 2007

int value = 0;  // variable to keep the actual value 
int ledpin = 0; // light connected to analog pin 0
int ledpin2=5; // light connected to analog pin 5
int value2=0;  // hold values for pin 5

void setup()
{
  // nothing for setup 
}

void loop()
{
  value2=255;
  for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
  {
    analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
    analogWrite(ledpin2,value2);
    value2-=5;
    delay(30);     // waits for 30 milli seconds to see the dimming effect
  }
  value2=0;
  for(value = 255; value >=0; value-=5)   // fade out (from max to min)
  {
    analogWrite(ledpin, value);
    analogWrite(ledpin2,value2);
    value2+=5;
    delay(30);
  } 
  
  
}
 

If you compile this in Wiring and upload it to the board, it will start varying voltage to pins 0 and 5 of the analog outputs. These are the PWM (Pulse Width Modulation) pins in the upper right hand corner of the board. You can measure this with a volt meter (set to the 15V DC scale). Attach the negative lead of your meter to ground (two ground pins are available at the bottom right of the Wiring.org board) and touch the positive to pin 0 or pin 5 of the PWM pins. You'll see the voltage swinging from 0 to 5 volts.

Those pins actually work in the same way most dimmers do. They actually switch on and off thousands of times per second - this Wikipedia article gives the general idea.

In the book Making Things Talk, the PWM outputs are used to drive a meter based on reading air quality from an Internet web page.

Now let's hook up the LEDS. I used a standard breadboard, but you can use hookup wire and alligator clips too. Basically you want the short leg of each LED hooked up to the ground, and one of the LEDS hooked on its other leg to PWM pin 0, and the other LED connected to PWM pin 5. Simple enough?

I didn't have any hookup wire, so I broke up a couple of RS-232 RJ45 jacks and pulled out the wires.. hey, copper is copper and the male pins are the right size for a breadboard and the females fit reasonably well on the Wiring.org I/O board's pins.

If you hook up your voltmeter while the LEDS are connected, you'll probably see that it won't swing as high if you are powering the board through your computer's USB port. You can't drive a tremendous amount of outputs through USB - you need to hook up a real power supply.

The LED's aren't really dimming; they are actually turning on and off with a different voltage applied on the "on" cycle, but it happens too fast for your eyes to tell. You can experiment with different values for the delay - if you make it long enough, it won't look like dimming any more because the change is too slow. If you make the delay very short (5ms), the lights just seem to blink.

A voltmeter doesn't react so quickly - once it gets a high voltage, it tends to stay there for a bit and vice versa. However, you can certainly tell the difference between 5ms and 30ms.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Watch the blinking lights




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





There's no obfuscated Perl contest because it's pointless. (Jeff Polk )

Lawyer — One who protects us against robbers by taking away the temptation. (H.L. Mencken)












This post tagged: