Photo by Louis Reed on Unsplash

Let’s play with Arduino — part 2

Madhav Mansuriya
5 min readJan 3, 2021

Welcome back to the second part of Let's play with Arduino

Without wasting a bit let’s get our hands dirty with code 😃

Prerequisites: Arduino Board (any), Arduino IDE (Integrated Development Environment) and you 😄

Coverage of this post

  • About Arduino IDE
  • Code Structure
  • LED blinking <code>

About Arduino IDE

Arduino provides an IDE to write a code and interact with Arduino board from the IDE itself, it makes your life easy to play with Arduino board, it provides a bunch of drivers to interact with any Arduino board and also has a list of other drivers that are used to interact with other modules connected with Arduino (can download in IDE itself). A program, written in Arduino is called sketch and saved with .ino extension, IDE provides a text editor to write sketches and has all features that are present in the text editor and much more

Arduino IDE

File, Edit, Sketch, Tools, Help are the options provided in the menu bar

Just below the menu bar, there is the command bar with 6 icons

✔️ : Verify → once you write your Arduino sketch and you want to verify whether you have some syntactical error.

➡️ : Upload → this button helps you to upload the code to Arduino board, it also verifies the code before uploading to the board.

📄 : New → once you click this icon you will be provided with a new sketch file (new file).

⬆️ : Open → on clicking of an open button it will open the file dialogue of your computer to choose a file to open in IDE.

⬇️ : Save → on clicking of save icon it will save your sketch to the directed space of your PC.

🔎 : Serial Monitor → once you click this serial monitor icon a new window on you IDE will be poped, it is used to log any of the debugging point or logs that you have added in your code.

Just below the command bar, there is a tabs bar, all your open sketches will be listed there

and now comes the hot section of the IDE that is code editor, you can write your code here and editor comes with a bit of intel sense 😅

after the editor, there is a status bar where all the status are been displayed while uploading your code i.e. compiling sketch, uploading sketch etc, and also there is a progress bar which denotes the progress of the process

and how can we forget about the console, after joining this field each and every black screen I see, the console comes in my mind 😅 (i know that was a bad joke), the console is for displaying the state of progress or errors if any, it is a very important part of IDE.

and the bottom bar is to state on which port your board is connected 🔌, there are many ports in the system on which all the external hardware are connected, to know on which port your Arduino is connected you can simply go to tools menu on the menu bar and select the port option

Ahh… that's all about IDE for tyro’s, you can explore more by going to each menu in IDE

<Code Structure/>

just giving the overview of methods, you all are awesome programmers 😎

there are 2 main methods in sketch and without that, your sketch won’t run don’t remove any of these, if removed you will bang your head why it's not working, but IDE is the smart devil 😜 it won’t allow you to go forward without this, it will give some errors in the console.

2 main methods: void setup and void loop

void setup: you can call this an initializer method, all the initialization will be done in this method i.e. initialising serial monitor, initialising 3rd party modules.

void loop: as the name denotes, this method takes all the logic that has to be executed repeatedly.

you can also make users define methods, to make your code look clean and readable.

Code Example for blinking LED

// the setup function runs once when you press reset or power the boardvoid setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forevervoid loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

as mentioned in the previous post, Arduino has a built-in LED on pin 13, let's blink it 💡

in the setup method, we will initialise the pin on which led is connected, to perform command given by the board.

to initialise pin pinMode() method is used, this method takes 2 parameters. 1st is pin number and 2nd is whether this pin will take input or give output
pinMode(14, OUTPUT); don’t miss ; it is as IMP as your life in coding 😅

now the 2nd step is to iterate blinking (on, of, on, of ……)
for that void loop() method is used

as mentioned in a previous post 13 is a digital pin, so it will take/give the digital signal.

digitalWrite(13, HIGH) → this method is used to communicate with given digital pin (send or receive signals)

digitalWrite() method takes 2 parameters 1st pin number, 2nd signal, here we have passed 13, HIGH so it denotes send signal high i.e. is boolean value also denoted as 1 to pin no. 13 this will turn on the LED

to take a pause delay() method is used, this method takes 1 parameter i.e. microseconds ex: delay(1000) it will wait for 1 second before executing the next line of code

and now the time is to turn off the LED again same method will be used but with different parameters digitalWrite(13, LOW) sending the LOW signal to pin 13 also denoted 0

and again wait for a second and repeat the same task, again and again, this will not stop until you unplug the power from Arduino

if you want to stop the execution after some time you need to add some conditions to it, take this up as a challenge to stop LED blinking after 5 times and again start after 5 seconds.

to explore more there are examples given in the File menu in the menu bar feel free to explore

if facing issues please add a comment to this post.

Happy Learning 😎

--

--

Madhav Mansuriya
Madhav Mansuriya

No responses yet