Arduino Lesson 2 - digitalRead & digitalWrite

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys and welcome to this tutorial on the Arduino Uno we're going to be doing digital reads and writes 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 gonna 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 set up any code we put inside the void set up 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 on the code as the name suggests loops repeatedly after we've done 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 pen mode to set whether the digital pins 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 gonna start hearing the void set up am I going to write pin mode and then we're going to open and close the brackets and put a semicolon on a sidenote 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 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 pins 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 wire could point up to pin 13 we'd write 13 or a slightly better way of doing it is to write up at all int and then an English name for your component so for example I've written led because I'm wiring 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 pen 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 rights which means we can actually make our Arduino do something so first up we're just gonna hook or Arduino up to our computer using a USB cable this little PCB here is just a LED wired in series with a 220 ohm 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 gonna write digital write don't forget that as a capsule 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 write 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 pen to be set at five volts or zero volts then you end the line with a semicolon here I'm writing delay a thousand which is just a one-second delay before the next piece of code is is written so the the number you write in the brackets is in milliseconds so now I've done a digital write low which will start the LED turned off I'm going to do a digital write high on the same LED this will then turn the LED on after one second because of that delay 1000 I've out of the second delay after the digital write high because without it the LED would turn on and then immediately back off again because this code code is in the void loop it's gonna 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 do it a name and just click Save it will compile the sketch and just say done compiling if you've made a mistake anywhere yeah 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 will 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 Arduino 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 tick at the top left hand side of your screen the sketch will compile and if it's worked the white writing will show up with no errors you should see the RX and TX LEDs on your arduino flashing I discussed that in 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 pen which is supplying either 0 for a second or five holes for a second so jumping back to the code we're going to change our delays from 1000 milliseconds to 200 milliseconds so we're going to upload this code to the board and see how it effects our LED output so as the code uploads we can see the TX 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 kilo ohms in resistance as we know electricity will always take the path of least resistance so when we shut the switch pin 2 will be pulled high because it's connected to the 5 volt line at the other side the Arduino the reason it's pulled high is because there's no resistor between pin 2 and the 5 volt line whereas there is a resistor between pin 2 and ground so the path of least resistance is to be pulled high at 5 volt 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 your 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 bun because I've set the button as pin 1 up the top you could also put a 1 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 it statements
Info
Channel: Benduino
Views: 57,761
Rating: 4.813375 out of 5
Keywords: arduino, arduino uno, digitalwrite, digitalread, arduino course, learn arduino, arduino programming, what is an arduino, arduino 101, arduino tutorial, how does an arduino work, arduino coding, arduino code, arduino beginner, arduino programming for beginners, arduino tutorial for beginners, learn to code arduino, arduino beginners course, arduino for beginners, arduino software
Id: 8bufqdXwCpY
Channel Id: undefined
Length: 8min 56sec (536 seconds)
Published: Mon Dec 05 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.