企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
Hello World是所有编程语言的第一课,不过在Arduino中,我们的Hello World叫做Blink。 Arduino提供了很多示例代码,使用这些示例代码,我们可以很轻松的开始我们的Arduino学习之旅。 如图,通过 Arduino**IDE菜单>文件>示例>01.Basics>Blink**找到我们要使用的例程,单击便可打开。 ![](https://www.arduino.cn/data/attachment/forum/201804/10/104543vm3tvyvuf0v3uufa.jpg) 注意:和以下代码不同,新版IDE自带例程使用的是宏LED\_BUILTIN,在大部分arduino开发板上,这个宏定义对应的是13引脚。以下代码只是为了让你知道控制的是13引脚。 打开后示例程序后可以看到以下代码: ``` /* Blink Turns an LED on for one second, then off for one second, repeatedly. Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to the correct LED pin independent of which board is used. If you want to know what pin the on-board LED is connected to on your Arduino model, check the Technical Specs of your board at: https://www.arduino.cc/en/Main/Products // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } ``` 这些代码的具体含义将在本书第三章中进行讲解。 在编译该程序前,需要先在 Arduino**IDE菜单>工具>开发板**中选择“**Arduino Uno**”选项,如图所示。 ![](https://www.arduino.cn/data/attachment/forum/201804/10/104645dg6gttu086p0lo3j.jpg) 如图1-25,接着在 Arduino IDE菜单>工具>端口 中选择Arduino Uno对应的串口。当Arduino IDE检测到Arduino Uno后,会在对应的串口名称后显示“Arduino/Arduino Uno”,以提示用户选择。 在Windows系统中,串口名称为“COM”加数字编号,如COM3; 在Mac OS中串口名称为“/dev/cu.usbmodem”加数字编号; 在Ubuntu中串口名称为“/dev/ttyACM”加数字编号。 ![](https://www.arduino.cn/data/attachment/forum/201804/10/104943cx97qttxaa09q9ta.jpg) 板卡和串口设置完成后,你可以在IDE的右下角看到当前设置的Arduino控制器型号,及对应串口。 接着点击 校验(Verify)按键,IDE会自动检测程序是否正确,如果程序没有错误,调试提示区会依次显示“正在编译项目…”、“编译完成”。 编译完成后,你将看到如图提示信息。 ![](https://www.arduino.cn/data/attachment/forum/201804/10/105109itk8vhzay2bykn8y.jpg) “928字节”为当前程序编译后的大小,括号中“最大32,256字节”指当前控制器可使用的Flash程序存储空间大小。如果程序有误,调试提示区会显示错误相关提示。 点击 上传(Upload)按键,调试提示区会显示“正在编译项目…”,很快该提示会变成“上传”,此时Arduino Uno上标有TX、RX的两个LED会快速闪烁,这说明你的程序正在被写入Arduino Uno中。 当显示“**上传成功**”时,说明该程序已经上传到Arduino中 大概5秒后,可以看到该段程序的效果——板子上的标有L的LED在按设定的程序闪烁了。 ![](https://box.kancloud.cn/721816e73efcd00201bf073f4bea449c_1140x544.jpg)