Department of Electromechanical Engineering
Workshop for Mechatronics
                                              1 / 38
Interfacing
   1 Introduction
   2 Display LED
   3 Push button and LED with Arduino
       Pull-up circuit
   4 7-Segment Display
       Programming 7 segment display without Library
       Programming of 7-Segment Display Using SevSeg Library
   5 Interfacing of Keys
       Arduino Programming of Keypad
   6 Liquid Crystal Display (LCD)
       Temperature sensor and LCD
   7 Analog Input Output
                                                               2 / 38
 Introduction
Introduction
     Interfacing is the process of connecting devices together so that
     they can exchange the information and that proves to be easier
     to write the programs. There are different type of input and out-
     put devices as for our requirement such as LEDs, LCDs, 7segment,
     keypad, motors and other devices.
     Microcontrollers are useful to the extent that they communicate with
     other devices, such as sensors, motors, switches, keypads, displays,
     memory and even other micro-controllers.
     Many microcontroller designs typically mix multiple interfacing meth-
     ods. In a very simplistic form, a micro-controller system can be
     viewed as a system that reads from (monitors) inputs, performs pro-
     cessing and writes to ( controls ) outputs.
                                                                             3 / 38
 Introduction
Microcontroller Interfaces
                             4 / 38
 Introduction
Microcontroller Interfaces
                             5 / 38
 Display LED
Display LED
    LEDs are most commonly used in many applications for indicating
    the output. They find huge range of applications as indicators during
    test to check the validity of results at different stages. They are very
    cheap and easily available in a variety of shape, color and size.
    The principle of operation of LEDs is very easy. A simple LEDs
    also servers as a basic display devices, it On and OFF state express
    meaning full information about a device.
    The common available LEDs have a 1.7v voltage drop that means
    when we apply above 1.7V, the diode conducts. The diode needs
    10mA current to glow with full intensity.
                                                                               6 / 38
Display LED
   LEDs can be interfaced to the microcontroller in either common
   anode or common cathode configuration. Here the LEDs are con-
   nected in common anode configuration because the common cath-
   ode configuration consumes more power.
                                                                    7 / 38
 Push button and LED with Arduino
Push button and LED with Arduino
    This lesson will teach you how to control LED using a push button,
    which stays on as long as the button is being pressed.
                                                                         8 / 38
Push button and LED with Arduino
   The following figure is the protues and arduino code for push button
   and LED with Arduino.
   When your button is not pressed, the internal pull-up resistor con-
   nects to 5 volts. This causes the arduino to report ”1” or HIGH.
   When the button is pressed, the arduino pin is pulled to ground,
   causing the arduino report a ”0”, or LOW.
                                                                          9 / 38
 Push button and LED with Arduino
   Pull-up circuit
pull-up circuit
    when the switch is open the voltage of the gate input is pulled up
    to the level of Vin . when the switch is closed, the input voltage at
    the gate goes to ground.
                                                                            10 / 38
 Push button and LED with Arduino
   Pull-up circuit
RGB Programming
    The RGB LED can emit different colors by mixing the 3 basic colors
    red, green and blue. So it actually consists of 3 separate LEDs red,
    green and blue packed in a single case.
    That’s why it has 4 leads, one lead for each of the 3 colors and one
    common cathode or anode depending of the RGB LED type. In this
    tutorial I will be using a common cathode one.
                                                                           11 / 38
Push button and LED with Arduino
  Pull-up circuit
   The cathode will be connected to the ground and the 3 anodes will
   be connected through 220 Ohms resistors to 3 digital pins on the
   Arduino Board that can provide PWM signal. We will use PWM for
   simulating analog output which will provide different voltage levels
   to the LEDs so we can get the desired colors.
                                                                          12 / 38
Push button and LED with Arduino
  Pull-up circuit
   We will use PWM for simulating analog output which will provide
   different voltage levels to the LEDs so we can get the desired colors.
                                                                            13 / 38
 Push button and LED with Arduino
   Pull-up circuit
RGB Arduino Programming
    I will use the pins number 7, 6 and 5 and I will name them redPin,
    greenPin and bluePin. In the setup section we need to define them
    as outputs.
    At the bottom of the sketch we have this custom made function
    named setColor() which takes 3 different arguments redValue, green-
    Value and blueValue. These arguments represents the brightness of
    the LEDs or the duty cycle of the PWM signal which is created using
    the analogWrite() function.
    These values can vary from 0 to 255 which represents 100% duty
    cycle of the PWM signal or maximum LED brightness.
                                                                          14 / 38
Push button and LED with Arduino
  Pull-up circuit
                                   15 / 38
Push button and LED with Arduino
  Pull-up circuit
   So now in the loop function we will make our program which will
   change the color of the LED each second.
   In order to get red light on the LED we will call the setColor()
   function and set value of 255 for the redValue argument and 0 for
   the two others. Respectively we can get the two other basic colors,
   green and blue.
   For getting other colors we need to mix the arguments values. So for
   example if set all 3 LEDS to maximum brightness we will get White
   color and we will get a purple color if we set the following values to
   the arguments: 170 redValue, 0 greenValue and 255 blueValue.
                                                                            16 / 38
 7-Segment Display
7-Segment Display
    Seven segment display is the most basic electronic display. It consists
    of eight LEDs which are associated in a sequence manner so as to
    display digits from 0 to 9 when proper combinations of LEDs are
    switched on. A 7-segment display uses seven LEDs to display digits
    from 0 to 9 and the 8th LED is used for dot.
                                                                              17 / 38
7-Segment Display
   7-segment displays are used in a number of systems to display nu-
   meric information. Thus the number of segments used depends on
   the number of digits to display. The digits 0 to 9 are displayed
   continuously at a predefined time delay.
   7-segment displays are available in two configurations which are
   common anode and common cathode. Common anode configu-
   ration is used because output current of the microcontroller is suffi-
   cient enough to drive the LEDs. 7-segment display works on negative
   logic, we have to provide logic 0 to the corresponding pin to make
   on LED glow.
   In a common cathode configuration, all seven LEDs plus a dot LED
   have the cathodes connected to pins 3 and pin 8. To use this display,
   we need to connect GROUND to pin 3 and pin 8 and, and +5V to
   the other pins to make the individual segments light up.
                                                                            18 / 38
7-Segment Display
         The following table shows the hex values used to display the
                               different digits.
                                                                        19 / 38
 7-Segment Display
   Programming 7 segment display without Library
Programming 7 segment display without Library
    The pins of seven-segment display are connected to Arduino pins
    2-9, as shown in the table below. Common pins (pin 3 and pin 8)
    are connected to GND and dp is left unconnected, because it is not
    used in this experiment
                                                                         20 / 38
7-Segment Display
  Programming 7 segment display without Library
                                                  21 / 38
7-Segment Display
  Programming 7 segment display without Library
                                                  22 / 38
 7-Segment Display
   Programming of 7-Segment Display Using SevSeg Library
Programming of 7-Segment Display Using SevSeg Library
    We will use the library called SevSeg to control the display. The
    library works with single digit and multi-digit seven segment display.
    You can download the library’s ZIP file from GitHub.
    To install it, open the Arduino IDE, go to Sketch >include Library
    >Add.ZIP Library, then select the SevSeg ZIP file that you down-
    loaded.                                                                  23 / 38
7-Segment Display
  Programming of 7-Segment Display Using SevSeg Library
   Function Discription of SevSeg Library
   sevseg.begin(): Its a function which initials the display
   hardwareConfig = COMMON CATHODE ; this sets the type of dis-
   play. I am using a common cathode, but if you are using a common
   anode then use COMMON ANODE instead.
   byte numDigits =1; this sets the number of digits on your display.
   I am using a single digit display, so I set it to 1. if you are using a
   four digit display, set this to 4.
   byte digitPins[] = {} ; create an array that defines the ground pins
   when using a 4 digit or multi-digit display. Leave it empty if you
   have a single digit display. For example, if you have a 4 digit display
   and want to use Arduino pins 10, 11, 12 and 13 as the digit ground
   pins, you would use this:
   byte digitPins[] = {10, 11, 12, 13};
                                                                             24 / 38
7-Segment Display
  Programming of 7-Segment Display Using SevSeg Library
   byte segmentPins[] = {6, 5, 2, 3, 4, 7, 8, 9}; This declares an array
   that defines which Arduino pins are connected to each segment of
   the display. The order is alphabetical(A,B,C,D,E,F,G,DP where DP
   is the decimal point). So in this case, Arduino pin 6 connects to
   segment A, pin 5 connects segment B, and so on.
   resistorsOnSegments = true; This needs to be set to true if your
   current limiting resistors are in series with the segment pins. If the
   resistors are in series with the digital pins, set this to false. Set this
   to true when using multi-digit displays.
   sevseg.setBrightness(90); This function sets the brightness of the
   display. It can be adjusted from 0 to 100.
                                                                                25 / 38
