Arduino Lesson 2 - digitalRead & digitalWrite

Benduino
5 Dec 201608:56

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

00:00

🛠️ 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.

05:01

🔍 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

Arduino Uno is an open-source microcontroller board based on the ATmega328P microcontroller and developed by the Arduino team. It is widely used for building electronics projects due to its ease of use and versatility. In the video, the Arduino Uno serves as the central platform where digital reads and writes are demonstrated, showcasing its capability to interact with various electronic components.

💡Digital Reads and Writes

Digital reads and writes refer to the process of reading input signals from or writing output signals to digital pins on a microcontroller. In the context of the video, digital reads are used to detect the state of an input device like a button, while digital writes are used to control output devices such as LEDs. The tutorial demonstrates how to set up these operations using the Arduino Uno.

💡Void Loop

The 'void loop' in Arduino programming is a function that contains code which continuously repeats as long as the Arduino is powered. It is the main body of the code where the continuous actions are placed. In the script, the loop is used to repeatedly turn an LED on and off, illustrating the continuous operation of the microcontroller.

💡Void Setup

The 'void setup' function in Arduino programming is used to initialize and configure the microcontroller's settings before the loop starts. It runs only once when the Arduino is powered on or reset. In the video, the setup function is used to configure the digital pins for input or output, setting the stage for the subsequent operations in the loop.

💡Pin Mode

Pin mode is a function in Arduino programming used to set a digital pin as either an input or an output. It is crucial for defining the behavior of the pins before they are used in the loop. The video script describes how to use the pinMode function to configure a pin connected to an LED as an output and a pin connected to a button as an input.

💡Input

In the context of the video, an input refers to a signal or data that is received by the microcontroller from an external device, such as a button. Inputs are typically read using digital reads in the loop function. The script explains how to set up a button as an input and how the Arduino detects the button's state.

💡Output

An output in the video refers to a signal or data that is sent from the microcontroller to an external device, such as an LED. Outputs are typically controlled using digital writes in the loop function. The tutorial demonstrates how to set up an LED as an output and how to control it to turn on and off.

💡Semicolon

A semicolon in programming, including Arduino, is used to mark the end of a statement. It is a syntactical element necessary for the correct structure of the code. The script emphasizes the importance of using semicolons to end lines of code, which is a common practice in many programming languages.

💡Comment

A comment in programming is a line or lines of code that are not executed but are used to explain or annotate the code for better readability and maintenance. In the video script, the use of double forward slashes to create comments in the Arduino code is mentioned, which helps the programmer to leave notes or reminders within the code.

💡Delay

Delay in Arduino programming is a function used to pause the program for a specified amount of time, measured in milliseconds. It is used to control the timing of actions, such as the on and off duration of an LED. The script demonstrates how to use the delay function to create a pause between the digital writes that control the LED.

💡Resistor

A resistor is an electronic component that opposes or limits the flow of electrical current in a circuit. In the video, a resistor is used in series with an LED to limit the current and protect the LED from burning out. The script also explains the role of a resistor in a button circuit, where it provides a path to ground when the button is not pressed.

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

play00:01

hi guys and welcome to this tutorial on

play00:03

the Arduino Uno we're going to be doing

play00:05

digital reads and writs in this session

play00:08

so first up if you haven't installed the

play00:09

Arduino software you can do so for free

play00:11

on the Arduino website I'll leave a link

play00:13

in the description so first up we're

play00:15

going to be looking at the general

play00:16

structure of the Arduino code so once

play00:19

you've installed your software and

play00:21

opened it up you'll notice that there's

play00:22

a void Loop and a void setup any code we

play00:26

put inside the void setup only runs once

play00:28

when we first turn the Arduino on it's

play00:30

usually used to set things up that are

play00:32

connected to the

play00:33

Arduino the void Loop section of the

play00:35

code as the name suggest Loops

play00:37

repeatedly after we've turned the

play00:39

Arduino on this is where we put the main

play00:42

body of our

play00:43

code so before we start coding digital

play00:46

reads and writes we have to do a bit of

play00:47

setup code in the void setup Loop so

play00:50

first up we're going to use a function

play00:52

called pin mode to set whether the

play00:54

digital pin are going to be inputs or

play00:56

outputs an input would be something with

play00:59

digital reading like a button an output

