$USD
  • EUR€
  • £GBP
  • $USD
PROJECTS Arduino

Arduino Project 1: LED Flashing

DFRobot Mar 29 2017 2721

Related Product: Beginner Kit for Arduino

Let’s kickstart our Arduino adventure! In the first lesson, you will learn the basics of components such as LEDs, buttons and resistors - including pull-up and pull-down resistors. Additionally, you will start to write Arduino sketches to control a LED with your Arduino starter kit.

LED Flashing

In the first project, we will use the "Blink" sketch again but with a proper LED instead of the on-board LED. Through this, we can have a clear idea of how a LED works and how they can be used in a circuit.

Required Components:

DFRduino UNO (similar as Arduino UNO R3)*3
prototype Shield with Breadboard *1
Jumper M/M *2
Resistor 220R*1
5MM LED *1


*You may need to choose a different value resistor depending on the LED you will use. We will mention how to calculate resistance value in the latter part of this lesson.
*DFRduino is DF Robot’s signature Arduino board and functions the same as any other Arduino board.

Hardware

DF Wiring Definitions:
Green:      Digital Connections
Blue:        Analog Connections  :  
Red:         Vcc Power Supply
Black:     GND
White: Other

To  build the circut, you need to take your arduino and stack the  Prototype Shield on top of it.

The male header pins on the bottom of the Prototype Shield should line up and slide in to the female headers on the arduino easily. Be gentle and be careful not to bend them.

Peel the adhesive strip off the back of the Breadboard and then stick it on to the Prototype Shield.  You can now set up the circuit according to the picture below.

It is standard practice to use wires of  different  colored  insulation  for  your own reference, but using  different combinations  of  colors  wont stop the circuit working.

Normally red wire indicates power supply (Vcc), black  wire  indicates ground (GND), green wire indicates digital pins, blue wire indicates analog pins, and white is other.
Double  check  the orientation of  LED leads on the circuit. LEDs are po-larized (will only work if placed in the circuit the correct way around).  The long leg of the LED connects to Vcc. (In this example Pin 10) and the short leg connects to GND.

When you finish the circuit, connect the Arduino controller and computer with the provided USB cable.

Arduino Sketch

Open the Arduino IDE and enter the code as sample code 1-1 shows. (We highly recommend you type code instead of copying and pasting so that you can develop your coding skills and learn to code by heart.)


The sample code 1-1:
//Project -- Blinking a LED
/*
Description: turn LED on and off every other second.
*/
int ledPin = 10;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
}
 When  you've  finished  entering  the  code,  click  on "Verify" to  check if   the   code   can   be   compiled.  If  the  code  has  no  errors, click "Upload" to  upload  code  to  the  micro-controller. Now your  onboard LED should be blinking on and off.

CODE

Comments:
//Project -- blink a LED


Any line of code that has “//” put before it will not be compiled by the complier. The Arduino IDE indicates this by turning the line of code grey automatically. This allows you to write plain English descriptions of the code you or others have written and makes it easier for other people who might not be able to read code to understand. We refer to this as commenting out.


Multi-Line Comments:
/ * the text between these two symbols will be commented out; the compiler will ignore the text and the text will appear gray * /
Similar to single line comments, any text between / * and * / will be ignored by the compiler. Once again, the Arduino IDE will turn this text grey to show that it is is commented out. This is a useful way of annotating code.


Declaring Variables:


Declaring variables is a useful way of naming and storing values for later use by the program. Integers (int) represent numbers ranging from -32768 to 32767.
In the above example, we have input an integer: 10. “LedPin” is the variable name we have chosen. Of course, you may it anything you like instead of ledPin, but it's better to name the variable according to its function. Here the variable ledPin indicates that the LED is connected to Pin-out 10 of Arduino.
Use a semicolon (;) to conclude the declaration. If you don’t use the semicolon, the program will not recognise the declaration, so this is important!

What is a variable?


A variable is a place to store a piece of data. It has a name, a value, and a type. In the above example, “int” (integer) is the type, “ledPin” is the name and “10” is the value. In this example we’re declaring a variable named ledPin of type “int” (integer), meaning the LED is connected to digital pin 10. Variables display as orange text in the sketch.


Integers can store numbers from -32768 to 32767. You must introduce, or declare variables before you use them. Later on in the program, you can simply type the variable name rather than typing out the data over and over again in each line of code, at which point its value will be looked up and used by the IDE.


