/ / navigation

Digital in-Digital out
Digital in -Analog out
Analog in-Digital out
Analog in-Analog out
Serial
Sensors

/ / digital in-digital out



Buttons
LEDs
Motors



These little switches are a 1/4" on each side, and can plug directly into a breadboard. These mechanical devices have 4 legs, which may make you think that there are 4 wires that are switched on and off, but in fact, two on each side are actually connected together inside. So really, this switch is just a 2-wire switch.
buttonlegsdiag.jpg
Normally, the two wires are disconnected (normally open) but when you press the little button on top, they are mechanically connected.
pushbuttons.gif

To get the buttons to sit better in the protoshield, you may want to straighten out the legs (just squish them with a pair of pliers) so that they look like the button on the left.
buttonlegs.jpg

Adding the Microcontroller

Now connect the led and the switch to the chip:
NO Push button

The Arduino chip has uncommitted inputs. That is, when an I/O pin is not connected and acting as an input, it cannot be assured to be either HIGH or LOW. Pull-up and pull-down resistors are needed to commit the input to the non-active (open) state for switches.



  • When the button IS NOT pressed (open), P10 will sense Vss (0V, LOW, 0) because it is pulled-down to Vss.

  • When PB1 IS pressed (closed), P10 will sense Vdd (5V, HIGH, 1) making it Active-High.


/* using a push button*/
int LED1=8;
int BTN1=10;
void setup(){
    pinMode(LED1,OUTPUT);
    pinMode(BTN1,INPUT);
}
 void loop(){
 if (digitalRead(BTN1)==0){
        digitalWrite(LED1,LOW);
     } else if  (digitalRead(BTN1)==1){
        digitalWrite(LED1,HIGH);
     }
 }


This code can be rewritten:
/* using a push button*/
int LED1=8;
int BTN1=10;
void setup(){
    pinMode(LED1,OUTPUT);
    pinMode(BTN1,INPUT);
}
 void loop(){
 if (!digitalRead(BTN1)){
        digitalWrite(LED1,LOW);
     } else if  (digitalRead(BTN1)){
        digitalWrite(LED1,HIGH);
     }
 }
