Ch-1 Introduction to ATmega328P Microcontroller

1.1 What is an ATmega328P Microcontroller?
It is this 28-pin semiconductor chip of Fig-1.1. The full name is “Micro Controlling Unit”, the short name is “Microcontroller”, and the abbreviated name is “MCU”. The MCU can process only 8-bit data at a time.
Atmega328PMCUPic.png
(a) Pictorial view of ATmega328P MCU

Atmega328PMCUPin.png
(b) Pin/signal diagram of ATmega328P MCU

Figure-1.1: Pictorial view, pin diagram, and pin signals of ATmega328P microcontroller

1.2 Use of a Microcontroller
We can use a MCU to build the following instruments:
(1) Taxi Meter,
(2) Prepaid Electrical Energy Meter (PEM), ,and
(3) Digital Weighing Machine (DWM), etc.
TaxiMeterPic.png
(a) Taxi Meter

PEMPic.png
(b) Prepaid Electrical Energy Meter

DWMPic.png
(c) Digital Weighing Machine

Figure-1.2: Microcontroller based measuring instruments

1.3 Basic Tasks of a Microcontroller
(1) Takes command (+, _, *, and /) from the user via keyboard,
(2) Takes data from the user via keyboard,
(3) Modifies data in a way the user wants, and
(4) Delivers the result to the user via 7-segment/LCD display device.

1.4 Pin Diagram and Pin Signals of ATmega328P MCU
ATmega328P MCU has 28 pins (Fig-1.1) of which 20 are IO line (also known as port-line or port-pin). An IO (input and output) line is used to connect input device like switch and output device like LED.

A pin is associated with one or more signals. For example: Pin-5 is associated with four signals, and these are: PCINT19, OC2B, INT1, and PD3. The signal PD3 which is outside the parentheses is the default function of the pin. The other three signals (PCINT19, OC2B, and INT1) are the alternate signals, and they can be activated if needed through a process known as software initialization or software configuration. A pin has the following features:
(1) It has a serial number. For example: Pin-5
(2) It is associated with one or more signal. For example: Pin-5 is associated with signal PD3
(3) The signal has a meaning. For example: PD3 stands for ‘Bit-3 of PORTD as Output Line’.
(4) The pin/signal has a direction. For example: PD3 signal is an output line.
(5) The pin/signal has electrical characteristics. For example: PD3 can deliver 20 mA current at 4.20 V.

The words pin and signal will interchangeably refer to the same thing. Thus, saying that “Pin-6” works as IO line is equivalent to saying that “signal PD4” works as IO line. The pins are used to connect input devices (switches, temperature sensor, and etc.) and output devices (7-seg display unit, stepper motor, and etc.) with the MCU. A pin is also known as port-pin or port-line as it exchanges 1-bit data with IO (input and output) devices. In Ch-2, we will study the alternate functions of the IO lines.

1.5 Port Structured Diagram for the Pins of ATmega328P Microcontroller
(1) In Fig-1.1, we observe that the MCU has 20 IO lines spread over various pins with symbolic names: PB0 – PB5; PC0 – PC5; PD0 – PD7. In this Subsection, let us organize the pins so that pins with similar function stay together (Fig-1.3).
50lxy.png

Figure-1.3: Port structured diagram of ATmega328P MCU

(2) All the IO lines of the MCU have similar functional characteristics. Therefore, any discussion that will be done on a particular IO line (say, Bit-2 of Port-D) will apply to all other IO lines.

(3) Port-D: It will refer to a port when the directions of its IO lines are not yet determined as to be working as input or output lines. Thus, pd0 – pd7 will refer to bit-0 to bit-7 of Port-D Register.

(4) PORTD: It will refer to a port when the directions of the IO line are configured to work as output lines. Thus, PD0–PD7 (or PORTD0–PORTD7) will refer to bit-0 to bit-7 of PORTD Register.

(5) PIND: It will refer to a port when the directions of the IO lines are configured to work as input lines. Thus, PIND0 – PIND7 will refer to bit-0 to bit-7 of PIND Register.

