Blink LED-The First Program on Arduino
Now, with the upload successfully done, lets write the basic "Hello World" Program of electronics world. In electronics, the basic program that's written is testing a LED. Before proceeding with this program, i would suggest a quick read on GPIO pins of arduino. Fortunately, there's an on-board LED attached to the pin 13 on Arduino board. So in a nutshell, we will be controlling this LED attached to the pin 13 on Arduino. You can find a rough schematic of our little experiment here. This schematic is inbuilt in the arduino board and is just for our understanding. Later on, we can extend this concept to controlling an external LED.
I am simply posting the entire code snapshot here as it is a very basic code and is available everywhere. You can also find the blink example in the examples under File Menu.
Now, lets go over the different sections of the program. This will also explain the basics of arudino programming and the functions being used.
I am simply posting the entire code snapshot here as it is a very basic code and is available everywhere. You can also find the blink example in the examples under File Menu.
Now, lets go over the different sections of the program. This will also explain the basics of arudino programming and the functions being used.
- The Setup or Configuration Portion[void setup()]: As discussed earlier, this function is for configuration of the program. Here, since we have to control the LED attached to pin 13, we need to specify whether the GPIO is being used as an input or output. Since LED is an output device and we need to provide values on the pin, we will set the GPIO 13 to be output. The function pinMode(13, OUTPUT) sets the pin in output mode.
- Looping Portion[void loop()]: This portion repeats the piece of code written inside it. Now, our objective here is to blink the LED i.e. make the LED on and then off. However, if we implement these functionalities as it is, the instructions will be processed so fast that we wont be able to observe the blinking. Thus, we will also give a small amount of delay so we can observe this blinking. To switch on the LED, we need to give a logic '1' to the pin 13. For this we use, digitalWrite(13, HIGH) which gives a value of 1 to the pin 13 thereby turning the LED on. Similarly, to turn the LED off, we give digitalWrite(13, LOW). Now these 2 statements are followed by delay which is given by delay(x) where x is a value in miliseconds(ms). So our basic algorithm becomes:
LED ON
DELAY
LED OFF
DELAY
Repeat the above steps indefinitely
I would suggest you to write the program yourself so you get the hang of basic Arduino programming. After successfully writing the program, verify and upload the program. Refer to the Dummy Program Upload on arduino to first check if your arduino is connected to the PC and is able to receive the program sent from Arduino IDE. Try to change the delay and observe your output on hardware.
Note on Arduino Libraries and functions: There is a huge variety of functions like digitalWrite() and libraries available in arduino. The Arduino site has a very good documentation and reference area where the syntax as well as functionalities of all the functions is available. Click here to visit the reference section. For instance, digitalWrite functionality can be seen here. You can also google the function name to directly get its reference on the arduino site. Happy Programming and Prototyping :)
Comments
Post a Comment