USING THE ARDUINO TO CONTROL LEDS
The "hello world" of microcontroller programming is making an LED blink. One of the initial steps in learning how to program a new microcontroller is to accomplish this. A great tool for monitoring activity at the output pins is an LED. When the LED turns on, you can see that the pin to which it is linked is high. The programming techniques used to operate an LED are the same programming concepts used to control a large number of other modules, including motors, relays, buzzers, and other sensors.
In this article, we will learn how to make LEDs blink on and off, how to control the speed of a blinking LED, and how to control multiple LEDs at the same time. We will learn how to use the pinMode(), digitalWrite(), and delay() functions as well.
How do LEDS work?
All diodes regulate the flow of electricity in only one direction. They resemble a one-way valve for electricity. This is the schematic symbol of a diode: A box with two arrows inside.
Diodes and LEDs both only allow current to travel in one way. But as current passes through LEDs, they also produce light. A light-emitting diode is what it stands for. The LED schematic looks like this:
LED POLARITY
Current can only flow in one direction, so LEDs need to be connected in the right way around or they'll be useless. The side that comes in from the positive voltage is known as the cathode; the side that comes out from the negative voltage and is known as the anode.
The wire that is attached to the positive pole is known as the anode. The wire that becomes connected to the negative pole is known as the cathode. One of the sides of the LED clear plastic may be flat, which means the cathode side.
LED BRIGHTNESS
How much current is permitted to pass through an LED affects how brilliant it is. The LED can be made brighter or darker by varying the current. However, the amount of electricity that can pass through an LED is limited. If you give them too much current, they will shortly burn out. We connect a current-limiting resistor in series with the LED and the power source to restrict the flow of current:
The brightness of the LED is influenced by the current limiting resistor's value. Higher resistor values impede the passage of electricity and cause the LED to fade. Lower resistor values enable more current flow, increasing LED brightness.
Can you connect your Arduino to the LED?
These are the parts you need to connect an LED to the Arduino: These are the items you will need.
- Arduino Uno
- One 200-1K Ohm resistor
- One LED
- Jumper wires
- Breadboard
The current limiting resistor's value can range from roughly 200 Ohms to roughly 1K Ohms.
HOW TO PROGRAM AN LED ON THE ARDUINO
Upload the following code to the Arduino once you've connected the LED and current-limiting resistor. In this drawing, the LED will flash on for one second, then off for another, and so on.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
CODE explanation
The Arduino must first be informed of the pin that the LED is attached to and set as an output. The setup() section's pinMode() method is used for it. The pin number and the input/output mode are the two arguments to the pinMode() function:
pinMode(pin, mode);
The LEDs connected to pin 13 enable the pin to serve as an output pin, so we use the connection of pin 13 to the LED.
pinMode(13, OUTPUT);
digitalWrite(pin, value);
A high voltage level for the Arduino is 5 volts, and a low voltage level is 0 volts. We write a high voltage to pin 13 as follows to turn on the LED:
digitalWrite(13, HIGH);
The Arduino must then be instructed as to how long pin 13 should be high. With the delay() method, we accomplish that. The Arduino is instructed to pause and wait via the delay() method before continuing with the following line of code. The delay time in milliseconds is indicated by the number included in parentheses:
delay(time);
delay(1000);
We must switch off the LED now that it has been on for one second. In order to send a low signal to pin 13, we once more use the digitalWrite() function.
digitalWrite(13, LOW);
delay(1000);
By adjusting the numbers in the two delay() calls, you may modify how long the LED remains on or off.
By adjusting the numbers in the two delay() calls, you may modify how long the LED remains on or off.
HOW TO CONNECT TWO LEDS TO THE ARDUINO
The components required to connect two LEDs to an Arduino are as follows:
- Arduino Uno
- Two LEDs
- Two 200-1K Ohm resistors
- Breadboard
- Jumper wires
Let's add a second LED to this circuit now that we've learned how to connect one LED to the Arduino. To go with the red LED, we can add a green LED. How to connect the circuit is as follows:
The current limiting resistors can be anywhere from about 200 Ohms up to about 1,000 Ohms.
HOW TO USE THE ARDUINO TO PROGRAM TWO LEDS
We can essentially just repeat the code for a single LED in order to program the two LEDs:
int redLED = 13;
int greenLED = 2;
void setup() {
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
}
void loop() {
digitalWrite(redLED, HIGH);
delay(1000);
digitalWrite(redLED, LOW);
delay(1000);
digitalWrite(greenLED, HIGH);
delay(1000);
digitalWrite(greenLED, LOW);
delay(1000);
}
The sole distinction is that pin 13 is placed in the redLED variable whereas pin 2 is stored in the greenLED variable. The pin number is substituted by these variables in the pinMode() and digitalWrite() methods.
After connecting the LEDs and uploading the aforementioned program, you ought to notice the two LEDs blinking alternately:
No comments: