How to Control LED by Arduino?

This is a very basic project for controlling 1 LED using the Arduino board and basic knowledge of circuit design and Arduino programming

How to Control LED by Arduino?

Components are basically the same as those in the last section. Push-button is no more needed.

Component list

  Arduino UNO x1

 Buy Now

  Breadboard x1

 Buy Now

  USB cable x1

 Buy Now

  LED x1

 Buy Now

  Resistor 220Ω x1

 Buy Now

  Jumper M/M x2

 Buy Now

Circuit knowledge

Analog signal and Digital signal

The analog signal is a continuous signal in time and value. On the contrary, the digital signal is a discrete signal in time and value.

Most signals in life are analog signals, for example, the temperature in one day is continuously changing, and will not appear a sudden change directly from 0℃ to 10℃, while the digital signal is a jump change, which can be directly from 1 to 0.

Their difference can be illustrated by the following figure.

In practical application, the frequently used digital signal is a binary signal, that is 0 and 1. The binary signal only has two forms (0 or 1), so it has strong stability. And digital signal and analog signal can be converted to each other.

Low level and High level

In circuit, the form of binary (0 and 1) is present as low level and high level.

Low level is generally equal to ground voltage(0V). High level is generally equal to the operating voltage of components.

The low level of Arduino UNO is 0V and high level is 5V, as shown below. When IO port on Arduino UNO outputs high level, components of small power can be directly lit, like LED.

Code knowledge

Before start writing code, we should learn about the basic programming knowledge.

Comments 

Comments are the words used to explain for the sketches, and won't affect the running of code. There are two ways to use the comments of sketches.

1. symbol "//"

"//" will comment out the content behind it in current line:

// this is a comment area in this line.​

The content in front of "//" will not be affected.

delay(1000);              // wait for a second

2. Symbol "/*"and "*/"

"/*" and "*/" will comment out the content between them:

/* this is comment area. */

/*
this is a comment line.
this is a comment line.
*/

Data type 

When programming, we often use digital, characters and other data. C language has several basic data types as follows:

  • int: A number that does not have a fractional part, an integer, such as 0, 12, -1;
  • float: A number that has a fractional part, such as 0.1, -1.2;
  • char: It means character, such as 'a', '@', '0';

For more data types, please visit the website: https://www.arduino.cc-Learning-Reference-Data-Types.

Constant 

Constant is a kind of data that can not be changed, such as int type 0, 1, float type 0.1, -0.1, char type 'a', 'B'.

Variable

A variable is a kind of data that can be changed. It consists of a name, a value, and a type. Variables need to be defined before use, such as:

 int i;

"int" indicates the type, ";" indicates the end of the statement. The statement is usually written in one single line, and these statements from the code.

After the declaration of the variable, you can use it. The following is an assignment to a variable:

  i = 0;                  // after the execution, the value of i is 0

"=" is used to passes the value of a variable or constant on the right side to the variable on the left.

A certain number of variables can be declared in one statement, and a variable can be assigned multiple times. Also, the value of a variable can be passed to other variables. For example:

int i, j;
i = 0;                    // after the execution,the value of i is 0
i = 1;                    // after the execution,the value of i is 1
j = i;                    // after the execution,the value of j is 1

Function

The function is a collection of statements with a sequence of orders, which perform a defined task. Let's define a function void blink() as follows:

void blink() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

"()" its inside is parameters of a function (chapter 2 will detail the parameters of the function). No content inside it indicates that this function has no parameters;

"{}" contains the entire code of the function.

After the function is defined, it is necessary to be called before it is executed. Let's call the function void blink(), as shown below.

 blink();

When the code is executed to a statement calling the function, the function will be executed. After execution

of the function is finished, it will go back to the statement and execute the next statement.

Some functions have one or more parameters. When you call such functions, you need to write parameters inside "()":

digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000);                   // wait for a second 

Circuit

Now, let 's use the IO port of Arduino UNO to provide power for the LED. D13 pin of Arduino UNO is the digital pin. It can output high level or low level. In this way, Arduino UNO can control the sate of LED.

 _

Hardware connection

Sketch

In order to make the LED blink, we need to make the 13 pin (D13) of Arduino UNO output high and low level alternately.

We highly recommend you type the code in person instead of copying and pasting, by this method, you can develop your coding skills and get more knowledge.

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output
pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
delay(1000);                   // wait for a second
digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
delay(1000);                   // wait for a second
}

void loop().

After Arduino board is reset, the setup() function will be executed firstly, then the loop() function will be executed.

setup() function is generally used to write code to initialize the hardware. And loop() function is used to write code to achieve certain functions. loop() function is executed repeatedly. When the execution reaches the end of loop(), it will jump to the beginning of loop() to run again.

Reset operation will lead the code to be executed from the beginning. Switching on the power, finishing uploading the code and pressing the reset button will trigger reset operation.

In the setup () function, first, we set the 13 pin of UNO board as output mode, which can make the port output high level or low level.

// initialize digital pin 13 as an output.
pinMode(13, OUTPUT); 

Then, in the loop () function, set the 13 pin of UNO to output a high level to make LED light up.

digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level) 

Wait for 1000ms, that is 1s. Delay () function is used to make the control board wait for a moment before executing the next statement. The parameter indicates the number of milliseconds to wait for.

delay(1000);              // wait for a second 

Then set the 13 pin to output low level, and LED light off. One second later, the execution of loop () function will be completed.

digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000);                   // wait for a second 

The loop() function is constantly being executed, so LED will keep blinking.

The function called above is the standard function of the Arduino, which has been defined in the Arduino Software. Those functions can be called directly. We will introduce more common standard functions in later chapters. For more standard functions and the specific use method, please visit https://www.arduino.cc-Learning- Reference-Functions.

Verify and upload the code, then the LED starts blinking.

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0