or:
/* using a push button*/
int LED1=8;
int BTN1=10;
void setup(){
    pinMode(LED1,OUTPUT);
    pinMode(BTN1,INPUT);
}
 void loop(){
     digitalWrite(LED1,digitalRead(BTN1);  
 }




LEDs



LEDs are cool because they're tiny and long lasting (up to 10 years). Unlike incandescent bulbs, LEDs don't get hot. They also don't consume a lot of power.


LEDs come in many colors, brightnesses, sizes and shapes.

LEDs are polarized. The positive side (the side connected with the + side of the battery) is the longer leg and is referred to as the anode. The shorter leg, the negative side, is called the cathode. You won't hurt the LED if you accidentally plug it in backwards. It just won't work.


The color of an LED is determined by the semiconductor material and not by the package (plastic body). Different colored LEDs require different amounts of voltage to light up. Red, green, and yellow LEDs typically require 2.2V-2.4V. Super-bright white and blue LEDs can require up to 3.4V.The optimum voltage is referred to as forward voltage (VF).

Most LEDs require 20mAh of current flowing from the battery. Most batteries supply more than 20mAh, so you need to use a resistor to limit the flow of current to each LED so it won't burn out.

VF Forward Voltage is also referred to as Voltage Drop. It is the minimum amount of voltage needed to light up an LED.

lV Luminous Intensity is the amount of light emitted from an LED in a particular direction. It is measured in millicandela (mcd). You can consider this property as brightess. The greater the millicandelas the brighter the LED.

IF Forward Current is the amount of current the LED uses.

Viewing Angle is the spatial distribution or spread of light. It is expressed in degrees that measure the width of the light beam. LEDs with a small viewing angle produce a more focused beam, and LEDs with larger viewing angles produce a softer more dispersed beam.

Type Color IF VF Typ VF Max Luminous Intensity Viewing Angle Wavelength
Standard Red 30mA 1.7V 2.1V 5mcd@10mA 60° 660nm
Standard Yellow 30mA 2.1V 2.5V 32mcd@20mA 60° 565nm
Super bright Red 30mA 1.85V 2.5V 500mcd@20mA 60° 660nm

UV LED's emit a light that you don't see, but that can damage your eyes. Be careful!


To use a resistor calculator you need the following information:
  • Supply voltage, the voltage from your power source
  • LED voltage Drop
  • LED Forward Current
  • Number of LEDs you want to connect
  • Connection— whether the LEDs are connected in series or in parallel


There are 3 ways to connect LEDs together:
  1. In parallel—each positive lead is connected to the positive lead of the next LED, and each negative lead is conected to the negative lead of the following LED.


  2. In series—each positive lead of an LED is connected to the negative lead of the next LED.


  3. Separate LED chains—chains of LEDs wired together in parallel can be combined all together in series


The decision to wire LEDs in series versus in parallel depends mainly on the following factors:
  1. the power source
  2. the number of LEDs
  3. whether you are using different colored LEDs together
Parallel—LEDs wired together in a parallel circuit work great if you need to string together a number of same-colored LEDs and have them powered by a small voltage source.

Electricity takes the path of least resistance If you have two different colored LEDs, one red with a forward voltage of 2.2V and the other green with a forward voltage of 2.4V, the current will flow through and light only the red LED. The green LED will be circumvented. It is possible to mix colored LEDs with the same specifications in a parallel circuit, but often some of the LEDs end up appearing dimmer than the others.

When LEDs are connected in parallel, the voltage across each LED remains the same, and the current is divided between them.

A circuit with a 3V battery and a resistor will light up 2 red LEDs with forward voltages of 2.2V when wired in parallel. Each LED will receive its 2V and the current will be dispersed between the two LEDs—therefore, you'll drain your battery more quickly than if you had wired them in series.

When resistors are connected so that the same voltage is applied across each resistor, the resistors are said to be connected in parallel. The single equivalent resistance R of three parallel resistors can be determined from:

1/Rtotal=1/R1+1/R2+1/R3

A connection of two resistors in parallel is called a current divider



In series— When LEDs are connected in series, the voltage is divided equally across each LED, and the current remains the same. If you had a series circuit with a 3V battery, a resistor and 2 red LEDs with forward voltages of 2.2V, neither would light up as each would receive only 1.5V.

When resistors are connected in a circuit so that the same current flows through each resistor, the resistors are said to be connected in series. It is possible to determine the resistance of a single resistor that has the same resistance as a number of resistors in series. This single equivalent resistance is just the sum of the individual resistances. For three resistors in series, the equivalent resistance R is given by:

Rtotal = R1 + R2 + R3

A series connection of two resistors is called a voltage divider


Connecting different colored LEDs

The power supply must be equal to or more than the sum of the voltage requirements for each LED and its resistor. If you want to mix colors and wire your LEDs in parallel, then you need to add the right resistor to each LED (not one for the entire circuit). This levels the amount of power required to illuminate each LED.

If your project requires the use of multiple LEDS and a small power supply, parallel wiring is the way to go.



Flashing LED

These LEDs contain an integrated circuit. They do not require resistors.

IR LED

These LEDs emit Infrared light that is invisible to the human eye

These are found in remote control devices

Piranha or High-Flux LED

These LEDs are square and have four leads: two are positive and two are negative.To distinguish the cathodes from the anodes you must refer to the LEDs' datasheet or create a simple ccircuit with alligator clips, a battery and a resistor.

Blinking an LED without delay



 /* Blinking LED without using delay*/

// LED should be the pin connected to an LED	
int led = ___;

int btn=____;
int btnVal;
int btnState;
boolean doYourThing=false;

// set value to the state of the LED before the program runs 
int val = 0; 

// store last time LED was updated
unsigned long previousMillis = 0; 

// interval at which to blink (milliseconds)
unsigned long interval = _____;           

void setup(){ 
    // sets the digital pin as output
    pinMode(led,OUTPUT); 
    // sets the digital pin as input
    pinMode(btn,INPUT); 
    btnState=digitalRead(btn);
}

void loop(){
   btnVal=digitalRead(btn);
   
   if(btnVal!=btnState){
       if(btnVal){
           doYourThing=!doYourThing;
       }
   }
   
   if(doYourThing){
      // check to see if the difference between the current time //and last time you blinked the LED is greater than
      // the interval at which you want to blink the LED.
      if (millis() - previousMillis > interval) {
  
          // remember the last time you blinked the LED
          //by setting previousMillis to millis()
          _________________
    
          // if the LED is off turn it on and vice-versa.
          if (val == LOW){
              val = HIGH;
      
          }else{
             val = ____;
          }
      }
  }else{
      val=0;
      
  }
    
    digitalWrite(led, val);
  }
}

