【Follow me Season 2 Episode 2】+ Getting Started Tasks: Setting up the Environment, Blink, and Serial Port Printing Output
[Copy link]
This post was last edited by cqutmianma on 2024-9-30 00:43
1. Environment Construction
First, download Arduino IDE https://www.arduino.cc from the official website according to your computer's operating system and install it. Then open Arduino IDE and connect the Arduino UNO R4 development board to the computer via a type-c cable. At this time, you need to select the development board version model connected to the serial port on the IDE as shown in Figure 1 below. The corresponding software package will pop up in the lower right corner automatically, as shown in Figure 2 below. Click and wait for the installation to complete.
Figure 1. Select the Arduino UNO R4 development board to connect
Figure 2. Waiting for the software package to be installed
2. Run Blink
Click File, Create a New Project, and then Save. The first thing we need to achieve is lighting, yes, hahahaha, a qualified embedded engineer must be able to light various lights.
Open the schematic diagram of Arduino UNO R4, as shown in Figure 3 below, you can see that the pin of the LED is P102.
Figure 3. LED pin control diagram
From the schematic diagram, we can see that directly controlling the high and low of P102 can achieve the flipping of the DL4 light. According to the pin diagram of Arduino UNO R4, Figure 4, we can know that P102 is D13 of the development board.
Figure 4. Development board pin diagram
So we can directly control D13 in the code. The specific code is as follows, which mainly flips the level once every 500ms. Before each flip, we read the level first, and then take the inverted level to flip.
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT); /* 设置13引脚为输出模式 */
digitalWrite(13, HIGH); /* 默认输出为高*/
}
void loop() {
// put your main code here, to run repeatedly:
delay(500);
digitalWrite(13, !digitalRead(13)); /* 每500ms进行一次LED灯的翻转*/
}
3. Serial port output
The official documentation tells us that D0 and D1 are used for USB serial port input and output. So we don't need to configure the IO of serial port 0, we can just use it directly.
First, initialize the baud rate to 115200, and then output the serial port data every 500ms directly in the loop function. The specific code is as follows:
/*串口初始化函数*/
void uart_init(uint32_t baud)
{
Serial.begin(baud);
}
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT); /* 设置13引脚为输出模式 */
digitalWrite(13, HIGH); /* 默认输出为高*/
uart_init(115200); /* 串口初始化 */
}
void loop() {
// put your main code here, to run repeatedly:
delay(500);
digitalWrite(13, !digitalRead(13)); /* 每500ms进行一次LED灯的翻转*/
Serial.println("Hello EEWorld !");
}
Then open the serial port monitor and set the baud rate to 115200. You can see that the serial port monitor outputs a frame of serial port data every 500ms.
4. Display videos and source code files
|