Microcontrollers are small computers integrated in a single chip. They consist of memory (RAM and ROM), peripherals and a processor (CPU), as opposed to microprocessors which contain only a CPU.
Excellent for embedded applications
Low cost and low power consumption (ATMEGA328P ~ $1.5)
Can work up to tens of MHz as clock frequency
Standalone devices; most of them only need power and a clock source to run
Any gadget or equipment that does things like measuring, controlling, displaying and calculating values usually contains a microcontroller. They are present in:
Almost all contemporary home appliances
Data acquisition or monitoring systems
Toys
Modern cars
Traffic lights
Smartphones
Office instruments
Satellites & space applications
You may have heard of some of the companies that make microcontrollers:
Microchip
Atmel (now part of Microchip)
Intel
Analog Devices
Parts of a Microcontroller
CPU: The most important part of a microcontroller is a central processing unit with a word length ranging from 4-bit to 64-bit. The central processing unit fetches, decodes and executes the instructions. It coordinates various activities taking place in the microcontroller.
I/O ports: There are several parallel input/output ports in a microcontroller. They are used to interface various peripherals such as printers, external memories, LEDs and LCDs to the microcontroller. Apart from parallel ports, there are serial ports to interface serially connected peripherals with the microcontroller.
Memory: Microcontrollers usually contain a certain amount of flash memory to store program source code as well as ROM (EEPROM) for permanent storage and RAM for temporary storage.
Timers and counters: Timers and counters are used in operations which include modulation, clock functions, frequency generation and measuring and pulse generation.
Analog to digital converters (ADCs): Such converters are useful while converting the output of a sensor which would be in analog form.
Digital to analog converter (DAC): The working of a DAC is just the reverse of an analog to digital converter. As it is obvious, the output will be an analog signal which can be used to control the analog peripherals such a motor.
Power and Logic
Digital vs. Analog: Most microcontrollers (including Arduino) can input and output analog signals as well as digital signals.
An analog signal is one that can take on any number of values, unlike a digital signal which has only two values: HIGH and LOW. To measure the value of analog signals, the Arduino has a built-in analog-to-digital converter (ADC). The ADC turns the analog voltage into a digital value. The function that you use to obtain the value of an analog signal is analogRead(pin). This function converts the value of the voltage on an analog input pin and returns a digital value from 0 to 1023, relative to the reference value. The default reference voltage is 5 V (for 5 V Arduino boards) or 3.3 V (for 3.3 V Arduino boards). It has one parameter which is the pin number.
Pulse Width Modulation (or PWM) is a technique for controlling power. We can use it to control the speed of a motor or the brightness of LEDs. The diagram below shows the signal from one of the PWM pins on the Arduino.
Roughly every 1/500 of a second, the PWM output will produce a pulse. The length of this pulse is controlled by the 'analogWrite' function. So 'analogWrite(0)' will not produce any pulse at all and 'analogWrite(255)' will produce a pulse that lasts all the way until the next pulse is due, so that the output is actually on all the time.
If we specify a value in the analogWrite that is somewhere in between 0 and 255 then we will produce a pulse. If the output pulse is only high for 5% of the time then whatever we are driving will only receive 5% of full power.
If however the output is at 5V for 90% of the time then the load will get 90% of the power delivered to it. We cannot see the LEDs turning on and off at that speed, so to us, it just looks like the brightness is changing
Memory
Flash memory:
Remembers data after power is turned off.
Slow read/write speeds compared to RAM/ROM
Can be manufactured in higher storage capacity than RAM/ROM
Used for SSDs, USB drives
RAM (Random Access Memory)
Loses data when powered off.
Fast read/write speeds
Relatively expensive to manufacture
Used for storing program memory while computer is running
ROM (Read Only Memory)
Does not lose data when powered off
Hard to write to
Speeds vary from very fast to flash memory speed
Usually used for storing the software to boot a computer/device nowadays.