Getting Started with Arduino Programming

Hello friends, i hope you all are fine enjoying. Today i am going to share a new project tutorial which is Getting Started with Arduino Programming. In the previous tutorial, which was titled as Getting started with Arduino Software, I have explained in detail the basics of Arduino software. How this software is installed and run.

Today's post is also related to arduino software but, the difference is that, in previous post we learn How to use the arduino software, What are the function keys of this software and Where to write the code(sketch) and after writing the sketch, how it is debugged. Now in today's post which is ' Getting started with Arduino Programming', we will see How a Code is written in Arduino software? What are the built-in functions of Arduino software? How comments are made in sketch? What are the main loops, used in Arduino programming? How pin selection is done in Arduino software? How Digital pins are controlled, which are embeded on Arduino board? To explain this whole software in detail, i have divided this tutorial into major parts and also their sub-parts. Above was a little introduction about what actually we are going to learn in this post. Now without wasting any further time on introduction (as i will be explaining each and every step in below tutorial) i think now we should move towards the actual learning of the Arduino software to use it for programming.

You should also read:

1- How to make Comments in Arduino Programming

  • I hope that you are a beginner and i will start to elaborate the soaftware from very beginning.
  • First of all open your Arduino software. Go to file menu, a new window will open, from there click on the Examples and then go the Basics.
  • After clicking on basics a third new window will open and it will be showing all the basic Arduino built-in basic functions.
Note:
  • If you haven't bought your Arduino UNO yet, then you can buy it from this reliable source:

  • For beginners the function named as blink is very useful, so i will also teach you from this basic level tutorial.
  • When you will click on 'blink' option then, a new window will open, which is shown in the image given below:
  • In the above shown image you can see that we have opened a basic level example from built in Arduino software.
  • Now under this heading title, i will explain how to make comments in Arduino programming?
  • This is in fact the syntax to make comments in your sketch.The above code is of very basic level and it elaborates how to blink a single led by using Arduino board.
  • If you look closely in the above image then you will see that some words are written in the start of the sketch and these words are encloses in these signs " /* ..........*/ ".
Note: Comments have no rule in sketch, neither they are as necessary to write in every sketch. We only add them in code to make the code user friendly or to make the user understand that what actually in happening in the code.
  • An other way to add the comments is to write " // .........." before every line in your sketch.
  • When you will do that before any particular line then, that line will become green and it will no longer have any effect in the sketch.
  • There is also a little difference in these both techniques of making comments.
  • First technique is used at that place where we have to write 3 or 4 lines together, or a large paragraph is to introduced in the sketch to make the user understand the sketch. We don't have make every line comments one by one. So, this method of making comments gives us ease.
  • While the second technique is used at that place, where we have to make comments in a single line.
  • Both these techniques have there own purposes and are used at that place where, we wish to use them.

2- Main Loops In Arduino Programming

  • Now the next step in learning Arduino programming is to learn about the main loops, which are used in Arduino programming.
  • To see what are the main loops, you simply go to the file menu and click on the new button and a second new window will open, which is shown in the image given below:
  • Above image is showing the main loops, which are used in Arduino software while programming.
  • The first loop is 'void setup' and the second loop is 'void loop'.
  • The syntax to write these both loops is also shown in the above image. But you should don't worry about the syntax of these loops because the good news is that the software it-self generated the syntax, when you open a new file to write the sketch.
  • Void setup is the first and the main loop while programming in Arduino. It is used for initialization and for configuration of constant values.
  • For example while writing a sketch if you are gonna need some constant values in your code then, it is necessary that you must initialize them first in this loop.
  • If you didn't initialize those constants first then, code will not execute properly and it will generate error.
  • The other main loop which is used while using programming in Arduino is "void loop", as you can also see it from the above given image.
  • An important thing to note here is that the compiler checks each and every line one by one in steps.
  • The compiler moves from top to bottom and in the first step, it will read every line of the " void setup() " and then it comes to the " void loop() " and it reads/compiles every line written in this portion.
