Pages Menu
Categories Menu

Posted by on Jun 6, 2011 in Getting Started, Microcontrollers | 56 comments

Basics of Microcontrollers

Basics of Microcontrollers

Microcontroller ConceptsHello everyone! In my previous post, we analyzed the difference between a microcontroller (MCU) and a microprocessor (MPU) and some of the applications of microcontrollers. We concluded that our life would suck without microcontrollers!

So, interested in controlling these microcontrollers? And make some really cool stuff out of it? Well, for that you should be familiar with their basics and how a microcontroller development process takes place.

Elementary Microcontroller Concepts

Before we start off, it’s essential for you to know some elementary concepts related to MCU. To make it quick, let’s codify them as follows:

  • A MCU is an Integrated Circuit (IC). The number of pins, size, structure and architecture may vary depending upon the manufacturer and model.
  • A combination of pins in a MCU is known as a port. The configuration of ports may vary from manufacturer to manufacturer.
  • Every MCU has a flash memory. It is equivalent to the hard drive of a PC.
  • Data can be erased and rewritten as many times as you want.
  • Every MCU has a lifespan of around 1000-1500 read/write cycles. After that it becomes dead and stops working. In fact every IC has a lifetime in terms of read/write cycles. For example, pendrives usually have 10000 read/write cycles. Taking an average pendrive, it should stop working after 10-12 years.

Now that you are familiar with some basic concepts, let’s design a simple MCU based application.

Simple MCU Application – Hello World!

The simplest possible MCU based solution could be blinking an LED continuously at a specified time interval. Okay, let’s design a solution for it. Don’t worry; we will be designing a theoretical solution for it. A practical design will be made later (not in this post).

Simple MCU Process

Simplest MCU Process (Image courtesy

Problem Statement: Design a microcontroller based solution to blink an LED every 250ms.


  • Let us assume our supply voltage to be 5 volts i.e. Vcc = 5V
  • Connect the LED to a pin (not port, mind it! View next section for more details on ports and pins of a microcontroller) of the MCU as shown.
  • The resistor is provided to prevent the LED from blowing off.
  • Now, when we make it voltage of the corresponding pin to zero (0 volts) (LOW), the LED glows due to the generated potential difference.
  • When we make the voltage of the pin to Vcc (5 volts) (HIGH), the LED switches off due to insufficient potential difference.
  • The timing can be easily altered by introducing appropriate delays.
  • The following pseudo code includes this feature. Yes, let’s code it! It’s not tough!
void main()
     InitializePort();          //Initialization
     while(1)                   //Run infinitely
           PORTB = 0b00000001;  //HIGH
           Wait(0.25);          //Wait 250ms
           PORTB = 0b00000000;  //LOW
           Wait(0.25);          //Wait 250ms

Code explained:

  • Starting with the best part, it’s written in C :)
  • InitializePort() – this function call initializes the values of the pins of the port
  • while(1) – ‘1’ implies TRUE condition. While(condition) continues iterating until a false condition is met. Since the condition is always true, the loop is an infinite loop. Now, in C, an infinite loop is treated as a runtime error, but not in this case. This is because we want the LED to blink continuously as long as the circuit is powered. We don’t want it to stop blinking after blinking for some particular number of times.
  • PORTB = 0b00000001 – don’t worry about this line much! We will discuss about this syntax later on. All you need to notice is the ‘1’ in the end. Here, ‘1’ implies HIGH i.e. 5 volts, which will switch off the LED.
  • Wait(0.25) – again here, don’t worry about the syntax. Just know that it introduces a delay of 250ms.
  • PORTB = 0b00000000 – notice that the ‘1’ is changed to ‘0’ here, which means LOW i.e. 0 volts, which turns on the LED.
  • Wait(0.25) – once again a 250ms delay is introduced.

Congratulations! You have successfully designed an embedded solution! :)

Please note that I have assumed that you have a prior knowledge of C programming. If not, then go to the market and buy “Let Us C” by Yeshwant P Kanetkar and start reading it.

Ports and Pins of a MCU

  • As I already said, port contains the pins of a MCU. In the adjoining figure, PORT A of an AVR microcontroller is shown. Have a close look at it and you will come to know that it consistes of 8 digital GPIO pins.

    Port vs Pin

    Port comprises of several pins

  • In a MCU, most of the pins are digital GPIO pins (General Purpose Input Output pins). These digital pins can be turned on or off (or can be turned HIGH or LOW) as per the requirement.
  • Not all pins are GPIO pins, there are some pins (like Vcc, GND, XTAL, etc) which are for other functions and cannot be turned on/off for interfacing.
  • GPIO have two modes:
    • Output mode: It’s quite simple. Setting it HIGH (1) gives an output of Vcc at that pin. Setting it LOW (0) gives an output of zero at that pin.
    • Input mode: In this mode, the MCU can read the values at the pins. Here, a threshold is defined. Threshold voltage is usually half of Vcc. If a voltage above the threshold is read, it reads it as HIGH (1) or else LOW (0). For example, suppose our supply voltage Vcc = 5V. Hence, threshold = 2.5V. When we apply a voltage 5V, it reads as HIGH (as 5 > 2.5). When we apply a voltage of 0V, it reads as LOW (as 0 < 2.5). Suppose 2V is applied. In this case, it will consider it as LOW (as 2 < 2.5).
    • The input/output mode of GPIO pins is set by DDR register (which we will discuss later).

