Program Your First LED With Arduino
30-45 min
Ages 14+
What Will You Learn?
You will learn to program a LED with Arduino to control it easily, using basic programming concepts.
Make Breadboard Connections
Step 1
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.
Step 2
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.
Step 3
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
Step 4
Now you will download the Arduino IDE. Go to the Arduino page (https://www.arduino.cc/ ) Select software, and then your operating system. When you finish downloading, you must install it.
Step 5
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
Step 6
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.
Step 7
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
Step 8
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.
Step 9
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.
Step 10
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).
Step 11
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.
Step 12
Finally, download it with the arrow button.
You will see how the greenLed turns on.
Other LED Setup
Step 13
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.
Step 14
Then under the “void setup” section you will write:
“
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
“
Step 15
Finally, under the “void loop” section you will write:
“
digitalWrite(redLed, HIGH);
digitalWrite(yellowLed, HIGH);
“
Step 16
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
Step 17
Great, your LEDs are already on, now at the end of the “void loop” section you will write:
“
delay(500);
digitalWrite(greenLed, LOW);
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, LOW);
delay(500);
“
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.
Step 18
Upload the code to your arduino, and see it in action.
Create a Sequence With Your LEDs
Step 19
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:
“
digitalWrite(greenLed, LOW);
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, LOW);
delay(500);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, LOW);
delay(500);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, HIGH);
digitalWrite(yellowLed, LOW);
delay(500);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, HIGH);
digitalWrite(yellowLed, HIGH);
delay(500);
“
Step 20
Upload the code to your arduino, and test it.
What's Next?
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.
Further Resources
Learn More!
About MoonMakers
MoonMakers — led by Camila and Diego Luna — are a community of creators passionate about knowledge. A Makerspace, an open space with different digital manufacturing machines. And a YouTube channel where we promote science, technology and the maker movement.
MoonMakers have collaborated with companies such as: Sesame Street, Make Community and in Mexico with Educational Television and Fundación Televisa, creating educational content.
We have given workshops throughout the Mexican Republic with: Talent Land, Secretary of Education in Jalisco, Conacyt, Centro Cultural España.
Materials:
- Arduino microcontroller (We will use the Arduino Nano 33 loT, but you can use the Arduino microcontroller of your choice.)
- A micro usb cable
- A computer
- 3 LEDs, red, yellow, and green
- 3 330 ohm resistors
- 4 cables
- Breadboard
Vocabulary:
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.).