When you try to name a variable, start it with a letter followed with letter, number or underscore. The language we are using (C) is case sensitive. There are certain names that you cannot use such as “main”, “if”, “while” as these have their own pre-assigned function in the IDE.
Don’t forget the variable names are case-sensitive!


The setup() function

The setup() function is read by the Arduino when a sketch starts. It is used it to initialize variables, pin modes, initialize libraries, etc. The setup function will only run once after each power-up or reset of the Arduino board.


In this example there  is  only one line in the setup() function:
pinMode
pinMode(ledPin, OUTPUT);
This function is used to define digital pin working behavior. Digital pins are defined as an input (INPUT) or an output (OUTPUT). In the example above you can see brackets containing two parameters: the variable (ledPin) and its behaviour (OUTPUT).

pinMode” configures the specified digital pin to behave either as an input or an output. It has two parameters:

pin”: the number of the pin whose mode you wish to set
mode”: INPUT, OUTPUT, or INPUT_PULLUP.
If you want to set the digital pin 2 to input mode, what code would you type?

Function

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.
There are two required functions in an Arduino sketch, setup() and loop(). Other functions must be created outside the brackets of those two functions.

Difference of INPUT and OUTPUT

INPUT is signal that sent from outside events to Arduino such as button. OUTPUT is signal that sent from Arduino to the environment such as LED and buzzer.


If we scroll further down, we can see the main part of the  code. This is the loop.
void loop() {
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
}
The Arduino program must include the setup () and loop () function, otherwise it won t work.
After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board. Here we want to control the LED constantly on and off every other second. How can we make that happen?

In this project we want the LED to turn on for 1 second and then turn off for 1 second, and repeat this action over and over. How can we express this in code?
Look at the loop () function within the first statement. This involves another function: digitalWrite ().
digitalWrite(ledPin,HIGH);

The function format is as follows:

digitalWrite writes a HIGH (on) or a LOW (off) value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH and 0V (ground) for LOW. Please note that digitalWrite() is applied only under the condition that pinMode() is set as OUTPUT. Why? Read on!

The Relation of pinMode(), digitalWrite() and digitalRead()

If pinMode configures a digital pin to behave as an input, you should use the digitalRead() function. If the pin is configured as an output, then you should use digitalWrite().
NOTE: If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim. This is because without explicitly setting a pin-Mode(), digitalWrite() will enable the internal pull-up resistor, which acts like a large current-limiting resistor.


Next:
delay(1000);
delay() pauses the program for the amount of time specified (in miliseconds). (There are 1000 milliseconds in 1 second.)

Hardware

Breadboard

Breadboard is a way of constructing and testing circuits without having to permanently solder them in place. Components are pushed into the sockets on the breadboard and then extra "jumper" wires are used to make connections.


The breadboard has two columns, each with 17 strips of connections. In this diagram the black lines show how the top row is connected. This is the same for every other row. The two columns are isolated from one-another.


Resistors    

As the name suggests, resistors resist the flow of electricity. The higher the value of the resistor, the more resistance it has and the less electrical current will flow through it. The unit of resistance is called the Ohm, which is usually shortened to Ω( The geek letter Omega).

Unlike LEDs, resistors are not polarized (do not have a positive and negative lead) - they can be connected either way around. Normally a LED needs 2V of voltage and 35 mA current to be lit, so with a resistor of would be able to control the flow you might risk burning it out. Be careful of this because resistors can get hot!

If you want to read the resistance value from the resistor color code, visit this website for calculation tables: http://www.21ic.com/tools/compo nent/201003/54192.htm

Read Resistor Color Rings

The resistor value will be marked on the on the outer package of your resistors, but what should we do in case the label gets lost and there are no measuring tools at hand? The answer is to read the resistor value. This is a group of colored rings around the resistor. Details are available online for those who are interested in having a try.
Here is an online calculator for five-color-ring resistor value calculating: http://www.21ic.com/tools/compo nent/201003/54192.htm

LEDs

A light-emitting diode (LED) is a two-lead semiconductor light source. It is basic pnjunction diode, which emits light when activated.


Typically, LEDs have two leads, one positive and one negative. 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, theremis a flat edge to the case of the LED.


LEDs are polarized, so they have to be connected the right way around. If you put the negative lead of an LED in to the power supply and the positive lead to ground, the component will not wor , as can be seen in the diagram to the right.
In your kit, you can also find LEDs with 4 leads. This is an RGB LED with 3 primary color LEDs embedded in to it. This will be explored later.


Related category: arduino kits > education kits

REVIEW