How to wire an LED to an Arduino board
The Arduino platform is wildly popular all over the world. It is the ideal tool for the first steps in learning how to program and control the hardware. As your skills grow, you can increase the architecture by adding peripheral boards and create more complex systems that run more complex programs. The Arduino Uno and Arduino Nano boards are suitable for the initial training. On this example we will connect the LED's to the Arduino.
What are Arduino Uno and Arduino Nano?
The basis of the Arduino Uno board is the ATmega328 microcontroller. It also has additional elements on it:
- a quartz resonator;
- reset button;
- USB connector;
- integrated voltage regulator;
- power connector;
- several LEDs for mode indication;
- communication chip for the USB channel;
- connector for on-chip programming;
- a few more active and passive elements.
All this allows you to make the first steps without using a soldering iron and avoid the step of making a circuit board. The unit is powered from an external 7...12V power supply or via a USB connector. The module is also connected to the PC to load the sketch. The board has a 3.3V voltage source for powering external devices. There are 6, 14 general purpose digital outputs available for operation. The load capacity of the digital output when powered from 5 V is 40 mA. This means that a LED can be directly connected to it via a limiting resistor.
The Arduino Nano board is fully compatible with the Uno, but is smaller in size and has some differences and simplifications as shown in the table.
The board | Controller | Connector for external power | Microcircuit for USB communication | USB connector |
---|---|---|---|---|
Arduino Uno | ATmega328 | Check out | ATmega8U2 | USB A-B |
Arduino Nano | ATmega328 | No | FT232RL | micro USB |
The differences are not fundamental and are not important for the topic of this review.
What you need to connect the LED to the Arduino board
There are two ways to connect the LED. For training purposes you can choose either one.
- Use the built-in LED. In this case, you don't need anything else except a cable to connect to a PC via USB - for power and programming. It makes no sense to use an external voltage source to power the board: the current consumption is small.
- Connect the external LEDs. Here you will additionally need:
- The LED itself;
- a 0.25W current limiting resistor (or more) with a rating of 250-1000 ohms (depending on the LED);
- wires and a soldering iron to connect the external circuit.
LEDs are connected with cathode to any digital pin of microcontroller, with anode to common wire through ballast resistor. If you have a large number of LEDs you may need an additional power supply.
Is it possible to connect multiple LEDs to one terminal
It may be necessary to connect an external LED or group of LEDs to any of the pins. The load capacity of one pin of the microcontroller, as mentioned above, is small. One or two LEDs with a current consumption of 15mA can be directly connected in parallel to it. You should not test the survivability of the pin with a load on the brink of or exceeding it. It is better to use a switch on a transistor (field or bipolar).
Resistor R1 must be chosen so that the current through it does not exceed the carrying capacity of the output. It is better to take half or less of the maximum. So, to set a moderate current of 10 mA., the resistance at 5 volts supply should be 500 Ohm..
Each LED should have its own ballast resistor, replacing it with one common resistor is undesirable. The Rbal is chosen so as to set each LED to its operating current. So, for a supply voltage of 5 volts and a current of 20 mA, the resistance should be 250 ohms or the nearest standard value.
It must be ensured that the total current through the collector of the transistor does not exceed its maximum value. So for a transistor KT3102 the highest Ik should be limited to 100 mA. This means that not more than 6 LEDs can be connected to it with a current of 15 mA. If this is not enough, you have to use a more powerful switch. This is the only limitation for the choice of an n-p-n transistor in this circuit. Theoretically, you should also consider the triode gain, but for given conditions (input current 10 mA, output 100) it should be at least 10. This h21e can be produced by any modern transistor.
This circuit is not only suitable for current boosting the output of the Microcontroller. This way you can connect quite powerful actuators (relays, solenoids, motors) powered by a higher voltage (e.g. 12 volt). When calculating, you must take the appropriate voltage value.
You can also use MOSFET transistorsbut they may require a higher voltage to open than the Arduino output can provide. In this case you have to provide additional circuits and elements. To avoid this you should use so-called "digital" field effect transistors - they need only 5 5 volt to open. But they are less common.
Software control of LEDs
Simply connecting a LED to the output of the microcontroller doesn't do much. You have to learn how to control the LED from the Arduino programmatically. You can do this with the Arduino language which is based on C (C). This programming language is an adaptation of C for the initial training. After mastering it, the transition to C++ will not be difficult. To write sketches (this is the name of the programs for Arduino) and to debug them live, you have to do the following:
- install the Arduino IDE environment on the personal computer;
- you may have to install the driver for the USB communication chip;
- connect the board to the PC with a USB-microUSB cable.
You can use computer simulators to debug simple programs and circuits. For example, Proteus (from version 8 on) supports simulation of Arduino Uno and Nano boards. The convenience of the simulator is that it is impossible to disable the iron if the circuit is assembled incorrectly.
The schematics consist of two modules:
- setup - is executed once when the program is started and initializes the variables and the modes of the iron;
- loop - runs cyclically after setup until infinity.
For LED can use any of the 14 free pins (pins), which are often incorrectly called ports. In fact, a port is, simplistically speaking, a group of pins. A pin is just an element.
An example of control is considered for pin 13 - a LED is already connected to it on the board (through a repeater amplifier on the Uno board, through a resistor on the Nano board). To work with the port pin it must be configured in input or output modes. It is convenient to do this in the body of setup, but not obligatory - the destination of the output can be changed dynamically. This means that during the execution of the program the port can work for input or output.
The initialization of pin 13 of the Arduino (pin PB5 of port B of the ATmega 328 microcontroller) looks like this
void setup ()
{
pinMode (13, Output);
}
After executing this command the pin 13 of the board will work in output mode and by default it will have a low logic level. You can write zero or one to it during the program execution. The writing of one looks like this:
void loop ()
{
digitalWrite (13, HIGH);
}
Now the board's pin 13 will be set to a high level, a logical one, and it can be used to light the LED.
To turn off the LED, you must set the output to zero:
digitalWrite (13, LOW);
This way you can control external devices by writing alternately one and zero to the corresponding bit of the port register.
Now you can complicate the program on the Arduino to control the LED and learn how to blink the light-emitting element:
void setup ()
{
pinMode (13, Output);
}
void loop ()
{
digitalWrite (13, HIGH);
delay(1000);
digitalWrite (13, LOW);
delay(1000);
}
The command delay(1000) command creates a delay of 1000 milliseconds or one second. By changing this value you can change the frequency or the flashing frequency of the LED. If you connect an external LED to another pin of the board, then you also need to specify the number of the selected pin instead of 13 in the program.
For clarity we recommend a series of videos.
Once you have mastered the LED connection to the Arduino and learned how to control it, you can go to the next level and write other, more complex programs. For example, you can learn how to switch two or more LEDs with a button, change the flashing frequency with an external potentiometer, adjust the brightness of the glow with PWM, change the color of the RGB-emitter. The level of tasks is limited only by imagination.