1 - Arduino Uno
1 - Arduino Uno
ATmega328
Created by: Dr. Daniel van Niekerk
                       Arduino Uno (ATmega328)
• Overview
  ̶ The Arduino Uno is a development board based on the ATmega328 microcontroller.
     > It has 14 digital input/output pins (0 to 13).
     > Six of the digital pins can be used as PWM outputs (3,5,6,9,10 & 11).
     > It has six 10-bit resolution, analog to digital converter (ADC) inputs of A0 to A5.
     > A 16 MHz crystal oscillator to drive microcontroller instruction execution.
     > A universal serial bus (USB) type B jack connection.
     > A 2.1 mm center-positive female DC power jack.
     > An in-circuit serial programming (ICSP) header.
     > A reset button to restart code execution of the microcontroller.
   ̶ The microcontroller on the Arduino Uno board can be powered by:
     > Connecting it to a computer via a standard USB-A to USB-B type cable.
     > Or connecting it to an AC-to-DC adapter via the onboard DC power jack (7 to 9V).
     > Or connecting it to an unregulated external battery via the VIN pin (7 to 9V).
• Note
  ̶ An onboard ATmega8U2 programmed as a USB-to-serial converter is used to enable PC
     programming of the ATmega328 and serial communication via USB cable connection.
   ̶ "Uno" means one in Italian, the name given to mark the release of Arduino 1.0.
                                                                        Arduino Uno (ATmega328)
• Technical Specifications
       Microcontroller                                                         ATmega328
       Operating Voltage                                                       5V
       Input Voltage (recommended)                                             7-9V
       Input Voltage (limits)                                                  6-20V
       Digital I/O Pins                                                        14 (of which 6 can be used for PWM output)
   ̶    The 1 KB of EEPROM can be read and written using the EEPROM library.
   ̶ 5V pin: is the regulated power supply used to power the microcontroller and other
      components on the Arduino board.
    ̶ This can come either from VIN pin via the onboard 5 volt regulator or be supplied by
      the USB connection or another external regulated 5 volt supply connected to 5V pin.
   ̶ 3V3 pin: can supply 3.3 volts with a maximum output current of 150 mA, from another
     on-board 3.3 volt regulator that is connected to the onboard 5 volt regulator output.
   ̶ GND pins: are connected to all the Arduino board common circuit grounds.
                        Arduino Uno (ATmega328)
• Microcontroller Port Pins
   ̶ Each of the 14 digital pins on the Uno can be used as an input or output that operate at
        5 volts, using pinMode(), digitalWrite() and digitalRead() functions.
    ̶ All digital pins have an internal pull-up resistor disconnected by default, of 20-50 kΩ.
     ̶ Digital pins operate at 5 volts and each pin can provide or sink a maximum of 40 mA.
      ̶ In addition, some pins have the following specialized functions:
     > Serial pins: 0 (RX) and 1 (TX), used to receive (RX) and transmit (TX) TTL serial data.
      These pins are connected to the corresponding pins of the ATmega8U2, USB-to-TTL
      serial chip.
     > External Interrupt pins: 2 (interrupt 0) and 3 (interrupt 1), these pins can be
      configured to trigger an interrupt on a low value, a change in value, a rising or falling
      edge by using the attachInterrupt() function.
     > PWM pins: 3, 5, 6, 9, 10 and 11, can provide 8-bit PWM output by making use of the
      analogWrite() function.
     > SPI pins: 10 (SS), 11 (MOSI), 12 (MISO) and 13 (SCK), support the Serial Peripheral
      Interface (SPI) communication by using the SPI library.
     > LED pin: 13, a buffered built-in LED is connected to digital pin 13 and when the pin
      output is set HIGH the LED is on and when the pin output is set LOW, it's off.
                      Arduino Uno (ATmega328)
 ̶ The Uno has 6 analog inputs labeled A0 through to A5 (or pin 14 to 19), each of which
    provide 10-bit resolution that results in 1024 different values (0 to (2^10 - 1) = 1023).
  ̶ By default they measure from ground to 5 volts, all-though is it possible to change the
    upper end of their range using the AREF pin and the analogReference() function.
