You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: led and button (1)/LedAndButton.html
+86-12Lines changed: 86 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -8,11 +8,13 @@
8
8
</head>
9
9
<body>
10
10
<h1>Button and LED Basics with Arduino</h1>
11
-
<p>This project introduces the fundamental concepts of Arduino.</p>
11
+
<p>This project introduces fundamental concepts of Arduino programming by demonstrating how to control an LED with a pushbutton.</p>
12
+
12
13
<p>You can simulate this project online using Wokwi: <ahref="https://wokwi.com/projects/411336385024901121">Button and LED Simulation</a></p>
13
-
<p>If you find this helpful, please consider giving it a like ❤️!</p>
14
14
15
-
<h2>What You Will Learn</h2>
15
+
---
16
+
17
+
<h2>What You'll Learn</h2>
16
18
<p>This project will guide you through the following essential Arduino concepts:</p>
17
19
<ol>
18
20
<li><strong>Declaring Variables:</strong> Learn how to create and use variables in your Arduino sketches.</li>
@@ -21,22 +23,94 @@ <h2>What You Will Learn</h2>
21
23
<li><strong>Using the Serial Monitor:</strong> Discover how to use the Serial Monitor for debugging and displaying information.</li>
22
24
</ol>
23
25
26
+
---
27
+
24
28
<h2>Project Diagram</h2>
25
-
<p>Circuit setup:</p>
29
+
<p>Here is the circuit setup for this project.</p>
26
30
<imgsrc="https://github.com/user-attachments/assets/6bb95536-b864-4568-97d8-5654944a87fb" alt="Button and LED Diagram">
27
31
28
-
<h2>Code</h2>
29
-
<p>Let's dive deeper into the code elements:</p>
32
+
---
33
+
34
+
<h2>Code Breakdown</h2>
35
+
<p>Let's dive deeper into the code elements, explaining each part of the program.</p>
36
+
37
+
<h3>Declaring Variables</h3>
38
+
<p>We start by declaring variables to represent our components. This makes our code easier to read and modify.</p>
39
+
<pre><code>int button = 3;
40
+
int LED1 = 10;</code></pre>
41
+
<ul>
42
+
<li><code>int button = 3;</code>: We create an integer variable named <code>button</code> and assign it the value <code>3</code>, which corresponds to digital pin 3 on the Arduino.</li>
43
+
<li><code>int LED1 = 10;</code>: We create an integer variable named <code>LED1</code> and assign it the value <code>10</code>, which corresponds to digital pin 10.</li>
44
+
</ul>
30
45
31
-
<h3>Serial Monitor</h3>
32
-
<p>The Serial Monitor is an tool for debugging your Arduino code and displaying text information from your board to your computer.</p>
33
-
<pre><code>Serial.begin(9600);</code></pre>
34
-
<p>by using the code given, we can set up serial communication. it's recommended to run it inside of the <code>setup</code> function as it's only need to be run once:</p>
46
+
<h3>The <code>setup()</code> Function</h3>
47
+
<p>The <code>setup()</code> function runs just once when the Arduino board is powered on or reset. We use it to initialize our pins and serial communication.</p>
35
48
<pre><code>void setup()
36
49
{
37
50
Serial.begin(9600);
51
+
pinMode(button, INPUT_PULLUP);
52
+
pinMode(LED1, OUTPUT);
53
+
}</code></pre>
54
+
<ul>
55
+
<li><code>Serial.begin(9600);</code>: This line starts serial communication at a <strong>baud rate</strong> of 9600 bits per second, which allows the Arduino to send messages to the computer's Serial Monitor.</li>
56
+
<li><code>pinMode(button, INPUT_PULLUP);</code>: The <code>pinMode()</code> function configures a specified pin to behave as an input or an output. Here, we set the <code>button</code> pin to <code>INPUT_PULLUP</code>. This is a convenient way to read a button's state without needing an external <strong>pull-up resistor</strong>.</li>
57
+
<li><code>pinMode(LED1, OUTPUT);</code>: We set the <code>LED1</code> pin as an <code>OUTPUT</code> so that we can send a voltage to it to turn the LED on or off.</li>
58
+
</ul>
59
+
60
+
<h3>The <code>loop()</code> Function</h3>
61
+
<p>The <code>loop()</code> function runs continuously after the <code>setup()</code> function has finished. This is where the main logic of our program resides.</p>
62
+
<pre><code>void loop()
63
+
{
64
+
int Button1Pressed = digitalRead(button);
65
+
66
+
if (Button1Pressed == LOW)
67
+
{
68
+
Serial.println("Button pressed!");
69
+
digitalWrite(LED1, HIGH);
70
+
}
71
+
else
72
+
{
73
+
digitalWrite(LED1, LOW);
74
+
}
75
+
}</code></pre>
76
+
<ul>
77
+
<li><code>int Button1Pressed = digitalRead(button);</code>: The <code>digitalRead()</code> function reads the state of the specified pin. It returns <code>LOW</code> (0 volts) if the pin is connected to ground and <code>HIGH</code> (5 volts) if it is connected to a power source. With <code>INPUT_PULLUP</code>, the pin is normally <code>HIGH</code>, and when the button is pressed, it connects the pin to ground, making the state <code>LOW</code>.</li>
78
+
<li><code>if (Button1Pressed == LOW)</code>: We use an <code>if</code> statement to check if the button has been pressed. If the condition is true (the button is pressed), the code inside the curly braces <code>{}</code> will run.</li>
79
+
<li><code>Serial.println("Button pressed!");</code>: This line prints the message <strong>"Button pressed!"</strong> to the Serial Monitor. The <code>ln</code> at the end of <code>println</code> adds a new line after the message, which makes the output much cleaner.</li>
80
+
<li><code>digitalWrite(LED1, HIGH);</code>: The <code>digitalWrite()</code> function writes a <code>HIGH</code> (5 volts) or <code>LOW</code> (0 volts) value to a digital pin. When the button is pressed, we write <code>HIGH</code> to the <code>LED1</code> pin to turn on the LED.</li>
81
+
<li><code>else</code>: If the <code>if</code> condition is false (the button is not pressed), the code inside this <code>else</code> block will run instead.</li>
82
+
<li><code>digitalWrite(LED1, LOW);</code>: If the button is not pressed, we write <code>LOW</code> to the <code>LED1</code> pin, which turns the LED off.</li>
83
+
</ul>
84
+
85
+
---
86
+
87
+
<h3>Full Code</h3>
88
+
<p>Here's the complete code, ready to upload to your Arduino board.</p>
89
+
<pre><code>int button = 3;
90
+
int LED1 = 10;
91
+
92
+
void setup()
93
+
{
94
+
Serial.begin(9600);
95
+
pinMode(button, INPUT_PULLUP);
96
+
pinMode(LED1, OUTPUT);
97
+
}
98
+
99
+
void loop()
100
+
{
101
+
int Button1Pressed = digitalRead(button);
102
+
103
+
if (Button1Pressed == LOW)
104
+
{
105
+
Serial.println("Button pressed!");
106
+
digitalWrite(LED1, HIGH);
107
+
}
108
+
else
109
+
{
110
+
digitalWrite(LED1, LOW);
111
+
}
38
112
}</code></pre>
39
-
<p>now let's print something . to print something in the serial monitor we need to use the function:</p>
This project introduces the fundamental concepts of Arduino,
3
+
This project introduces fundamental concepts of Arduino programming by demonstrating how to control an LED with a pushbutton.
4
4
5
5
You can simulate this project online using Wokwi: [Button and LED Simulation](https://wokwi.com/projects/411336385024901121)
6
6
7
-
If you find this helpful, please consider giving it a like ❤️!
7
+
---
8
8
9
-
## What You Will Learn
9
+
## What You'll Learn
10
10
11
11
This project will guide you through the following essential Arduino concepts:
12
12
@@ -15,32 +15,98 @@ This project will guide you through the following essential Arduino concepts:
15
15
3.**Writing Digital Output:** Learn how to control a digital output pin connected to an LED.
16
16
4.**Using the Serial Monitor:** Discover how to use the Serial Monitor for debugging and displaying information.
17
17
18
+
---
19
+
18
20
## Project Diagram
19
-
Circuit setup:
21
+
Here is the circuit setup for this project.
20
22
21
23

22
24
23
-
## Code
25
+
---
26
+
27
+
## Code Breakdown
24
28
25
-
Let's dive deeper into the code elements:
29
+
Let's dive deeper into the code elements, explaining each part of the program.
26
30
27
-
### Serial Monitor
31
+
### Declaring Variables
28
32
29
-
The Serial Monitor is an tool for debugging your Arduino code and displaying text information from your board to your computer.
33
+
We start by declaring variables to represent our components. This makes our code easier to read and modify.
30
34
31
35
```cpp
32
-
Serial.begin(9600);
36
+
int button = 3;
37
+
int LED1 = 10;
33
38
```
34
-
by using the code given, we can set up serial communication. it's recommended to run it inside of the ***setup*** function as it's only need to be run once:
39
+
40
+
* int button = 3;: We create an integer variable named button and assign it the value 3, which corresponds to digital pin 3 on the Arduino.
41
+
* int LED1 = 10;: We create an integer variable named LED1 and assign it the value 10, which corresponds to digital pin 10.
42
+
43
+
### The setup() Function
44
+
The setup() function runs just once when the Arduino board is powered on or reset. We use it to initialize our pins and serial communication.
35
45
36
46
```cpp
37
47
voidsetup()
38
48
{
39
49
Serial.begin(9600);
50
+
pinMode(button, INPUT_PULLUP);
51
+
pinMode(LED1, OUTPUT);
52
+
}
53
+
```
54
+
55
+
Serial.begin(9600);: This line starts serial communication at a baud rate of 9600 bits per second, which allows the Arduino to send messages to the computer's Serial Monitor.
56
+
* pinMode(button, INPUT_PULLUP);: The pinMode() function configures a specified pin to behave as an input or an output. Here, we set the button pin to INPUT_PULLUP. This is a convenient way to read a button's state without needing an external pull-up resistor.
57
+
* pinMode(LED1, OUTPUT);: We set the LED1 pin as an OUTPUT so that we can send a voltage to it to turn the LED on or off.
58
+
59
+
### The loop() Function
60
+
The loop() function runs continuously after the setup() function has finished. This is where the main logic of our program resides.
61
+
```cpp
62
+
voidloop()
63
+
{
64
+
int Button1Pressed = digitalRead(button);
65
+
66
+
if (Button1Pressed == LOW)
67
+
{
68
+
Serial.println("Button pressed!");
69
+
digitalWrite(LED1, HIGH);
70
+
}
71
+
else
72
+
{
73
+
digitalWrite(LED1, LOW);
74
+
}
40
75
}
41
76
```
42
77
43
-
now let's print something . to print something in the serial monitor we need to use the function:
78
+
* int Button1Pressed = digitalRead(button);: The digitalRead() function reads the state of the specified pin. It returns LOW (0 volts) if the pin is connected to ground and HIGH (5 volts) if it is connected to a power source. With INPUT_PULLUP, the pin is normally HIGH, and when the button is pressed, it connects the pin to ground, making the state LOW.
79
+
* if (Button1Pressed == LOW): We use an if statement to check if the button has been pressed. If the condition is true (the button is pressed), the code inside the curly braces {} will run.
80
+
* Serial.println("Button pressed!");: This line prints the message "Button pressed!" to the Serial Monitor. The ln at the end of println adds a new line after the message, which makes the output much cleaner.
81
+
* digitalWrite(LED1, HIGH);: The digitalWrite() function writes a HIGH (5 volts) or LOW (0 volts) value to a digital pin. When the button is pressed, we write HIGH to the LED1 pin to turn on the LED.
82
+
* else: If the if condition is false (the button is not pressed), the code inside this else block will run instead.
83
+
* digitalWrite(LED1, LOW);: If the button is not pressed, we write LOW to the LED1 pin, which turns the LED off.
84
+
85
+
### Full Code
86
+
Here's the complete code, ready to upload to your Arduino board.
0 commit comments