Arduino projects1.docx

(5231 KB) Pobierz

Simple Labs' Quick Start Kit for Arduino - LED Interfacing - How To?

The world of LEDS

 

LEDs (Light Emitting Diodes) are the most commonly used of electronic components. They are everywhere – torches, displays, indicators, etc. Every project will eventually end up having atleast one led. The Official Arduino Boards and most of the Clones do come with an on-board LED which you can try with the default blink program found in the File->Examples->Basic menu of Arduino IDE.

 

So How to Connect a LED?

 

The Long of the LED is the Anode (Positive Terminal!) and the short led is the Cathode(Negative Terminal). 

 

http://1.bp.blogspot.com/-8-KNdPfCIbQ/T6TACkQ8t_I/AAAAAAAAAf8/FH20Bj8_jzE/s640/LED1.jpg

The Terminals of the LED

 

http://4.bp.blogspot.com/-CDxrYDMV09I/T6TAEHaS_eI/AAAAAAAAAgE/gkcKvxIEoWE/s640/LED2.jpg

 

http://3.bp.blogspot.com/-nM75l8j7W-I/T6TAF1DXsDI/AAAAAAAAAgM/pj3hSAfwq6M/s640/LED3.jpg

 

Place the LED as shown

 

 

Place a Current Limiting Resistor (too much current passing through an LED can burn it!) between the negative terminal of the LED and the '-'ve terminal on the power rail (we will soon connect this to the '-'ve (aka ground) of our Arduino Board!)

 

http://1.bp.blogspot.com/-e7rlXr-5kJ8/T6TAHeGmrdI/AAAAAAAAAgU/bcZv1YihYh8/s640/LED4.jpg

Take a wire from the pin marked 'GND' of your arduino and connect it to the '-'ve power rail  of the breadboard. Now all points of the '-' ve power rail will be connected to the ground of the arduino!

 

http://4.bp.blogspot.com/-jVKMt0JsVvI/T6TAJNaeITI/AAAAAAAAAgc/gJHh4cXWNOM/s640/LED5.jpg

Next, Connect a wire between the 11th pin of the arduino(yes the 11th pin and not the 13th pin!)  and the  positive terminal of the LED. This is going to be our control line for the LED

Programming to control the LED

So how can you control a LED? Well there are only 2 ways to control an LED. you can either switch it ON / OFF or you can control the intensity with which it glows (Very much like a fan!). So lets see how to do the first control - ON/OFF (we can call this digital control! Very much 0s & 1s)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Try the following code. [Blink.ino]

 /* 

  Blink  

  Turns on an LED on for one second, then off for one second, repeatedly.  

  This example code is in the public domain.  

  */ 

 void setup() {         

  // initialize the digital pin as an output. 

  // We have our LED connected to Pin 11 

  pinMode(11, OUTPUT);     // Sets the 11th pin as an Output pin

 }  

 void loop() { 

  digitalWrite(11, HIGH);  // set the LED on 

  delay(1000);       // wait for a second 

  digitalWrite(11, LOW);  // set the LED off 

  delay(1000);       // wait for a second 

 } 

 

Next, Lets see how to control the intensity of the LED.


The Intensity can be varied by controlling the voltage applied to the individual pins. If you take a look at the 11th PIN on the arduino, you would see a marking 'PWM' next to it (Remember we connected our LED to the 11th pin). The PWM pins in addition to generating digital HIGH / LOW signals can generate analog voltages between 0 & 5.

The PWM Pins [Pins 3,5,6,9,10,11] that can generate a PWM signal of 8-bit resolution.[8-bits can represent a maximum value of 255, and a 8-bit resolution here means that 5 volts is represented by 255 divisions. So if you want to generate 1 volt, you would use the value 51]

The analogWrite function will take a 8-bit numerical value as a parameter [called duty cycle] and produce an output voltage corresponding to this value. It will set the pin to generate a steady square wave of the specified duty cycle at roughly 490Hz frequency.

Finally, when using a pin in the PWM mode, we don't have to use the pinMode() function.

Try the following code with the same setup and see how the intensity increases and decreases...[Intensity.ino]

 /* 

  Intensity 

  Increases the Intensity of a LED from 0 to maximum and on reaching maximum

  starts decreasing back to 0

  */ 

  

 int intensity = 0;

 

 void setup() {         

 

 }  

 void loop() { 

   

   while(intensity < 255) // Check if intensity has reached maximum value, if yes then exit the loop

   {

     analogWrite(11,intensity);

     delay(25);

     intensity++;

   }

   

   while(intensity > 0) // Check if intensity has reached minimum value, if yes then exit the loop

   {

     analogWrite(11,intensity);

     delay(25);

     intensity--;

   }

     

 } 

 

 

Simple Labs' Quick Start Kit for Arduino - RGB LED Interfacing - How To?

The RGB LED

The RGB led aka the tricolor led is a led that can help generate a multitude of colors by mixing red, blue & green colors. Its more like 3 leds (red, green & blue) put together into a single led.

It has 4 pins with 1 of the pins being a common cathode and the other 3 pins acting as anodes for the 3 different colours. by varying the intensity of each of the 3 colours individually, we can generate various colours. This led is the same as 1 pixel of a LED TV!.

Here is how to wire it up

http://3.bp.blogspot.com/-JbriF93O5ic/T6YPFSqSsnI/AAAAAAAAAgw/zWJZ9j0pWf4/s400/RGB1.jpg

Pin Mappings of the RGB LED

 

http://3.bp.blogspot.com/-0aW-Jlvs2G0/T6YPGzpQKTI/AAAAAAAAAg4/Z0C5b0txvF0/s640/RGB2.jpg

Place resistor between the common cathode and the '-'ve terminal

 

http://3.bp.blogspot.com/-WkT4xhWGb4w/T6YPI7-XokI/AAAAAAAAAhA/f13IxFV9h-Q/s640/RGB3.jpg

 

 

 

 

Connect RED to Pin 11, Blue to Pin 10 & Green to Pin 9 on the Arduino (these are PWM pins)

Now try the following code first. This code is a normal digital control of all the three colors separately.[RGB_Blink.ino]

/*

  RGB_Blink

  Turns on each of the color spectrums for 4 seconds, repeatedly.

  */

 

void setup() {               

  // initialize the digital pins as an output.

  pinMode(11, OUTPUT);

  pinMode(10, OUTPUT); 

  pinMode(9, OUTPUT); 

}

 

void loop() {

  digitalWrite(9,LOW);

  digitalWrite(11, HIGH); 

  delay(4000);             

  digitalWrite(11, LOW);   

  digitalWrite(10, HIGH);

  delay(4000);             

  digitalWrite(10, LOW);   

  digitalWrite(9, HIGH);

  delay(4000);          

}

 


 

 

 

Next Lets get generating Colors, try the following code. Play around with the values and get yourselves comfortable.[RGB.ino]

/* RGB

 

 Sets some random intensity value to the various colours of...

Zgłoś jeśli naruszono regulamin