Three Functions for Controlling Arduino Pins
Free Arduino Course
Arduino Pin Control
Let’s face it, to do anything remotely useful or cool with your Arduino, you’ll need to connect stuff to its pins. If you’re new, this may be confusing. Here are 3 useful functions for controlling things you connect to the Arduino’s pins.
Become the Maker you were born to be. Try Arduino Academy for FREE!
Three Functions for Controlling Arduino Pins Video Transcription
Hi, I hope everybody’s doing great!
In this lesson. We’re going to do our own take on the classic Hello world program. Traditionally hello world is the first program you write when learning to code — for example, if you learn to program in C or C++ on a desktop, you may have written a simple program to write hello world to the monitor.
While it’s possible to write to an LCD screen with an Arduino, we’re going to Blink an LED instead to make it simple for beginners. We’ll get into using LCDs a little bit later.
In this lesson, we’re going to learn about a few things including two basic electronic components: resistors and LEDs and three functions you’ll use a lot: pinMode(), digitalWrite(), and delay(). Before we get into coding, let’s take a look at the schematic. Here we can see the schematic’s pretty simple. We have the LED, a series resistor, and just a jumper wire going to the Arduino board.
Now I want to talk a bit about LEDs real quick. On an LED, you’ll notice if you take a close look at it, one of the sides of the plastic dome is flat like it has part of it cut off almost and that leg on that side is shorter. This is the cathode of the LED and always goes to the negative side. In this case, which would be ground the anode is the opposite one which has a longer leg in this schematic and can see there’s a little Bend in the leg there is a positive side and that goes to 5 volts a course with a resistor.
You’ll use LEDs a lot. So it’s important to remember this. Now, speaking of that resistor you don’t ever want to use an LED without a resistor. If you do that, it’s going to draw a lot of current, burn real bright and then burn out pretty quickly.
You may be wondering how we picked a value of that resistor or what the value even is. To decipher the value of a resistor we need to look at its colored bands or color code. Now, I have a nice tutorial on resistor color code here and there’s a link [http://www.circuitcrush.com/resistor-color-code/] so I’m not going to go into any detail, but I’ll gloss over it real quick right now.
The first band – the red one on the left – represents the first digit in the value of this resistor. In this case red is 2 — actually in every case in resistor color code red equals 2, but just in case you can’t tell that is red. The second band on a resistor represents the second digit which again is red. So it’s 2 two’s so far. We have a value of 22. The third band is a multiplier and we can see here it’s brown and in this case Brown represents a multiplier of 10. So what we have is 22 times 10 or 220 ohms. And finally that fourth band, that gold one all the way on the right, is a tolerance in gold is 5% tolerance.
So back to the schematic we can see we have a current limiting resistor on the anode and also pin 8, which is going to put out five volts, then we have the cathode of the LED grounded which is going to put out 0 volts. So now that we have a schematic down. Let’s go write some code.
So here we are in the Arduino IDE ready to write some code and as usual when we first open up the IDE, We see these two functions here setup() and loop() and again, it is absolutely critical that you include these two functions. If you don’t your program will not compile and it might be a good idea to just to practice typing them out every once in a while just in case for some reason if they don’t show up, but I’m going to leave it here. We got void setup() and void loop() ready to go and we’re going to start writing some code now in setup.
We’re going to put things that only run once or happen once, we want to Blink an LED that’s connected to pin 8. So the first thing that we need to do is tell the Arduino IDE which pin were using and what that pin is — either an input or an output. To do that, we’re going to use a little function called pinMode() and we see here right away I type in mode and it changes color and notice too that the p is lowercase in the m is a capital M. This is something known as camel case. And if you don’t type this exactly right, for example, if I were to type pinmode, it doesn’t change color or if I were to capitalize the P instead of the m.
So it’s really important that when we’re typing functions in Arduino that contain two words in their name that we do it right or we’re going to get errors if we try to compile. So we got pinMode() and now pinMode() takes two parameters.
One of them is the pin and that’s just the number 8 because that’s pin 8. Now we have to tell it either is this an input or an output? There is a third one, an input pull up but we’re not going to discuss that here. It’s a little bit more advanced. But for now pinMode() is either going to be an input or an output and honestly by default, all Arduino pins are inputs and we want this to be an output. We want to Output 5 volts. So we need to specifically say output.
And notice again how it changed color and also notice how I had to type output, the second parameter in all capital letters. If I don’t do that it doesn’t change colors and then I go to compile and I get an error just to demonstrate I’m gonna verify it and boom. I got an error. So be sure to be careful and I’m sure you’ll make these mistakes, I did.
But be sure that the second parameter is all capital and we’ll verify it again really that’s all we need in set up it’s all we need to do is setup pin 8 as an output now we need to get that led to Blink and we want it to Blink over and over again, so obviously we want to put the rest of the code in the loop() function here because this is going to repeat over and over and to make it blink we have to turn it on and then off. So to turn it on means I’m going to apply 5 volts to the LED also known as a high or logic level high. A lot of people get wrapped up in logic levels and some instructors even have complete lessons on logic levels. Honestly, it’s not that hard to understand high is about 5 volts, low is about zero volts or close to it. That’s really all there is to know and I’m going to put a nice little picture here of a digital sigal so you can see graphically what high and low looks like but it just doesn’t need a whole lesson to explain digital logic levels, which are high or low. So to write a high I’m going to go with digitalWrite() and notice here how I use the same camel case with a lowercase D uppercase w.
And we’re writing to of course pin eight. And to write a high we’re just going to type in the word high. And again, I had to type in all caps and it change colors to let me know I’m doing things right and then we want to turn it off. So now we’re going to write a low to pin 8 again and low in all uppercase.
So let’s go ahead and just verify this just to make sure so far that we haven’t made any mistakes before we test it out. Okay looks good. Let’s go ahead and upload this and see what happens.
Okay, here we are and we can see that it’s not blinking. Something must be wrong. I mean it looks like it would work. Let’s go back to the code and check it out.
Okay. So here we are at the code. Let’s go through it. Okay, we set pin 8 as an output with the pinMode() function. Okay that checks out, then we step into the loop and now we’re writing a high and a low — on off — 5 volt 0 volt. So why the heck isn’t this thing blinking? And the reason is that it’s just happening really, really fast. The Arduino’s crystal is a 16 megahertz crystal that clocks the chip at 16 megahertz or 16 million and each instruction — I mean some instructions are going to take more than one clock cycle guys — So know that but for all intent, let’s just say that each instruction takes a clock cycle. So we’re looking at this thing blinking probably several million times in a second. I don’t know the exact number but it’s blinking really, really fast because it’s running through this loop really, really fast and that’s why we can’t see it because of our persistence of vision. Your Eyes can’t perceive something that happens so quick and honestly, I’m not even sure the LED can respond that fast.
So we have to do something to slow this down so we can see the thing blink and that’s where the delay() function comes in. Now, this is just one word. So it’s going to be all lowercase and delay takes one parameter – milliseconds. I’m going to type in a thousand because a thousand milliseconds is one second. Now we’re going to have a one second delay.
And let’s see what happens when we run this code.
Okay, it’s still not blinking. What the heck is going on? Well, let’s go back to the code and look again.
Okay, here we are and let’s just step through. Nothing wrong here. Let’s step through this one at a time. So first we put 5 volts to pin 8 then we come in here and we keep it high for one second. Then it goes low. Ah, here’s the problem. It goes low for such a short amount of time that we can’t see it. Again the same trap got us. So let’s fix that. I’m going to copy that and I’m going to paste it here now. Let’s see what happens.
And look at that the LED is finally blinking and it’s blinking slow enough so we can actually perceive it and the LED actually has time to react.
So guys to recap, we learned about some basic electronic components and some of their properties the LED and the resistor and we learned about three functions pinMode(), digitalWrite(), and delay() which you’re going to be seeing a lot now.
In the future we’ll have other ways to insert delays other than using the delay() function, there’s better ways to do that but for simple programs like this that just have one purpose delay() is usually what you’re going to be using. We also learned about camel case and the correct way to type functions and they the correct way to type the parameters of a function. So that’s it for this lesson guys. I’ll see you in the next lesson.
Thanks for helping out, great info.
Good post but I was wanting to know if you could write a litte
more on this subject? I’d be very thankful if you could elaborate a
little bit further. Thanks!