Learn IoT from scratch #3 - What are microcontrollers and how they work?
Tags: iot, tutorial, beginners, computerscience
Published: 2021-08-17 12:03:24

## Introduction

This is the #3 post of the IoT series that I am writing, in this article I will talk about microcontrollers, explain what they are, how they work and give examples of some microcontrollers.

&nbsp;

## What are microcontrollers?

Microcontrollers are small, low-cost, and self-contained computers-on-a-chip that can be used as an embedded systems, they are used mainly in electronic devices, industrial and home automation, smart devices and embedded systems. Microcontrollers usually must have low-power requirements since many devices they control are battery-operated.

&nbsp;

## Structure and components

Microcontrollers generally follow the same pattern of components and structures, they are:
1. CPU(*Control Processing Unit*) - The brain of the microcontroller, the CPU is responsible to "demand tasks" like fetch, decode, and execute (Harvard architecure).
2. Memory - It is used to store data and program. A microcontroller usually has a division between RAM and ROM.
3. I/O ports(*digital*) - These ports are used as interfaces for different appliances like LEDs, LCDs, GPS sensors, etc.
4. Serial ports(*analog*) - These are analog ports, they are used to connect non-digital external components.
5. Timers and counters - These are used to manage all the operations of timing and counting in a microcontroller. Its operation is very similar to the clock on personal computers.
6. ADC (*Analog to Digital Converter*) - The main function of ADC is to change the signals from analog to digital.
7. DAC (*Digital to Analog Converter*) - The main function of DAC is to change the signals from digital to analog.
8. Interrupt control - The interrupt control used for providing interruption behavior for a working program. This interruption can be either delay or break.

![Microcontroller structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s565qh68oz21i9d1vf8s.jpg)

&nbsp;

## What are the differences between microcontrollers and microprocessors?

![Comparison between micros](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwehf6qefpr2whxn6l7i.png)

Arduino is an example of a microcontroller, as a Raspberry Pi is an example of a microprocessor, both of them can be used for IoT, data collection and automation, but they have differences. 
People sometimes confuse microcontrollers with microprocessors, but there are many differences between these micros, so I listed only the key differences and organized them on a table.

Microprocessors | Microcontrollers
--- | --- | 
Can perform the most of the daily tasks that a decent computer is capable of doing  | Your range is more limited to electronics and sensors, or small systems
Based on Von Neumann model | Based on Harvard architecture
Large number of instructions to process | Fewer instructions to process
Offers built-in monitor/debugger program with interrupt capability | No built-in monitors and requires a manual interruption
Consumes too much energy | Perfect for compact systems with low energy consumption
Many microprocessors are clocking speeds of up to 4 GHz (faster) | Microcontrollers can operate with speeds of 200 MHz or less (slower)

&nbsp;

## How micros works?

A microcontroller processes data given to it’s input pins using it’s CPU and gives output via output pins. It executes or driven by a synchronous sequential logic circuit. The programs are stored temporarily at RAM memory, and executed by the CPU, that involves the external and internal components, based on what is specified in the program, this program that usually is inputted through a cable, that transfers the source code from your PC to the micro. 

&nbsp;