Wednesday, 18 April 2018

Getting started with Arduino coding. (A beginner’s quick guide)



Welcome to my first Arduino coding guide. This would take you to kickstart with Arduino boards. I would go through following things really quickly and thoroughly. You would enjoy this post. Arduino boards are really famous for hobby and rapid prototyping, types of applications. Kids really love Arduino boards. If you are an engineering student. I would request you to please stay away from Arduino boards if you really want to learn how to build your own custom hardware boards. It might be good for the project but you may lose a lot of essential skills needed to design reliable hardware. You get lifelong skills when you get a problem and you solve it all the way on your own. You never forget that problem and learning.

 1)      Arduino setup (software and hardware)
 2)      Coding example

Arduino setup:

To program an Arduino board you need to install Arduino software. Here is the link to download Arduino software https://www.arduino.cc/en/Main/Software

Please, select Windows if you are using windows 10 operating system as marked in figure 1 below.

Figure 1 Download page.

Please click on just download as marked in figure 2 below. I would encourage you to donate them some amount because they are contributing towards a better future for your children and for young engineers. Your contribution would enhance the development process and I believe that your good deeds would come to you one day for sure. 

Figure 2. download the setup file

Please, wait for the Arduino setup to download. grab some popcorns with you :) 

Figure 3 waiting for a program to download

Once it is downloaded click to install it. use next→ next and you will reach to this point. you are almost done just wait for a couple of minutes for this installation as shown in figure 4.

Figure 4 waiting for the installation to complete

Wow at this point you are down with installation just double-click on Arduino you can find this icon on your desktop as shown in figure 5 below.

Figure 5. Wow its done Just double-click on this
Now, you would come to a window as shown in figure 6 below. we are done with the software setup.

Figure 6. Software installation is done.

Now, At this time I have an Arduino Mega 2560. This board comes with USB type B cable. Connect your board with USB cable connector on board and to computer's USB port. USB connector is marked in figure 7 below
Figure 7 USB connection to this board.

Now, you have to select the communication port for Arduino board. Go to Tools→Ports→(Select Mega2560) as shown in figure 8 below.

Figure 8 Communication port selection in Arduino (COM6).

Now, You have to select which board you are using. Go to Tools→Boards→(Select Mega2560) as shown in figure 9 below.

Figure 9. Arduino board selection.

At this point, you are done with Arduino setup both hardware and software. Now, you are ready to program your first "Hello world" code. By means, Hello World is the very basic code that we can program to test hardware.

Coding Example:

The basic thing about Arduino is there are two functions as you can see in figure 10 below. The first setup function is called at the beginning when you turn on the hardware. and the loop function would be called every time after that. So the tasks which you want to do once at the startup of a board like initializations of Input and Output ports should be done in setup() function.

Figure 10 Setup function
There is a default LED on almost every Arduino board and it is connected to PIN 13. For setup pin 13 to drive that LED we need to setup this pin in output mode. so that we can write onto it. As shown in figure 11 below. and then press Ctrl + S to save your code. I saved it as HelloWorld.

Figure 11 Pin mode as Output
At this point you are done with pin setup Now you have to write a value to this pin. High means this pin would give 5 volts output and LOW means this would give 0 volts to pin 13. Now you should also know that microcontrollers work at high speed. So you have to add delay within one high and one low to see that PIN is really blinking or not otherwise it would blink fast enough that you can never be able to see it. Please see the code in figure 12 below. I coded delay for 500millisecond. You would get one ON and one OFF in a single second on LED or PIN13. Save it.

Figure 12 LED blinking code.
Now you are done with HelloWorld test code. Just program your Arduino board and see LED will blink. Click on the upload button as shown in figure 13 below.

Figure 12 Upload the code to Arduino board.
Once your code is uploaded you will see the message in the green area at the bottom "Done uploading". Now, check your board it will look like the figure 13 below. Please remember this is not realtime simulation.


Figure 13 LED blinking 

Thank you for reading this post. Just try it and you would love to do programing.



If you learned something interesting please contribute to the future posts. I would appreciate your contribution to opensource free information.




All the text and graphics contained on this page belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 

© Copyright 2018 IbrahimLabs. All rights reserved.









Friday, 29 August 2014

Defining code regions in MPLAB X IDE


     In this post I would like to share "How to define code in regions using MPLAB X". This post has very much importance in good programming practice that's why I want to share it with you guys. I personally like this feature in MPLAB X remember this feature is only available in MPLAB X. With this feature you can define code in regions likewise you may have done in Microsoft Visual Studio. When you define certain code in regions you may easily handle the code you are interesting in. For example if you define certain interrupt setting registers code in one region and frequency setting code in other regions.


    Following figure 1 shows how an organized code looks like. Which I do in coding as general practice. In this code I organized all Debug LEDs, Spare Pins, Input buttons etc in separate regions. So whenever I want to change certain regions I quickly click on "+"  sign and made changes without looking at other regions code.
