Arduino Lesson 2 - digitalRead & digitalWrite
Summary
TLDRThis tutorial introduces digital read and write operations on the Arduino Uno. It begins with the installation of Arduino software and an overview of the code structure, focusing on the 'void setup' and 'void loop' functions. The video explains how to set digital pins as inputs or outputs using the 'pinMode' function and demonstrates controlling an LED with 'digitalWrite'. It also covers the concept of delays and uploading code to the Arduino board. Additionally, the tutorial delves into digital reading, explaining how to read button inputs and set up a simple button circuit, with a sneak peek at using 'if' statements in the next session.
Takeaways
- 💻 The tutorial focuses on digital reads and writes using Arduino Uno, a popular microcontroller board.
- 🔧 Arduino software can be downloaded for free from the official Arduino website.
- 📝 The script explains the structure of Arduino code, highlighting the roles of `void setup()` and `void loop()` functions.
- 🔩 The `pinMode` function is crucial for setting digital pins as inputs or outputs, which determines whether they read from or send signals to connected components.
- 🔑 Variables at the top of the code can be used to represent pin numbers, making the code easier to update and understand.
- 💡 The tutorial demonstrates how to control an LED using digital writes to turn it on and off.
- 🔌 The process of uploading code to the Arduino board is outlined, including selecting the correct port and board type.
- ⏲️ Delays in the code are used to control the timing of actions, such as turning an LED on and off.
- 🔑 The concept of digital reading is introduced, explaining how it can be used to read input from components like buttons.
- 🔄 The tutorial provides a simple circuit schematic for a button, explaining how it interacts with an Arduino pin to produce a digital high or low signal.
Q & A
What is the first step to begin programming with an Arduino Uno?
-The first step is to install the Arduino software, which can be done for free from the Arduino website.
What are the two main functions in an Arduino sketch?
-The two main functions in an Arduino sketch are 'void setup()' and 'void loop()'. The 'setup()' function runs once when the Arduino is powered on or reset, and the 'loop()' function runs repeatedly.
What is the purpose of the 'pinMode' function in Arduino?
-The 'pinMode' function is used to set a digital pin as either an input or an output, which determines whether the pin will be used to read data (input) or send data (output).
How do you define a pin number in the 'pinMode' function?
-You can define a pin number directly by writing the number, or you can use a variable at the top of the code that is assigned to the pin number, making it easier to update if the pin changes.
Why is it important to use semicolons in Arduino code?
-Semicolons are used to end lines of code in Arduino, just as in many other programming languages. They are necessary to properly terminate statements.
What is the purpose of the 'digitalWrite' function?
-The 'digitalWrite' function is used to write a digital signal (HIGH or LOW) to a specific digital pin, which can turn an LED on or off, for example.
How do you add a delay in Arduino code?
-You can add a delay in Arduino code using the 'delay' function followed by the number of milliseconds you want to pause, enclosed in brackets.
What does it mean if the 'RX' and 'TX' LEDs on the Arduino board are flashing?
-If the 'RX' and 'TX' LEDs on the Arduino board are flashing, it usually indicates that the board is communicating with the computer, typically during the upload of a new sketch.
How can you change the pin number for a component without searching through your entire Arduino code?
-You can change the pin number for a component by defining a variable at the top of your code with the pin number, and then using that variable throughout your code. This way, you only need to update the pin number in one place.
What is the significance of using a resistor in a button circuit with an Arduino?
-A resistor in a button circuit with an Arduino ensures that when the button is not pressed, the pin is connected to ground through the resistor, and when the button is pressed, it is pulled high to the 5V line due to the lack of resistance in that path.
How do you read the state of a digital pin in Arduino?
-You read the state of a digital pin in Arduino using the 'digitalRead' function followed by the pin number in brackets, which will return either HIGH or LOW depending on the input.
Outlines
🛠️ Arduino Uno Setup and Digital Writes
The tutorial begins with an introduction to the Arduino Uno, focusing on digital reads and writes. It advises installing the Arduino software from the official website and provides a link in the description. The general structure of Arduino code is explained, highlighting the 'void setup' and 'void loop' functions. 'Void setup' runs once when the Arduino is powered on, used for initial setup, while 'void loop' contains the main code that continuously loops after setup. The 'pinMode' function is introduced to set digital pins as inputs or outputs, with examples given for an LED and a button. The tutorial emphasizes the importance of capitalization and semicolons in code, and the use of variables to represent pin numbers for easier code management. It concludes with a brief on how to write code to turn an LED on and off using 'digitalWrite' and 'delay' functions.
🔍 Debugging and Uploading Arduino Code
This part of the tutorial covers the process of checking and uploading the written code to the Arduino board. It explains how to save the sketch with a name and how the Arduino software compiles it, indicating successful compilation with a 'done compiling' message. Errors are highlighted with an orange bar, and advice is given to search for solutions online if faced with complex error messages. The tutorial then demonstrates how to select the correct port and board in the Arduino software, and how to upload the sketch to the board using the upload button. It describes the expected behavior of the RX and TX LEDs during the upload process. The tutorial modifies the delay in the code to change the LED's blinking speed, illustrating the immediate effect of code changes on the hardware. Lastly, it introduces digital reading, explaining the concept with a simple button circuit and how it works with the Arduino's digital pins, setting the stage for using 'digitalRead' in subsequent tutorials.
Mindmap
Keywords
💡Arduino Uno
💡Digital Reads and Writes
💡Void Loop
💡Void Setup
💡Pin Mode
💡Input
💡Output
💡Semicolon
💡Comment
💡Delay
💡Resistor
Highlights
Introduction to Arduino Uno and digital reads and writes.
Instructions on installing Arduino software for free from the official website.
Explanation of the general structure of Arduino code, including the 'void setup' and 'void loop' functions.
Demonstration of setting up the Arduino environment and writing the initial code.
Use of the 'pinMode' function to set digital pins as inputs or outputs.
Importance of capitalization and the use of semicolons in Arduino code.
How to define pin numbers with descriptive names for easier code management.
Setting up an LED as an output pin in the 'pinMode' function.
Setting up a button as an input pin in the 'pinMode' function.
Using comments in the code to remind oneself of the code's purpose.
Coding 'digitalWrite' to control an LED's state.
Explanation of the 'delay' function and its use in controlling the timing of actions.
Compiling and uploading the sketch to the Arduino board.
Troubleshooting code errors by checking for missing semicolons or other syntax issues.
Selecting the correct port and board type before uploading the sketch.
Observing the LED behavior change after uploading the code with different delay settings.
Introduction to digital reading and its application in reading inputs like buttons.
Understanding the circuit schematic for a button and how digital reads work.
Coding 'digitalRead' to read the state of an input pin.
Transcripts
hi guys and welcome to this tutorial on
the Arduino Uno we're going to be doing
digital reads and writs in this session
so first up if you haven't installed the
Arduino software you can do so for free
on the Arduino website I'll leave a link
in the description so first up we're
going to be looking at the general
structure of the Arduino code so once
you've installed your software and
opened it up you'll notice that there's
a void Loop and a void setup any code we
put inside the void setup only runs once
when we first turn the Arduino on it's
usually used to set things up that are
connected to the
Arduino the void Loop section of the
code as the name suggest Loops
repeatedly after we've turned the
Arduino on this is where we put the main
body of our
code so before we start coding digital
reads and writes we have to do a bit of
setup code in the void setup Loop so
first up we're going to use a function
called pin mode to set whether the
digital pin are going to be inputs or
outputs an input would be something with
digital reading like a button an output
would be something with digital writing
like an
LED so we're going to start here in the
void setup and we're going to write pin
mode and then we're going to open and
close the brackets and put a
semicolon on a side note capitalization
does matter here so make sure you use a
capital m on mode semicolons are always
used to end lines of code so make sure
you don't forget them you'll know you've
written the pin mode function right
because it will turn
orange whenever you see brackets after a
function it means that whatever's
written inside those brackets is setting
parameters of the function so for
example here we're saying pin mode which
tells the Arduino software that we're
about to set whether a pin's an input or
an output we then need to tell the
software which pin we're talking about
and whether it's an input or an output
so that's what we put in the brackets so
first we have to tell the pin mode
function which pin we're talking about
so we write the pin number then a comma
followed by either input or output
depending on if we want the PIN to be an
input or an
output where I've written pin number you
either write the number that the pin is
connected to so for example if we were a
component up to pin 13 we'd write
13 or a slightly better way of doing it
is to write up the top int and then an
English name for your component so for
example I've written LED because I'm
wearing up an LED and then equals and
the pin number that that led is
connected to that means that instead of
writing 13 in your PIN mode function you
can just write LED because at the top
we've set led to equal 13 so it's
exactly the same thing this means that
if you change the pin that the LEDs
connected to it will update that PIN
number in all of the functions you've
written in the code so you don't have to
for example go through your whole code
writing 12 instead of 13 you can just
update it once at the top of the
code so now I'm going to use the same
principle to set up a
button now because an LED is an output
because we're writing a signal to it we
write output in the pin mode function
because a button is an input because
we're reading from it we write
input if you want to comment on the code
to remind yourself what it's doing you
can use double forward slash anything
you write after that point will be
ignored by the Arduino
software so so far everything we've been
doing has been set up now we're going to
move on to the void Loop and do some
digital wrs which means we can actually
make our Arduino do something so first
up we're just going to hook our Arduino
up to our computer using a USB cable
this little PCB here is just a LED wired
in series with a 220m resistor you can
make it really easily on a breadboard
you don't have to put it on a
PCB so now we can start writing code to
turn that led on and off so first off
we're going to write digital right don't
forget that there's a capital w in the
right then we're going to open and close
some brackets and write a
semicolon now like with all functions
the brackets are looking for us to add
parameters so for a digital right it
wants to know the pin that we're talking
about now don't forget like we talked
about earlier up the top we've defined
that led is equal to 12 so we could also
write LED here then you write a comma
and either high or low depending if you
want the PIN to be set at 5 volts or 0
volts then you end the line with a
semicolon here I'm writing delay a th000
which is just a 1 second delay before
the next piece of code is written so the
the number you write in
the brackets is in
milliseconds so now I've done a digital
right low which will start the LED
turned off I'm going to do a digital
right high on the Same Led this will
then turn the LED on after 1 second
because of that delay
1000 I've added a second delay after the
digital right High because without it
the LED would turn on and then
immediately back off
again because this Co code is in the
void loop it's going to run continuously
until we unplug the Arduino so now we've
finished writing our little bit of code
we can check it by clicking the tick in
the top left hand corner of the screen
it will pop up with this menu asking you
to give it a name and just click save it
will compile the sketch and just say
done compiling if you've made a mistake
anywhere the orange bar will pop up like
this and it will give you a warning of
which error might be occurring usually
it's things like missing a semicolon but
if it gives you a more complicated error
message just give it a Google and it'll
probably tell you what you've done
wrong so now we've checked the code and
We Know It Works we can go up here to
tools port and you can select which Port
you've connected your uino up to the
names that it gives are just the names
your computer has given to your USB
ports the right Port will usually have
Arduino Uno written next to it because
your computer will automatically pick up
on the
board after you've selected the right
Port go to board and make sure you've
got Arduino Uno
selected after this click the arrow that
faces right next to the tech at the top
left hand side of your screen the sketch
will compile and if it's worked the
white writing will sharp with no
errors you should see the RX and TX LEDs
on your Arduino flashing I discussed
that the last tutorial and then your
code should be uploaded to your
board so we can see here the green LED
I've wired up one side to ground and the
other side to the digital pin which is
supplying either Zer Volts for a second
or 5 volts for a
second so jumping back to the code we're
going to change our delays from 1,000
milliseconds to 200 milliseconds so
we're going to upload this code to the
board and see how it affects our LED
output so as the code uploads we can see
the txx on RX LEDs flashing and there
goes our green LED flashing five times
faster than it was previously thanks to
our code
updates so now we've looked at digital
writing we can take a look at digital
reading it's kind of the same in the
fact it's binary it's either on or off
but we use it to read things that are
giving inputs like buttons for
example here we have a simple circuit
schematic for a button this will help us
understand how digital reads work when
the switch is open pin 2 is connected to
ground via a resistor which will be
between 1 and 10 kilohms in
resistance as we know electricity will
always take the path of least resistance
so when we shut the switch pin two will
be pulled High because it's connected to
the 5vt line at the other side of the
ardino the reason it's pulled high is
because there's no resistor between pin
2 and the 5vt line whereas there is a
resistor between pin 2 and ground so the
path of leas resistance is to be pulled
high at 5
Vol so now we understand how a simple
button circuit works and the sort of
thing we should be using digital reads
for we can move on to the code to see
how they
work first off if you're digital reading
from anything you should make sure that
in the pin mode in the void setup Loop
that your device is set as an input not
an
output so in the void Loop to do a
digital read you just write digital read
with a capital R brackets and then
whichever pin you want to read I've
written button because I've set the
button as pin one up the
top you could also put a one but it's
needed to put button you then close the
brackets and add a semicolon and that
will return either high or low and I'll
show you how to use that return in the
next tutorial using IF statements
浏览更多相关视频
Belajar Arduino #4 - Pengenalan Bahasa Pemrograman Arduino
شرح طريقة تحكم step motor من خلال الاردوينو Arduino
INTEGRATION OF SENSOR AND ACTUATORS WITH ARDUINO-I
BELAJAR ARDUINO #101 - Simulator Arduino Yang Cocok Bagi Kita Semua!!
Introduction to Arduino Mega 2560 | Pinout | Features | Proteus Simulation
VERY EASY Arduino IR Sensor Tutorial for Beginners | IR Sensor Arduino Tutorial | Infrared Sensor
5.0 / 5 (0 votes)