In this lesson, we'll learn below.
* digitalRead
* if
-Table of Contents
0:00 Opening
0:18 Material preparation
0:27 Circuit diagram
1:10 Assemble circuit
1:36 Make program
2:44 Write to board
2:56 Advice
3:23 Conclusion
-Introduction
Hardware engineer
9 years experience as a Maker
-SNS
Web : start-electronics.com/
Twitter : / buonoatsushi
#Electronics #Arduino
-Content
Here, in addition to the circuit used in Lesson 3, we will use a pushbutton switch to program the LED to turn on while the switch is pressed and off while the switch is released.
The materials we will be using are an Arduino Uno, breadboard, 5 male and 5 female jumper wires, red LED, pushbutton switch, and 330Ω and 10kΩ resistors.
Here is the circuit diagram we will be creating. When the switch is pressed, the resistor in the switch is 0Ω, so the voltage at point A is at the level of GND, or 0V. On the other hand, when the switch is not pressed, the switch has a very high resistance value. In such a high resistance state, even a small current flow due to noise can generate a high voltage and cause a failure, so it is common to connect a resistor of about 10 kΩ, called a pull-up resistor, to the supply voltage, as in this circuit. By connecting this resistor, the voltage at point A can be fixed to the power supply voltage when the switch is not pressed, thus making the circuit more resistant to noise. Actually, this circuit can be made even cleaner by using a certain trick, which will be introduced later.
Next, here is a breadboard diagram following the schematic. The pushbutton switch has four legs, which are connected as shown in this figure, so make sure to check the direction when connecting them. So, we are ready to build the circuit.
Now let's start coding. Click "File" - "New File" and write two pinMode functions in the Setup function like this. If you want to use a digital pin as input, specify the pin number and then write INPUT. Next, write an if statement in the loop function like this. if statements are used when you want to perform processing only when some condition is satisfied, and are often used when you want to change processing depending on whether or not a button is pressed, as in this case. The meaning of the "if" statement is that if the expression in the parentheses is true, then the processing in the following curly brackets is performed, and if the expression is not true, then the processing in the curly brackets following the "else" statement is performed. In addition, the symbols listed here are used in the judgment expression. When determining equality, two equal signs must be written, so be careful not to make a mistake. In addition, digitalRead is a function that reads whether the voltage level of a specified digital pin is HIGH or LOW. In terms of specific voltage levels, if the power supply voltage is 5 V, the specification is to judge HIGH if the voltage is 3.0 V or higher, and LOW if the voltage is 1.5 V or lower.
Finally, save the file and press the "Write to microcontroller board" button on the toolbar to write the file. If you confirm that the LED is not lit when the button is not pressed and lights up when the button is pressed, you are done.
Note that the pinMode function actually has another useful mode, which I would like to introduce. pinMode function has a parameter called INPUT_PULLUP in addition to OUTPUT and INPUT introduced so far. This is the mode that will connect a pull-up resistor inside the Arduino when that pin is set to the input setting. This eliminates the need for the 10kΩ resistor used in this case, making the circuit cleaner. When you want to reduce the circuit size or do not have a resistor on hand, make active use of this mode.
So, this is what you have learned this time. change digitalRead to the INPUT_PULLUP parameter and try to modify your program and circuit. Once you have done that, let's go to the next lesson. So that's all for this time.
Негізгі бет Тәжірибелік нұсқаулар және стиль [First Arduino] 4. Switch | Starter course with Hands-on
Пікірлер: 25