Перейти к содержимому

Led builtin какой пин

  • автор:

The Arduino built-in LED

Arduino boards come with a little utility: the built-in LED.

It is identified by the letter L next to it. On the Arduino Uno, it is near pin #13:

On the Arduino MKR 1010 WiFi it is near the 5V output pin:

This LED is connected to the digital I/O pin #13 in most boards. In some boards, like the Arduino MKR series, it’s linked to the pin #6.

In any case you can reference the exact pin using the LED_BUILTIN constant, that is always correctly mapped by the Arduino IDE to the correct pin, depending on the board you are compiling for.

To light up the LED, first you need to set the pin to be an output in setup() :

pinMode(LED_BUILTIN, OUTPUT);

Then you can send it a HIGH signal:

digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(LED_BUILTIN, 1);

Here is a simple program that makes the built-in LED blink every second:

void setup()   pinMode(LED_BUILTIN, OUTPUT); > void loop()   digitalWrite(LED_BUILTIN, HIGH);  delay(1000);  digitalWrite(LED_BUILTIN, LOW);  delay(1000); >

I wrote 16 books for beginner software developers:

You can download them all at $0 cost, all you need to do is subscribe to my newsletter:

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that’s present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it’s only collected for the primary purpose of keeping you informed through the newsletter. It’s stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

THE VALLEY OF CODE

THE WEB DEVELOPER’s MANUAL

You might be interested in those things I do:

  • Learn to code in THE VALLEY OF CODE, your your web development manual
  • Find a ton of Web Development projects to learn modern tech stacks in practice in THE VALLEY OF CODE PRO
  • I wrote 16 books for beginner software developers, DOWNLOAD THEM NOW
  • Every year I organize a hands-on cohort course coding BOOTCAMP to teach you how to build a complex, modern Web Application in practice (next edition February-March-April-May 2024)
  • Learn how to start a solopreneur business on the Internet with SOLO LAB (next edition in 2024)
  • Find me on X

# Controlling LEDs

So far everything we have seen does not involve controlling any hardware electronics using the Arduino.

We will start by writing a simple Sketch that turns on the built-in LED on the Uno board. It is the small LED labeled L on the board.

There are two kinds of pins, digital and analog. We will not cover the analog pins for now. The digital pins mean they can be either HIGH or LOW . This also means we either output 5V through that pin or 0V.

A pin can be either INPUT or OUTPUT . If we are outputting voltage from the Arduino board to an electronic component connected to that pin, then our pin is OUTPUT pin. If the electronic component is producing voltage through that pin, and the Arduino board is reading that voltage then the pin is INPUT pin. The INPUT or OUTPUT mode is with respect to the Arduino board. If we want to connect an LED to the Arduino, we will want to output voltage from the Arduino to the LED. Therefore, the pin we connect the LED to will be in OUTPUT mode. The digital pins can be seen on the top side of the Uno board labeled from 0 to 13.

On the Uno board, there is a built-in LED next to the L letter. This LED is connected to pin 13 internally. In the Arduino platform, if you use LED_BUILTIN in your code, it will be translated into 13 since that is the pin for the built-in LED.

The above program turns on the built-in LED and then waits for 3 seconds.

# pinMode

In the setup function, the first thing we do is specify for each pin we are using whether that pin is going to be used as an output or input pin. We do that by calling the Arduino function pinMode(pinNumber, mode); . The pinNumber is the number of the pin we want to specify its mode, and the mode is whether it is OUTPUT or INPUT . On line 2 of the program, we are specifying that pin 13 is an output pin. This means we will be deciding if the pin has 5V or 0V. If we output 5V then the LED will turn on.

# digitalWrite

Line 7 specifies the output to be HIGH to the LED_BUILTIN pin. Which is the same if we use 13 instead of LED_BUILTIN . We specify the output to a digital pin by using the Arduino function digitalWrite(pinNumber, HIGHorLOW); .

In the above code, can you turn the built-in LED off after 3 seconds?

# Blinking LED

We want to continuously blink the built-in LED every second. This sounds like a task for the loop function!

Arduino for STM32

Pin Names for LED_BUILTIN in Arduino 1.8.15 (STM32F401RE).

Development environment specific, Arduino, Eclipse, VS2013, Em::Blocks etc
10 posts • Page 1 of 1
Vadim777 Posts: 5 Joined: Fri Jun 18, 2021 6:37 pm

Pin Names for LED_BUILTIN in Arduino 1.8.15 (STM32F401RE).

Post by Vadim777 » Fri Jun 18, 2021 6:52 pm

I’m going to use the Blink example from Arduino IDE 1.8.15 (my board is STM32F401RE).
How shall I put the names of the built-in LED into an Arduino sketch? Various names (D13, 13, LED_BUILTIN, etc.) don’t work for me.
LED doesn’t blink.
Upload via Mass Storage works (MBED Blink Example; .BIN export; DragNDrop to NUCLEO storage).

Thanks in advance,

mrburnette Posts: 633 Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Pin Names for LED_BUILTIN in Arduino 1.8.15 (STM32F401RE).

Post by mrburnette » Sat Jun 19, 2021 12:34 am

Vadim777 post_[code wrote: [/code]id=7575 time=1624042360 user_id=1506]
Hi all.

I’m going to use the Blink example from Arduino IDE 1.8.15 (my board is STM32F401RE).
How shall I put the names of the built-in LED into an Arduino sketch? Various names (D13, 13, LED_BUILTIN, etc.) don’t work for me.

The LED_BUILTIN is a «board» selection in the IDE, not by chip architecture/model.

The variant header sets the variable as below: (from Example)

//On-board LED pin number #ifndef LED_BUILTIN #define LED_BUILTIN PC9 #endif

So, looking at supported boards using your uC, you must select from:
https://github.com/stm32duino/Arduino_C . -64-boards

STM32F401RE Nucleo F401RE 0.2.1
or
STM32F401RB
STM32F401RC
STM32F401RD
STM32F401RE Generic Board 1.8.0

If you are using a generic board, you likely need to define your own LED_BUILTIN

#1 · Blinking Arduino’s builtin LED

Bas on Tech earns a small fee for each sale made via these links.
All products shown are not being sold by Bas on Tech.

Summary

The video is a tutorial on using an Arduino board, which is a microcontroller that can read and write signals. Bas explains the different parts of the board, like the USB connector, power plug, digital and analog pins, power section, and reset button. They also show you how to use the Arduino IDE to upload code and run programs. In the tutorial, Bas demonstrates how to make an LED on the board blink using a pre-written example code from the IDE. They also mention that there are many other examples available for different sensors, displays, GSM, and SD card readers that can be used for future projects.

Introduction

When I bought my first Arduino there was no YouTube with fancy instruction videos. What I saw was: a printed circuit board with chips, pins, a button and LEDs. The only thing I could think of was to start as fast as possible by just trying. I skipped explanations of the basics because I wanted to get my LCD display working now, not after reading boring theory.

Soon I discovered that getting things to work was not as simple as it looked in the first place. A tough lesson was that I could even damage components when I wired things the wrong way. If you don’t mind things being damaged, by all means go ahead and discover yourself ��. If you want a little bit more guidance, please continue reading.

I could a start an endless story on electronics, bombarding you with circuit diagrams and stories about signals. That is exactly what Bas on Tech tends not to do. You find this information on many webpages with a click of the mouse. This is our first Arduino tutorial and therefore I have to explain some things to get you started. This will make it easier for you to understand the working of the circuit. Something which I prefer over just copy/pasting the Arduino code (can’t wait? You can download the code with the course material button at the bottom of this page).

Course material

At the bottom of this page you’ll find the course material button. This button allows you to download the code, circuit diagram and other files relevant to this Arduino tutorial.

What is an Arduino?

The Arduino is a so called microcontroller. It processes analog and digital signals and can react to these. The heart of the Arduino is the AVR-chip. What this chip has to do is written in a program written in Arduino code. Via an USB cable the compiled program could be uploaded to the Arduino. The possibilities are endless: mp3 player, mobile phone, robot, weather station, game computer, RC cards, home automation and much much more!

The design of the Arduino is open source. This means that the manufacturer is allowed to bring its own Arduino to market. There are lots of Chinese «Arduino clones» sold. These can do exactly the same as the original Arduino. Often cheaper components are being used which is not an advantage. Clones could need special drivers being installed, while the original is just plug and play.

In this tutorial I am using the Arduino Uno, but there are many more like the Nano, Pro Mini, Micro and Mega. Each one has its own pros and cons. This could be the amount of inputs / outputs, speed but als the form factor. In this video I show the differences between several Arduino boards.

How to progam with the Arduino IDE?

In this Arduino tutorial we’re going to program the builtin LED. The tool we are going to use is the Arduino IDE which is freely available on the Arduino website

IDE is an abbreviation of Integrated Development Environment. A tool which converts your code into the bits and bytes which the Arduino understands. This process is called compiling. Besides compiling the IDE also checks if the code is correct. In the next tutorials you’ll learn more about this.

Arduino CH340/CH341 drivers

Some non-original Arduinos might require a driver to be installed. This is because these boards are using the CH340/CH341 chip for USB communication with your computer. You’ll find more information about this driver on the Sparkfun website.

Which pins does the Arduino have?

For this tutorial I use the Arduino UNO, which has many pins to connect components to. When reading the word pins you might expect solid metal pins. For the UNO this is not the case. The UNO uses female headers. Instead the Arduino Nano uses real pins. Since the hardware implementation can differ per Arduino the common name is pins.

Arduino UNO with large AVR-chip

At the top of the Arduino you’ll see the digital pins. These can only have one of these two values: HIGH or LOW . When high, a small current flow through the pin. For low it does not. On a 5V Arduino, this current is 5V, for the 3.3V variant this is 3.3V.

The bottom right shows the analog pins, which has 1024 possible values: 0 to 1023. On the bottom left you’ll see the current pins like 5V, 3.3V, ground GND and the reset pin RES .

The Arduino UNO is also equipped with some special pins. One of these is pin 13 (top right). This pin is connected with the builtin LED. This is a LED which you can program and is not being used by the Arduino. The builtin LED is marked L on the PCB.

In the picture of the Arduino UNO you see a large chip. This is the AVR-chip, the heart of the Arduino. You might see a smaller chip in the center of your Arduino. This is the SMD variant of the chip. It is a lot smaller and not easy to replace.

Arduino Code

Time to dive into the code �� For this tutorial we are going to use off the shelf example code. In the op menu of the Arduino IDE you can choose:

File ▸ Examples ▸ 01. Basics ▸ Blink

The IDE should open the code to blink the builtin LED automatically. Take some time to read the code before you continue. Are you able to figure what each line does?

1 // the setup function runs once when you press reset 2 // or power the board 3 void setup() < 4 // initialize digital pin LED_BUILTIN as an output. 5 pinMode(LED_BUILTIN, OUTPUT); 6 > 7 8 // the loop function runs over and over again forever 9 void loop() < 10 digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) 11 delay(1000); // wait for a second 12 digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW 13 delay(1000); // wait for a second 14 > 

In the code you see all kinds of commands. These commands are written in a syntax that the computer understands. Misplacing a dot or comma could result in the computer being unreadable to read your code. No worries, as a software developer this where you run into many times and is part of the job.

Comments in the code

Line 1, 2 and 8 start with two slashes // . This line of code is what we call comment. Comments allow you to provide human readable additional information which is completely ignored by the computer. Another form of comment starts with /* and ends with */ . Everything between the start and end will be seen as part of the comment e.g.:

1 /* 2 This is comment 3 over 4 multiple lines 5 */ 

You can use this syntax for a single line comments as well:

1 /* This is a single line comment */ 

The setup() function

The code has been split into two parts: setup and loop. We refer to these blocks as functions. In another tutorial we are going to dive deeper in how to make your own functions. The indenting is not mandatory, but helps to quickly see what lines belong to the function.

When the program starts it executes the setup() function once. After this the program continues with the loop() . The setup() is for example used to assign pins.

In this example we are using the pinMode function to specify we want to use the LED_BUILTIN pin as a OUTPUT . On the Arduino UNO, LED_BUILTIN is an alias for 13 (the builtin LED pin). Therefore you could have typed 13 as well. The advantage of using LED_BUILTIN is that it works on all Arduinos. Even when the builtin LED is connected to another pin.

The loop() function

After the setup() , the program continues with the loop() . This function will be called over and over again. Every time the function has finished, it will be called again. The only way to stop the program is to disconnect the power or to upload a new program.

If you look closely at the code you see two other functions being called: digitalWrite() and delay() . digitalWrite() writes the value ( LOW or HIGH ) specified to a given pin. The pin we are using is specified by the function’s first parameter, in our case LED_BUILTIN . The second parameter specifies the written value, here HIGH . digitalWrite() in this case, makes sure that the LED on pin 13 will be lit.

The next line shows the delay() function with a single parameter. Can you think about what the value of this parameter represents?

Its value is the amount milliseconds the program has to wait. In our case this is 1000 milliseconds, which is the equivalent of 1 second.

After this brief pause, the program continues and writes LOW to the LED_BUILTIN pin. As a result the LED goes off.

Uploading code to the Arduino

Now our program is ready to upload to the Arduino. First we have to connect our Arduino to the computer with the USB cable. Make sure you’ve selected the correct board in the IDE:

Tools ▸ Board ▸ Arduino/Genuino UNO

and the correct port:

If you are not sure which port to use, try them all until you can successfully upload your code.

The top left shows a round button with a checkmark. By pressing this button you tell the IDE to verify your code for possible errors. The IDE only checks if it can read your code. It does not check if you have written correct code for what you are trying to program.

If everything works the IDE shows the Compiling completed message. You can now upload your code by pressing the round button with the arrow to the right. The uploading is complete when the Avrdude done. Thank you. messages appears. Your program will immediately start after uploading. As a result you should now see your Arduino LED blink with 1000ms intervals.

Challenge ��

Great to see you made it this far! Are you ready for a challenge?

Change the code so the LED tuns on, wait for 0.5 second, turn off the LED and wait for 2 seconds.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *