First off, what exactly is a microcontroller? Microcontrollers are often compared to computers due to the similarity of the base components. However, a microcontroller does not exactly work like a computer. A big difference between the two, computers usually run multiple programs or scripts simultaneously where a microcontroller only runs one script that has been coded onto it. In the datasheet of the ATMega328P you will find this block diagram.
This diagram shows the most important components. For example, the CPU. The CPU handles all the processing. It works on a certain clock speed determined by the oscillator. In case of the ATMega328P the oscillator has an oscillating speed of 0-20MHz. (The Arduino Uno uses a 16MHz oscillator). Depending on the architecture used for processing on the microcontroller an instruction takes an x amount of clock cycles to process. It is important to know the clock speed since the microcontrollers only runs a single instruction at the time. This may result in latency whenever you try to measure something. Sometimes this latency is created on purpose to create delays. More on this in a future post about delays.
Like a computer a microcontroller has different kinds of memory. Some are read- and writeable, while other are read-only. The ATMega328P uses Flash, SRAM and EEPROM memory.
In a nutshell, Flash is like the system hard disk on your pc. It contains the program the microcontroller needs to run. So, while the program is running it will be read-only. However, when programming the microcontroller, you will be able to write onto the flash memory. The ATMega328p has 32KB of flash memory. On Arduino boards 0.5KB will be used for the bootloader.
SRAM (static random-access memory) is as the name implies like RAM memory on a computer. This memory is volatile. This means whenever your microcontroller loses power the SRAM memory will be erased. SRAM memory is used to store temporarily values like variables. These do change while the program is running and are used to interpret certain actions. When the power is lost the program will start over again and the old values will be gone. The ATMega328P has 2KB SRAM. If you are using data that doesn’t need to change while de script is running, you could also store this in Flash. For this use the PROGMEM keyword. Since flash memory only can be rewritten several times, 10.000 times for the ATMega328P, doing this will greatly reduce the lifespan of the microcontroller.
EEPROM is a non-volatile memory. This means whenever the power is lost, the data will remain unharmed. The EEPROM is 1KB for the ATMega328P. This is rather tiny. Using the EEPROM can have some significant pros, but cons as well. Whenever data needs to be stored you do not expect to change at all, or very rarely, the EEPROM could come in very handy to store this data. It is easier to use than Flash and often has more rewrite cycles. This means the microcontroller will last longer. Still EEPROM lasts a lot shorter than SRAM, 100.000 cycles for the ATMega328P, and will certainly cut down the lifespan of the microcontroller when used frequently.
To give a microcontroller a purpose it would be useful to be able to connect things to it. Things like sensors, actuators, buttons or displays. To do this, microcontrollers have IO (in- and output) pins. IO pins can be differentiated into two types. The digital and the analog pins.
The ATMega328P features an 8 channel 10 bits ADC (Analog to Digital Converter). What this does is converting an analog signal between 0 and 5 volts into integer values between 0 and 1023. These pins enable analog components to be used as sensors. For example, a photoresistor, or LDR (light-dependent resistor). Whenever these components are placed in a voltage divider this allows a voltage to be read by an analog pin.
Digital pins are much simpler. They either read HIGH or LOW. And the other way can either write HIGH or LOW. It is important to use a pull-down or pull-up resistor when trying to read a signal with these pins. When using a pull-up resistor, you will be reading a falling (LOW) voltage, when using a pull-down, you will be reading a rising (HIGH) voltage. With 5V and the ATMega328P a 10K resistor will do. Using such a resistor is important to eliminate electrical interference. This interference will cause a pin to float, meaning the pin randomly reads HIGH or LOW.
The ATMega328P does not feature a real DAC (Digital to Analog Converter). Therefore, it uses PWM to approach the same result. A PWM (Pulse Width Modulation) signal is a signal send with pulses. These pulses are always the same voltage. However, the pulse time (duty cycle) can be regulated. Logically, when the duty cycle is 100% the average voltage will be 5V. But if the duty cycle is 50%, the signal will only be 5V for half the time, resulting an average voltage of 2,5V. The frequency for this PWM signal is about 500Hz.
This is the absolute basic about microcontrollers. Yet, very important to understand if you are trying to work with these wonderful chips. There will be more related posts about certain subjects specifically later. For now, keep creating and most importantly have fun doing it.