Program Your First LED With Arduino
What Will You Learn?
You will learn to program a LED with Arduino to control it easily, using basic programming concepts.
Make Breadboard Connections
You will start making the connections on your breadboard, place a wire in GND of your Arduino and the other end to the negative line of your breadboard.
Then place the resistors with one pin on the GND line that we just placed, and the other to a different line on our breadboard.
After the light emitting diodes, better known as LEDs, these have two pins of different sizes, the longest is the positive (+) and the other, the short, which is the negative (-). Take the Yellow Led and connect the positive (long) pin to the D4 pin of your Arduino. The negative pin (small) to the same line of one of your resistors. You will do the same with the following LEDs, green to pin D3 and Red to pin D2, as in image 3.
Arduino IDE Setup
When it is installed, we will go to Tools – Board – Board Manager and put the name of the board that you will use, then install.
We will use the Arduino Nano 33 iOT.
Verify Operation of Your Board
You can verify the operation of your board, selecting one of the example codes. Go to File – Examples – 01 Basics – Blink
Connect your Arduino and select Tools – Port – and then select the port where it is located.
Finally upload your program to the board, press the button (Arrow) at the top of our code with the button.
You will see how the Led integrated in your Arduino microcontroller will blink. The code you just uploaded makes the Led turn on and off every second infinitely.
Turn on the First LED
With everything ready, let’s turn on our first Led, writing at the top:
“int greenLed = 2”.
You defined your variable with the name Green Led, and then the pin where it is located.
Now under the “void setup” section:
“pinMode (greenLed, OUTPUT);”
This section is for before starting your program, this defines your pin as an output, these are the terminals of a microcontroller that allow you to deliver a voltage and create an action.
A tip: when you finish writing each of the lines of code, do not forget to write a semicolon (;) to end your instruction.
Now under the “void loop” section you will write:
“digitalWrite (greenLed, HIGH);”
This is to send electricity to the pin of your led, which we had previously defined as green led, making it turn on (HIGH).
Then press the check button at the top of our window, this will tell you if there is something wrong in your code, this will also save your project, so name it (“Hello world”) and place it somewhere on your computer.
Finally, download it with the arrow button.
You will see how the greenLed turns on.
Other LED Setup
Now you will create the variables of the two missing LEDs, repeating the same thing you did with the Green Led, write the following below your first variable:
int redLed = 3;
int yellowLed = 4;
Remember, you define the name of your variable and its location in the Arduino.
Then under the “void setup” section you will write:
Finally, under the “void loop” section you will write:
With your code finished, we verify the code (Check button), save it and upload your code to your Arduino microcontroller.
You will see how the 3 LEDs light up.
Blink Your LEDs
Great, your LEDs are already on, now at the end of the “void loop” section you will write:
When you have programmed your LEDs on, you added a wait of half a second, and then it will turn off, then it will wait half a second and the code will be repeated, making them turn on again, This is because they are within the brackets of the “void loop”, It will run infinitely.
Upload the code to your arduino, and see it in action.
Create a Sequence With Your LEDs
Create different sequence of LEDs with what we have just learned by modifying the waiting time and the LEDs
Here’s an example, copy and paste it into the “void loop” section:
Upload the code to your arduino, and test it.
You can experiment by changing the waiting time between each led, as well as adding different LEDs and colors. Use your creativity to create and experiment with what you have learned, for example creating a traffic light.
Pin: In electronics, it is called a pin, an English word that means “plug”, to the terminal or pin of each of the metal contacts of a connector or of a component made of an electrically conductive material.
LED: A light-emitting diode or led is a light source made of a semiconductor material equipped with two terminals, which emits light when activated.
Diode: A diode is an electronic component with two terminals that allows the circulation of electric current through it in one direction only, blocking the passage if the current flows in the opposite direction,
Microcontroller: A microcontroller is a programmable integrated circuit, capable of executing the orders recorded in its memory. It is composed of several functional blocks that fulfill a specific task.
Arduino: Arduino is a free software and hardware development company, as well as an international community that designs and manufactures hardware development boards to build digital devices and interactive devices that can detect and control real-world objects.
function: is a section of a program that calculates a value independently from the rest of the program
void setup: The setup() function is called when a sketch begins. Use it to initialize variables, pin modes, start using libraries, etc. The setup() function will only run once, after every power-up or reboot of the Arduino board.
void loop: After creating a setup() function, which initializes and sets initial values, the loop() function does just what its name suggests and repeats consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.
int: Integers are your main data type for storing numbers.
pinMode: Sets the specified pin to behave as an input or an output.
digitalWrite: Write a HIGH or LOW value to a digital pin. Its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
delay: Pauses the program for the time (in milliseconds) specified as a parameter. (There are 1000 milliseconds in a second.).