Now that you are familiar with the basics, let’s have a look at the MCU based Development Process.

MCU Based Development Process

A MCU based development process consists of two simple steps:

  • Step 1: Write a desired code for the given problem statement in a PC/laptop in an IDE, edit, compile and debug it. Say if you code in C, your source file will be something like mycode.c, which will be converted into a hex file mycode.hex.
MCU based Development Process Step 1

MCU based Development Process Step 1

  • Step 2: Open up the programmer software, locate hex file and click on “Program”. The software first reads the flash of the MCU to check the amount of data, then an erase cycle will be performed, and then your hex file will be burnt into the flash! But the process doesn’t end here… the file is verified so that you can be sure that your code has been transferred successfully! All this process happens with just one click!
MCU based Development Process Step 2

MCU based Development Process Step 2

Don’t worry regarding the hardware shown in the diagram. We will come across them in my next post. Just concentrate on the process.

Why use hex file?

We know that processors understand only machine/binary language. The .hex file generated by the compiler is the hexadecimal representation of this very machine language! The cross-compiler used converts the high level program (written in C) to the machine-readable hex/binary format. We will discuss more about the build process later on.

Manufacturers, at the time of manufacturing a MCU, have no idea whether that MCU will be used to blink LEDs, used to control satellites or used as target detection mechanisms in missiles. A hex code may seem useless to blink LEDs, but it’s quite valuable for the remaining two applications.

So this is it folks! In the next post, we will see the things that you require (both hardware and software) to get started! Till then, enjoy! ;)

And don’t forget to drop in your comments!

Published on June 6, 2011.
Last updated on December 20, 2014.
Last reviewed on December 20, 2014.


  1. Really nice post!! Keep up the superb work mate!! :-) :D

  2. gr8 job….thnx

  3. good post………keep it up………

  4. Really nice post mate …
    Ready to enjoy your next post. :D

  5. wow, really nice post…
    it is very useful for beginners .

  6. Nice 1 , I am getting to understand things , thanks.

  7. you have really helped me a more than lot.

  8. really really awesome 2 have dat much info abt all dix stuff. thanx a lot 4 sharing such useful information.. i m damn sure itx gonna help me much more in ma rc car project.. :)

  9. Thanks for great tutorials!

    The end is bit puzzling: “But did you give it a thought why did we burn the hex file and not the C file? Well, it’s a natural fact that hex files execute much faster than c files.”

    Does the c file even execute? Processors & microcontrollers run machine code, which is compiled from c code. To them, c file is just a bunch of text but the machine code in hex file contains instructions they understand. Assembly language is a way to directly write machine code without c.

    • And that is another reason for not using C! You are correct! I wrote it in that manner so that a novice can understand it (they usually don’t have any idea as to what a hex file does), though it doesn’t depict the concept properly. I will try to modify that portion.

      Btw, assembly code is also different from hex. Whether you program it in assembly (.asm) or C, a hex file is generated for sure. As you said, hex file contains instructions which can be directly understood by the CPU, whereas assembly contains mnemonics and has a proper syntax so that it becomes easier for the user/programmer to program efficiently.

      • If you are going to re-write the last chapter, I would consider removing the speed discussions which are quite misleading since the speed difference is mostly a question of is “man made assembly -> machine code” faster than “man made c -> compiled to machine code”. Simply put that uC’s eat hex files and you get them by compiling c files.

        Best thing about your tutorials are that they are short and straight to the point with good illustrations, it feels like watching someone do and explain it! Keep up the good work.

  10. can the codes in AVR studio 5 be written in any of the language?? C++ ,java or any other??

    • For embedded programming, we mostly use assembly language, C and python.
      However, if you are familiar with C++ or Java, then it shouldn’t be a problem for you to program using C, since C++ and Java are based upon C.

  11. Wait(0.25); //Wait 250ms

    for this syntax which header file should included??
    Actually I don’t know programming even a little in this context means microcontroller programming and trying to learn from here …..

    • Hi Gauri
      The code that I wrote above is just a pseudo-code, which means that it merely describes the algorithm. For the exact code, view this post. You can find the code towards the end.

  12. nice!!

  13. Nice job, thx

  14. Dear Mayank,
    It’s nice to read you.
    > C, BASIC are high level programming & Hex is low level programming.
    So, mcu can understand hex code directly whereas one more software require in mcu if we burn C or BASIC code in mcu!
    Am I ? Or something else.

    • Hi quickanswerer,
      Yes you are right, but partially. The mcu can execute the hex file directly. The mcu cannot execute file of any other type. And you cannot install a software in a mcu. If you are writing your code in C, then you need a software in your PC to compile it and convert it into hex code. This hex code is then burned into the mcu.

  15. very good explanation. Thanks


  1. What do we need to get started? « maxEmbedded - [...] and welcome! In my previous post Basics of Microcontrollers, we came across some of the elementary concepts and how…
  2. AVR Basics « maxEmbedded - [...] Basics of microcontroller [...]
  3. Using AVR Studio 5 « maxEmbedded - [...] in the AVR Studio Code Window. Let’s write the code for the problem statement discussed in this post. We…
  4. Using Atmel Studio 6 « maxEmbedded - [...] in the AVR Studio Code Window. Let’s write the code for the problem statement discussed in this post. We…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: