Arduino SA
Search…
Button Control LED Delay Switch
In this lesson, you will learn how to use the button control LEDs to implement the delay control function.

Component Required:

  • 1 x Uno R3
  • 1 x 5mm red LED
  • 1 x 220 ohm resistor
  • 1 x 10k ohm resistor
  • 1 x 830 Tie Points Breadboard
  • 1 x button
  • 5 x M-M wires (Male to Male jumper wires)

Component Introduction:

BREADBOARD MB-102:

A breadboard enables you to prototype circuits quickly, without having to solder the connections.
Below is an example.
Breadboards come in various sizes and configurations. The simplest kind is just a grid of holes in a plastic block. Inside are strips of metal that provide electrical connection between holes in the shorter rows. Pushing the legs of two different components into the same row joins them together electrically. A deep channel running down the middle indicates that there is a break in connections there, meaning, you can push a chip in with the legs at either side of the channel without connecting them together.
Some breadboards have two strips of holes running along the long edges of the board that are separated from the main grid. These have stripes running down the length of the board inside and provide a way to connect a common voltage. They are usually in pairs for +5 volts and ground. These strips are referred to as rails and they enable you to connect power to many components or points in the board.
While breadboards are great for prototyping, they have some limitations. Because the connections are push-fit and temporary, they are not as reliable as soldered connections. If you are having intermittent problems with a circuit, it could be due to a poor connection on a breadboard.

LED:

LED (Light Emitting Diode), which converts electrical energy into light energy, also has unidirectional conductivity and a reverse breakdown voltage of about 5V. Its forward volt-ampere characteristic curve is very steep, and the current-limiting resistor must be connected in series. In a 5V circuit, a resistor of about 400 ohms is generally used.
The longer of the two pins of the LED is the positive pole.
There are two ways to connect the LED to the Arduino:
  1. 1.
    Connect the positive pole of the led through the current limiting resistor and Arduino**.** The I/O port is connected and the other end is grounded. At this time, when the Arduino output is high, the led is lit, and when the output is low, the led is off.
  2. 2.
    When the negative pole of the led is connected to the I/O port of the Arduino, the other end is connected to the 5V voltage through the current limiting resistor. At this time, Arduino when the output is low, the led is lit, and when the output is high, the led is off.
If you do not use a resistor with an LED, then it may well be destroyed almost immediately, as too much current will flow through, heating it and destroying the 'junction' where the light is produced.
There are two ways to tell which is the positive lead of the LED and which the negative.
Firstly, the positive lead is longer. Secondly, where the negative lead enters the body of the LED, there is a flat edge to the case of the LED.
If you happen to have an LED that has a flat side next to the longer lead, you should assume that the longer lead is positive.

RESISTORS:

As the name suggests, resistors resist the flow of electricity. The higher the value of the resistor, the more it resists and the less electrical current will flow through it. We are going to use this to control how much electricity flows through the LED and therefore, how brightly it shines.
Unlike LEDs, resistors do not have a positive and negative lead. They can be connected either way around.
If you find this approach method too complicated, you can read the colour ring flag on our resistors directly to determine its resistance value. Or you may use a digital multimeter instead.

Button:

The button as an I/O port input device has seen the effect of the last use. It is actually used to control the on/off of the line, and the effect of changing the I/O port signal is achieved by adding a device such as a pull-up resistor.

Connection Diagram:

Wiring schematic:

Wiring Step by Step

  1. 1.
    Connect a jumper from the GND (Ground) PIN on the UNO to the negative side of the breadboard. This will make the entire row (horizontally) negative. See black jumper wire in diagram.
  2. 2.
    Connect the LED to the breadboard as shown above. Pay attention to the positive and negative of the LED. Positive if the 'bend' LED leg.
  3. 3.
    Connect the 220 ohm resistor across the middle of the breadboard. One side should join the positive rail of the LED and the other side will be on the opposite side of the breadboard.
  4. 4.
    Connect a jumper wire from the negative side of the LED to any negative rail on the breadboard. This now make this connection a negative connection to the LED. See grey jumper in diagram
  5. 5.
    Connect a jumper wire from the LED resistor to PIN 13 on the UNO board. This now allows you to communicate to the LED via PIN 13. See yellow jumper in diagram
  6. 6.
    Connect a switch to the middle of the breadboard as shown in the diagram above.
  7. 7.
    Connect the 10k resistor from the bottom right leg of the switch to the breadboard negative rail.
  8. 8.
    Connect a jumper wire from that same leg to PIN 4 on the UNO. See blue jumper in diagram.
  9. 9.
    Lastly connect a jumper wire from the bottom left leg of the switch to the 5V PIN on the UNO. See red jumper in diagram

Physical wiring diagram:

Code:

void setup (){
pinMode(4,INPUT); //Set port 4 as input and port 13 as output mode
pinMode(13,OUTPUT);
}
void loop(){
int n = digitalRead(4); //Create a variable n and assign it the state of the number 4 digital port.
//To determine whether n is a high level, if the following statement is executed, then skip.
if (n==HIGH){
delay(1000);
digitalWrite(13,HIGH);
delay(5000);
digitalWrite(13,LOW);
}
}
You can download the code below
3_Button_Control_LED_Delay_Switch.ino
426B
Binary
Last modified 7mo ago