Now add a button that acts as a switch:
 /* Blinking LED without using delay*/

// LED should be the pin connected to an LED	
int led = ___;


// set value to the state of the LED before the program runs 
int val = 0; 

// store last time LED was updated
unsigned long previousMillis = 0; 

// interval at which to blink (milliseconds)
unsigned long interval = _____;           

void setup(){ 
// sets the digital pin as output
 _____________ 
}

void loop(){
  // check to see if the difference between the current time and 
  //last time you blinked the LED is greater than
  // the interval at which you want to blink the LED.
  if (millis() - previousMillis > interval) {
  
  // remember the last time you blinked the LED
  //by setting previousMillis to millis()
   _________________
    
    // if the LED is off turn it on and vice-versa.
    if (val == LOW){
      val = HIGH;
      
    }else{
      val = ____;
    }
    digitalWrite(led, val);
  }
}



/ / Transistors



Transistors are used to amplify current. The leads are divided into BASE, COLLECTOR and EMITTER or GAIN.

The GAIN is simply the amount of amplification.





Collector current is always greater than base current, sometimes by many times.
If the collector current of a transistor is 0.12 amps and the gain is 40, what is the base current?


If the collector current of a transistor is 0.4 amps and the base current is 0.002 amps, what is the current gain?


If the collector current of a transistor is 0.5 amps and the gain is 100, what is the base current?




/ /Darlington Transistors and HIGH CURRENT DC DEVICES:



You may want to use the Arduino to control a DC powered device that draws higher current. A solution for this situation is to use an NPN Darlington Transistor designed for medium power linear switching applications. This component can be used to power devices such as motors, solenoids and fans, where the only necessary operation control operation is ON and OFF.

The base of the transistor (PIN 1) is connected to the Arduino output pin (D6) through a 1K OHM resistor. The emitter (PIN3) is connected to ground and the collector (PIN2) is connected to one end of the coil of the device being driven. The other end of the coil is connected to the +12VDC external power supply (the ground from this power supply is connected to a common ground with the Arduino - this is necessary for the transistor to function).

It is very important to put a diode across the coil of the device being powered to protect the control circuit from a potential voltage spike that can be created when current is released from the device being powered.



The Tip120 transistor can let you to control a high-current DC load such as a DC motor or an incandescent light from a microcontroller.

Parts
  • Solderless breadboard
  • hookup wire
  • Arduino Microcontroller
  • Light Emiting Diodes, LED
  • 10uF electrolytic capacitor
  • 10Kohm resistors
  • 10Kohm potentiometer
  • power diodes
    (for DC Motor version only)
  • DC power supply
  • TIP120
  • DC Motor - or - Incandescent lamp and socket


Prepare the breadboard
Conect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V and any of the ground connections:


