1.4 Your First Arduino Sketch: Hello World with an LED

Описание к видео 1.4 Your First Arduino Sketch: Hello World with an LED

1.4 Your First Arduino Sketch: Hello World with an LED
Ah, got it! In your circuit diagram, we're using pin 2 instead of pin 13 to make our LED blink. Here's how you can make your Arduino Nano say "Hello" with an LED using pin 2:
Setting Up Your Circuit:
● Place your Arduino Nano on the breadboard so that all its feet (pins) have their own space on the playground (breadboard).
● Grab your LED, the little light that will be our Nano's way of saying "Hello." Put the longer leg (that's the positive side, also called the anode) into a hole that lines up with pin 2 on your Nano.
● Now, take the shorter leg (the negative side, known as the cathode) and put it into another row on the breadboard.
● Next, take your 220-ohm resistor (it's like a little pillow that makes sure our LED doesn't get too much electricity and get tired out) and connect one end to the row where the shorter leg of the LED is.
● Connect the other end of the resistor to a line on the breadboard that goes to one of the GND pins on the Nano. This is like a slide that lets the electricity go back to the ground safely.
Writing Your Arduino Sketch:
Open up your secret clubhouse (Arduino IDE) and get ready to write your spell (code):
// The setup function runs once when we turn on our Arduino Nano or press reset
void setup() {
// Initialize digital pin 2 as an output.
pinMode(2, OUTPUT);
}
// The loop function runs over and over again forever
void loop() {
digitalWrite(2, HIGH); // Turn the LED on (HIGH is the voltage level) delay(1000); // Wait for a second
digitalWrite(2, LOW); // Turn the LED off by making the voltage LOW delay(1000); // Wait for another second
}
How the Magic Happens:
● pinMode(2, OUTPUT); is telling our Nano that pin 2 will be used to send out the magic (electricity) to the LED.
● digitalWrite(2, HIGH); is like saying "Ok, LED, time to shine!" And the LED lights up.
● delay(1000); tells everything to pause for a moment—1,000 milliseconds or one whole second, to be exact.
● digitalWrite(2, LOW); then says "Ok, take a break, LED," and the light turns off.
● Once you've typed up your spell in the Arduino IDE, click the "Upload" button to send your spell to the Nano. When the Nano understands what you've told it, you'll see the LED start to blink on and off every second.
● And there you have it! You've just made your Nano come alive with a blinking LED. It's like your Nano is winking at you, saying "Hello!" in LED language. Now, you're on your way to becoming an awesome young inventor! Keep having fun with your magical creations!
1.5 Understanding the Basics of Arduino Programming
Alright, young magicians! You've just made an LED blink with your Arduino Nano—it's like you've whispered a secret to it and it understood you perfectly. Now, let's peek behind the curtain to see how the magic works.
Think of Arduino programming like giving your toys instructions. Here's what each part of the code (or the secret spell) does:
void setup() { ... }
● Imagine you're the director of a play. Before the play starts, you need to make sure everything is ready—like props and costumes. The setup is where you tell the Nano what to get ready. We used it to say, "Hey Nano, we're going to use pin 2 like a flashlight!"
void loop() { ... }
● Now, the play is on! The loop is the part where the actors keep repeating their lines and actions. For our Nano, this means turning the flashlight on and off, over and over again.
digitalWrite(2, HIGH);
● This line is like saying, "Lights on, please!" The Nano makes the LED light up bright. delay(1000);
● Then, we wait for just a moment (about as long as it takes to say "one thousand one")—that's our delay. It tells the Nano to keep the flashlight on so we can see it.
digitalWrite(2, LOW);
● Next, we say "And... lights off!" with digitalWrite(2, LOW);. The LED turns off, just like when you close your eyes for a quick nap.
delay(1000);
● And we wait again, for another "one thousand one."
In your code, you'll see some lines that start with two slashes (//), like this:
● // This is a comment, it's a note for people, not for the Nano.

Комментарии

Информация по комментариям в разработке