Figure 1. Organized code in regions.


     In the following figure 2. I unfold the comment and I have all the #defines for Input Buttons Pins. So this would help you to quickly identify certain regions as quickly as possible. you have seen that the <editor ... > statement.

Figure 2. unfolding


How to write Folding Comments:

     To write code in regions you need to write following statement at the top of code which you want to place it in region. Your statement which would appear above folded code write it in comment(shown in bold).

// <editor-fold defaultstate="collapsed" desc="comment">

and at the end of regional code you need to have following statement.

// </editor-fold>

your code should be in between these statements which you want to collapse.

     Now you are thinking that this method is little awful so don't be upset there is a shortcut direct method of writing these statements. Now simply type fcom and then press tab key you will quickly get these statements given in figure 3 below.
   
     To remember this I decoded this instruction as fcom = f (folding) com (Comment). So when ever I want to put code in regions I use folding comments.

Figure 3. Folding Comment using fcom

     Hope that this would helped you to get rid of awful codes and you would start writing awesome codes like I am doing or even better than me. Remember these statements only working in MPLAB X IDE. For more details on Folding code Click here.

     If you learned something interesting please consider donating some amount so that I continue producing such interesting free content for young engineers thank you.





All the text and graphics contained on this page belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 

© Copyright 2014 IbrahimLabs. All rights reserved.

Saturday, 16 August 2014

Graphical LCD interfacing with PIC16F877 Code + Schematic + Proteus Simulation

   
     This tutorial is all about "How to interface Graphical LCD with PIC16F877". You may download code, schematic and Proteus simulation from the download section at the bottom of this page. 

     From this point I assumed that you know following (if not then read them before proceeding any further in this post).

  1. How to use Mplab X IDE (Click Here).
  2. How to set Configuration bits in code (Click Here).
  3. How to blink an LED (Click Here).
Proteus Simulation:

     In the following figure 1. We have proteus simulation in which I am using graphical LCD 128x64 with PIC16F877. This LCD is similar to JHD12864E (datasheet) with only addition of two pins those for LCD backlight. I have tested this code on real graphical LCD it is working.

Figure 1. Proteus Simulation.

    In the following figure 2. PortD is connected to DATA and ADDRESS bus of graphical LCD. whereas PortC higher bits are connected to LCD control signals. As you have seen the Reset (RST) pin is active low.A variable resister is for LCD contrast. MCLR pin is pulled high for micro-controller normal operation. I am using 10MHz crystal. You may see PIC16F877 datasheet for more details

Figure 2. Schematic Graphical LCD
General Details:

     As I mentioned before this graphical LCD is similar to JHD12864 (Controller K0108 type) so I would like to go briefly into details rest of it you may read from its datasheet. This LCD is split logically into two half left and right with two control pins ChipSelect1(CS1) and ChipSelect2 (CS2) respectively. CS1 is responsible for displaying in left half and CS2 is responsible for displaying in right half. further more each chip has 8 vertical sections named as pages (page 0~7). Every single page is further divided into 64 horizontal sections named columns and each column has 8 bits. When you stack them horizontally they are 64 bytes as shown in figure 3. So if you want to display one picture having resolution of 128x64 on this LCD you have total 1024 bytes. For example (pages) times (columns) times (no. of chips) => 8*64*2 = 1024.
Figure 3. Graphical LCD explained.
     Now we should move ahead for "How to generate display Screens for 128x64 Graphical LCD". To generate code for one picture we need to have a picture of size 128x64. So Paint it in Microsoft paint. The first step is to set the size of picture go into file>Properties a dialog box will open select colour "black and white", width "128" and height "64". as shown in figure 4.

Figure 4. Pixel settings
      Now you can zoom this page and draw any picture like I have drawn following figure 5. The important step is to save this picture as "monochrome bitmap (.bmp)" format. 

Figure 5. monochrome (.bmp) picture.
     Now we have to translate this picture into its equivalent binary data or constant array of bytes so here is a software for translating this into its binaries called LCD Assistant by Radoslaw (All rights reserved for Radoslaw). You may download it (Click Here).  as shown in figure 6. To export code you need to go file>export this would generate a file open it in Notepad and copy the code and paste it in Mplab X and compile.

Figure 6. LCD Assistant.

Code:

     In the following figure 7 we have a main function in which we have InitializeGraphicalLCD() which is responsible for initializing graphical LCD according to LCD datasheet. Then we have a fill screen function this will fill full screen with all pixels ON and then a one second delay after that all screen is cleared after this I have displayed a complete picture on LCD that I have generated as shown above in this post.

