Blink (闪光) - My First Arduino Project (我的第一个微处理器项目)

In this project, you'll learn about the Arduino (微处理器) IDE programming environment (集成开发环境) and how to make a LED light (发光二极管) blink (眨).

Parts needed: Software stuff you'll learn: Arduino USB cable
1 If the Arduino IDE program has not been installed on your computer, you can click here to download and install (安装) the program. Otherwise, go to step 2.
2 Double-click on the Arduino icon to start the Arduino IDE program.

Click here if you are using the Arduino Nano instead of the Uno.
Click here for fast track (快速通道), or continue on to step 3.
Arduino icon
3 Create a new program by selecting File from the menu
  • then select New
Bare minimum
4 You will see this template (模板).







Commands (命令) that you put inside the void setup() routine are executed (执行) only once (一次) on startup (启动)or on reset (重设).

"Inside" means between the two curly brackets { and }.


Commands that you put inside the void loop() routine are executed repeatedly forever (反复永远) (or until you turn off the power)


Lines starting with the two slashes // are comments (评语), and they are ignored (忽视) by the computer. In other words, to save time, you don't need to type anything that follows the two slashes (斜线).

Comments (评语) are there to tell you what the code (代码) is doing.
BareMinimum template
5 There is a built-in (内置) light (LED) (发光二极管) on the Arduino board that is connected (连接) to digital I/O pin 13. Arduino digital I/O pins and built-in LED
6 To control the LED (发光二极管) we need to issue (发出) two commands (命令) to the Arduino:







First, we use the pinMode command to tell the Arduino that we will be controlling (控制) the LED that is connected to pin 13 for OUTPUT.

Second, we use the digitalWrite command (命令) to send a HIGH signal (信号) to pin 13. HIGH means to turn on (打开) the LED.

Type in these two commands inside the void setup() routine as shown. Commands are inside the void setup() routine if they are placed between the open and close braces {  }. Do not put commands outside of the braces (大括号).

A semicolon (分号) is placed after each command line; in this case the pinMode and digitalWrite commands. Always remember to do this.
commands to turn on LED
7 Connect your Arduino board to your computer using the USB cable (数据线). USB cable
8 From the Arduino IDE (集成开发环境) program menu, select Tools
  • then select Serial Port (系列站点)
  • then select the COM port that your Arduino is connected to. This usually is the COM port with the largest number.
If you don't see one, then you need to follow these instructions (指导) to install (安装) the driver.
selecting COM port
9 Press the upload button to upload (上载) the program onto the Arduino board. Upload icon

If the upload is successful (成功) you should see the words Done uploading (上载) at the bottom, and the Arduino will automatically (自动) run the program.


You should see the LED on the Arduino board turned on (打开).

Done uploading

If you have selected the wrong COM port or if there are errors (错误) in your program, you will see an orange error message (错误信息) at the bottom. You'll have to correct the error(s) and then upload your program again.

COM port error
10 To make the LED blink (闪烁), we need to turn on and then turn off the LED.










To turn off the LED, we use another digitalWrite command, but this time we send a LOW signal instead (代替). LOW means to turn off (关掉) the LED.
commands to turn on and off LED
11 Upload the program again. Did you see the LED blink? --- NO

You only see the LED off because the Arduino executed (执行) the two digitalWrite commands (one to turn on the LED followed by the one to turn off the LED) so fast (很快) that you can't even see (看不到) it when it is turned on for a fraction (分数) of a second (秒).
12 To keep the LED on a little longer so that you can see it, we need to use the delay command to make the Arduino wait a bit (等一会儿) before continuing (继续).









The number specified (指定) in the delay command is the number of milliseconds (毫秒) to wait.

1000 ms = 1 sec
delay
13 Upload the program again. What do you see? --- The LED turns on for 1 second and then it turns off (关闭).
14 To make the LED blink continuously (持续), we need to keep repeating (重复) the on and off sequence.













To do that, you move the two digitalWrite commands and the delay command from the void setup() routine to the void loop() routine. Remember that commands inside the void loop() routine are executed (执行) repeatedly forever.
half blink
15 Upload the program again. What do you see? --- The LED just stays on. Why?

Because there's no delay after the digitalWrite LOW command and the loop goes back right away to the digitalWrite HIGH command.
16 Solution (解决), we need another delay command after turning off the LED before it repeats the loop to turn the LED on again. blink
17 Upload the program again. This time you'll see the LED blink on for 1 second and then off for 1 second.


Congratulations (祝贺)! You've completed your first Arduino project to make a LED blink!

Now click on the Experiments (实验) button below to test (测试) yourself.