Project 1 How to Blink an LED?

0 7405 Medium
projectImage

Step on the adventure of programming from an LED! In the first project, you will learn how to control the output of Arduino board, and the basics of components such as LED, button, resistor, and pull-up/down resistor. At the same time, you will first come into contact with graphical programming of Mind+. By comparing the code blocks and auto-generated programs, you will start to learn something about coding. 

projectImage

Now, let’s get started: use an Arduino board to control an external LED! 

The test code “Blink” in the previous chapter will be used here. Besides that, we are going to connect an external LED to a digital Pin instead of using the onboard Pin 13 LED, so we can learn the principle of LED flashing and circuit building.

projectImage
projectImage

Hardware Connection 

Peel the adhesive tape off the back of the breadboard and stick it on Prototype shield, and then plug the shield onto the UNO board. Connect all parts as the diagram shown below.

projectImage
projectImage

Use green and black jumper wires to connect the components. (Normally, for the DFRobot products, green is for digital port, blue for analog, red for VCC, black for GND.) You can use other holes in the breadboard, but please keep the connection order be the same as the picture above. 

The long leg of LED is +, VCC; short leg is -, GND. Please connect the LED correctly. Power the Arduino via USB, and be ready to download codes.

projectImage

Open Mind+, load the Arduino UNO blocks. Connect your Arduino board to a computer, and then drag the blocks to the script section to complete a program as the example shows. 

Example Program 1-1:

projectImage

Click “Upload “ to burn the codes into Arduino board. 

Result: the red LED on the breadboard flashes once every 2 seconds. 

Well, now let’s learn the commands we used in the program.

We have realized the LED blinking via graphical programming. Now let’s learn the auto-generated codes of this program.

projectImage
projectImage
projectImage

Here are the C codes of the project, we will introduce the meaning of codes line by line.

CODE
1.void setup () {  
2.}  
3.  
4.void loop() {  
5.    digitalWrite (13, HIGH);  
6.    delay (1000);  
7.    digitalWrite (13, LOW);  
8.    delay (1000);  
9.}  
projectImage

Any code that lives inside setup()’s curly brackets from “{“ to “}” runs once at the very beginning of your program and then never runs again--at least not until you reset the Arduino, or upload new code. It is useful for initializing variables, pin modes, initialize libraries, etc. 

projectImage

Segmenting code into functions allows a programmer to create modular pieces of code that perform a defined task and then return to the area of code from which the function was “called”. The typical case for creating a function is when one needs to perform the same action multiple times in a program. The setup() and loop() cannot be called repeatedly, and in Arduino sketch, other functions must be created outside the brackets of those two functions. 

In addition, we have to comprehend the concept of the return of a function. A return is a value that a function returns to the calling script or function when it completes its task. It can be regarded as feedback. How do we know if the function will return a value? Well, the function declaration can give us the answer. For instance, “void” is the signal that there is no return in this function. We will use it frequently later. How about function with a return? If you are interested, google it!

projectImage

As the name implies, the loop() function...loops! The program starts directly after the opening curly bracket “{“, runs until it sees the closing curly bracket “}”, and jumps back up to the first statement in the loop() and starts all over. The function will run over-and-over until the Arduino is reset.

projectImage

With the guide above, it would be quite easy to understand this function, right! Apparently, it means set P13 to Low(0V) to make its LED go off.

projectImage

Since we have known how code works, let’s change the project a little bit. Keep the LED off for 5s and then make it flash quickly(once every 0.25s), just like the light on the car alarm. Give it a try!

projectImage

Changing the time the LED keeps on or off is able to realize various lighting effects. Come and explore! 

projectImage
Prototyping Shield

There are limited ports on an Arduino UNO board, especially the 5V and GND ports. Our project development may often be bogged down by the pin resource constraints. So most of the time we need a shield to expand the ports on Arduino board.

This prototyping shield is compatible with Arduino Uno, on which you can build circuits, solder components and so on. There is a breadboard on the shield for you to connect. Its digital and analog ports conform with UNO board. Besides, the 5V ports and GND ports marked below are all the same. (The connection diagrams in later projects are based on the old version of the shield.)

projectImage
projectImage

Breadboard

A breadboard is a solderless device for temporary prototypes with electronics and test circuit designs. The breadboard has strips of metal underneath the board. Most electronic components in electronic circuits can be interconnected by inserting their leads or terminals into the holes and then making connections through wires where appropriate.

projectImage

From the figure above, it can be seen that the breadboard has been split into two parts. The five holes in the column marked with blue are connected together. There is a 7.62mm space reserved for Narrow DIP IC chip. The figure shown below is a breadboard with a DIP Chip inserted. 

projectImage
Resistor

A resistor is an electrical component that lowers the electric current. The resistor’s ability to reduce the current is called resistance, measured in units of ohms(symbol: Ω). If we make an analogy to water flow through pipes, the resistor is a thin pipe that reduces the water flow. 

Resistors can be divided into various types according to the different jobs they do in electronic circuits: pull-up resistor, pull-down resistor, current limiting resistor and so on. In this project, the digital pin 10 outputs 5V, and the input current is 40mA(DC). Typically, an LED requires the power of 2V and 35mA to light up. So here we need a resistor to reduce the voltage from 5V to 2V, the current from 40mA to 35mA. Please be careful, over-current would burn the LED.

Read Resistor Color 

The resistor value will be marked on the outer package of your resistors, but what should we do when the label is missing and there is no measuring tool at hand? The answer is to read the resistor value. This is a group of colored rings around the resistor. Details are available on Google for those interested in trying.


projectImage
 LED

A light-emitting diode (LED) is a semiconductor device that emits light when an electric current is passed through it. Light is produced when the particles that carry the current (known as electrons and holes) combine together within the semiconductor material.

Typically, LEDs have two leads, one longer than the other, the longer lead is the positive lead (also known as the anode). If the LED’s two leads are equal in length, you can look at the metal plate inside the LED. The smaller plate indicates the positive (anode) lead; the larger plate belongs to the negative (cathode) lead. If the LED has a flat area (on the plastic housing), the lead adjacent to the flat area is the negative (cathode) lead.

LEDs are polarized and must therefore be connected in the correct manner. If connected reversely, the component won’t work, seen as the diagram below:

projectImage

In the kit of DFR0100, you may find LEDs with 4 leads. Don’t get misunderstood, it is just an RGB LED with 3 primary color LEDs embedded into it. This will be explained in detail later. 

Since you have known how these hardware work, let’s start making something fun!     

Thanks for reading!

License
All Rights
Reserved
licensBg
0