(6) The direction of an IO line is set as output by executing the following command:

pinMode(DPin, OUTPUT); //DPin = digital Pin Connector: 0–7 (PD0–PD7), 8–13 (PB0-P5), 14/A0–19/A5 (PC0–PC5)

(7) The direction of an IO line is set as input by executing the following command:

pinMode(DPin, INPUT);

** **(8)** **
The direction of an IO line is set as input with internal pull-up resistor by executing this command:

pinMode(DPin, INPUT_PULLUP); //every input line has an internal pull-up resistor (Rip = 20k – 50k) Fig-1.3

(9) In Fig-1.3, LED1 is an output device. It is to be placed on the breadboard along with a series current limiting resistor R1. One side of LED1 would be connected with GND pin of UNO (Fig-1.4) with the help of a jumper wire. The other side of LED1 (actually, the other side of R1) will be connected with DPin-8 of UNO (Fig-1.5).

In Fig-1.3, it is observed that DPin-8 is internally connected with pin-14 of MCU which is Bit-0 of Port-B. Bit-0 has been assigned a “signal name” called PB0. Here, the LED1 is connected with an output line named DPin-8 or PB0; however, the name DPin-8 (simply 8) would be used in the sketch during programming.

(10) To turn ON LED1, we must send Logic High (LH: 3V – 5V) signal at DPin-8 by executing the following Arduino Code/command/instruction (Section-1.12, Table-1.1).

digitalWrite(8, HIGH);

(11) In Fig-1.3, K1 is an input device. One side of K1 is connected with 5V of UNO (Fig-1.5). The other side of K1 is connected with an input line of MCU which is named as DPin-A3 or DPin-17 or PINC3 or Bit-3 of Port-C.

When K1 is not pressed, the logic level of DPin-A3 is GND (0V) by virtue of pull-down resistor R2. DPin-A3 will assume LH signal when K1 is pressed; as a result, HIGH (1) will appear at Bit-3 of Port-C.

Careful observation reveals that there is an internal pull-up resistor Rip which can be connected with DPin-A3 by executing the following command. Let us note that both external pull-down and internal pull-up must not be connected with DPin-A3 at the same time. Also, not that every input line has its own Rip that could be connected or left unconnected.

pinMode(A3, INPUT_PULLUP);     //DPin-A3 is an input line with internal pull-up connected.
pinMode(A3, INPUT);    //DPin-A3 is an input line without internal pull-up.

(12) To check the closing condition of K1 of Fig-1.3, we may execute the following code lines:

pinMode(A3, INPUT);      //DPin-3 works as input line with external pull-down connected
bool n = digitalRead(A3); //if K1 is closed, then LH (5V) will be stored in variable n
if(n == HIGH)
{
   Serial.print("K1 is found at closed condition."); //message will appear on Serial Monitor
} 
elae
{
    Serial.print("K1 is found at opened condition."); //message will appear on Serial Monitor
}

(13) The operating frequency of the MCU of UNO Board can be set to either 8 MHz from an internal oscillator or to 16 MHz using an external crystal Y1. Currently, the MCU runs at 16 MHz clock frequency.

... to be continued in the next post.

Atmega328PMCUPic.png

Atmega328PMCUPin.png

TaxiMeterPic.png

PEMPic.png

DWMPic.png

UNOLearningSystem.png

UNOBoardPic.png

50lxy.png

Ch1FundOnline.pdf (851 KB)

1.6 Hardware Modules within ATmega328P MCU
In Fig-1.3, it has been shown that there are 3 digital IO ports inside the MCU. In fact, there are many more hardware modules inside the MCU. The names and functions of these modules are given below in Fig-1.4. Details could be covered in Ch-2 (Architecture).


Figure-1.4: Hardware modules inside ATmega328P MCU

