Arduino project: make the LED blink

I'm introducing ArduinoIntroduction to Arduino.

In this tutorial, I want to build the first Arduino project. We will turn on and off the LED lights.

You will learn how to create your first Arduino program, how to upload it to the Arduino board via USB, and how to write to digital I/O pins.

This is a very simple project, but if you are the first time, you will learn a lot about Arduino.

I will useArduino Uno rev 3Clone the plate.

If you already have an Arduino development board, you can use the existing development board. The important thing to note here is that the board should work on 5V I/O pins.

Some boards (such as Arduino MKR WiFi 1010) only work with 3.3VI/O pins. If that’s your board, that’s okay, but remember that there is a difference between the twoVoltage.

This is the board:

We can power it through USB-B port or battery (9V battery works well because the recommended input voltage is 7-12V):

We have a set of power pins and analog I/O pins on one side:

There is also a set of digital I/O pins on the other side:

Let's build a simple circuit that lights up the LED. We use a 1kΩ resistor, a 5mm yellow LED, and connect it to-with+as usual:

This+with-Connect to the Arduino power pins for 5V and GND:

As you can see, when we use the battery to power the Arduino, the LED will light up:

Now, in this circuit, the Arduino does nothing useful except for scaling the 9V in the input provided by the battery to 5V.

Let's make the LED blink by writing the first Arduino program.

For this, we must first install the Arduino IDE on the computer.

go withhttps://www.arduino.cc/en/softwareAnd select your operating system version:

After downloading the software, on macOS, you need to moveArduinoApply to yourApplicationsfolder. an examinationWindowswithLinuxInstallation Notes.

Start the program and you should see a blank program:

As inIntroduction to Arduino programming languageTutorial,setup()The function is executed once immediately after the program is started, for example, the pin mode is usually set here.

Thisloop()The function is executed continuously and cyclically forever.

In our program, we first set the digital I/O pin number 13 as an output pin:

#define LED_PIN 13

void setup() {
    // Configure pin 13 to be a digital output
    pinMode(LED_PIN, OUTPUT);
}

Then inloop()We tell Arduino to write high level (5V), wait 1 second, write low level (0V = ground), wait 1 second, and then repeat forever:

void loop() {
    digitalWrite(LED_PIN, HIGH);
    delay(1000);
    digitalWrite(LED_PIN, LOW);
    delay(1000);
}

HIGHwithLOWIt is the constant provided by our Arduino program by default.

digitalWrite()Write the HIGH or LOW value to a specific digital output pin. You pass the pin number and HIGH or LOW as parameters.

delay()Specifies the number of milliseconds to pause the program as a parameter.

When starting the program for the first time, you may have to select the Arduino board on the motherboard.Tools -> Portmenu.

Make sure the Arduino is connected to the computer.

I use a MacBook Pro with a USB-C port, and the Arduino has a USB-B port, so I use an adapter.

Before compiling the program and writing it to the Arduino, you need to save the file. You can save it anywhere. I created aArduinoThe folder is placed in my Documents folder, and all the Arduino programs I write are stored here.

Click the upload button (the button with the right arrow), the program should be compiled and installed on the Arduino. The LED should start blinking.

Now, if you disconnect the USB cable from the computer, you will see the LED go out because the Arduino is no longer supplying power.

However, if you connect the USB cable to a normal USB charger, the LED will flash. No need for a computer anymore. Arduino runs the program we loaded, only this program, no other software is running.

And start and run the program immediately after power-on.


More electronic tutorials: