Skip to main content

Arduino Basics - Software, Code Basics

Arduino Basics - Software, Code Basics

Arduino is an open source family of electronic microprocessor boards that we can easily program to understand and interact with the environment.
Over the years, Arduino has become the standard for building electronics projects. Arduino has been sent into space to run micro satellites, it has been sent to the bottom of the ocean to control small robotic submersibles, and now, Arduino has arrived for you.
The first thing we need is the Arduino Integrated Development Environment (IDE). One of the best parts about Arduino is that the software in which we need to program the boards is free and open source. The Arduino IDE is compatible with Windows, Mac OS X, and Linux.
Now I assume that you have installed Arduino, Lets see whats inside.
Arduino IDE Basics - Software, Code Basics

Here is a screenshot of Arduino IDE Software on my Laptop.
First lets discuss the Tool Bar. In the Tool Bar, we can find the most used buttons:


Button
Description

The Verify button compiles the code and checks it for errors.


The Upload button compiles the code and, if there is no error in the code, uploads it to the Arduino board.
The New button starts a new program. In the Arduino world, programs are called sketches.

The Open button simply allows us to open a saved sketch.


The Save button saves the current sketch.


This button opens the Serial Monitor window that allows us to communicate with the Arduino board. It is extremely helpful when we debug a program.
In the Sketch tab, we can see all the opened Arduino Sketches. This comes handy when we want to work on multiple programs at the same time.

The Code Space area is where all the magic happens. That's where we write the code that powers satellites and cat food dispensers. It's a code editor with automatic syntax highlighting and auto arranging.

The Status Display area indicates all the bad stuff. Whenever there are errors in the code, they will be displayed there. It also displays errors in the connection with the board. The only good thing it can display is that the code has been successfully uploaded to the Arduino board.

Additional functionality can be found in the main menu bar. Here, we have the classic File menu where we have Save, Open, Close, and also some examples. In the following recipes, more will be discussed about the menu bar components. A nice trick worth sharing is in the Tools menu—the Auto Format tool will format the code to look professional and clean.

Uploading Code to Arduino
It's time to power on the Arduino board and make it do something. In this recipe, we will connect the Arduino to the computer and upload an example sketch from the Arduino IDE.

 How to do it?

1. Connect the Arduino to the computer using a USB cable. If everything is properly connected, the green LED light will turn on.
2. If this is the first time the Arduino has been connected to the computer, driver installation might be required. Please follow the Connecting Arduino recipe to properly set up the Arduino board.
3. Start the Arduino IDE and, in the Menu Bar, go to File | Examples | 01. Basics and click on the Blink example. This will load the Blink sketch.
4. Make sure your Arduino board is selected in the Board menu. The menu can be found in the Menu bar in Tools | Board.
5. We need to check whether the correct serial port is selected. Under Tools | Serial Port, we can see all available serial port devices connected to the computer. On Windows, each port will be labeled as COM followed by a number. Usually, Arduino installs on COM3, but not always. A fast way to check which serial port the Arduino is connected to is to unplug the cable and see which COM port disappears in the menu. That will be our Arduino board. In the Mac, the port should be called something beginning with /dev/tty.usbmodem or /dev/tty.usbserial.
6. Click on the Upload button on the Tool Bar. If everything runs properly, the TX RX LED's on the Arduino board will begin blinking for a short time until the upload is done. After this, one LED light on the Arduino Board should slowly blink with a delay of 1sec
Learning Arduino Code Basics
Here we begin with the basics of coding for Arduino. Writing code for Arduino and other embedded platforms is a little different from writing code for a computer. But don't fear—the differences are small.

Code Basics

These are the two mandatory functions in the Arduino coding environment:

void setup()
{
//only executes one when the Arduino boots
}
void loop()
{
//code executes top to down and repeats continuously
}

Explanation :
Each Arduino sketch has two mandatory functions:

The setup() function and the loop() function. The setup() function only executes once: either when we apply 
power to the Arduino or when it resets. Usually, we use this function to configure the pins of the
Arduino, to start communication protocols, such as serial communication, or to perform actions we only
want to perform once when the Arduino boots.

The loop() function executes continuously. Code in this function is executed top-down; when it reaches the end of the function, it jumps back to the start and runs again. This happens forever until the Arduino is switched off. In here, we write the code we want to run continuously.

Comments

Popular posts from this blog

7400 - Quad Two Input NAND Gate

7400 - Quad Two Input NAND Gate 74LS00 is part of the 74XXYY IC series. 74xxyy integrated circuits are logical gates of digital electronics. The 74LS00 IC has four NAND gates(NAND Gate is the combination of AND and NOT Gate). In addition, each door has two entrances. Hence the name QUAD TWO INPUT NAND GATE.     Pin Diagram of 7400 IC 74LS00 is a 14-pin device. The chip is available in different packages and is chosen according to the needs.  The description of each pin is given below. Pin Number Description 1 A1-INPUT1 of GATE 1 2 B1-INPUT2 of GATE 1 3 Y1-OUTPUT of GATE1 4 A2-INPUT1 of GATE 2 5 B2-INPUT2 of GATE 2 6 Y2-OUTPUT of GATE2 9 A3-INPUT1 of GATE 3 10 B3-INPUT2 of GATE 3 8 Y3-OUTPUT of GATE3 12 A4-INPUT1 of GATE 4 13 B4-INPUT2 of GATE 4 11 Y4-OUTPUT of GATE4 7 GND- Connected to ground 14 VCC-Connected to positive voltage to provide smdksmkm power to all four gates Internal Ci

Arduino Code Basics - C, Pins

Code Basics -Arduino C The Arduino uses a slightly reduced C/C++ programming language. In this recipe, we will remember a few basics of C/C++. Ensure that you have Arduino IDE Installed on your Laptop/PC. Here is a simple example of basic Arduino C/C++ manipulating two variables: // Global Variables  int var1 = 10;  int var2 = 20; void setup() {   // Only execute once when the Arduino boots   var2 = 5;   // var2 becomes 5 once the Arduino boots } void loop(){   // Code executes top-down and repeats continuously   if (var1 > var2) {  // If var1 is greater than var2 var2++; // Increment var2 by 1   } else {  // If var1 is NOT greater than var2     var2 = 0; // var2 becomes 0   }  } When we upload a sketch to the board, the Arduino software first compiles the code. If there is an error in the code, it will write it in the Status Display area and will stop the upload. If no errors are found, it will begin writing the compiled co

RGB Led Fading effect using Arduino

The RGB LED can emit different colors by mixing the 3 basic colors red, green and blue. So, it actually consists of 3 separate red, green and blue LEDs, packaged in one box. This is why it has 4 leads, one for each of the 3 colors and a common cathode or anode depending on the type of RGB LED. In this tutorial I will use a common cathode. Components required for this Project : RGB Led - Buy Arduino Uno - Buy Breadboard and Jumpers - Buy Circuit Diagram Above is the circuit diagram of the RGB Led and Arduino Uno , As you can see the ground pin  of the RGB LED is connected to the ground of the Arduino and other 3 pins of RGB Led are connected to the PWM Pins of Arduino Uno (9, 10, 11). Code int ledPin = 9;  int ledPin1 = 10; int ledPin2 = 11; void setup() {} void loop() {   for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)    {     analogWrite(ledPin, fadeValue);     delay(20);   }   for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -