Amazon Shopping Mall

Sunday, March 31, 2013

Using a Switch to control an LED

 

What is a Switch?

A Switch can be any device that has two state 0 or 1 (on/off). This can be use to break and make connection in an electric circuit.It comes in different forms and the one you are seeing on this page is just a type of switch. Henceforth, for the purpose of this project, any switch at can be used.

Remember that a switch may directly be manipulated by a person as a control signal to an electric circuit or a system.

Setting Up the Switch on the Arduino board

Components

  • 1 Arduino 
  • 1 LED
  • 1 220 ohm resistor for the LED
  • 1 10K ohm resistor for the LED
  • 2 data wires

Images 





Codes for programming the Switch

Hi Everybody, my name is Elizabeth Yeboah and  this is project 3. Copy and paste this code in your sketch whiles following the video tutorial for the setup.

// The switch codes

int led = 13;                                               // my LED is connected to pin 13
int switchPin = 2;                                        // my switch is connected to pin 2
int switchState = 0;                                      // initialize the variable as 0
void setup() {                                              // Tell the arduino that pin 13 is to be treated as an OUTPUT 
pinMode(led, OUTPUT);                             //command(variable,mode);
pinMode(switchPin, INPUT);                       // Tell the arduino that pin 2 is to be treated as an INPUT
}
void loop() {
// set the variable to the value of the pin. This determines if the switch is on or off. //syntax //variableName = digitalRead(pin); switchState = digitalRead(switchPin); // Use an if statement to take an action. We compare the variable with the value of the switchPin.

if(switchState == 1){

//if the switch is ON
//turn the led ON using digitalWrite() by setting the mode to HIGH. This sends 5 volts out of the assigned pin

//digitalWrite(variableName, MODE);

digitalWrite(led, HIGH);

// if the switch is not on, then do the following action
}
else{

//turn the led OFF using digitalWrite() by setting the mode to LOW. This stops the 5 volts on the assigned pin

digitalWrite(led, LOW);
  }
}

Watch the Switch Video

Please if this tutorial was helpful, send me your comments. Thanks
By Lis Brown.

Blinking an LED using Arduino

Project 1

Setting Up your LED on the Arduino Board

Components

  • 1 Arduino
  • 1 LED
  • 1 220 ohm resistor (red, red, brown)
  • 1 piece of data wire


LED setup on the Arduino Board



Codes to use on the Arduino IDE

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

  This example code is in the public domain.
 */


// Pin 13 has an LED connected on most Arduino boards.

int led = 13;
int blinkTime = 1000;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
blinkMe();
}
//blinkme function

void blinkMe()
{
  digitalWrite(led, HIGH);
  delay(blinkTime);
  digitalWrite(led, LOW);
  delay(blinkTime);
}

Watch A Short Video

If this tutorial was helpful, please do post your comments. Thanks 
By Lis Brown.

Using The Potentiometer to Blink an LED

Project 2

What is Potentiometer?
Potentiometer is a three-terminal resistor which is used to control electrical devices as well as measure the potential(voltage) of a particular electric device.

So in this project, the potentiometer is been used to regulate the amount of current or voltage that the LED can receive in order to light. In our codes, we declared the map function to a range to which the potentiometer can lie within.  

Setting Up the Potentiometer

Components 

  • 1 Arduino
  • 1 LED
  • 1 220 ohm resistor (red red brown)
  • 1 10K ohm potentiometer
  • 2 data wires
Always note that the red wire is for power , the black for ground and the white or yellow for data/signal.

 Images 



Setup for Potentiometer and  LED

The Codes to use on your Arduino IDE

Hi Everybody, this is Elizabeth Yeboah and  this is project 2. Copy and paste this code in your sketch whiles following the video tutorial for the setup.

// Using the Potentiometer codes

/*analogRead()
This is an example of analogRead(). analogRead() listens for a range of voltages (5v - 0v) on an assigned analog port.
This code outputs the data to an led and affects the LEDs brightness using analogWrite().
syntax
analogRead(analog pin); 
*/

//DECLARE YOUR VARIABLES AND PIN NAMES
//Assign a variable name to a pin number.
//Assign the variable name led0 to analog/digital pin 3
//syntax:
//datatype variableName = value;

int led0 = 3;
int variableResistor = A0;        //declare a variable called variableResistor and assign it to analog input A0.
int analogValue;                 //declare a variable called analogValue to hold the data from the variableResistor.

// SETUP ROUTINE
//The setup runs once on startup or reset.

void setup() {

//initialize the serial connection
//syntax
//Serial.begin(baud rate)

Serial.begin(9600);
}

//MAIN LOOP
//this will run forever as long as the arduino has power.
void loop() {
//Read the specified pin.
//Assign to the variable, the reading from the analog input pin.
//syntax
//variableName = digitalRead(analog pin);

analogValue = analogRead(variableResistor); 
analogValue = map(analogValue, 0, 1023, 0, 255);                //map the values from 0 - 1024 to 0-255

//assign a level to the brightness of the led.
//Use the variable analogValue for the level of brightness.
//syntax
//analogWrite(variableName, value);

analogWrite(led0, analogValue);

//Send a label for the data with no carridge return.
//syntax
//Serial.print(data);

Serial.print("analogValue = " );

//Send the data with a carridge return.
//syntax
//Serial.print(data);

 Serial.println(analogValue);
 
delay(5);                                                                     //delay for 5ms to allow the chip to maintain continuity
}

Watch a Potentiometer Video

Please if this tutorial was helpful send me your comments. Thanks
By Lis Brown