Add a potentiometer
Connect a potentiometer to analog in pin 0. You'll use this later to control the output, whether it's a motor or a light.
arduino_pot_schem.png bb_pot.jpg

Connect a TIP120 transistor to the microcontroller.
The transistor allows you to control a circuit that's carrying higher current and voltage than the microcontroller. It acts as an electronic switch. The one you're we're using is an NPN-type transistor called a TIP120. It's designed for switching high-current loads. It has three connections, the base, the collector, and the emitter. The base is connected to the microcontroller's output. The high-current load (i.e. the motor or light) is attached to its power source, and then to the collector of the transistor. The emitter of the transistor is connected to ground.

The schematic symbol of an NPN transistor where B is the base, C is the collector, and E is the emitter.


Connect a motor

In the motor circuit below, there is a diode in parallel with the collector and emitter of the transistor, pointing away from ground. This is there to protect the transistor should the polarity of the current reverse (for example, if the motor was turned in reverse, or when it releases back voltage). Be sure to add the diode to your circuit correctly. The silver band on the diode denotes the cathode which is the tip of the arrow in the schematic.
arduino_tip120_schem.png
bboard_tip120_motor.JPG.jpeg

This circuit assumes you're using a 12V motor. Your motor requires 6-9V, so make sure to use the appropriate power supply. The motor and the microcontroller must share a common ground or the circuit won't work properly.

Connect a lamp
Likewise, the lamp circuit below assumes a 12V lamp. Change your power supply accordingly if you're using a different lamp. In the lamp circuit, the protection diode is not needed, since there's no way for the polarity to get reversed in this circuit:
arduino_tip120_lamp_schem.png
bboard_tip120_lamp.JPG.jpeg

Program the microcontroller
Here's a quick program to test the circuit:

 const int transistorPin = 9;    // connected to the base of the transistor

 void setup() {
   // set  the transistor pin as output:
   pinMode(transistorPin, OUTPUT);
 }

 void loop() {
   digitalWrite(transistorPin, HIGH);
   delay(1000);
   digitalWrite(transistorPin, LOW);
   delay(1000);
 }

Now that you see it working, try changing the speed of the motor or the intensity of the lamp using the potentiometer. Try this code:

 const int potPin = 0;           // Analog in 0 connected to the potentiometer
 const int transistorPin = 9;    // connected to the base of the transistor
 const int potValue = 0;         // value returned from the potentiometer

 void setup() {
   // set  the transistor pin as output:
   pinMode(transistorPin, OUTPUT);
 }

 void loop() {
   // read the potentiometer, convert it to 0 - 255:
   potValue = analogRead(potPin) / 4;
   // use that to control the transistor:
   analogWrite(9, potValue);
 }



Notes

For the motor users:
A motor controlled like this can only be turned in one direction. To be able to reverse the direction of the motor, an H-bridge circuit is required.



/ / Multiple LEDs and Transistors




Each digital pin on the Arduino has an internal pull up resistor that can be turned on and off using the digitalWrite() command when it is configured as an output. When it is HIGH, 5V sent to the pin and can deliver 40mA of current. This is adequate to power an LED, but most devices that you will want to power will require more current. A device that is trying to draw more current than this can cause the Arduino to shutdown and could easily damage the chip. There are many methods to increase the current that can be sources on an output pin. When a relatively small amount of additional current is needed, a 2N3904 NPN Transistor (or any similar type) can be used to accomplish this. In this example, you will connect four LEDs to one output pin using this simple transistor circuit.









Sources:
http://itp.nyu.edu/physcomp/Labs/
Building Wireless Sensor Networks [Kindle Edition] Robert Faludi
Open Softwear
Fashioning Technology: A DIY Intro to Smart Crafting (Craft: Projects) by Syuzi Pakhchyan
www.kpsec.freeuk.com/trancirc.htm http://www.technologystudent.com/elec1/transis1.htm http://www.allaboutcircuits.com/vol_3/chpt_4/1.html