What is an Arduino?

Learn electronics and Arduino programming by creating 3 projects!

If you would like to learn electronics and Arduino programming step by step so this article is for you, in this article you will learn how to program the Arduino microcontroller and some electronics basics by creating hands-on projects , also you will  master programming basics that will help you to learn any other programming language so let’s get started



What is an Arduino?

Arduino is an open-source microcontroller platform and open source here means that you can get the internal design and customize it; also you can build new products based on the Arduino board.


Schematic of Arduino UNO Rev3

Why Arduino is so popular and why should you use it?

Arduino has many advantages that make it very popular for engineers, makers, and hobbyists:

Arduino is Very easy to use: You can program the Arduino by the USB cable and without using the burner like the other microcontrollers.

Arduino has a lot of libraries: if you want to save your time in writing code and develop your project in no time so the Arduino is a good choice for you.

The Arduino programming language: it is very easy to learn and understand the Arduino programming language. The language is derived from the C programming language and the processing programming language.

Let’s take a look at the different Arduino boards


There are many of Arduino boards and you should keep in mind that there is no big difference between them just the differences are

● The number of inputs and outputs pins

● The microprocessor type on the board

● The number of built-in parts on the Arduino board itself



Go deeper with the Arduino UNO

Now it’s time to explain you every single part on the Arduino board

1. Microcontroller: This is the heart of the Arduino Board. Arduino UNO and most boards contain an Atmel microcontroller unit (MCU), use an AVR microcontroller. Arduino UNO here uses an ATMega 328p.This microcontroller is responsible for processing all compiled and the execution of all commands.The Arduino programming language makes it so easy to access all the peripherals like Analog to Digital converter (ADCs) , general purpose input/output pins (GPIO).also, it contains 16MHz Crystal oscillator.

2. USB Port: used to connect the Arduino to the computer and provide the 5v power to turn on the board.

3. DC power jack: when you’re ready to untether your project from the computer, you have other power options like the DC power jack.

4. Power pins: The Arduino has two main regulators:

- 5v for digital input / output.

- 3.3v for used when you connect shields and external circuitry.

And also two pins for the ground.

5. Digital Input / Output pins: the most important part that we will care about during your projects is the general – purpose input/output pins. We will use it via the programs. They can serve as an input or output and also they have other special function like Pulse width modulation (PWM).

6. Analog Pins: the ADCs pins act as analog inputs to measure the voltage between 0v and 5v.

7. Reset Button: is used to reload the program on the Arduino board.

Memory Types on ATMEGA 328P (the heart of Arduino UNO)

● SRAM: The memory that used to store the variables temporarily.

● Flash Disk: It is a storage area that used to store the program which makes the microcontroller work.

● EEPROM: Its responsibility is to store some variables permanently like your hard disk in the PC.

● Bootloader: its functionality to enable programming via USB with no external hardware.



Let’s try and get your hands dirty

First, you are going to install the Arduino IDE and prepare your hardware

You will need the following components

● An Arduino UNO Board           ● USB Cable

● Breadboard                                ● LEDs

● Resistors                                    ● Multimeter

● Wires

An Arduino UNO rev.3

● This is the board that contains the ATMega328P microcontroller. It has 14digital input/output pins including 6 pins can be used as PWM outputs. A 16MHz resonator, a USB connection, a power jack... etc. (for more details you can review chapter 1).

Rev3 features:

●The reset circuit is stronger than the older revisions.

● It includes ATMega 16U2 instead of 8u2.



A-B USB Cable

● This cable used to connect your Arduino to your computer you can buy any type but I prefer to be as short as possible




Breadboard

● We will use this board to connect the components together without soldering you can use any type.



LEDs

● LED stands for light emitting diode. LED something like bulbs also they are available in many types and colors like Red, Green, Yellow, White, orange. They mainly used for debugging purposes.



Multimeter device (optional)
● It’s an electronic device which used to measure voltage, current, resistant, capacitance



Resistors

● A resistor is an electrical component that used to control the flow of current in the circuit.

Wires

● Jumper wires used to connect our components with each other on the breadboard.

- Male to female wires.

- Male to male wires.

- Female to female wires.

The Arduino IDE

● The Arduino Integrated development environment is the tool that will be used to write and upload the code to your Arduino.

● It uses a very simple programming language which is the Arduino C.

Install Arduino IDE on Linux

● You can install the Arduino IDE from the software center in Ubuntu.



● Write “Arduino” on the search form then click enter/install.

● If you use any other Linux distro you can search for the Arduino IDE on its software center.

Install Arduino IDE on Mac / Windows

● Go to https://www.arduino.cc/en/main/software .

● Select the windows/mac installer.

After the installation

● connect the cable to the Arduino board.


● now open the Arduino IDE.



1. The code will be written in this area.

2. Navigation bar to upload, verify, save

3. Console area to show errors and warning.

4. Menu bar.


Prepare the Arduino IDE

• First, we will go to tools menu, the board then choose Arduino UNO.



● Second, we will select Tools, Port then Com (“x”).

X is the port number.



First example: LED blinking (Wiring)



Steps

● connect the longest leg (+) of the led to pin number 13.

● connect the other leg with the 560 ohm resistor.

● connect the 5v pin and GND pin on the Arduino to the breadboard as shown


First example: LED blinking (Coding)

Steps

● Open the Arduino IDE select File then new.

● Write the following code.

const int LED = 13;                                                        
void setup ( )
{
  pinMode(LED, OUTPUT);                                                    
}
void loop()
{
  digitalWrite(LED, HIGH);  
  delay(1000);
  digitalWrite(LED, LOW);
  delay(1000);                      
}


After writing the code now you can press verify for the quick bar on the IDE and wait until “done compiling” then select upload to load the code on the Arduino board.


Congratulations! you have done your first program on the Arduino

Now it’s time to explain you the code

● Const int LED = 13;

This statement means that you will create constant of type integer with name LED and value 13

● We use constants here to make it easy in naming (Input and output) pins

On the microcontroller in example 1, we declare the constant to control the pin 13 by using name “LED” so we can use the name instead of using the pin 13 for readability.

● Void setup ()

{
   PinMode (LED, OUTPUT);
}
 To set the pin number 13 to the output mode this has the name “LED”.


 There are three steps on to write a program Arduino or any microcontroller

● First of all, declaring the variables that we will use in the program.

● Secondly, it’s really important to understand that all of the digital pins we can set them as an input or output pin, In our example we set the pin 13 as an output.

● PinMode (pin number, state)

- PinMode-> function name.

- Pin number -> the number of the pin we will use.

- State - > to set the pin as an input or output.

● You should write “OUTPUT” or “INPUT” with capital letters.

● Also, we should write all of our input and output setting inside the function braces → {}

Setup () {Write you configuration here} , for example
If we want to set pin10, pin11, pin 13 as an output
and pin 2 as an input :
Void setup()
{
 pinMode(10,OUTPUT);
 pinMode(11,OUTPUT);
pinMode(13,OUTPUT);
pinMode(10,INPUT);

note that every statement must end with semicolon”;”.
}

● The final step is to write what the microcontroller should do as in the first example :
Void loop()

{
 digitalWrite(LED, HIGH);→ turn on the led.
delay(1000); →wait 1000 millisecond “1 second”.

digitalWrite(LED, LOW);→ turn off the led.
delay(1000);→wait 1000 millisecond “1second”.
}

● keep in mind that the program will be written inside the function

 void loop () {Write you code here}

● digital write (LED, HIGH)

HIGH = 5 volt

LOW = 0 volt

First, we write the pin name and then the voltage.

● delay (1000)
To tell the microcontroller how much time to wait before the execution of the next instruction so we can control in time of LED turning off or on

● As we can see in our example:

            digitalWrite(LED,HIGH);
            delay(1000);
 
These instructions mean that the microcontroller will apply the 5volt on the output pin which connected to the LED, then it will wait 1000 millisecond.

Note: when we write programs on microcontrollers time should be written in millisecond instead of second “1000 millisecond = 1 second”.

● There are two types of comments

      // this is a one-line comment

    /*    This is

a multiline

 comment*/

● We use a comment for readability or to describe the code and make it easy to understand

● The Arduino IDE will ignore any comment




Example 2  (Wiring)

In this example will use a pushbutton to turn on / off the LED



Parts you will need:

- Breadboard

- Push button

- LED

- 10k ohm resistor

- 560 ohm resistor

- Some wires


Steps:

● put the pushbutton on the breadboard.

● Connect one side of the button with the 5v using the wires.