(1) M1: Crystal Oscillator
(2) M2: Clock Prescaler
(3) M3: Internal Oscillator
(4) M4: Serial Communication Module
(5) M5: 8-bit and 16-Bit Counter
(6) M6: 8-bit and 16-Bit Timer
(7) M7: External Interrupt Structure Module
(8) M8: Analog-to-Digital Converter
(9) M9: Port-B
(10) M10: Port-C
(11) M11: Port-D
(12) M12: Watchdog Timer
(13) M13: 8-Bit AVR (Advanced Virtual RISC) CPU
(14) M14: Code Memory (Flash)
(15) M15: EEPROM Memory
(16) M16: Control Matrix (Sequence Generator)
(17) M17: Static RAM Memory
(18) M18: In System Programming Interface (ISP)
(19) M19: SPI Interface
(20) M20: Analog Comparator
(21) M21: TWI/I2C Interface
(22) M22: Pulse Width Modulator
(23) M14: Fuse Bits (24) Lock Bits
(24) M14: Lock Bits

1.7 Arduino UNO Learning Board Using ATmega328P Microcontroller
Fig-1.5 shows the pictorial view of the official version of Arduino UNO Learning Board. The UNO Board (simply UNO) offers friendly environment to learn architecture, programming, and interfacing of ATmega328P Microcontroller to build instruments like Temperature-Humidity Meter, Taxi Meter, Digital Weighing Machine, and Prepaid Electrical Energy Meter. The UNO also offers an excellent means for the understanding of the fundamentals of C/C++ Programming Language through the programming and operation of input/output devices and sensors.
UNOBoardPic.png

Figure-1.5: Arduino UNO Learning Board

1.7 Arduino UNO Learning System
(1) Arduino UNO Learning System (simply UNO System, Fig-1.6) is just a repackaged version of the UNO Board of Fig-1.5. The purpose of the UNO System is to provide the user with a handsome amount of hardware items so that he can comfortably practice the ATmega328P Microcontroller programming and interfacing. The UNO System contains the following items that are mounted on a plastic plate.
UNOLearningSystem.png

Figure-1.6: Pictorial view of Arduino UNO Learning System

(2) Parts Needed for Lab Works
PL1-4.png
PL5-6.png
PL7-12.png
PL13-21.png

Figure-1.7: Parts needed to do Lab Works

(3) The Arduino UNO Learning System comes with Edge Connectors, Breadboard, ‘Arduino Integrated Development Environment’ (IDE App., Fig-1.8) and Arduino Serial Monitor (SM, Fig-1.9) which offer the following facilities:
(1) IO devices can be connected with MCU pins using edge connectors of the UNO and the breadboard.
(2) An Interface (IDE of Fig-1.8) to create sketch/program/source codes for the solution of a problem using C++ Programming Language and Arduino’s commands/macros.

(3) Program can be compiled to create binary codes in Intel-Hex formatted file using IDE of Fig-1.8.
(4) Binary codes of the Intel-Hex formatted file can be uploaded into the flash of the MCU using IDE.
(5) Free Library Functions (ready-made routines) are available for programming and operation of commonly used industrial sensors/devices like DS18B20 Temperature Sensor, DS3231 RTC etc.

1.9 Integrated Development Environment (IDE)
IDEPic.png

Figure-1.8: IDE Interface of UNO Learning System

1.10 Serial Monitor (SM)

Figure-1.9: Serial Monitor of Arduino UNO Learning System

The Serial Monitor (Fig-1.9) has an InptBox via which a user can send command/data to the UNO Board. It has an OutputBox via which the user can receive data/results from UNO. The Serial Monitor (SM) has many fields of which only one field is being discussed below -- the ‘Line ending tab’. The Line ending tab has the following four options:

(1) No line ending: When this option is selected, the SM does not send any code to the UNO after sending the ASCII codes for the characters of the InputBox.

(2) Newline: When this option is selected, the SM sends 0x0A to the UNO after sending the characters of the InputBox. In C Language, the character representation of "Newline" is ‘\n’.

(3) Carriage return: When this option is selected, the SM sends 0x0D to the UNO after sending the characters of the InputBox. In C Language, the character representation of "Carriage return" is ‘\r’.

(4) Both NL & CR: When this option is selected, the SM sends 0x0D followed by 0x0A to the UNO after sending the characters of the InputBox. In C Language, the code is: “\r\n”).

