0

$USD

$USD
PROJECTS Arduino

Arduino Projects 3: Make an LED Blink

DFRobot Jun 21 2017 1982

Make an LED Blink

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 used the blink program to test the microcontroller. This time we’ll attach our own LED to digital pin 13 instead of using the microcontroller’s integrated LED.
 

Parts Needed:

DFRduino UNO (similar as Arduino UNO R3) x1
I/O Sensor Expansion Shields V7.1 x1
Digital Piranha LED-R x1
 

Connections

Connect the Digital Piranha LED-R module to digital pin 13 on the DFRduino UNO microcontroller (as Arduino uno). 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 microcontroller to your computer via the USB cable.
 

 

Hardware Analysis (Digital Output)

In the previous sessions, we’ve mentioned inputs and outputs. The device we will make is composed of two parts: a control and an output. The arduino microcontroller is the control unit and the digital Piranha LED-R module is the output unit. No input unit is included in this particular device.
 

Control Device

Output Device

 

 

Code Input

Open Arduino IDE.
You can open the program by navigating to File > Examples > 01.Basics > Blink
(Although 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
//The LED will turn on for one second and then turn off for one second
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 and you are happy with it, 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 Arduino IDE will upload the code to your microcontroller. When the upload is complete, your external LED will blink!

 

 

Now let’s review the program and see how it works

 

Code Analysis

A program 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 or a component fails). This is where all the key arguments of the code are contained.
Consider this code:

pinMode(pin,mode)

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.

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;

Here we have assigned pin13 the variable name “ledPin”. In other words ledPin represents pin 13 on the microcontroller “int” stands for integer and means that ledPin is an integer data type.
What if you want to attach the LED to pin10 instead of pin13?

pinMode(10, OUTPUT);

You just have to change the pin value in the code from 13 to 10.
Let’s take a look at the loop() function, which contains only
one function:

digitalWrite().

The syntax of the 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).

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

When HIGH is included in digitalWrite() as shown above, pin 13 will be HIGH and the LED will turn on. When LOW is included in digitalWrite()as shown above, pin 13 will be LOW and the LED will turn off.
Don’t forget the following line:

delay(1000);

The number in 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
*/