In this lesson, we will program the Arduino's GPIO output high level (+5V) and low level (0V), and then make
the LED which is connected to the Arduino’s GPIO flicker with a certain frequency.
1. What is the LED?The LED is the abbreviation of light emitting diode. It is usually made of gallium arsenide, gallium phosphide
semiconductor materials. The LED has two electrodes: a positive electrode and a negative one. It lights up only when a forward current passes, and it can flash red, blue, green, yellow, etc. The colour of the light depends on the material it is made.
In general, the drive current for LED is 5-20mA. Therefore, in reality it usually needs an extra resistor for current limitation so as to protect the LED.
The main function of the resistor is to limit currents. In the circuit, the character ‘R’ represents resistor, and
the unit of resistor is ohm(Ω).
A band resistor is used in this experiment. It is a resistor with a surface coated with some particular colour
through which the resistance can be identified directly.
There are two methods for connecting LED to pins of an Arduino board:
As shown in the schematic diagram, the anode of the LED is connected to Arduino’s GPIO via a resistor, and
the cathode to the ground (GND). When the GPIO outputs high level, the LED is on; when it outputs low,
the LED is off.
The resistance of a current-limiting resistor is calculated as follows: 5~20mA current is required to make an
LED on, and the output voltage of the Arduino UNO’s GPIO is 5V, so we can get the resistance:
R = U / I = 5V / (5~20mA) = 250Ω~1kΩ
Since an LED is a resistor itself, here we use a 220ohm resistor.
As shown in the schematic diagram above, the anode of LED is connected to VCC(+5V), and the cathode of LED is connected to the Arduino’s GPIO. When the GPIO output low level, the LED is on; when the GPIO output high level, the LED is off.
The experiment is made based on method 1 – use pin D8 of the Arduino board to control an LED. When D8 is programmed to output high level, the LED will be turned on. Next, delay for some time. Then D8 is programmed to output low level to turn the LED off. Repeat the above process and you can get a blinking LED then.3.
Key functions:
setup()
The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start using
libraries, etc. The setup function will only run once, after each powerup or reset of the Arduino board.
loop()
After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely
what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to
actively control the Arduino board.
pinMode()
Configures the specified pin to behave either as an input or an output.
As of Arduino 1.0.1, it is possible to enable the internal pullup resistors with the mode INPUT_PULLUP.
Additionally, the INPUT mode explicitly disables the internal pullups.
digitalWrite()
Write a HIGH or a LOW value to a digital pin.
If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding
value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup
on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up
resistor.
delay()
Pauses the program for the amount of time (in millisecond’s) specified as parameter. (There are 1000
milliseconds in a second.)
Procedure:
Step 1: Build the circuit.
Step 2: Program
Step 3: Compile the program and upload to Arduino UNO board. Now you can see the LED blinking.
Comments
Please log in or sign up to comment.