PROJECT 1: BLINKING A LIGHT EMITTING DIODE (LED) #
In this experiment, we are going to make a program which controls the Arduino pin output high level (+5V) and low level (0V) to make the LED (Light Emitting Diode) connected to the Arduino’s pins blinks.
At the end of this experiment, you will be able: To control the LED using an onboard microcontroller, blink the LED ON and OFF with delay in between.
- Component knowledge
A Light Emitting Diode (LED) is an electronic component that emits light when electric current passes through it. It consists of a semiconductor material, usually made of silicon that allows the flow of current in one direction. When electrons move through the material, they release energy in the form of photons, which we see as visible light.
Figure: Light Emitting Diode (LED)
An LED typically has two pins:
- Anode (+): This is usually the longer pin, which is the positive terminal. It connects to the positive side of the power source.
- Cathode (-): This is usually the shorter pin, which is the negative terminal. It connects to the negative side of the power source. The cathode side may also be marked by a flat edge at the base of the LED.
How to Test LED Pins?
To test the pins of an LED, you can use a multimeter set to diode mode:
- Set the Multimeter: Turn the multimeter to the diode testing mode.
- Connect the Probes:
- Connect the red probe to the anode (long pin).
- Connect the black probe to the cathode (short pin).
- Check the Reading:
- If the LED is functional, it will glow faintly, and the multimeter will display a voltage drop (typically around 1.5V to 3.3V, depending on the LED type).
- If no light appears or the reading is zero, try reversing the probes.
NOTE: If the LED doesn’t light up in either orientation, the LED may be faulty.
- Materials to use
- Circuit diagram
Now, we will use the IO port of the control board to provide power for the LED. Pin 8 of the control board is the digital pin. It can output high level or low level. In this way, the control board can control the state of the LED.
Figure: Circuit diagram for LED connection to the arduino
Figure: Components to be used on the board for the experiment
- Arduino Code
In order to make the LED blink, we need to make pin 8 of the control board output high and low level alternately. We highly recommend you type the code manually instead of copying and pasting, so that you can develop your coding skills and learn more.
// the setup function runs once when you press reset or power the board
int ledPin=8; //definition digital 8 pins as pin to control the LED
void setup (){
// Declare digital pin 8 which is to ledPin8 as an output
pinMode (ledPin, OUTPUT); //Set the digital pin 8, OUTPUT pin Mode
}
void loop (){
// the loop function runs over and over again forever
digitalWrite (ledPin, HIGH); //turn the LED on (HIGH is the voltage level = 5V)
delay (1000); //wait for a second, Set the delay time, 1000 = 1S
digitalWrite (ledPin, LOW); //turn the LED off by making the voltage LOW
delay (1000); //wait for a second, Set the delay time, 1000 = 1S
}
- Code knowledge
The code usually contains two basic functions: void setup() and void loop().
When you reset the control board, it runs the setup() function first and then runs the loop() function.
You generally use the setup() function to write code that initializes the hardware.
Use the loop() function to write code that performs specific tasks, and it runs repeatedly. When the execution reaches the end of loop(), it will jump to the beginning of loop() to run again.
Resetting will restart the code from the beginning. If you switch on the power, after finishing uploading the code and pressing the reset button will trigger reset operation.
// the setup function runs once when you press reset or power OFF the board
void setup (){
...
}
void loop (){
// the loop function runs over and over again forever
...
}
In the setup () function, first, we set pin 8 of the control board as output mode, which can make the port output high level or low level.
int ledPin=8; //definition digital 8 pins as pin to control the LED
void setup (){
// Declare digital pin 8 which is to ledPin8 as an output
pinMode (ledPin, OUTPUT); //Set the digital pin 8, OUTPUT pin Mode
}
First, in the loop() function, set pin 8 of the control board to output a high level so that the LED lights up.
digitalWrite (ledPin, HIGH); //turn the LED on (HIGH is the voltage level = 5V)
Next, wait for 1000 ms, which is 1 s. The delay() function is used to make the control board pause briefly before executing the next statement. The parameter indicates the number of milliseconds to wait for.
delay (1000); //wait for a second, Set the delay time, 1000 = 1S
Then, set pin 8 to output low level, and the LED turns off. Subsequently, one second later, the execution of the loop() function will be completed.
digitalWrite (ledPin, LOW); //turn the LED off by making the voltage LOW
delay (1000); //wait for a second, Set the delay time, 1000 = 1S
As a result, the loop() function is constantly being executed, so the LED will keep blinking. The functions above are standard Arduino IDE functions. The Arduino IDE already defines them, and you can call them directly.
In addition, we will introduce more common standard functions in later experiments. For more standard functions and the specific use method, please visit https://docs.arduino.cc/language-reference/
- Results
Verify and upload the code, then the LED starts blinking
Figure: Output results where LED lights up