play01:01

would be something with digital writing

play01:02

like an

play01:03

LED so we're going to start here in the

play01:05

void setup and we're going to write pin

play01:08

mode and then we're going to open and

play01:10

close the brackets and put a

play01:12

semicolon on a side note capitalization

play01:14

does matter here so make sure you use a

play01:16

capital m on mode semicolons are always

play01:20

used to end lines of code so make sure

play01:22

you don't forget them you'll know you've

play01:25

written the pin mode function right

play01:27

because it will turn

play01:28

orange whenever you see brackets after a

play01:31

function it means that whatever's

play01:33

written inside those brackets is setting

play01:35

parameters of the function so for

play01:37

example here we're saying pin mode which

play01:39

tells the Arduino software that we're

play01:40

about to set whether a pin's an input or

play01:42

an output we then need to tell the

play01:44

software which pin we're talking about

play01:46

and whether it's an input or an output

play01:48

so that's what we put in the brackets so

play01:51

first we have to tell the pin mode

play01:52

function which pin we're talking about

play01:54

so we write the pin number then a comma

play01:57

followed by either input or output

play01:59

depending on if we want the PIN to be an

play02:00

input or an

play02:01

output where I've written pin number you

play02:04

either write the number that the pin is

play02:06

connected to so for example if we were a

play02:08

component up to pin 13 we'd write

play02:11

13 or a slightly better way of doing it

play02:14

is to write up the top int and then an

play02:17

English name for your component so for

play02:18

example I've written LED because I'm

play02:20

wearing up an LED and then equals and

play02:22

the pin number that that led is

play02:24

connected to that means that instead of

play02:26

writing 13 in your PIN mode function you

play02:28

can just write LED because at the top

play02:30

we've set led to equal 13 so it's

play02:33

exactly the same thing this means that

play02:36

if you change the pin that the LEDs

play02:38

connected to it will update that PIN

play02:41

number in all of the functions you've

play02:43

written in the code so you don't have to

play02:44

for example go through your whole code

play02:47

writing 12 instead of 13 you can just

play02:49

update it once at the top of the

play02:52

code so now I'm going to use the same

play02:54

principle to set up a

play02:56

button now because an LED is an output

play03:01

because we're writing a signal to it we

play03:02

write output in the pin mode function

play03:05

because a button is an input because

play03:06

we're reading from it we write

play03:08

input if you want to comment on the code

play03:11

to remind yourself what it's doing you

play03:12

can use double forward slash anything

play03:14

you write after that point will be

play03:16

ignored by the Arduino

play03:18

software so so far everything we've been

play03:20

doing has been set up now we're going to

play03:22

move on to the void Loop and do some

play03:24

digital wrs which means we can actually

play03:26

make our Arduino do something so first

play03:30

up we're just going to hook our Arduino

play03:32

up to our computer using a USB cable

play03:35

this little PCB here is just a LED wired

play03:37

in series with a 220m resistor you can

play03:40

make it really easily on a breadboard

play03:42

you don't have to put it on a

play03:43

PCB so now we can start writing code to

play03:46

turn that led on and off so first off

play03:49

we're going to write digital right don't

play03:51

forget that there's a capital w in the

play03:53

right then we're going to open and close

play03:55

some brackets and write a

play03:57

semicolon now like with all functions

play04:00

the brackets are looking for us to add

play04:03

parameters so for a digital right it

play04:05

wants to know the pin that we're talking

play04:08

about now don't forget like we talked

play04:11

about earlier up the top we've defined

play04:14

that led is equal to 12 so we could also

play04:16

write LED here then you write a comma

play04:18

and either high or low depending if you

play04:22

want the PIN to be set at 5 volts or 0

play04:24

volts then you end the line with a

play04:27

semicolon here I'm writing delay a th000

play04:30

which is just a 1 second delay before

play04:32

the next piece of code is written so the

play04:35

the number you write in

play04:37

the brackets is in

play04:41

milliseconds so now I've done a digital

play04:44

right low which will start the LED

play04:46

turned off I'm going to do a digital

play04:48

right high on the Same Led this will

play04:50

then turn the LED on after 1 second

play04:52

because of that delay

play04:54

1000 I've added a second delay after the

play04:58

digital right High because without it

play05:01

the LED would turn on and then

play05:02