● Connect the other side to the 10k ohm resistor.

● Connect the wire to pin 2 on the Arduino.

● connect the other leg of the resistor to the ground.

Example 2 (Coding)

/*first step variables declaration and assignment */
const int ledPin = 13;        
const int buttonPin = 2;      
int val;          

/*Second step define the pins and its directions */
void setup ( )

 {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

 }
/*Third step writing the program */

void loop()
{
  val = digitalRead(buttonPin);

  if (val == HIGH)
 
     {
  digitalWrite(ledPin, HIGH);  
  delay(1000);        
  digitalWrite(ledPin, LOW);
  delay(1000);  
      }                  
  else {digitalWrite(ledPin, LOW); } }

● Now click on verify button and after the compilation, click the upload button to burn the code on the Arduino board.

● Now it’s time to explain the code

● In the first block
 
   int ledPin = 13;
 
   int buttonPin = 2;
 
   int val = 0;

● We declared a variable called ledPin which assigned to pin13, also we declared another variable called buttunPin which assigned to pin2
And Val variable we will use it to store the input state.

● in the second block
               void setup ( )
          {
             pinMode(ledPin, OUTPUT);
              pinMode(buttonPin, INPUT);
           }
● We make the controller work with the pin 13 as an output which assigned before as “ledPin” then we set  the pin2  as an input to receive the digital signals

Low or high

● In the third block

Val = digitalRead (buttonPin);

In this line, the Arduino will measure the voltage and store the value in the variable Val using the digitalRead () function for example:

- If the button was pressed, so the value will be 5v = HIGH.

- Otherwise, the value will be 0v = LOW.

If (Val == HIGH)
 {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite (ledPin, LOW);
}
else
{
  digitalWrite (ledPin, LOW);
}

In the above code, we used if / else statement

To compare variables and make the microcontroller do some actions based on the results.

● The Arduino will measure the voltage and store the value in Val.

● If the value is equal to 5v or High, the controller will turn on the led for 1 1 second and turn it off for 1 second

● Unless the value doesn’t equal to 5, so the microcontroller will not turn on the led and will be off.




Example 3 (Wiring) led blinking using two push buttons




Parts you will need:

Arduino Uno

Breadboard

Led

10 k ohm resistors (2)

Push buttons (2)

560 ohm resistor

Wires

In this example, we will apply what we have learned in the last 2 examples
Example 3 (Coding)

● From the Arduino IDE select file > new and write the following code

/* declaration and assignment of the variables*/

Const int ledPin = 13;

Const int inputPin1 = 2;

Const int inputPin2 = 3;

/* define the direction of the pins */
Void setup ()
 {
   pinMode (ledPin, OUTPUT);
  pinMode (inputPin1,INPUT);
  pinMode (inputPin2, INPUT);
}
/* the main program */
Void loop ()
{
   If (digitalRead(inputPin1) == HIGH)
  {
      digitalWrite (ledPin, LOW);
  }
   else if (digitalRead (inputPin2)== HIGH);
{
   digitalWrite (ledPin, HIGH);
}
}

● In this example, we used else if for adding more than one condition in one if statement.

Void setup () → This function used to set the pins directions as an input or output.

Void loop () → in this function body you will write your main program.

Int name = value;  statement to define a variable and its value  

example: const int led = 13;  statement to define a constant

pinMode(pin number, state);  to define the pin number and its direction

example: pinMode(11,INPUT);

digitalWrite(pin number, state)  to determine the voltage on the pin

example: digitalWrite(13, OUTPUT);

digitalRead(pin number) To read the voltage from the pin

example: digitalRead(4);

delay(time)   this function used to  determine how much time should the Arduino wait

example: delay(1000);

if (the condition)  {what to do}

else if (another condition)

{what to do }

else(last condition)

{what to do }

Conditional statements to determine what to do based on some variables

Data Type example Value (range)

Integer int led = 13; From -32768 to 32768

Float float sensor = 12.5; With decimal numbers

Character char name = ‘a’; character/text

Long Long variable = 99999.9; From -2,147,483,648 to

2,147,483,648

Byte Byte number = 55; from 0 to 255

Conditional statements to determine what to do based on some variables



Comments

Popular posts from this blog

Embedded Systems

"Gas Alert!" Gas Project Prototype Overview