1.11 Programming Built-in LED (L) of Arduino UNO Board
(1) The Circuit Diagram
Built-inL.png

Figure-1.10: Circuit for the built-in LED (L) of Arduino UNO Board

(2) Arduino Sketch/Program to Blink L Continuously at 1 sec Interval
Let us execute the Arduino App (application) from the Start Menu of the PC and observe that the Arduino IDE (Fig-1.8) has appeared on the Desktop. Let us also observe that the IDE contains the following two empty functions: (The MCU executes the setup() function first and then it executes the loop() function and then the user defined functions (placed below loop() function) as and when called upon.)

void setup()
{

}

void loop()
{

}

(3) Tasks that will be executed only once or for a ‘number of times’ will be within the setup() function.
(4) Tasks that will be executed repetitively will be kept within loop() function.

(5) In the present program we have the following tasks that will be executed only once and hence they will be under setup() function:
To set the direction of the pb5 line as output.

(6) In the present sketch we have the following tasks that will be executed again and again; hence, they will be under loop() function.

(a) To write logic-high at PB5-pin (DPin-3) to ON the LED (L) of Fig-1.10.
(b) To insert 1sec time delay.
(c) To write logic-low at PB5-pin (DPin-3) to OFF the LED (L) of Fig-1.10.
(d) To insert 1sec time delay.
(e) Goto Step-a to repeat the process.

(7) The Final Sketch

void setup()
{
 pinMode(13, OUTPUT); //to set direction of PB5 line as output
}

void loop()
{
 digitalWrite(13, HIGH); //L is ON
 delay(1000); //1000 ms =  1 sec ; Arduino’s Library Function
 digitalWrite(13, LOW); //L is OFF
 delay(1000); //insert time delay 
} //automatically goes to the beginning because of loop() function; no need for goto or jump.

1.12 Instructions/Commands Summary for IO Lines
InstructionSetIO.png

Figure-1.11: Table showing the software commands for IO Lines of UNO System

1.13 Data Types supported by Standard Arduino (UNO/NANO/MEGA)


Figure-1.12: Table showing data types supported by Arduino

... to be continued

UNOLearningSystem.png

IDEPic.png

Built-inL.png

InstructionSetIO.png

PL1-4.png

PL5-6.png

PL7-12.png

PL13-21.png

1.14 Problems and Solutions
1 Refer Fig-1.3 and then write Arduino Codes/Codes/Commands/Instructions to “Turn ON” LED1.

2 Refer Fig-1.3 and then write Codes to “Turn ON” L (built-in LED of UNO).

3 Write codes to check that K1 of Fig-1.3 is at closed condition.
K1 is an input device. One side of K1 is connected with 5V. The other side is connected with DPin-A3 of UNO. There is a pull-down resistor (R2) attached with DPin-3; as a result, the logic level of DPin-A3 is LOW. When K1 will be closed LH (5V) will appear at DPin-A3. So, the closed condition of K1 could be known by reading the value of DPin-A3. For codes, see Step-1.5(12).

4 Write codes using for() loop to set the directions of the IO lines of Port-D of Fig-1.3 as output.

5 Write codes to show 2 on the 7-segment display device of the following circuit.
7segOneCir.png

There are, in fact, 8 segments in a 7-segment display device and these are (read from right to left): p, g, f, e, d, c, b, and a. A segment turns ON when LH is applied at its anode pin and apply LL at it the CC (cathode cathode) pin.

In Fig-1.13(a), DP0 stands for “Display number 0:; CC7SD stands for “cc-type 7-segment display device”; cc0 stands for “cc-pin of DP0.

In Fig-1.13(a), The segments of the display devices are connected with UNO via IO lines of PORTB and PORTD; where, PORTB drives 6 segments (f, e, d, c, b, a) and PORTD drives 2 segments (p, g).

To see 2 on the display devices, the cc-code of 2 (0101 1011 = p, g, f, e, d, c, b, a = 0x5B) is to be sent to the segment lines of the display; where lower 6-bit will be delivered via PORTB (DPins: 13, 12, 11, 10, 9, 8) and the upper 2-bit will be delivered via PORTD (DPins: 7, 6). And then LL is to be asserted on DPin-A0.

The Codes:
(1) Set directions of DPins: 7-6 and 13-8 as output.

for(int i = 8; i < 15; i++)  
{
   pinMode(i, OUTPUT);    //covers: DPins: 7-6, 13-8, and A0 (14)
}

(2) Send the bits for the cc-code of 2 on DPins: 7-6 and 13-8.

byte x = 0x5B;       //cc-code of 2 
PORTB = 0x5B;      //0101 1011  ; lower bits 011000 would be accepted by PORTB (DPins: 13-8)

bool n = bitRead(x, 6);       //bit-6 of x (1) enters into variable n; for bitRead() function see 1.12(5)
digitalWrite(6, n);          //bt-6 of x goes on DPin-6; 

n = bitRead(x, 7);
bitWrite(PORT, 7, n);        //bit-7 of x goes on DPin-7; for bitWrite() function see 1.12(3)

6 Data Types
(1) To store 1-bit data like HIGH/LOW and true/false, we declare a variable preceded by keyword bool. Here, “bool” is a data type. For example:

bool n = digitalRead(A3);     //variable n will hold LH if K1 is closed.

(2) To store only positive valued 8-bit data (0 to 255 = 0x00 to 0xFF), we declare a variable preceded by keyword byte. Here, “byte” is a data type. For example:

byte x = 0x31;       //variable x holds the binary number 00110001 (Hex is a compact form of binary)

(3) For other data types, see Section-1.13 Table-1.2.

7 Under what condition, the pb6 and pb7 IO lines are available to the user.

8 MCU is the short name for Microcontroller and Microcontrolling Unit is the full name.

9 Assume that LED3 is connected with PD3 line of Fig-1.3in series with a 2.2k current limiting resistor. Write all possible codes/commands/instructions to ignite LED3?

10 In Arduino Platform, the leading 0 (zero) has an especial meaning to refer to octal base. Explain with examples, the validity of the statement.

Serial.println(14, DEC);     //shows: 14 (decimal 14 appears in decimal base)
Serial.println(014, OCT);   	//shows: 14 (octal 14 appears in octal base)
Serial.print(14, OCT);      	//shows: 14 (decimal 14 appears in octal base)  
Serial.println(014, DEC);   	//shows: 12 (octal 14 appears in decimal base)

.. to be continued

7segOneCir.png

...reserved.

Table 1.4

Your understanding of “Boolean” for “pinMode” seems a little … confused.

digitalWrite does not accept a Boolen [sic] value.

@TheMemberFormerlyKnownAsAWOL

Your comments agree with the Arduino Reference Manual, and I have corrected my mistakes (ignorance?) in the referred Table.

Thank you very much for taking time to go through my posts and pointing the mistakes from which myself and the online students (readers) would be immensely benefited.

small typo in first post

pinMode(A3, INPUT_[color=red]PULLIP[/color]);     //DPin-A3 is an input line with internal pull-up connected.

thanks for sharing your work

J-M-L:
small typo in first post

pinMode(A3, INPUT_[color=red]PULLIP[/color]);     //DPin-A3 is an input line with internal pull-up connected.

thanks for sharing your work

Yes! There was a typo mistake in the first post that I corrected later.

Thank you so much for reviewing my materials and pointing the mistakes.

not a mistake, a simple typo. that happens :slight_smile:
(it's still there in the forum though)

J-M-L:
not a mistake, a simple typo. that happens :slight_smile:
(it's still there in the forum though)

Now correction is done in the Forum's post! :slight_smile:

(In fact, I corrected it in my original .docx file which I shared with my online students over Zoom Platform.)

By the by, what is the tolerance level of mistake that happens out of ignorance? If it is not intentional, then?

It’s not about tolerance, there is no impact to us besides a small annoying feeling that something is not right :wink:

It’s about helping you get a better and crisp document

J-M-L:
It’s about helping you get a better and crisp document

Appreciate a lot.