Note: An important thing to remember here is that, the compiler reeds ' void setup() ' loop only once and on the other hand it reeds the second loop, named as ( void loop() ) again and again.
  • So from the above given info, we can conclude that ' void loop() ' is similar to ' while(1) ' loop, which is in fact an infinite loop.

3- Pin Mode Selection in Arduino Programming

  • In the above two portions of the today's tutorial we have seen, How to make comments while doing programming in Arduino software and secondly What are the main loop being used in Arduino programming.
  • Now we are going to the third stage which is Pin Mode selection in Arduino programming.
  • If you are aware of the other micro controllers like ATmel series, PIC or AVR, then you may also know that the pin configuration of these micro controllers are fixed.
  • Which means you have pre-decided input and output pins of the micro controller. Those micro controllers have definite pins for inputs and also for outputs and they can only be used for that specific purpose.
  • For example if a micro controller have a input port then all the pins on that port will be used to receive data and you can't use them to send data.
  • While working with Arduino micro controller gives us a flexibility that every I/O pin available on the board (except for some definite pins like VCC,GND, etc) can be used as input or output pin.
  • For example if we wish to connect a led at the pin#2 of our Arduino board, which means we are going to make this pin#2 as a output pin.
  • For Arduino board we will send data to this pin through our code, which means we have made this pin as an output pin.
  • The syntax to write command to do this function is:
pinMode (2 , OUTPUT)
  • By writing this command in the code, we have made pin#2 as a output pin.
  • And if we connect some kind of sensor at this port and we have to take the value of that sensor as an input then, we will have to make pin#2 as an input pin.
  • In order to make the same pin as an input pin, we will have to write the command in the following syntax:
pinMode (2 , INPUT)
  • By writing this command, Arduino board will automatically made the pin#2 as an output pin.
  • Pull-up criteria is also very important in writing any commands for Arduino board.
  • There is only one problem with all the Arduino boards that, we whenever an un-initialized pin is used in our code then the Arduino board send some garbage value to that particular pin.
  • To overcome this problem we have to set some particular value for each and every pin. and we ahve to define state of every pin that either it is HIGH or LOW.
  • For example we can say that the function of pin#3 is that GND is always connected to that pin and if ground is not available at any case then we will send +5V to that pin to keep that pin in a particular position.
  • The Arduino board command for pull-up purpose is:
pinMode(2 , INPUT_PULLUP);
  • This command will automatically decide the particular value of every pin.

4- Digital Pins Controlling in Arduino Programming

  • Now the fourth part of the today's tutorial in learning of Arduino Programming is How digital pins are controlled while coding in Arduino.
  • Arduino board is a multi purpose micro controller and it is also capable of sending and receiving data from Digital as well as Analogue data.
  • It is our choice to make every pin as an input or output pin and similarly either it has to send or receive Digital data or it has to send or receive analogue data.

I/P Digital Pins:

  • For example, i wish to make my pin#2 as an Digital input pin, which means it will read digital data from some external source.
  • The command to do this function is given below as:
digitalRead(2); boolean sensor=digitalRead(2);
  • The first command is teeling the Arduino compiler to make pin#2 as a digital pin and the compiler will take Digital input data from that pin.
  • While the second command i have written is to save the command in a variable.
  • In this command i have introduced a datatype named as 'boolean' . You can also change the name of this datatype.
  • And the name of the variable is 'sensor'. I have kept the name 'sensor' and you can also change this name according to your own choice.

O/P Digital Pins:

  • Next if you wish to make these pins as an output pins then we can also do that.
  • Suppose now if you want to make your pin#2 as an output pin and you wish to send data to this pin or you want to define the state of this pin then the commands, which you will write are:
digitalWrite(2,HIGH); digitalWrite(2,LOW);
  • In the syntax of writing the command, we will first write no of the pin and then we write it's state.
  • The first command is showing that we are writing digital data on our pin#2 and we are going to set it in HIGH/ON state.
  • The second command shows that we are going to set the pin#2 in OFF state.

5- Analogue pins controlling in arduino programming

  • In this section of the tutorial we are going to see how to control the Analogue pins of an Arduino board.

I/P Analog Pins:

  • As i have explained above in detail that how we can digitally take or send data using Arduino pins.
  • An interesting feature is that Arduino board pins can also be used to send or receive data in an analog pattern.
  • Since my heading is to input analog data, to understand this pattern suppose that you are going to read some analog voltage from pin#2 of arduino board.
  • In order to do that, the commands which you will write in Arduino software are as:
analogRead(2); int sensor Value = analogRead(2);
  • As you can see that the syntax to read Digital and Analog Data is same.
  • In first command i am reading the analog data from external source at pin#2 of arduino board.
  • In the next command i have stored that data into a variable named "sensor value" .
  • After that all the analog data which will appear at pin#2 will be stored in 'sensor value'.

O/P Analog Pins:

  • Now if we wish to use the pins of Arduino board as an output pins then it is also very easy and you can also do that by following some simple steps.
  • If you wish to use any pin to write data or we can say that you wish to turn a switch ON or OFF then, the syntax will be as:
analogWrite (3,HIGH); analogWrite (3,LOW);
  • The first is showing that a switch has been connected at pin#3 and you wish to turn it ON and you will write the first command in Arduino sketch.
  • The second command is showing that a switch is already in ON state, which is connected at pin#3 and we wish to turn it OFF then, we will write the second command in our Arduino sketch.
Alright friends, today's tutorial was a little bit lengthy but very informative for beginners who are keen to learn Arduino software. If you have any questions then, ask in comments and i will try my best to solve the problem. Till next tutorial take care !!! :)

What is Serial Port

Hello friends, i hope you all are fine and enjoying. Today i am going to share a new project tutorial which is What is serial port? We can also judge from its name that this pin that this may be the pin used for data communication. So we can say that Serial port is such a device which is used for communication of data in a serial manner. and an important thing to remember here is that this port can communicate only one bit at a time.

Serial port is now used in almost all type of computers and it is used to connect some some devices like modems, monitors, LCDs and some models of printers to the mother board of computer. If we talk broadly then some devices connected to the computer like Ethernet, USB etc  also requires serial data stream for communication but the name serial port is reserved for that case when you connect a Modem or some similar device which needs to communicate data in a serial manner. Serial ports have been used in past with almost all models of the computers but with the invention of modern technologies like USB, FireWire and some other faster means of data communication, serial ports have been eliminated now and are now replaced by the modern means of data communication. If you still have a old model of a computer at your home or work and you wish to identify that which one is serial port then, look at the back side of your computer and you will observe a 8 pins D shape male connector. As i have shown one of them in the featured image of this tutorial. Now lets move towards the hardware of the serial port and see whats hidden inside this small port and what are the practical applications of this port. so'save a the answer question,

What is a Serial Port?

The old models of IBM computers had been using a small electronic integrated circuit named as UART and it was used to transmit and receive data to and from a external asynchronous device in a serial manner. But with the inventions of modern technology some small home computers then started to make a algorithm in which CPU was used directly to send and receive data from external means. And this technique was known as bit-banging technique.

Early invented computers also have a at-least one serial port and this serial port had different pins in it and every pin was designed to work on a different voltage level, and this serial pin was compatible to work with RS-232. But there was a problem with the operation of such system that the serial port was unable to understand the different voltage levels of RS-232 and different manufacturers had different devices that operated on different voltage levels. So there was need to design such device which should be able to work on same voltages and should be compatible with all the devices, of any manufacturer. Now with the invention of modern devices like USB, FireWire the modern and even low cost processors are able to serial communicate data with a faster speed. And they are able to support devices like mass storage, sound players and video player devices. Now a days in-spite of that fast that what have been invented, every personal computer in use also contains a serial port. The small laptops have a very conserve space so it is possible that some companies may have omitted serial ports from their models but as i have stated above that serial ports had been used for a very long time periods and the circuits used to control and operate the serial ports have become very cheap and some models also comes with the serial port chip fabricated with the circuitry of parallel port.

Pin Configuration of Serial Port

