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

Arduino Intermediate Kit Tutorial 4: Make an LED blink

DFRobot Aug 31 2017 775

We’ve given you a brief idea of what Arduino is and how it works as well as some theory behind digital and analog signals. Now let’s start making some real stuff. We will start with the basics: how to make an LED blink.

In the previous session, we’ve used the blink program to test the Arduino. This time we’ll attach our own LED to digital pin 13 instead of using the Arduino’s LED.

ITEMS NEEDED

Digital Piranha LED-R

DFRduion UNO R3 (similar as Arduino UNO)

1× IO Expansion Shield for Arduino V7.1

HARDWARE CONNECTIONS
Connect the Digital Piranha LED-R module to digital pin 13 on the Arduino. Make sure your power, ground and signal connections are correct or you risk damaging your components.

When all your connections are made, connect the Arduino to your computer via the USB cable.




CODE INPUT

Open the Arduino IDE.

Although you can open the code via “File” > “Examples” > “01.Basic” > “Blink”, we encourage you to type the code out by hand to develop your skills.

The code is displayed below:

Sample Code 1-1:

// Item One —— LED Blink
     /*
Description: LED light on first; LED light off one second later; loop the above action
     */    
int ledPin = 13;
void setup() {
        pinMode(ledPin, OUTPUT);
}
void loop() {
        digitalWrite(ledPin,HIGH);
        delay(1000);
        digitalWrite(ledPin,LOW);
        delay(1000);
}

Once the code is input, click “Verify” in Arduino IDE. The IDE will check the code for errors. If there are no errors, you are ready to upload. Click “upload” and the IDE will transfer the code to your Arduino. When the upload is complete, you external LED will blink!

Now let’s review code and see how it works.

HARDWARE ANALYSIS (DIGITAL OUTPUT)

In the previous sessions, we’ve mentioned input and output. The whole device is composed of two parts: control and output. The Arduino is the control unit and the digital Piranha LED-R module is the output unit. No input unit is included in this particular device.



CODE ANALYSIS

Arduino’s code is composed of two main parts:

“void setup() {”

This part of the code is executed when the device is initialized. It runs only once. It is used to declare outputs and inputs of the device. 


“void loop() {“
This part of the code is executed after “void setup(){“ once initialized, it runs in a loop forever (or until the device is powered off). This is where all the key arguments of the code are contained.

Check out this code:
pinMode(ledPin, OUTPUT);

The code above shows that ledPin is set up as the output mode. The comma in the brackets is the code syntax and needs to be there to separate the parameters. 

What is ledPin?
Examine the first line of the code below:

int ledPin = 13;
 
We’ve named pin13 “ledPin”, therefore ledPin represents pin 13 on the Arduino “int” stands for integer and means that “ledPin” is an integer

What if you want to attach the LED to pin10 instead of pin13?

pinMode(10, OUTPUT);

You just have to change the pin in the code from 13 to 10.

Let’s take a look at the function “loop()”, which contains only one function digitalWrite().

The syntax of function “digitalWrite()” is as follows:

digitalWrite(pin,value)

When we set pinMode() as OUTPUT, the voltage will be either be HIGH (5V or 3.3V for the control board) or LOW (0V). “HIGH” means on and “LOW” means off.

digitalWrite(ledPin,HIGH);   // LED on
digitalWrite(ledPin,LOW);    // LED off

When HIGH is included in digitalWrite() just as it is shown above, pin 13 will be HIGH and the LED will be turned on. When LOW is included in digitalWrite() just as it is shown above, pin 13 will be LOW and the LED will be turned off.

Don’t forget the following line:

delay(1000);

The figure within the brackets means the amount of milliseconds of delay. There are 1000 milliseconds in 1 second, so this means the duration of delay is 1 second. The LED will turn on for 1 second and then turn off for 1 second continuously.

You might notice “//” and “/*…*/” located ahead of the code:


// Item One—LED Blink

     /
Description: LED on and off alternately every other second
 */  


Anything written after “//” or inside “/*…*/” will not be compiled. We use this function to write comments about the code in plain English. “//” will comment for one line of code. “/*…*/” will comment for multiple lines. The Arduino IDE will signify this by automatically turning the text grey.

EXERCISE
Try to make the LED light blink faster or slower. For example, keep the LED light off for 5 seconds and then quickly blink for 250 milliseconds.


If you have any questions, please feel free to comment.
There is also another best selling arduino starter kit for you to choose.

Related category: arduino kits > education kits
Last Arduino Tutorial:  Arduino Intermediate Kit Tutorial 3: Learn Digital and Analog Signals through Serial Port?
Next Arduino Tutorial: Arduino Intermediate Kit Tutorial 5: Sensor Light


REVIEW