0% found this document useful (0 votes)
22 views

5 Simple Button and Led Projects With Arduino

This document describes 5 simple Arduino projects using buttons and LEDs: 1. A button connected to the serial monitor to display a "1" when pressed. 2. A button controlling an LED so the LED turns on when the button is pressed. 3. Three buttons controlling the color of an RGB LED to light different colors. 4. Controlling an LED from the serial monitor by typing "1" or "0". 5. A random number generator using LEDs as a bar graph display.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

5 Simple Button and Led Projects With Arduino

This document describes 5 simple Arduino projects using buttons and LEDs: 1. A button connected to the serial monitor to display a "1" when pressed. 2. A button controlling an LED so the LED turns on when the button is pressed. 3. Three buttons controlling the color of an RGB LED to light different colors. 4. Controlling an LED from the serial monitor by typing "1" or "0". 5. A random number generator using LEDs as a bar graph display.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

instructables

5 Simple Button and Led Projects With Arduino

by Robert 7320

What you for Need these 5 Projects.


An Arduino uno or Clone.
3mm 5mm or 10mm Leds any will work and 1 RGB led.
Some Push Buttons.
A breadboard.
Male to Male Jumper Wires.
Some 10k and 220 Resistors.
10 led bar graph or leds will work

Step 1: Push Button and the Serial Monitor.

If you put this code into your Arduino , when you open the serial monitor and push the button it will come up as 1.

int BUTTON1 = 7;

void setup(){

Serial.begin(9600);

5 Simple Button and Led Projects With Arduino: Page 1


pinMode(BUTTON1,INPUT);

void loop(){

Serial.println( );

if(digitalRead(BUTTON1) == HIGH)

{ Serial.println("Button1 1");

}else{

Serial.println("Button1 0");

} delay(200);

1. The Resistor is 10k

Step 2: 1 Button 1 Led.

int LED = 13;

int BUTTON = 2;

void setup(){

pinMode(LED,OUTPUT);

pinMode(BUTTON,INPUT);

5 Simple Button and Led Projects With Arduino: Page 2


}

void loop(){

if(digitalRead(BUTTON) == HIGH){

digitalWrite(LED,1);

}else{

digitalWrite(LED,0);

this code will make it so when you push the button the led will light up.

1. 10k
2. 220

Step 3: 3 Buttons and RGB LED.

this code should make it so when you push one button one color should light up if you push all three buttons it will
make a whitish color.

5 Simple Button and Led Projects With Arduino: Page 3


int BUTTON1 = 9;
int BUTTON2 = 10;
int BUTTON3=11;
int BLUE=3;
int GREEN=5;
int RED=6;</p><p>void setup(){</p><p>pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
pinMode(BLUE,OUTPUT);
pinMode(RED,OUTPUT);
pinMode(GREEN,OUTPUT);
}</p><p>void loop(){
if(digitalRead(BUTTON1) == HIGH){
digitalWrite(BLUE,1);
}else{
digitalWrite(BLUE,0);
}
if(digitalRead(BUTTON2) == HIGH){
digitalWrite(RED,1);
}else{
digitalWrite(RED,0);
}
if(digitalRead(BUTTON3) == HIGH){
digitalWrite(GREEN,1);
}else{
digitalWrite(GREEN,0);
}</p>

1. 220

Step 4: Led Pin 13 and Serial Monitor.

5 Simple Button and Led Projects With Arduino: Page 4


int outPin = 13;
void setup(){

pinMode(outPin,OUTPUT);
Serial.begin(9600);
Serial.println("Enter 1 or 0");
}
void loop(){
if(Serial.available()>0)
{
char ch = Serial.read();
if (ch == '1')
{
digitalWrite(outPin,1);
}
else if (ch == '0')
{
digitalWrite(outPin,LOW);
}

this code will make it so when you go into the serial monitor and type 1 the led on pin 13 will light up then when

you type 0 the led will be off.

Step 5: Random Number Generator With Led Bar Graph.

5 Simple Button and Led Projects With Arduino: Page 5


int LED0 = 2;
int LED1 = 3;
int LED2 = 4;
int LED3 = 5;
int LED4 = 6;
int LED5 = 7;
int LED6 = 8;
int LED7 = 9;
int LED8 = 10;
int LED9 = 11;

long randomNumber;

void setup() {
Serial.begin(9600);
Serial.println("Randon Numbernesssssss");
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
randomSeed( analogRead(A0) );
}

void loop() {
randomNumber = random(2,12);
Serial.println(randomNumber);
digitalWrite(randomNumber,HIGH);
delay(40);
digitalWrite(randomNumber,LOW);
}

//This code will make it so when you connect the led or the bar graph to the Arduino it will make random numbers, and the number it makes is the pin number on the Ardui
no.

1. 220

Here we have the random number generator. Pretty neat little project. I used a prototype board
from Adafruit and soldered all the LED's, 330 Ohm resisters in place. I also added some male
headers to make the projects LED's usable in other projects

5 Simple Button and Led Projects With Arduino: Page 6


One tip, the code on the webpage has some issues with it. It has some HTML formatting that
somehow got posted with the code, this needs to be removed in order for it to compile. Everywhere
you see <p></p> or <p> or </> you need to delete it. See the images as I shared the code I used in
a screenshot.

We made number two. He was so excited to show his mom.

How about if the input im using is arduino laser sensor. When the laser been cut off it will light on
the led. And when there is no passing object it will stay off. Can you provide coding for it?
hey what shold happen on
Step 2: 1 Button 1 Led.
Hi. Nice little starter projects. However, I did notice that there is no code to compensate for switch
bounce and so sometimes when you press the button, especially on the RGB project you may not
get the led to light. This may cause confusion for some first timers.
There are two ways to fight bouncing: by software (just a few lines of code) and by hardware. The
latter is easier: you simply connect electrolytic capacitor in parallel to the button pins with the value
of some 1 to 5 microfarad.
your 3rd instuctable seems to be wrong:here is the code and a pic of the wiring
int BUTTON1 = 11;
int BUTTON2 = 10;
int BUTTON3=9;
int BLUE=5;
int GREEN=6;
int RED=3;
void setup(){
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
pinMode(BLUE,OUTPUT);
pinMode(RED,OUTPUT);
pinMode(GREEN,OUTPUT);
}
void loop(){
if(digitalRead(BUTTON1) == HIGH){
digitalWrite(GREEN,1);
}else{

5 Simple Button and Led Projects With Arduino: Page 7


digitalWrite(GREEN,0);
}
if(digitalRead(BUTTON2) == HIGH){
digitalWrite(BLUE,1);
}else{
digitalWrite(BLUE,0);
}
if(digitalRead(BUTTON3) == HIGH){
digitalWrite(RED,1);
}else{
digitalWrite(RED,0);
}
}

if you use INPUT_PULLUP and put your button between an IO and GND, you don't even need the
resistor(s) - the arduino in this mode has a built-in resistor to +5V. Because this is the opposite of
your pull-DOWN resistor, you then need to watch for the pin to go LOW instead of HIGH.
If you hook it us like in the diagram this won't do anything. You need to connect the yellow wire to
pin 2 according to the code. The picture shows it connected to pin 7.
you've added an additional notes in step 1. 10k resistor.

Neat little set of starter projects for absolute beginners, a sort of what's next after blink. Nice. :)

5 Simple Button and Led Projects With Arduino: Page 8

You might also like