Figure 7. main function.
     Following figure 8 shows the header file in which I have #defined all the page addresses, indexes, necessary commands and Pins used to interface graphical LCD. The purpose of this defines are just to write a code that looks good, managed and readable. which is good code practice I recommend you to follow these good programming practices too.

Figure 8. Graphical LCD header file.
     In the following figure 9 we have InitializeGraphicalLCD() In which I configure all LCD pins as output. Then send some necessary commands to LCD which is described in its datasheet. As you have seen that I have to write these commands to LCDs both Chips so I send BothChip as parameter to function that is responsible for writing command to LCD.

Figure 9. Initialize Graphical LCD.
     In the following figure 10 we have ChipSelection() function which is responsible for selecting the chip either 1 , 2 or both. To select chip we need to pass chip no. as parameter.

Figure 10. ChipSelection.
     In the following figure 11 we have ToggleEnablePin() function which is responsible for toggling enable pin. After every data or address send to LCD we have to toggle its Enable pin state as described in LCD datasheet.

Figure 11. Toggle Enable Pin.
     In the following figure 12 we have WriteCommandToLCD() function which is responsible for writing commands to LCD by passing command and chip no.

Figure 12. Write command to LCD.
     In the following figure 13 we have WriteDataToLCD() which is responsible for writing data to LCD by passing data and chip no. as parameter to this function.

Figure 13. write data to LCD. 
     In the following figure 14 we have WriteAddressToLCD() which is responsible for sending address value on which data is written on LCD by passing addressX and addressY valve as well as chip no. to this function as parameter.

Figure 14. write address to LCD.
     In the following figure 15 we have DisplayPicture() which is responsible for displaying complete picture on LCD by passing a address of first byte of data as you can see there is a pointer for memory location. This function writes 1024 bytes of data to LCD. 

Figure 15. Display Picture on LCD.

Downloads:

 You may download Code which is written in C language. Using MPLAB X IDE and compiler XC8 for 8 bit Microcontrollers. Click here


If you learned something interesting please consider donation. Which would help me to produce such content for young engineers Thank you.




Reading Suggestions:
 For all topics Click here


All the text and graphics contained on this page belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 

© Copyright 2014 IbrahimLabs. All rights reserved.

Sunday, 10 August 2014

LCD interfacing with PIC16F877 Code and Simulation



          In this tutorial you will find Proteus simulation and Code of  "How to interface 16x2 LCD with PIC16F877 using MPLAB X IDE and XC8 compiler". You may download code, schematic and Proteus simulation from download section at the bottom of this page.

In this post I assumed that you must know following if not then please read them before proceeding any further in this post:

    1. How to use MPLAB X IDE (Click Here).
    2. How to set Configuration bits in code (Click Here).
    3. How to Blink an LED (Click Here).

Proteus Simulation:

In figure 1. I have used PIC16F877 with PortB connected to an LCD as data and address bus whereas Register Select (RS) and Enable (E) pins are connected to Pins RC0 and RC1 respectively. Whereas MCLR is connected to 5VDC which means Controller is in Normal Operating mode (see datasheet for further details). I have set Crystal frequency 10MHz.

Figure 1: Proteus Simulation.

Code:

In the following figure 2. We have a main function in which we initialize LCD and a while loop to display strings on LCD and update LCD after every 1 seconds. approx. Note that first string is displayed in first line by moving cursor to first line address and second string is displayed on second line by moving cursor to second line address.

Figure 2: main C file.

In the following figure 3 we have LCD.h file in which we have all the #define like LCD data bus, LCD enable and register select pins. First and second line addresses.

Figure 3. LCD.h


In Initialize_LCD() I made Enable and RS(Register Select) pins and portB  as output by setting their Tristate registers. And then I write some commands to LCD which are necessary for initialization as describes in LCD datasheet.

Figure 4. Initialize LCD.
In figure 5. we have Toggle_Enable_Pin_LCD() which is used to toggle Enable pin after every command or data send to LCD according to datasheet. In Initialize_LCD() I made Enable and RS(Register Select) pins and portB  as output by setting their Tristate registers. And then I write some commands to LCD which are necessary for initialization as describes in datasheet.

Figure 5. Toggle Enable pin.


This function is used to write commands to LCD In which we have RS low.
Figure 6. Write command to LCD.
This function is used to write Data to LCD In which we have RS High. 

Figure 7. Write Data to LCD.

Downloads:

                You may download Code which is written in C language. Using MPLAB X IDE and compiler xc8 for 8 bit Microcontrollers. Click here

If you learned something interesting please consider donation. Which would help me to produce such content for young engineers Thank you.




Reading Suggestions:
 For all topics Click here



All the text and graphics contained on this page belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 

© Copyright 2014 IbrahimLabs. All rights reserved.

Getting started with Arduino coding. (A beginner’s quick guide)

Welcome to my first Arduino coding guide. This would take you to kickstart with Arduino boards. I would go through following things real...