Arduino Lesson 2 – digitalRead & digitalWrite

So first up, if you havent installed the Arduino software, you can do so for free on the arduino website. Ill leave a link in the description, so first up were gon na, be looking at the general structure of the Arduino code. So once youve installed your software and opened it up, youll notice that theres 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 its 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 weve 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 were 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 were gon na start hearing the void set up. Am I going to write, pin mode and then were 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 dont forget them.

You know youve written the pin mode function right because it will turn orange. Whenever you see brackets after a function, it means that whatevers written inside those brackets is setting parameters of the function. So, for example, here were saying, pin mode which tells the Arduino software that were about to set whether a pins, an input or an output. We then need to tell the software which pin were talking about and whether its an input or an output so thats what we put in the brackets. So first we have to tell the pin mode function which pin were 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 Ive 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 wed, 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, Ive written led because Im 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 weve set LED to equal 13. So its 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 youve written in the code, so you dont 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 Im going to use the same principle to set up a button. Now, because an LED is an output because were writing a signal to it? We write output in the pin mode function because a button is an input because were reading from it. We write input. If you want to comment on the code to remind yourself what its doing you can use double forward, slash anything you write after that point will be ignored by the Arduino software. So so far everything weve been doing has been set up now were 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 were just gon na 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 dont have to put it on a PCB, so now we can start writing code to turn that LED on and off. So first off were gon na write. Digital write dont forget that as a capsule W in the right then were 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 were talking about now. Dont forget, like we talked about earlier up, the top weve 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. Im 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 Ive done a digital write low, which will start the LED turned off Im 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 Ive 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, its gon na run continuously until we unplug the Arduino so now weve 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 youve 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 its 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 youve done wrong so now – weve checked the code and we know it works.

We can go up here to tools port and you can select which port youve 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 youve selected, the right port go to board and make sure youve 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 its 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, Ive 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 were going to change our delays from 1000 milliseconds to 200 milliseconds. So were 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 weve looked at digital writing.

We can take a look at digital reading. Its kind of the same in the fact its binary its 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 its connected to the 5 volt line at the other side, the Arduino. The reason its pulled high is because theres 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 Ive written bun, because Ive set the button as pin 1 up the top.

You could also put a 1 but its needed to put button.

Share.
Exit mobile version