Arduino Getting Started to Proficient in Experiment 1: Let Arduino Say Hello World

1、Hello World!

First of all, let's practice a simple experiment that does not require other auxiliary components, only needs an Arduino and a download cable, let our Arduino say "Hello World!", this is an experiment for Arduino and PC to communicate, which is also An introductory experiment, hoping to lead everyone into the world of Arduino.

2. Electronic originals and electronic modules used in the experiment:

1. Arduino board 1 2.
USB data cable
Arduino controller
USB download cable

After we have installed the Arduino driver as described above, we will open the Arduino software and write a program to make the Arduino display the "Hello World!" string after receiving the command we sent. Of course, you can also make the Arduino not accept any The command will continue to echo "Hello World!". It's actually very simple. An if() statement can make your Arduino obey your command. When commanded, the LED flashes once, and then displays "Hello World!"

3. The following is a reference procedure for you.

//
int val;//定义变量val
int ledpin=13;//定义数字接口13
void setup()
{
    
    
  Serial.begin(9600);//设置波特率为9600,这里要跟软件设置相一致。当接入特定设备(如:蓝牙)时,我们也要跟其他设备的波特率达到一致。
  pinMode(ledpin,OUTPUT);//设置数字13 口为输出接口,Arduino 上我们用到的I/O 口都要进行类似这样的定义。
}
void loop()
{
    
    
  val=Serial.read();//读取PC 机发送给Arduino 的指令或字符,并将该指令或字符赋给val
  if(val=='R')//判断接收到的指令或字符是否是“R”。
  {
    
      //如果接收到的是“R”字符
    digitalWrite(ledpin,HIGH);//点亮数字13 口LED。
    delay(500);
    digitalWrite(ledpin,LOW);//熄灭数字13 口LED
    delay(500);
    Serial.println("Hello World!");//显示“Hello World!”字符串
  }
}

Click Serial Monitor under Tools

Enter R
LED light will flash once;
will receive message Hello World from Arduino

The experiment is over.
Note: The COM port must be selected correctly, otherwise the data will not be received. The following figure shows the COM port selection.

Port selection is correct!

Learn more about embedded knowledge. Welcome everyone to pay attention to the WeChat public account [zero-based play embedded] reply "resources" in the background of the WeChat public account to get a full set of embedded resources
insert image description here

Guess you like

Origin blog.csdn.net/qq_45172832/article/details/113593254