7-Segment Display
  Programming of 7-Segment Display Using SevSeg Library
   sevseg.setNumber();This function points the numaber to the display.
   For example, sevseg.setNumber(4); will print the number ”4”to the
   display. You can also print numbers with decimal points. For exam-
   ple, to print the number ”4.999”, you would use sevseg.setNumber(4999,
   3);The second parameter(the 3) defines where the decimal point is
   located. In this case it is 3 digits from the right most digit. On
   a single digit display, setting the second parameter to ”0” turns on
   the decimal point, while setting it to ”1” turns it off.
   sevseg.refreshDisplay();This function is required at the end of the
   loop section to continue display the number.
                                                                         26 / 38
 Interfacing of Keys
Interfacing of Keys
     Keypad is a widely used input device with lot of applications such
     as telephone, computer, ATM, electronic lock etc. A keypad is used
     to take input from the user for further processing. Here a 4 by 4
     matrix keypad consisting of switches arranged in rows and columns
     is interfaced to the microcontroller.
                                                                          27 / 38
Interfacing of Keys
    Every number of keypad is assigned two unique parameters that are
    row and column. Hence every time a key is pressed the number is
    identifying by detecting the row and column numbers of keypad.
    Initially all the rows are set to zero (’0’) by the controller and columns
    are scanned to check if any key is pressed. In case of no key is pressed
    the output of all columns will be high (’1’).
                                                                                 28 / 38
 Interfacing of Keys
   Arduino Programming of Keypad
Arduino programming of Keypad
     Keypad is a library for using matrix style keypads with arduino.
     getKey(): Returns the key that is pressed.
                                                                        29 / 38
 Liquid Crystal Display (LCD)
Liquid Crystal Display (LCD)
     LCD stands for liquid crystal display which can display the characters
     per line. Here 16 by 2 LCD display can display 16 characters per
     line and 2 lines. Each character is displayed in 5*7 pixel matrix.
     LCD is very important device which is used for almost all auto-
     mated devices such as washing machines, an autonomous robot,
     power control systems and other devices. This is achieved by dis-
     playing their status on small display modules like 7-seven segment
     displays, multi segment LEDs etc. LCDs have reasonable price, eas-
     ily programmable and they have no limitations of displaying special
     characters.
     It consists of two registers such as command/instruction register
     and data register.
                                                                              30 / 38
Liquid Crystal Display (LCD)
    The command/instruction register stores the command given to
    the LCD. A command is an instruction which is given to the LCD
    that perform a set of predefined tasks like initializing, clearing the
    screen, setting the cursor posing, controlling display etc.
      The data register stores the data to be displayed on LCD. The
      data is an ASCII value of the characters to be displayed on the
                                   LCD.                                      31 / 38
Liquid Crystal Display (LCD)
    Operation of LCD is controlled by two commands. When RS=0,
    R/W=1 it reads the data and when RS=1, R/W=0, it writes (print)
    the data.
                               LCD uses following command codes:
                                                                      32 / 38
 Liquid Crystal Display (LCD)
Example: LCD Interface
                                33 / 38
 Liquid Crystal Display (LCD)
   Temperature sensor and LCD
Temperature sensor and LCD
     The LM35 series are precision integrated-circuit temperature devices
     with an output voltage linearly proportional to the Centigrade tem-
     perature
     The accuracy specifications of the LM35 are given with respect to
                     a simple linear transfer function:
                                        mv
                             Vout = 10 ◦ ∗ T
                                         C                                  34 / 38
Liquid Crystal Display (LCD)
  Temperature sensor and LCD
    where
        Vout is the LM35 output voltage
        T is the temperature in degree centigrade
    The LM35 device has a very wide 4-V to 30-V power supply voltage
    range, which makes it ideal for many applications
    The hardware connection is given as follows
                                                                       35 / 38
Liquid Crystal Display (LCD)
  Temperature sensor and LCD
    The protus simulation connection and the arduino code is given
    below
                                                                     36 / 38
 Analog Input Output
Analog Input Output
                       37 / 38
 Analog Input Output
Example of analog Interface
                              38 / 38