In this lesson, we'll learn below.
* analogWrite
* Variable number
* Constant number
-Table of Contents
0:00 What are full color LED and analogWrite
1:37 What we make
1:43 Material preparation
1:53 Circuit diagram
2:18 Assemble circuit
2:48 Make program
4:23 Write to board
4:34 Conclusion
-Introduction
Hardware engineer
9 years experience as a Maker
-SNS
Web : start-electronics.com/
Twitter : / buonoatsushi
#Electronics
-Content
We will continue with full-color LEDs and learn about analogWrite functions, variables, and constants.
In Lessons 3 and 4, we have learned how to input and output at two levels, HIGH and LOW: digital output and digital input. In the next two lessons, we will discuss analog outputs and inputs, which allow us to perform input/output at a more granular level. Let's start by learning analog output using full-color LEDs. As we learned in science, red, green, and blue are the three primary colors of light, and any color can be created by changing the ratio of these colors. The full-color LED has three LEDs corresponding to these three primary colors inside, and by adjusting the amount of light from each of them using the Arduino's analog output, a variety of colors can be expressed. The analog output can be adjusted in 256 steps for each color, so in principle it is possible to create 256 to the third power, or 16.77 million colors.
The analog output is a 490 Hz or 980 Hz pulse signal with a certain ON period and OFF period, called PWM. The ratio of this ON period to the period is called the duty ratio. This pulse signal actually makes the LEDs blink at high speed, but if the frequency is this high, the human eye cannot perceive the blinking, and the intensity of the light will appear to vary according to the duty ratio. This property is used to achieve light intensity adjustment. Note that while analog output generally refers to the ability to freely change the voltage, in the case of Arduino, it is only possible to change the duty ratio of the PWM signal.
This is the circuit we created this time, and as you can see, the color can be freely changed. Now let's make this circuit.
The materials we will use this time are an Arduino Uno, a breadboard, four male and four female jumper wires, a full-color LED, and three 330Ω resistors.
Here is the circuit diagram we will create. The full-color LED has three LEDs inside, as explained earlier. The type used in this project has a common cathode side, and we will connect a 330Ω resistor to each anode side. the Arduino has several pins marked with a wave symbol, and these pins serve both digital and analog output functions. Here, pins 9, 10, and 11 are used.
Next, here is a breadboard diagram following the schematic. The full-color LED has four legs, and the longest leg is the cathode, as shown here. Pay attention to the orientation, and let's build the circuit. Yes, so we have assembled the circuit.
Now let's do the coding: start the Arduino IDE, click File→New File, and enter the program shown on this screen. This time, we are writing a 6-line program before the setup function. The part circled in red here is called a variable, which is a function that allows you to name and store data. The int in front of it stands for integer, meaning that the variable has an integer value. In the programming world, the data type is used to indicate what form of data it is, and in the case of int, it is called the integer type because it is an integer. There are many other data types, which I will introduce in another lesson. Variables are often used in multiple places in a program, or when you want to give an easy-to-understand name to data. In this example, we use it where we assign a pin number and where we set the duty of the PWM. const in front of int is called a modifier, and when prefixed with it, the variable is treated as a constant and can be made read-only. The program itself works fine with or without this modifier, but once a pin number is set, it is basically never changed afterwards, so the const modifier is added to prevent accidental changes.
In the setup function, the RED, GREEN, and BLUE pins are set as output pins respectively. Then, in the loop function, the PWM Duty ratio of each pin is set using the analogWrite function. Duty is a range from 0 to 255, where 0 corresponds to Duty=0% and 255 corresponds to Duty=100%. So, for example, if you want to set the Duty ratio to 50%, set it to 128.
Finally, save the file and press the "Write to microcontroller board" button on the toolbar to write the file. If you can confirm that the LED glows differently each time you change the Duty value in this way, you are done.
So this is what we have learned this time. When you can try changing the Duty ratio yourself and see that you can change the color at will, let's go on to the next lesson. So, that's all for this time.
Негізгі бет Тәжірибелік нұсқаулар және стиль [First Arduino] 5. Full color LED | Starter course with Hands-on
Пікірлер: 6