Self-test on Simple Programming of ATmega328 using Arduino UNO Platform

Learners like me and others may down load the attached quiz-question and try to answer.

Quiz.doc (804 KB)


Let us try to develop some Arduino IDE Codes to show 00, 01, 02, 03, ..., 98, 99, 00, 01 (cyclically) on DP0-DP1 positions of the above schematic.

I would like to begin with the following Pseudo Codes:

void setup()
   Set directions of IO lines as needed                                 // L11:
   Initialize other variables and data structures as needed   // L12:

void loop()
    byte x = 0x00;   //                                                L21:
    //-----------------------------------------------        L22:
    byte x1 = 8-bit cc-code for the left digit of x
    byte x2 = 8-bit cc-code for the right digit of x
    //-----------------------------------------------        L23:
    Send x1 onto DP0 via PORTD, PORTB
    Assert LL at cc0-pin of DPO
    Assert LH at cc1-pin of DP1
   //------------------------------------------------         L24:
    Send x2 onto DP1 via PORTD, PORTB
    Assert LH at cc0-pin of DPO
    Assert LL at cc1-pin of DP1
    //------------------------------------------------        L25:
   //-------------------------------------------------       L26:
   Executes codes similar to above in a linear way (inductive reasoning) or using [i]for()[/i] 
   structure (deductive reasoning) to show 01, 02, ..., 09.  
   //-- now facing problem: how to show the next number - 10?    

Learners may repack the bare Arduino UNO Kit as per following figure to accommodate the hardware resources of the above figure. And then the given Pseudo Codes could be coded and tested on 'SSS (Small Start Strategy)' methodology. This method has been tested in the class room; the participants showed satisfactory outcomes!


  1. Which port line is being referred by DPin-1?
    [Unclear. PINDx and PORTDx are the uC nomenclature, DPin-x is not. One might assume PD1, the TXD line, is being referred to.]

  2. Which two instructions of the following will ignite L?
    (a) digitalWrite(8, HIGH);
    (b) bitClear(PORTB, 5);
    (c) boolean n= HIGH;
    bitWrite(PORTB, 5, n);
    [c, assuming the pin had been set to output mode]

  3. Write assignment instruction for the function
    pinMode(0, HIGH) to set the direction of PD0-line.
    [incorrect syntax. pinMode (0, INPUT); or (0, INPUT_PULLUP) or (O, OUTPUT) are correct.

  4. Write instruction to read the value of Pin-4 of ATmega328 and write it into PD1-line. Assume that the directions of the port lines are already as needed.
    [digitalWrite (1, (digitalRead(2) ); assuming Pin-4 refers to the physical pin]

  5. Write instruction (s) to show 3 at DP2 position of the 7-segment display unit. Assume that the directions of the lines are already set as required.
    [digitalWrite (8, HIGH); // a
    digitalWrite (9, HIGH); //b
    digitalWrite (10, HIGH); // c
    digitalWrite (11, HIGH); // d
    digitalWrite (12, LOW); // e
    digitalWrite (13, LOW); // f
    digitalWrite (6, HIGH); // g
    digitalWrite (7, LOW); // p
    digitalWrite (1 HIGH); // mux 1
    digitalWrite (0, LOW); // mux 0

Note the drawing is missing any current limiting resistors for the LEDs, and 74LS138 is only recommended for 8mA sink current max.

  1. Write down the symbolic names of the Data Direction Register that are present in the system.

  2. After the inclusion of the 74LS138 decoder, how many port lines are now available in the system?
    [1, pin 3]

  3. Which segment (a or b or ……, p) of which display (DP0 or DP1, …DP3) will appear as ON after the execution of the following instruction?
    byte x = 0x87;
    boolean n = bitRead(x, 7);
    bitWrite(PORTD, 7, n);
    digitalWrite(1, HIGH);
    bitSet(PORTD, 0);
    [first 3 lines put HIGH on pin13, so p is driven.
    PORTD bits 1,0 = 1,0 → DP2 enabled]

  4. Assume that the wiper of R2 is exactly at the 1/3rd position from 0V-point. What will be the value of x (in hex format) after the execution of the following instruction?
    int x = analogRead(A5); (You can use Calculator.)
    [assuming Aref is set to External, it has only a 0.1uF cap to Gnd connected, and assuming a linear pot, then the voltage at A5 = 5V/3 = 1.67V, and x ~= 1.67*1024/5V = 342decimal = 0x155]

  5. What output values (0 or 1) would be produced by the following two instructions after pressing K1?
    (a) digiatlRead(2);
    (b) bitRead(PIND, 2);
    (c) bitRead(PORTD, 2);
    [which 2 of the 3 instructions?]


It's my great pleasure to see your interaction and involvement for better education of the learners in the way of reinforcing constructive criticisms.

In fact, the quiz-question has been set for testing the progress of my (about 135) undergraduate students.

Therefore, please wait until next Saturday to see what the answers are given by the students. I have also solution sheets/answer sheet for these questions. In the meantime, I would like to state that there could be mistakes in the composition of my questions either conceptually, syntactically, semantically or all. I will remain open with the readers to make necessary amendments in the present quiz-question in order to make a better quiz-question next time.

Few clarifications in response to your queries (I will not comment now on your answer): 1. DPin is our local symbolic name. It stands for Digital Pin Connector. It refers to the Arduino UNO's sockets (holes) with PCB designations 0, 1, .., 12, 13 into which we plug pins of the jumper wires.

APin stands for analog pin connector (A0 to A5). PPin stands for power pin connector (5V, GND 3.3V, ..).

PINDx (x = 0 to 7) : PIND2 refers to Bit-2 of PIND Register. PORTDx (x = 0 to 7): PORTD3 refers to Bit-3 of PORTD Regiter. (In AVR Studio it is called PD3.)

  1. No comment, now.

  2. You are right (I frequently make this mistake).

  3. No comment, now.

  4. We have avoided CLR to save space in the breadboard. Hopefully, segments will not destroy MCU's port lines.

Practically, 74LS138 is driving well the cc-pins of the display devices resulting well visibility of the segments.

6 - 8. No comment, now.

  1. The students will solve it assuming Vref as DEFAULT (5V). Probably they will use 1023 instead of 1024 (Input = 5V, Vref = 5V; Output = 000000 11 1111 1111 = 03FF = 1023). There are many references in the net/forum of using both 1023 and 1024! Which one is the preferred one and why?

  2. Typographic mistake (the word two is to be deleted).

#9, no doubt 1023 will be used, but 1024 is correct per the datasheet.

If you try harder, the schematic could be made a bit more unreadable, if that's what your intention was.



Soon, I will come up with a revised Schematic where the segment pins of the 7-segment devices will be driven by an 8-bit 'Bit Addressable Latch (74LS259)'. I along with my co-learners will test the functionality of the circuit, and then we will clarify the rationality for putting so many extra things with the basic Arduino. In the mean time, let the readers be pondering about our intentions.

Thanks for the comment which I had been expecting since the posting of the schematic.

we will clarify the rationality for putting so many extra things with the basic Arduino.

That's a good thing ;)


Maybe some version like this for the Arduino:

Arduino Rev3.jpg

Or for the 328:


Then, revisit the other items in the schematic.

Please confirm the blocks for accuracy.


  1. I am not familiar with the top diagram.

  2. The Bottom diagram agrees with the board layout signals of Arduino UNO R3.


A: Revised Schematic, which has no more chance to undergo further revision. |500x305

B: The repackaged Arduino UNO that conforms with the above Schematic. |500x286

C: Why have we added the 74LS138 decoder and 74LS259 addressable latch? (1) We have aded these two chips for expanding the IO lines of the ATmega328 from 20 to 25. (2) Now, we can carry out the following experiments without taking out any jumper wiring that we have done for the previous experiment.

  • 4-digit 7-segment display unit
  • LCD
  • Connecting input device (K1)
  • External hardware interrupt
  • Analog-to-Digital Conversion
  • Counting of external events
  • PWM
  • SPI Bus operation/ISP Interface
  • TWI Bus Operation
  • UART
  • Serial Monitor
  • Interprocessor Communication (Arduino UNO and Arduino NANO)
  • System Design (Temperature Measurement).

D: Let us remember that we started Lab Works in the following sequence (Lab Sheets are available for uploading). (1) Keep blinking L. (2) Blink L for 5 times using goto statement. (3) Blink L using for() structure (4) 7-segment display device (single) being driven by PORTD for the segments and PORTB for the cc-pins. (5) Building 2-digit multiplexed display,

........................ .........................

(n) we have arrived at the present state of expanding IO lines. Every student has his own Learning Kit which he will keep with him and practice MCU programming at the most convenient time.

** Things might appear clumsy; however, it gives pleasure when observing the scenario of how the parallel bits passing through the addressable latch and getting their places at the right positions of the 4-digit multiplexed display**

One might suggest for Arduino MEGA. The MEGA is very costly (900); Arduino UNO (450); 74LS138 (20), and 74LS259 (25).

Thanks for hearing.

That bird's nest of wires is a candidate for the Wall of Shame Thread


Let the Wall of Shame Thread be more enriched!

I'd like to see the schematic for that one.

Let me tell you the story/history! It’s not always dry!! There is always someone around to hear the tale!

Please find the attached file for the detailed schematics of an unpublished worksMTK8038Schematics

ICAEE13(DynamicBuSSizing-80386).pdf (363 KB)

MTK386Word.pdf (399 KB)

This is the second time you have tried to hi-jack another person's Thread with your question which has absolutely ZERO to do with the Thread. Don't do that.

Post I was referring to seem to have been removed



Subj : Quiz-question/Answer Review subject to mutual correction:

  1. -

  2. -

  3. I expected: DDRD0 = HIGH; (coneptually; it will not be compiled by IDE).

  4. Why should I assume that Pin-4 is a physical pin of the MCU? How is it ambiguous? Is there any other possible meaning (reference) other than the metallic pin of the MCU?

  5. I expected:

byte y = 0x4F; //(0100 1111 = p g f e d c b a = 3 = 0100 1111; 00 1111 ----> PB5 - PB0) PORTB = y;

bitWrite (PORTD, 6, bitRead(y, 6)); // y = y67 y6 y5 y4 y3 y2 y1 y0 or digitalWrite(6, bitRead(y, 6));

digitalWrite(7, bitRead(y, 7));

bitSet(0, LOW); bitSet(1, HIGH);

  1. -

  2. (After the inclusion of the 74LS138 decoder, how many port lines are now available in the system?) Let us correct the Question: After the inclusion of the 74LS138 decoder, how many port lines are now available in the ~~ system?~~ have increaded in the ATmega328 system?

I expected: 2 (we have 20 port lines in the MCU; now 74LS138 has gievn two more extra!)

8. I expected the students to analyse every line and then come to the result; they didn't except 5. byte x = 0x87; // x = 1000 0111 boolean n = bitRead(x, 7); // n = 1 bitWrite(PORTD, 7, n); // PD7 = LH // p (point) digitalWrite(1, HIGH); // PD1 = LH // DP2 bitSet(PORTD, 0); // PD2 = LH

[first 3 lines put HIGH on pin13, so p is driven. PORTD bits 1,0 = 1,0 -> DP2 enabled]

[The Proverb: British/French says: Get the knowledge first and then analyse. American says: Analyse and then find the answer. American says: It will be in the market next month. Japanese says: It (the same thing) is already in the market.]

  1. - I have a question. If I want to measure the internal TS's (Temperature Sensor) signal, which channel should I use apart from using Vref = 1.1V (INTERNAL)? Data sheet says: Ch-8. The syntax: analogRead(A8) is not accepted. I used analoagRead(8); the value is 60.00 deg C. It does not change! I have solved the following equation based on the TS' response points: A(25 degC, 0.314V), B(85 degC, 0.380V).
     DECT (Decimal Temperature) = 9090909* Vdt - 2604545  (10+4 Times amplified)
     BINT (Binary temperature, 10+6 Times amplified) = 0x0EEA6D* Vht - 0x0F863864
     ( Vdt (Output DC Voltage of TS at T degC) = (Vht*1.1)/1023); Vht = ADC value at T degC.
  1. The question is to be corrected as follows: What output values (0 or 1) would be produced by the Question-(c) of the following instructions after pressing K1? (The students are not yet familiar with the internal structure of PORTD Latch.) I would accept any answer.

Don't do that.

I think that the Moderators are here to offer guide lines as they think suitable (correction, motivation, punishment, insult, threat, ..., sometimes smile!). Others might suggest Moderators to take actions; but, certainly not the reporter himself as it violates the right of defence of the to be accused!

Those including me, who are engaged in the Learning/Teaching Process of ATmega328 MCU using Arduino UNO Platform may down load the attached Experiment-2 File for practising.

Experiment2 (1).pdf (566 KB)

GolamMostafa: I think that the Moderators ...

The Post I was referring to seems to have been removed.