What are LEDs?
The word LED is the acronym for light-emitting diode. LEDs are semi-conductive diodes that enable the current to circulate solely in one direction and which emit light when activated. One of the most common uses of LEDs is indicators on electronic devices, replacing the small bulbs that were used previously. What´s more, due to their capacity to operate at high frequencies, they are also used in advanced and control technologies.
The most recent development in LEDs means they can be used in ambient lighting. Although their use in this field is currently limited, growth is expected in the coming years as they offer numerous advantages over incandescent and fluorescent lamps. A few examples are:
- Low energy consumption.
- Longer lifespan.
- Compact in size.
- Greater robustness.
- Reduced heat emission.
- Greater vibration and impact resistance.
Want to learn how to program an LED using bitbloq? We will show you using examples:
You will need
We will give various examples to show you how to program an LED – now let´s get down to it!
Switching on an LED
This is one of the simplest examples of programming. You will need to connect the controller board to the computer via the USB cable. Then you can connect the LED to the controller board. When connecting any component, it´s critical to make sure that the colours of the cable match the colours of the pin that it will be connected to: black with black, red with red, and yellow or white with yellow.
The LED is a digital component as it only has two states: on and off. All components, whether sensors or actuators, which have only two states will be digital, another example is the switch (push button), which can either be on or off. The digital pins correspond with the long series of pins on the board, going from 0 to 13, although it´s not recommended to use pins 0 and 1, as they are the ones that enable the board to communicate with the computer, which could cause unexpected reactions or our programs might not load correctly.
We will follow the steps below to resolve the problem.
First we will connect the LED to digital PIN 8, as shown in the diagram.
Now we will program the controller board using bitbloq so that the LED switches on. To do this, we need to select the LED block, assign the digital pin 8 to it and set the action to be carried out: in this case, SWITCH ON.
If all goes well after programming the controller board, the LED connected to digital pin number 8 should be switched on. If it fails to switch on, check what could have happened by asking yourself these questions: Is the board connected to the computer? Is the board connected to the correct port? Do the colours of the LED cables correctly match the colours of the pin connected to it? Have I assigned a digital pin to the LED block in bitbloq? Have I remembered to SWITCH ON the LED in the block?
Switching off an LED
Now we will program our controller board to do the opposite, i.e. to switch off the LED. Don´t forget to reprogram the board on making the changes.
Let´s make things a bit more complicated, how about making an LED flash on and off? This time we will let you have a go by yourself. You will need to create a program to make an LED flash on and off (on 1 second, off 1 second). Try it using bitbloq and then read on for the solution.
It´s most likely that you will have done something like this:
This solution doesn´t work because the program switches the LED on (in one moment according the speed of the controller card microprocessor) and switches it off immediately in another moment, and it repeats over and over again. This program results in the LED being continuously switched on (with slightly less brightness than usual).
To make it function correctly, it´s necessary to insert pauses. This means that when we state that the LED must flash on and off, we understand it as: SWITCH ON – SWITCH OFF – SWITCH ON – SWITCH OFF – etc. This is incorrect, as in fact the necessary command would be: SWITCH ON – WAIT 1 SEC. – SWITCH OFF – WAIT 1 SEC. – SWITCH ON etc.
The correct program would go like this:
If we have programmed the board correctly, our LED should switch on for one second, switch off for one second, switch on again for one second… And it would continue like this infinitely because the program runs in a loop. Why for one second? Because this is the waiting time that we have set for each state, but this time can be modified to suit our needs. Try changing the frequency of the flash.
So now you know the basics of programming an LED. We will show you how to program multiple LEDs at the same time in another entry, but in the meantime, you can keep having a go by yourself.