immediately back off

play05:04

again because this Co code is in the

play05:06

void loop it's going to run continuously

play05:09

until we unplug the Arduino so now we've

play05:12

finished writing our little bit of code

play05:14

we can check it by clicking the tick in

play05:16

the top left hand corner of the screen

play05:18

it will pop up with this menu asking you

play05:20

to give it a name and just click save it

play05:23

will compile the sketch and just say

play05:24

done compiling if you've made a mistake

play05:27

anywhere the orange bar will pop up like

play05:30

this and it will give you a warning of

play05:31

which error might be occurring usually

play05:34

it's things like missing a semicolon but

play05:37

if it gives you a more complicated error

play05:38

message just give it a Google and it'll

play05:40

probably tell you what you've done

play05:42

wrong so now we've checked the code and

play05:44

We Know It Works we can go up here to

play05:46

tools port and you can select which Port

play05:50

you've connected your uino up to the

play05:53

names that it gives are just the names

play05:55

your computer has given to your USB

play05:57

ports the right Port will usually have

play05:59

Arduino Uno written next to it because

play06:01

your computer will automatically pick up

play06:03

on the

play06:04

board after you've selected the right

play06:07

Port go to board and make sure you've

play06:09

got Arduino Uno

play06:11

selected after this click the arrow that

play06:14

faces right next to the tech at the top

play06:16

left hand side of your screen the sketch

play06:18

will compile and if it's worked the

play06:21

white writing will sharp with no

play06:23

errors you should see the RX and TX LEDs

play06:27

on your Arduino flashing I discussed

play06:29

that the last tutorial and then your

play06:31

code should be uploaded to your

play06:33

board so we can see here the green LED

play06:36

I've wired up one side to ground and the

play06:38

other side to the digital pin which is

play06:39

supplying either Zer Volts for a second

play06:42

or 5 volts for a

play06:45

second so jumping back to the code we're

play06:48

going to change our delays from 1,000

play06:50

milliseconds to 200 milliseconds so

play06:54

we're going to upload this code to the

play06:55

board and see how it affects our LED

play06:58

output so as the code uploads we can see

play07:00

the txx on RX LEDs flashing and there

play07:04

goes our green LED flashing five times

play07:06

faster than it was previously thanks to

play07:09

our code

play07:11

updates so now we've looked at digital

play07:13

writing we can take a look at digital

play07:15

reading it's kind of the same in the

play07:17

fact it's binary it's either on or off

play07:19

but we use it to read things that are

play07:21

giving inputs like buttons for

play07:24

example here we have a simple circuit

play07:26

schematic for a button this will help us

play07:29

understand how digital reads work when

play07:31

the switch is open pin 2 is connected to

play07:34

ground via a resistor which will be

play07:36

between 1 and 10 kilohms in

play07:38

resistance as we know electricity will

play07:41

always take the path of least resistance

play07:43

so when we shut the switch pin two will

play07:46

be pulled High because it's connected to

play07:48

the 5vt line at the other side of the

play07:50

ardino the reason it's pulled high is

play07:53

because there's no resistor between pin

play07:55

2 and the 5vt line whereas there is a

play07:58

resistor between pin 2 and ground so the

play08:01

path of leas resistance is to be pulled

play08:02

high at 5

play08:05

Vol so now we understand how a simple

play08:07

button circuit works and the sort of

play08:09

thing we should be using digital reads

play08:11

for we can move on to the code to see

play08:13

how they

play08:14

work first off if you're digital reading

play08:16

from anything you should make sure that

play08:18

in the pin mode in the void setup Loop

play08:21

that your device is set as an input not

play08:25

an

play08:26

output so in the void Loop to do a

play08:29

digital read you just write digital read

play08:31

with a capital R brackets and then

play08:34

whichever pin you want to read I've

play08:36

written button because I've set the

play08:38

button as pin one up the

play08:40

top you could also put a one but it's

play08:43

needed to put button you then close the

play08:46

brackets and add a semicolon and that

play08:48

will return either high or low and I'll

play08:50

show you how to use that return in the

play08:53

next tutorial using IF statements

Rate This

5.0 / 5 (0 votes)

関連タグ
ArduinoTutorialDigital IOCodingElectronicsDIYMakerProgrammingHardware
英語で要約が必要ですか?