̶ Note that an Arduino shield is defined as a hardware device that can be mounted over
  the Arduino board to increase the capabilities of a given project. For example, Ethernet
  shields can be used to connect the Arduino board to the Internet thereby making project
  work easier. The pin position of the shields is similar to that of the Arduino board.
                        Arduino Uno (ATmega328)
• Communication
  ̶ The Arduino Uno has a number of facilities for communicating with a computer, another
           Arduino or other microcontrollers.
   ̶ The ATmega328 microcontroller provides Universal Asynchronous Receiver/Transmitter
           (UART) that is Transistor-Transistor Logic (TTL) or 5 V, serial communication available on
           digital receive pin 0 (RX) and transmit pin 1 (TX).
    ̶ An onboard ATmega8U2 microcontroller channels this serial communication over USB
           and it appears as a virtual com port, to software on a computer.
     ̶ The ATmega8U2 microcontroller firmware uses standard USB COM drivers and
           therefore, no external USB-to-serial device driver is required.
      ̶ The Arduino integrated development environment (IDE) software includes a serial
           monitor to allow simple ASCII serial data to be sent to and from the Arduino board.
       ̶ Onboard RX and TX LEDs will flash when data is being transferred via the USB-to-serial
           ATmega8U2 chip connection to a computer but not for external serial communication
           directly connected on pins 0 (RX) and 1 (TX).
        ̶ A SoftwareSerial library allows serial communication on other Arduino Uno digital pins.
         ̶ The ATmega328 also supports I2C or TWI and SPI communication.
    > The Arduino software includes a Wire library to simplify the use of I2C or TWI bus.
    > The Arduino software includes a SPI library to simplify the use of SPI bus.
                       Arduino Uno (ATmega328)
• Programming
  ̶ The Arduino Uno board can be programmed directly through the downloaded Arduino
        IDE software from the official Arduino website at “www.Arduino.cc”.
   ̶ In the IDE software, select “Arduino/Genuino Uno” from the “Tools > Board” menu.
    ̶ After connecting a USB cable between the Arduino Uno board and the PC, select the
        automatically detected virtual com port from the “Tools > Port” menu.
     ̶ The ATmega328 microcontroller on Arduino Uno comes pre-burned with a bootloader
        that allows uploading of new user program code to it, without the use of an external
        hardware programmer.
      ̶ The bootloader can be bypass by programming the microcontroller through the ICSP
        (In-Circuit Serial Programming) header by using an external (Arduino ISP or similar)
        hardware programing device.
                         Arduino Uno (ATmega328)
• Automatic Software Reset
   ̶ A physical press of the reset button before an upload is not required because the
             Arduino Uno can be reset by the Arduino IDE software running on a connected PC.
    ̶ One of the hardware flow control lines (DTR) of the ATmega8U2 is connected to the
             reset line of the ATmega328 via a 100 nano-farad capacitor.
     ̶ When this line is asserted or pulled LOW, the reset line drops long enough to reset the
             ATmega328 microcontroller chip.
      ̶ Using this capability, code can be uploaded simply by pressing the upload button on
             the Arduino IDE software.
       ̶ This means that the bootloader can have a shorter timeout, as the lowering of DTR
             control line can be well-coordinated with the start of the user code upload.
        ̶ If a sketch running on the board receives one-time configuration or other data when it
             first starts, make sure that the software with which it communicates, waits a second
             before sending this data, after opening the connection.
         ̶ The Arduino Uno board contains a trace that can be cut to disable the auto-reset.
          ̶ Pads on either side of the labeled “RESET-EN” trace under the Arduino Uno board, can
             be soldered together again to re-enable auto-reset.
           ̶ The auto-reset can also be disable by connecting a 110 ohm resistor from the 5V pin to
             the RESET pin.
                        Arduino Uno (ATmega328)
• USB Overcurrent Protection
   ̶ The Arduino Uno has a resettable poly-fuse that protects the computer's USB ports
       from shorts and overcurrent.
    ̶ If more than 500 mA is sourced from the USB port, the fuse will automatically break the
       connection until the short or overload is removed and time is allow for the fuse to cool
       down in order to re-connect the USB port.
     ̶ Although most computers provide their own internal protection, the fuse provides an
       extra layer of protection.
                               Arduino Uno (ATmega328)
                                Resettable poly-fuse
   Power jack to externally                              USB connection for
   power-up the Arduino                                  programming and
   UNO or use Vin pin                                    Serial communication
                                                         (Max 200mA at 5V)
   Max 1A from on board                                  Reset Button
   5V regulator.
   DC current for 5V per
   I/O Pin, 40 mA Max
   DC current for 3.3V per                               SCL or 10bit A/D or I/O, A5
   I/O Pin, 50 mA Max                                    SDA or 10bit A/D or I/O, A4
                                                         AREF
   DC Current VCC & GND                                  GND
   Pins: 200mA                                           Pin13, I/O or SCK, built-in LED
                         5V                              Pin12, I/O or MISO
                     RESET                               Pin11, I/O or PWM or MOSI
                       3.3V
                                                         Pin10, I/O or PWM or SS
                         5V                              Pin9, I/O or PWM
                      GND
                                                         Pin8, I/O
                      GND
           7 to 9V Max, Vin                              Pin7, I/O
                                                         Pin6, I/O or PWM
        10bit A/D or I/O, A0                             Pin5, I/O or PWM
        10bit A/D or I/O, A1                             Pin4, I/O
        10bit A/D or I/O, A2                             Pin3, I/O or Int1 or PWM
        10bit A/D or I/O, A3                             Pin2, I/O or Int0
SDA or 10bit A/D or I/O, A4                              Pin1, I/O or TX
 SCL or 10bit A/D or I/O, A5                             Pin0, I/O or RX
                        5V                                              5V
                                                                                 digitalWrite(pin, HIGH)
   Logic 1 state       digitalRead(pin) == HIGH   Minimum voltage       4.2V
                                                  state when HIGH
                        3V
Undefined state
                        1.5V
   Logic 0 state        digitalRead(pin) == LOW   Maximum voltage       0.9V
                                                  state when LOW                  digitalWrite(pin, LOW)
                        0V                                              0V
    Input or
     output       > Used for any input or output operation.
Predefined
 process
                  > Used to invoke a subroutine or function which is normally used as a
                   predefined process.
Decision > Used to ask a question in a program with either two or three outcomes.
                                         A
  READ                     READ
  TEST                     TEST
 SCORE                    SCORE
 LAST      NO              LAST     NO
SCORE?                    SCORE?         A
YES YES
COMPUTE                  COMPUTE
AVERAGE                  AVERAGE
DISPLAY                   DISPLAY
AVERAGE                   AVERAGE
  END                      END
                        Arduino Uno (ATmega328)
• Advantages of Flowcharts
   ̶ Standard symbols are used for flow-charting, resulting in flowcharts being widely
          recognised.
    ̶ Flowcharts can be understood and interpreted by someone without a programming
          background.
     ̶ Flowcharts can be used to divide the entire project into sub-tasks or functions.
      ̶ The flowchart can be examined to measure the overall programing progress.
       ̶ Flowcharts show the sequence of operations and can therefore, be used to aid in
          locating the source of logical errors.
        ̶ There are many aids available to create flowcharts, like programing templates and
          automated drawing packages.
                        Arduino Uno (ATmega328)
• Disadvantages of Flowcharting
   ̶ Flowcharts can be difficult to design, draw, or change except for simple programs.
    ̶ There is no easy way to debug or test a flowchart.
     ̶ Flowcharts tend to become cluttered because designers find it difficult to provide only
          the amount of detail needed to make the flowchart useful.
      ̶ Flowcharts show only the program organisation and they do not show the organisation
          of the data or the structure of the input/outputs.
       ̶ Flowcharts do not help with hardware or timing problems or give any indication as to
          where any of these problems might occur.
        ̶ Flowcharts can become unstructured because line arrows backtracking and looping all
          over the chart result in difficult to understand flowchart designs.
                        Arduino Uno (ATmega328)
                                                Start
• Software Development Steps
                                              Problem
   ̶ Problem Definition.                      Definition
    ̶ Program Design (Flowcharting).
     ̶ Coding (writing code & compiling).     Program
                                               Design
      ̶ Debugging (using simulator).
       ̶ Testing (run program in MCU).         Coding
                                               Logical      Yes
                                               Errors ?
                                               No
                                               Testing
                                              Functional    Yes
                                               Errors ?
                                               No
                                            Documentation
Maintenance
                                                 End
                       ADC Converter Calculations
•   ADC_VREF = 5V or 3.3V EXTERNAL              or 1.1V (INTERNAL)
                    ADCVOLTAGE
•   ∴ ADCRESULT =    ADC_VSTEP
    ̶ Calculate the gain of the amplifier to ensure best resolution by making use of the full
      ADC voltage measurement range.
                      𝐌𝐀𝐗_𝐀𝐃𝐂𝐕𝐎𝐋𝐓𝐀𝐆𝐄        𝐀𝐃𝐂_𝐕𝐑𝐄𝐅                           𝟓𝐕
      > 𝐀𝐌𝐏𝐆𝐀𝐈𝐍 𝐀𝐯 = 𝐌𝐀𝐗_𝐒𝐄𝐍𝐒𝐎𝐑      =
                                       𝐌𝐀𝐗_𝐒𝐄𝐍𝐒𝐎𝐑
                                                                          =
                                                                              𝟏.𝟓 𝐕
                                                                                      = 𝟑. 𝟑𝟑𝟑
                                      𝐕𝐎𝐋𝐓𝐀𝐆𝐄                   𝐕𝐎𝐋𝐓𝐀𝐆𝐄
    ̶ Calculate the voltage step of the A/D converter for each ADC bit result.
                      𝐀𝐃𝐂_𝐕𝐑𝐄𝐅             𝟓𝐕           𝟓𝐕
      > 𝐀𝐃𝐂_𝐕𝐒𝐓𝐄𝐏 = 𝐌𝐀𝐗_𝐀𝐃𝐂           =
                                          𝟐𝟏𝟎 −𝟏
                                                   =
                                                       𝟏𝟎𝟐𝟑
                                                              = 𝟒. 𝟖𝟖𝟕 𝐦𝐕/𝐛𝐢𝐭
                             𝐑𝐄𝐒𝐔𝐋𝐓
    ̶ When the sensor is reading 65 km/h, determine the sensor output voltage and the
      expected ADC result.
         𝟔𝟓 𝐤𝐦/𝐡   𝐒𝐄𝐍𝐒𝐎𝐑 𝐕𝐎𝐋𝐓𝐀𝐆𝐄                                 𝟔𝟓 𝐤𝐦/𝐡 ×𝟏.𝟓 𝐕
      > 𝟏𝟐𝟎 𝐤𝐦/𝐡
                 =
                       𝟏.𝟓 𝐕
                                  → ∴ 𝐒𝐄𝐍𝐒𝐎𝐑 𝐕𝐎𝐋𝐓𝐀𝐆𝐄 =
                                                                    𝟏𝟐𝟎 𝐤𝐦/𝐡
                                                                                      = 𝟎. 𝟖𝟏𝟑 𝐕
   •   Process symbol
   •   Pre-defined process symbol
   •   Input/output symbol
   •   Decision symbol
23.1 Calculate the voltage gain of the amplifier to ensure best resolution by using the full
     ADC range.
23.2 Design the amplifier circuit, if the selected feedback resistor is 1.2 KΩ.
23.3 Calculate the voltage step of the A/D converter for each ADC bit result.
23.4 When the current through the sensing resistor is 23 A, determine the sensor output
     voltage and the expected ADC result.
23.5 Calculate the scale factor to multiply the ADC result with, in order to determine the
     actual current flow through the sensing resistor in amps.