Our first program (led blinking) in Arduino
1. Requirements (Hardware)
i. Arduino Uno
ii. Male- female jumper wire (2)
iii. An LED
iv. 12v DC power adapter
v. //review current ratings// resistors
Requirements (Software)
i. Arduino IDE
ii. Our .ino file
2. We will now write our first Arduino code.
It is to note that Arduino code is stored or saved in a file with .ino extension.
Also, it is to note that an Arduino ino file must be saved in a folder having the same name.
if that folder is not present, Arduino itself creates the folder and put the ino file having the same name in that folder.
Now, open Arduino IDE and press File>New. Arduino IDE always opens the last modified file. On clicking File>New, a new blank file with working code blocks will open in the editor of the IDE.
Now write the following code in the editor.
******************************************************************
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
// initialize digital pin 8 as an output.
pinMode(8, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(8, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
Serial.println("This is the end of the loop");
}
led-blink.png:
********************************************************************
3. Circuit Setup
i. Connect Positive leg of the LED with the digital pin 8 with a jumper wire
ii. Connect Negative leg of the LED with GND pin of UNO with a jumper wire
iii. 1st connect the Arduino UNO board to the Computer with the USB cable and select the right board from Tools>Boards>[submenu], then select the right port from Tools>Port (It will be anyone like COM1, COM2 etc.)
iv. After uploading the program to UNO we need to disconnect the USB cable from the computer and connect the Arduino UNO with 220 AC power line with a 12v DC adapter.
Circuit:
4. Observations
Here we see that the Arduino code is devieded into two code blocks. One is setup() method and the other is loop() method. These are two main code blocks of any .ino file to be loaded (along with its allied files) to an Arduino board. The setup() method is the entry point of instructions for an Arduino board. Program control transfers from setup method to loop() method after finishing the initial tasks for the Arduino. Then, inside the loop method instruction or tasks are executed periodically and forever. Unless and until an external or internal event instructs it to stop, the Arduino board continue to loop forever. This fulfills the requirements of an embedded system.
5. Save, Compile & Upload your code
After writing the code the file is saved with FileSaveAs menu with a name Blink.ino in a folder of our desire.
Upon checking, we will see that the Blink.ino file has been created in a folder named Blink. This Blink folder will automatically created inside the folder in which we wanted to save our Blink.ino file.
Now Compile the file with the Verify/Compile button.
If compilation is successful, we will upload the .ino file to the Arduino board by clicking the Upload Button.
6. Testing our 1st program in Arduino
After uploading the code to the Arduino Board, we will open SeriaMonitor to see/check whether the loop is running properly or not. Open Serial Monitor by clicking ToolsSerialMonitor menu. The following window will open:
In this window, we will select the correct baud rate for our code. In this case it is 9600.
Arduino IDE:
Now look at the code inside the setup method. Look at the line:
Serial.begin(9600);
This instructs the Arduino mcu to open the serial port with a baud rate 9600. So , we have to select 9600 as the required baud rate in the ToolsSerialMonitor window.
In this SerialMonitor window we will see that the text mentioned in the following statement will appear repeatedly.
Serial.println("This is the end of the loop");
The above statement in the loop() method will be executed in each 2000 milliseconds. As there are two delays 1000 ms each inside the loop method, a total 2000 ms or 2 seconds delay will be generated.
At the same time, we will see that the LED connect to digital pin 8 is blinking on and off in each 1000 ms interval.
Now if we disconnect the USB cable from the computer and supply power from a 12V DC power adapter, we will see that the LED connected to Arduino UNO board is blinking as before.