Serial port have total 9 pins. Every pin has its own functioning and purpose. The purpose of each pin is explained below as:

  • The pin#1 of serial port is named as 'DCD'. The purpose of pin#1 is Data Carrier Detect. It is used to detect the data stream which is being carried bu the buses for sending to other devices or for receiving.
  • Pin#2 of serial port is named as 'RxData'. The purpose of this pin is to monitor the data which is received by serial port from some external means.
  • The next pin which is pin#3 of serial port is named as 'TxData'. The purpose of this pin is to transmit data from serial port to the external module, where we wish to send data. Remember that serial port only transmit data at the rate of one bit per second.
  • Pin#4 of serial port is named as 'DTR' and it is called Data Terminal Ready. The purpose of this pin is to see the data which is to transmit but it has not been sent yet. The data is available at the sending port of serial port and it needs the go signal from micro processor.
  • Pin#5 of the serial port is 'GND' and it provides ground to the serial port voltages for data communication.
  • Pin#6 is 'DSR' and it is abbrivation of Data Set Ready. This pin is used to determine the data transmission rate. Data is available at the port but it has not been sent yet.
  • Pin#7 is 'RTS'. This pin is the signal for sending the data to external means. When data is available at pin#3 of the serial port and to send this data you have to get permission from control circuit and pin#7 which is in fact RTS pin does this job.
  • Pin#8 is 'CTS'. It is known as Clear to send. When pin #7 gets requests to send the data and if it gets the permission then data is now ready to send and pin#8 sends it in serial manner.
  • Pin#9 is the last pin of serial port it is a ring indicator and it is abbreviated as 'RI'.

Applications and Usage of Serial Port

Serial port possess a large no of practical applications and usage. It is true that it have been eliminated by the modern technology but still there are many devices which are only connected through serial port. Some of serial port's applications are listed below as:

  • They are most commonly used on some engineering projects with micro controller, where they have to communicate with a personal computer or some other similar devices.
  • Biggest application of serial ports is that they are used to connect to modems.
  • LCD and flat screen monitors are used to connect to the computers through this serial port device.
  • Some devices which are used for network sharing are also connected through this serial port. Some networking devices are like firewalls and rotors etc.
  • Serial port is also used in special applications of GPS (global positioning system) and they are used either to send data or to receive data in serial manner.
  • Also used for scanning bar codes of different products. Mostly serial port is used in some big shops and markets to detect which product you are selling.
  • Serial port sends or receive data in a serial manner, so they are also used in some applications where you wish to display some text on a particular LCD.
  • Serial port is a portable and very much sensitive device and it is also used is modems of satellites and also in some transceivers.
  • Used in some digital testing and measuring electrical instruments like digital multimeter. Where you measure the voltages or current of a particular system.
  • Serial ports have a variety of applications in some mechanical design CNC machines. In these machines, serial port is used to transmit or receive data from micro controller of the machine.
  • Also used in some definite models of UPS (uninterruptible power supply). These UPS are specially designed to operate with computers or LCDs.
  • Used to connect some old models of printers or digital cameras with your computer.
  • The early models of telescopes also had a port for serial port and used for serial data communication.
  • Serial port is also used to operate serial type mouse.
Alright friends, that was all from today's post. I hope you have learned from today's post. If you have any problem then, feel free to ask. Till next tutorial Take Care!!! :)

Interfacing PIR sensor with Arduino

Hello friends, i hope you all are fine and enjoying. Today i am going to share a new project tutorial which is Interfacing PIR sensor with Arduino. First of all lets, have a little introduction about the basics and working of the PIR sensor. PIR sensors are in fact a Passive Infrared Sensor. PIR sensors are actually electronic sensors and they are used for motion detection. They are also used to detect the Infrared waves emitting from a particular object. You should also have a look at PIR Sensor Library for Proteus, using this library now you can easily simulate your PIR Sensor in Proteus software.

PIR sensors are widely used in motion detection projects now a days. Since my today's tutorial is about interfacing of PIR sensor with Arduino micro controller. Before going to that it is necessary that we should first understand the construction and working principle of PIR sensor. So i am going to divide my tutorial into different blocks and i will describe PIR sensor from its construction to practical applications. So first of all, lets see the construction and operating principle of PIR sensor.

Construction of PIR sensor

The construction of PIR sensor is very simple and easy to understand. In the bottom or you can say in the core of the PIR sensor we have a set of sensors which are made of pyro-electric materials. The properties of this material is that when this material is exposed to heat then, it generates energy. Different methods are used to to measure this energy generated by PIR sensor. On the top of the PIR sensor and above the internal sensors we have a small screen through which infrared radiations enters into sensor. When these radiations falls on the pyro-electric material then it generates energy. Generally the size of this sensor is very small and it is available in the form of thin film in market. Many other materials are also used in collaboration with pyro-electric material like galliam nitrite and cesium nitrate and all these materials take the form of an small integrated circuit.

Operating Principle of PIR sensor

The modern studies in the field of quantum physics tells us the fact each and every object when it is placed at a temperature above absolute zero, emits some energy in the form of heat and this heat energy is in fact the form of infrared radiations.  So an other question comes into mind that why our eyes can't see these waves? It is because that these waves have infrared wavelengths and his wavelength is invisible to human eyes. if you want to detect these waves then, you have to design a proper electronic circuit.

If you see closely the name of PIR sensor which is Passive Infrared Sensor. Passive elements are those elements that don't generate their own voltages or energy. They just only measures things. So we can say that this sensor is a passive infrared sensor and it doesn't generate anything by itself. It is only capable to measure the rediations emitted by other objects around it. It measures those raditions and do some desired calculations on them.

Interfacing of PIR Sensor with Arduino

PIR sensor have total 3 pins. The configuration of each pin is shown in the image given below:
  1. Pin#1 is of supply pin and it is used to connect +5 DC voltages.
  2. Pin#2 is of output pin and this pin is used to collect the output signal which is collected by PIR sensor.
  3. Pin#3 is marked as GND pin. This pin is used to provide ground to internal circuit of PIR sensor.
This whole configuration is also shown in the image given below:

The pin configuration of a PIR sensor is shown in the image given above. Since we have to interface the PIR sensor with Arduino micro controller. The image showing the interfacing of PIR sensor with Arduino is shown below as:

Interfacing Code

The code for interfacing Arduino micro controller with PIR sensor is given below as:
    #define pirPin 2

    int calibrationTime = 30;
    long unsigned int lowIn;
    long unsigned int pause = 5000;
    boolean lockLow = true;
    boolean takeLowTime;
    int PIRValue = 0;

    void setup()
    {
    Serial.begin(9600);
    pinMode(pirPin, INPUT);
    }

    void loop()
    {
    PIRSensor();
    }

    void PIRSensor()
    {
    if(digitalRead(pirPin) == HIGH)
    {
    if(lockLow)
    {
    PIRValue = 1;
    lockLow = false;
    Serial.println("Motion detected.");
    delay(50);
    }
    takeLowTime = true;
    }

    if(digitalRead(pirPin) == LOW)
    {

    if(takeLowTime){lowIn = millis();takeLowTime = false;}
    if(!lockLow && millis() - lowIn > pause)
    {
    PIRValue = 0;
    lockLow = true;
    Serial.println("Motion ended.");
    delay(50);
    }
    }
    }

Applications of PIR sensor

PIR sensors possess a no of applications and due to their low cost and much advanced features they are the main focus of different projects being made now a days. Some of their features and practical applications are listed below as:

  • They are able to sense the detection of people and other objects.
  • PIR sensors are also used in automatic lightening systems. In these type of systems, when a person comes in the vicinity of the sensor then, the lights are automatically turned ON.
  • They are used in outdoor lightening systems and also in some lift lobbies. You may have observed that when a person comes in front of the lift and if the doors are being closed then, the doors are opened again. This is all due to PIR sensors.
  • They are widely used in underground car parking system. At every parking position a PIR sensor is installed and when that position is vacant then, a green light glows over that place which means you can park here. And if that position has been occupied then, a red light will glow, representing that this position is already occupied.
  • PIR sensor is much compatible sensor and it has the ability to detect a particular motion and the output of this system is very sensitive and doesn't have any kind of noise in it.
Alright friends, that was all from today's post. If you have any questions, fell free to ask. Till next tutorial Take Care!!! :)  
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir