Arduino tutorial 1: How to turn/flicker on and off an LED
So today we have an led and were going to learn how to digitally write it to high or low uh. Sorry about that so theres two theres, two states for an led or the bmw, pins, pmw pins in general, theres, high or low a high meaning that its delivering power and then low, meaning that it is not so in other words high, is on low, is off. So if you look at your arduino uh, you have on the other side of the power pins so like 5v ground and the analog reed pins. You have all these digital bmw pins. All of these you can set to different states like high or low false or true one or zero were going to focus on high and low. So for this i recommend you be very careful with this, because this the led does tend to warm up a lot. So take your led. I dont have a camera right now, but youre just gon na have to trust me on this. So theres two pins on your led. There is a long one and then a shorter one. That short, one is called the cathode and then the longer one is called an anode. You put the cathode into ground and then the other one into some sort of power supply. So theres two ways you could do this. First of all, you can go to the power pins and put the anode and the five volt pin and the cathode in the in the ground uh in the ground.
So if you do that, it breaks up very, very um, bright now, heres one thing: i recommend it heats up a lot if you put it in the 5 volt and the ground, it tends to heat up very quickly and you can burn yourself even if you Put it in the 13 and ground pin um, but yeah anyway lets get into the coding, so were so. First of all, um we need to um define our pins, so were gon na do, pin mode actually, no, i should probably make an integer, so youre gon na make a variable or an integer called led, pin and its going to equal were going to use 13 Because that is next to the ground, wire or the ground, pin so youre going to do and then youre going to put pin mode with capital m, open parentheses led pin, and then, since this is outputting is since its putting like an output to the led were Gon na put it on out put in all caps so for the setup were gon na digitally or yeah digital right, led pin for this example. Right now were going to put it on hi and then youre going to upload. That code were just going to call it led test. So oh wait. What did i do wrong? Oh wait. I think i figured it out yeah. You got ta put parentheses in there. Sorry about this. I just need to pour then arduino uno.
Okay, i probably should have mentioned the setup but uh anyway. So now, if you put the long pin in 13 and the short pin in ground, it should light up, and i will show you right now now to make sure that you programmed it correctly. You should see this pin here under digital bmw, um be written up. So if you take your led and put the long pin and pin 13 and then the small one in the ground should light up like that, if you dont get that, then it could have been an arrow with the pin mode set up or the or just Digital writing. It like, if that is case like this is case sensitive, so if its not like that, then thats most likely why it didnt work but yeah, but now that we know how to digitally write uh pins on the arduino uh, we can also turn them off, like No, that sounds kind of dumb but like if we take led, pin and then set it to low, and then you upload it. Then if you try to do the exact same thing, it will not work because it is low, which means that its turned off. So what we can do with that now uh for the setup just turn that back to high, we can make it switch back and forth now. This is implementing something new called um delay if youre watching this is your very first arduino tutorial um, this probably isnt the best one, but anyway, um were gon na, involve a new thing called delay, so what you can do is make it flash.
So so here you have void setup. This will only run once avoid a loop will continuously run until it is until the task is like finished or until something goes wrong. So what were gon na do is digital right, led pin now, since its already high were gon na set it to low and then uh delay. Now, for this part, uh im gon na do 250. Now that might sound absurd, but like an absurd number. But this is timed in milliseconds, so what its really doing is quarter of a second. So this should do four times in a second. So what it will do is write it to low and it delays 250 milliseconds, then digi. Oh wait: digital right, led, pin high and then from the end, delay to 50 milliseconds. So this should blink um on and off for half a second because 250 milliseconds plus 250 milliseconds equals 500 and one more thing. After each line of code, or at least most of them like this integer um theres a semicolon pin mode semicolon um, you have to do that for most lines of code. Uh, um, im, just gon na explain this right now. If you have an if statement like if 8 10 is greater than 10., you dont have to put the semicolon there. If you dont have to put the semicolon there, if youre doing an else statement, um thats, thats, one example that you dont have to put the semicolon, but most lines of code.
You do so now. If we upload this and you stick the anode into the 13 and then the cathode into negative into ground, then it should blink, uh and ill show on camera in a sec. This is what it should look like uh. Obviously, you can adjust it, so you can make it delay by 500 milliseconds uh each making it a second long cycle, but uh yeah thats, mainly it for this tutorial. Um just want to warn you that these tutorials arent in order, so next video ill, probably be talking about boolean logic or like file statements, so uh yeah.