Introduction: The Arduino Uno R3 contains 13 digital pins and these pins can be configured to be input or output pins. This will be useful in tutorial as we will be using both modes of the pins to turn on and off 3 LEDs with the use of a simple button.
Equipment:
- Arduino UNO
- A USB Type A male to B male Printer Cable
- 6 Breadboard Jumper Wires
- 3 LEDs
- 3 230 Ohms Resistors
- 1 1k Ohm Resistor
- A Breadboard
Procedure:
- Place 3 LEDS on the breadboard, parallel to each other. *Remember to connect the shorter end of the LED to the ground
- Connect 3 230 Ohm resistors in series with the LEDs
- Connect 3 jumper wires in series from the resistors to the pins labeled 9, 4 and 3 on the Arduino
- Place the button on the breadboard, so that the pins on the button are on each side of the split on the breadboard and connect one jumper wire from one pin on the button to the pin labeled 12 on the Arduino
- Connect one end of a 1k Ohm resistor to the same pin the is connect to the pin 12 on the Arduino and the second end to the ground
- Connect a jumper cable to the other half of the button to the 5v pin on the Arduino
- Lastly connect one end of the usb cable to the Arduino and the other to the computer
Now comes the Coding part of the project, Here is the Sample code:
//Turn 3 Led one after the other with a push of a button
/**************************/
//Declaring the pins that will be used
const int buttonPin = 12;
const int ledPin1 = 9, ledPin2 = 6, ledPin3 = 3;
/**************************/
void setup() {
pinMode(buttonPin,INPUT); //set the pin to be of input type
pinMode(ledPin1,OUTPUT); //set the pin to be of output type
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
}
void loop()
{
//read the state of the key value
//and check if the button is pressed
//if it is,the state is HIGH, which means button is pressed
if(digitalRead(buttonPin) ==HIGH )
{
digitalWrite(ledPin1,HIGH);//turn on the led
delay(150);
digitalWrite(ledPin2,HIGH);
delay(150);
digitalWrite(ledPin3,HIGH);
delay(150);
}
else
{
delay(150);
digitalWrite(ledPin1,LOW);//turn off the led
delay(150);
digitalWrite(ledPin2,LOW);
delay(150);
digitalWrite(ledPin3,LOW);
}
}
Here is a short clip of it in action:
And there u have it our first tutorial, let me know how i did and if I have to change something, feedback is always welcome, anyways take care and happy reading.
Here is a short clip of it in action:
No comments:
Post a Comment